data['ML_Signal'] = 0 data.loc[X_test.index, 'ML_Signal'] = y_pred # Only trade on predictions
A model trained on 2021's bull market fails in 2022's bear market. Your model must detect regime changes (e.g., using Hidden Markov Models from hmmlearn). Algorithmic Trading A-Z with Python- Machine Le...
capital = 100000 position = 0 equity_curve = [] data['ML_Signal'] = 0 data
for i in range(len(probabilities)): prob = probabilities[i] current_price = data_clean['Close'].iloc[split_idx + i] A model trained on 2021's bull market fails
if prob > 0.6 and position == 0:
# Buy
position = capital / current_price
capital = 0
elif prob < 0.4 and position > 0:
# Sell
capital = position * current_price
position = 0
# Mark to market
current_equity = capital + (position * current_price)
equity_curve.append(current_equity)
The goal is usually to predict future price direction (Classification) or exact price (Regression).