print(result) "Part 6 of NotADev"

IsaIsa
4 min read

Aligning the Trading Script with the Enhanced Model

In the last article, I talked about how I collaborated with ChatGPT to introduce new features to my trading model, such as Recursive Feature Elimination (RFE), XGBoost, and hyperparameter tuning. With these enhancements, the next challenge was ensuring that my trading script could properly make use of the newly trained model. In this article, I'll discuss how I worked with ChatGPT to overcome the challenges of integrating the updated model into the trading logic.

Adapting the Trading Script

With the upgraded model featuring 17 new indicators, including the Average Directional Index (ADX), Momentum (MOM), and Rate of Change (ROC), it became apparent that my existing trading script needed significant changes to handle the expanded feature set. This was not just a simple update; it required a comprehensive alignment between the feature engineering used during model training and the real-time data preparation for trading decisions.

One of the most crucial aspects was ensuring consistency in feature names and their order. I noticed that, when the trading script tried to make predictions using real-time market data, it frequently ran into errors because the features did not match those expected by the model. The model would raise an error due to either missing features or incorrect ordering, resulting in frustrating setbacks.

After explaining these challenges to ChatGPT, it proposed a solution to ensure feature alignment throughout the scripts. Here's a snippet showing how I extracted and prepared the features for real-time predictions:

# Extracting features in the same order as during training
features_df = pd.DataFrame([features], columns=FEATURE_COLUMNS)
features_df = features_df.fillna(0)  # Handling any missing values

# Making prediction
prediction_proba = model.predict_proba(features_df)
buy_proba = prediction_proba[0][1]
sell_proba = prediction_proba[0][0]

Handling Feature Order and Consistency

To maintain consistency, I had to revisit the feature engineering steps I used during model training and replicate them exactly in the trading script. This meant incorporating calculations for all 17 features, such as the Exponential Moving Average (EMA), Bollinger Bands, and others, ensuring the order was identical.

Below is a snippet that shows the feature calculation in real-time within the trading script, which mirrors what was done during training:

# Calculate features for real-time data
latest_data['ema50'] = talib.EMA(latest_data['close'], timeperiod=50)
latest_data['ema200'] = talib.EMA(latest_data['close'], timeperiod=200)
latest_data['adx'] = talib.ADX(latest_data['high'], latest_data['low'], latest_data['close'], timeperiod=14)
latest_data['mom'] = talib.MOM(latest_data['close'], timeperiod=10)
# ... calculate other features as well

Once I had ensured the features were calculated consistently, ChatGPT suggested a way to handle any potential discrepancies by checking for missing or unordered features and then adjusting accordingly. This helped prevent runtime errors and allowed the model to predict as expected.

Managing Recent Market Activity

Another improvement ChatGPT helped me make was incorporating recent market activity to improve the accuracy of predictions. The idea was to add a "run rate" feature that considered recent volume trends and volatility. This helped the bot become more context-aware, allowing it to factor in the recent momentum of the market when making decisions. Here’s a simplified example of how this was integrated:

# Calculate recent volume trend
latest_data['volume_change'] = latest_data['volume'].pct_change(periods=3)

# Calculate rolling volatility
latest_data['volatility'] = latest_data['close'].rolling(window=10).std()

These additional metrics allowed the model to weigh recent market conditions more heavily, giving it a better edge in predicting buy and sell opportunities.

Overcoming Errors and Building Robustness

Throughout this process, I faced multiple errors. The infamous "feature mismatch" error returned multiple times, and the challenge of handling NaN values or incomplete data meant I had to be meticulous. ChatGPT was instrumental in suggesting techniques such as filling missing values with zeros or calculating forward fill methods to handle incomplete data, ensuring no features were left undefined.

Here is an example of how I handled missing values to avoid model errors:

# Handle missing values
features_df = features_df.fillna(0)  # Replace NaN values with zero to avoid prediction errors

ChatGPT's approach to problem-solving here was iterative: each time an error was encountered, it would analyse the traceback, suggest corrections, and propose enhancements. It felt like a collaborative debugging session, and over time, these iterative fixes made my bot much more resilient to runtime issues.

The Result

After many rounds of testing and troubleshooting, the new trading script, combined with the enhanced model, was finally able to operate smoothly. The bot could now consistently make predictions using all 17 features, providing a more comprehensive analysis of the market. The integration of recent market activity indicators added an extra layer of context, further improving the bot's decision-making abilities.

The improvements in prediction accuracy were evident—the bot was now making fewer false-positive trades and identifying profitable opportunities with more reliability. The journey was challenging, but having ChatGPT as a guide made all the difference, providing insights and coding solutions whenever I encountered roadblocks.

Stay tuned for the next article, where I'll share how I worked on optimizing the bot's transaction logic, minimizing transaction costs, and further improving overall profitability.

0
Subscribe to my newsletter

Read articles from Isa directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Isa
Isa

Former analyst with expertise in data, forecasting, and resource modeling, transitioned to cybersecurity over the past 4 years (as of May 2024). Passionate about security and problem-solving, utilising skills in data and analysis, for cybersecurity challenges. Experience: Extensive background in data analytics, forecasting, and predictive modelling. Experience with platforms like Bugcrowd, Intigriti, and HackerOne. Transitioned to Web3 cybersecurity with Immunefi, exploring smart contract vulnerabilities. Spoken languages: English (Native, British), Arabic (Fus-ha)