Bridging the Gap: Integrating Python with TradeStation and MultiCharts for Enhanced Trading Strategies

Domenico D'Errico
6 min readJun 2, 2024

--

For a while now, inside the Gandalf Project, we have been discussing how to bridge the communication gap between the scientific community, which develops AI models and genetic algorithms, and the financial community, which implements these models.

I have partially covered this topic in the January and February 2024 issues of Stocks & Commodities:

“Artificial Intelligence Applied to Technical Trading”
“Enhanced Technical Indicator Analysis Using Machine Learning”

However, to fully address this, we decided to create a “bridge” between the tools commonly used by both communities: Python and TradeStation/MultiCharts.

AiBridge is an API designed to make the results of complex AI models and genetic algorithms developed in Python accessible to professional users of TradeStation/Multicharts.

Traders and fund managers often aren’t experts in artificial intelligence or evolutionary algorithms (genetic algorithms). Additionally, to understand strategy logic, they usually need hands-on interaction with charts. They need to zoom in and out, examine entry and exit levels to see how the model performed in different market phases, and compare this to their own management of those phases. They prefer focusing on the tools they are most familiar with: charts and stock scanners.

The first project I am introducing, hosted by Giovanni Trombetta at the Gandalf incubator (with Giovanni also providing the genetic portfolio model for this prototype), is called AiBridge_100. Its goal is to provide TradeStation/Multicharts users with weekly forecasts from a genetic algorithm built on the 100 most capitalized stocks of the S&P 500 index.

In short (Giovanni can provide further details if needed), the genetic model assigns a weekly Risk ON / Risk OFF rating to each of the top 100 stocks in the S&P 500 index.

AiBridge Pipeline

The first thing that came to mind as soon as AiBridge was developed was to visualize the model’s behavior in the markets using the tools I am familiar with: TradeStation’s charts and RadarScreens.

So, using the data provided by Python, I created an initial RadarScreen to display the model’s behavior over the past two weeks.

Tradestation Radarscreen from AiBridge

This is an example of a TradeStation RadarScreen powered by AiBridge. In addition to the ticker, description, and sector of the stocks, the RadarScreen displays 12 columns, each representing a week. The dates are listed in the first row, arranged chronologically from left to right, similar to charts. For example, column 1 shows the most recent week available, which in this case is the week ending on Friday, May 17.

Each week that the genetic model evaluates as Risk ON is highlighted in green. For instance, as of Friday, May 17, the model identified 8 out of 100 stocks as Risk ON, visible in rows 4 to 11. The percentages within the cells represent the net change percentage for the following week, allowing for a quick check on the model’s performance. For example, the net change percentages for the week ending May 24 are shown for the week of May 17, and so on.

Row 2 displays the net change percentage for the SPY, also for the following week, enabling a quick comparison between the highlighted stocks’ performance and the market. Analyzing the RadarScreen suggests that the model effectively proposes a different portfolio from the initial 100 symbols each week.

Additionally, I added a histogram to TradeStation charts to indicate when the model switches to risk-ON mode and how this information can be interpreted alongside other standard technical indicators.

Tradestation Chart from AiBridge

In this picture you can see the Risk-ON evaluation provided by the genetic model in the form of a dark green histogram. The histogram highlights a Risk-ON evaluation by the genetic model. This visualization allows for an immediate visual analysis of the model’s historical behavior. For example, in the Chevron chart, you can observe that over the past 30 weeks, the model has maintained a nearly uninterrupted Risk-ON evaluation.

The model’s information can be integrated with the libraries of technical indicators available in commercial platforms to create customized chart templates. In this chart, I have added a stochastic oscillator and a moving average purely for demonstration purposes. Those who use the stochastic oscillator will notice that the system starts indicating a Risk-ON in an oversold situation, which could reinforce a buy or increase evaluation for Chevron. Users of moving averages will notice that the stock crosses the 40-period weekly moving average during the Risk-ON evaluation, but the average flattens out shortly after, indicating a potential weakness in the stock.

TradeStation Strategy from AiBridge

This is an attempt to link an EasyLanguage strategy that buys when the model’s evaluation is Risk-ON and sells when it switches back to Risk-OFF. This strategy, when applied to Chevron, produces the Equity Line shown in the picture below

TradeStation Equity Line

Then I became very curious: I thought, since the model was built on the top 100 stocks of the American index, what happens if we test it on the entire S&P 500?

AiBridge Performance on S&P 500

To this end, I set up TradeStation’s Portfolio Maestro with identical position sizes of $20,000 (for a total allocation of $10 million). The results are as follows:

TradeStation Portfolio Maestro from AiBridge: Performance Report S&P 500
TradeStation Portfolio Maestro from AiBridge: Out of Sample Realized Equity S&P 500
TradeStation Portfolio Maestro from AiBridge: Exposure S&P 500
TradeStation Portfolio Maestro from AiBridge: Out of sample P&L by symbol S&P 500

A yearly average performance of 4% and a maximum drawdown of 12% for an out-of-sample result (on a basket different from the one the model was built on) seems like a good starting point. Moreover, from the Exposure you see, the model has an average capital utilization of under $5 million USD, so it could easily be managed with leverage 2, potentially doubling the yearly average performance to 8% (with a maximum drawdown of 24%).

How does the model fare when faced with the issue that plagues all algorithmic managers of stock portfolios, especially in the American context: does the model outperform Buy & Hold in terms of risk-return ratio?

Buy & Hold Benchmark

Using Tradestation’s ROA (Return on Asset, which is the ratio of Total Profit to max drawdown multiplied by 100), we attempted to answer this question.

Among 492 analyzed stocks, we observed the following:

· Negative Performers: Among the 30 stocks with a negative Buy & Hold ROA, AiBridge manages to outperform Buy & Hold, raising the average ROA from -26 to 55.

· Average Performers: Among the 312 stocks showing a Buy & Hold ROA between 0 and 200, AiBridge outperforms, raising the average ROA from 96 to 162.

· Top Performers: Among the 150 stocks showing an ROA > 200, AiBridge underperforms in terms of overperformance, lowering the average ROA from 318 to 301.

Summary

In conclusion, as we delve deeper into the details, we feel like we’ve opened a “Pandora’s box” from which answers to questions such as:

Can such a tool be used in Asset Allocation?
What if we used multiple genetic models to provide a rating instead of just a green/red light?
How would the model perform on a portfolio of ETFs?
How would it perform on individual sectors?
What if we applied portfolio hedging?
What if we opened a short position on the index every time the model is in a risk-ON phase?
The results of the genetic model are obtained without any risk management: what if we applied volatility-linked position sizing?
..and neither with Money Management: what if we used the genetic forecast only for entering a position, or only for exiting?

It seems like we have something to investigate in terms of both Trading and Asset Management in the coming weeks.

--

--

Domenico D'Errico

Quant developer for professional traders. Actually researching in Machine Learning applied to Technical Trading. For info write to: domderrico@gmail.com