Optimizing an Algorithm trading program doesn’t run

I have written an algorithm trading strategy with the parameters gap_candles and Fib_back_candles and tried to use optuna for the Hyperparameter Tuning.
It should try about 480 combinations but stops after 17 trials.
The code doesn’t stop but he doesn’t calculate new trials.

import algotrad_signals as ats
from initial_parameters import start_date, end_date, ticker_list, stock_data_list, back_candles
from datetime import datetime
from itertools import product
import gc
import optuna
import math
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import logging
import psutil
import os
from concurrent.futures import ProcessPoolExecutor

# Output preparation and deactivate warnings
desired_width = 320
pd.set_option('display.width', desired_width)
pd.set_option('display.max_columns', 18)
pd.options.mode.chained_assignment = None  # default='warn'
pd.options.display.float_format = '{:.2f}'.format

# Logging
logging.basicConfig(level=logging.INFO)

# Load stock data; add RSI and EMA
for tick in ticker_list:
    stock = ats.load_and_prep_stock_data(tick, start_date, end_date, '1d')
    stock = ats.generate_EMA_signal(stock, back_candles)
    stock_data_list.append(stock)

# Add ticker
for idx, df in enumerate(stock_data_list):
    ticker = ticker_list[idx]
    df['Ticker'] = ticker

# Create portfolio
portfolio = pd.DataFrame(ticker_list, columns=['Ticker'])
portfolio['signal'] = 0
portfolio['Close'] = 0
portfolio['Abs_buy_limit'] = 0
portfolio['num_of_stocks'] = 0
portfolio['Value'] = 0

def fib_portfolio_invest(df, investment_rate, pf):
    week_of_previous_day = None
    df['Date'] = pd.to_datetime(df['Date'])
    cash = 0
    act_ticker = df.at[0, 'Ticker']
    cash += investment_rate
    pf.loc[pf['Ticker'] == act_ticker, 'signal'] = df.at[0, 'signal']
    pf.loc[pf['Ticker'] == act_ticker, 'Close'] = df.at[0, 'Close']
    for df_idx, df_row in df.iloc[1:].iterrows():
        current_week = df_row['Date'].isocalendar()[1]
        act_ticker = df_row['Ticker']
        if df_row['Date'] != df.loc[df_idx - 1, 'Date']:
            if current_week != week_of_previous_day:
                cash += investment_rate
            for pf_idx, pf_row in pf.iterrows():
                if pf.at[pf_idx, 'signal'] == 1:  # Sell-Signal
                    cash += pf.at[pf_idx, 'num_of_stocks'] * pf.at[pf_idx, 'Close']
                    pf.at[pf_idx, 'num_of_stocks'] = 0
                    pf.at[pf_idx, 'Value'] = 0
            portfolio_sum = pf['Value'].sum() + cash
            cap = 0.1 * portfolio_sum
            for pf_idx, pf_row in pf.iterrows():
                if pf.at[pf_idx, 'signal'] == 2 and pf.at[pf_idx, 'Value'] < cap:  # Buy-Signal
                    cap_buy_limit = math.floor((cap - pf.at[pf_idx, 'Value']) / pf.at[pf_idx, 'Close'])
                    affordable_stocks = math.floor(cash / pf.at[pf_idx, 'Close'])
                    pf.at[pf_idx, 'Abs_buy_limit'] = min(cap_buy_limit, affordable_stocks)
            while (pf['Abs_buy_limit'] != 0).any():
                min_index = pf[pf['signal'] == 2]['Value'].idxmin()
                pf.at[min_index, 'num_of_stocks'] += pf.at[min_index, 'Abs_buy_limit']
                cash -= pf.at[min_index, 'Abs_buy_limit'] * pf.at[min_index, 'Close']
                pf.at[min_index, 'Value'] = pf.at[min_index, 'num_of_stocks'] * pf.at[min_index, 'Close']
                pf.at[min_index, 'Abs_buy_limit'] = 0
                for pf_idx, pf_row in pf.iterrows():
                    if pf.at[pf_idx, 'signal'] == 2 and pf.at[pf_idx, 'Value'] < cap:  # Buy-Signal
                        cap_buy_limit = math.floor((cap - pf.at[pf_idx, 'Value']) / pf.at[pf_idx, 'Close'])
                        affordable_stocks = math.floor(cash / pf.at[pf_idx, 'Close'])
                        pf.at[pf_idx, 'Abs_buy_limit'] = min(cap_buy_limit, affordable_stocks)
            pf['signal'] = 0
            pf.loc[pf['Ticker'] == act_ticker, 'signal'] = df.at[df_idx, 'signal']
            pf.loc[pf['Ticker'] == act_ticker, 'Close'] = df.at[df_idx, 'Close']
        else:
            pf.loc[pf['Ticker'] == act_ticker, 'signal'] = df.at[df_idx, 'signal']
            pf.loc[pf['Ticker'] == act_ticker, 'Close'] = df.at[df_idx, 'Close']
        week_of_previous_day = current_week
    pf[['Value', 'Close']] = pf[['Value', 'Close']].apply(lambda x: x.round(2))
    pf_result = pf
    pf_result.loc[0] = {'Ticker': 'cash', 'signal': 0, 'Close': 0, 'Abs_buy_limit': 0, 'num_of_stocks': 0, 'Value': cash}
    return pf_result

def execute_portfolio_invest(input_list_stock_data, investment_rate, pf, gap_candles, Fib_back_candles):
    try:
        for input_stock in input_list_stock_data:
            ats.generate_fibonacci_table(input_stock, Fib_back_candles, gap_candles)
        stoxx50_data = pd.concat(input_list_stock_data)
        stoxx50_data = stoxx50_data.sort_values(by='Date')
        stoxx50_data = stoxx50_data.reset_index(drop=True)

        pf_output = fib_portfolio_invest(stoxx50_data, investment_rate, pf)
        result_fib_sum = round(pf_output['Value'].sum(), 2)

        # clean portfolio
        pf['signal'] = 0
        pf['Close'] = 0
        pf['Abs_buy_limit'] = 0
        pf['num_of_stocks'] = 0
        pf['Value'] = 0

    finally:
        del stoxx50_data
        del pf_output
        gc.collect()

    return result_fib_sum

# Function to monitor memory usage
def log_memory_usage():
    process = psutil.Process(os.getpid())
    mem_info = process.memory_info()
    logging.info(f"Memory Usage: {mem_info.rss / (1024 * 1024)} MB")

# Heatmap results collection
heatmap_results = []

# Objective function for Optuna
def objective(trial):
    gap_candles = trial.suggest_int('gap_candles', 2, 14)
    Fib_back_candles = trial.suggest_int('Fib_back_candles', 10, 51)
    logging.info(f"Testing with gap_candles: {gap_candles}, Fib_back_candles: {Fib_back_candles}")
    log_memory_usage()
    score = execute_portfolio_invest(stock_data_list, 5000, portfolio, gap_candles, Fib_back_candles)
    heatmap_results.append((gap_candles, Fib_back_candles, score))
    return score

def run_optimization():
    # Create Optuna study
    study = optuna.create_study(direction='maximize')

    # Enqueue trials
    gap_candles_range = range(2, 14)
    Fib_back_candles_range = range(10, 51)
    param_combinations = list(product(gap_candles_range, Fib_back_candles_range))
    for gap_candles, Fib_back_candles in param_combinations:
        study.enqueue_trial({'gap_candles': gap_candles, 'Fib_back_candles': Fib_back_candles})

    # Start optimization
    study.optimize(objective, n_trials=len(param_combinations))

    return study

if __name__ == "__main__":
    with ProcessPoolExecutor(max_workers=1) as executor:
        future = executor.submit(run_optimization)
        study = future.result()

    best_params = study.best_params
    best_score = study.best_value

    print(f"Start: {start_date} - End: {end_date}")
    print("Beste Parameterkombination durch Optuna:", best_params)
    print("Bester Score:", best_score)

    # Create DataFrame for the heatmap
    heatmap_df = pd.DataFrame(heatmap_results, columns=['gap_candles', 'Fib_back_candles', 'score'])

    # Compute the average score for each combination to avoid duplicates
    heatmap_df = heatmap_df.groupby(['gap_candles', 'Fib_back_candles'], as_index=False).mean()

    # Scale down the score values and round to integers
    heatmap_df['score'] = (heatmap_df['score'] / 1000).round()

    # Create a pivot table
    pivot_table = heatmap_df.pivot('Fib_back_candles', 'gap_candles', 'score')

    # Plot the heatmap
    plt.figure(figsize=(10, 6))
    sns.heatmap(pivot_table, annot=True, fmt=".0f", cmap="YlGnBu", cbar_kws={'label': 'Score'})
    plt.title('Performance Heatmap 01-01-2019 - 31-12-2023')
    plt.xlabel('Gap Candles')
    plt.ylabel('Fib Back Candles')
    plt.show()

    print(f"Start: {start_date} - End: {end_date}")

I’ve thougt it might be a problem with the memory usage, but it isn’t.
Is there a problem with my cpu?
Maybe I’ve created an endless loop? I don’t get it.

New contributor

Markus Weber is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật