Analysis – Performance of Indicator Trade Signals

In this post, we are going to use Backtrader together with Pandas to check the performance of trade signals generated by a simple indicator. I am interested whether certain signals are better at predicting moves in the market over a particular time period. In other words, do they appear to be more profitable in 10 bars following the signal or 200 bars? As such, the idea will be to generate a list of signals in backtrader and then plot the performance of each signal over its subsequent bars to see if any patterns emerge.

Code

Code Commentary

As this is not a tutorial, the commentary on each piece of code will be light. Instead, this section will provide some general notes on running the script and what the code does at a higher level.

First of all, the code in this example expects a CSV file for data input. You can get a copy of the data used in this post here: EUR_USD-D. If you use the example file, be sure to place it in a sub-directory of the main script called “data”.

A few runtime arguments have been added. These can be used to change the data feed or skip backtrader signal generation completely. This might be useful if you have already performed signal generation and just want to see the output chart again.

Note: If you are new to argparse and setting run-time options, see this article.  In addition, if you do use a run-time option to analyse your own data, you need to make sure the structure of the file and date format of your data the same as the example file. That means the Open,High,Low, CloseandVolumecolumns are in the same order. Finally, you will need to make sure the date format is the same. If your data does not follow this format and you don’t want to edit your data, you can edit the following section to match your CSV file format:

If you don’t know how to do this, have a look at the following links:

On launching the code, Backtrader will be used to run through a simple strategy. Then, during each call of next() the strategy will log OHLCVdata along with whether a Longor Shortsignal was generated. This log will be written to a CSV file that will subsequently make its way into Panda’s for analysis.

The signals generated in the test strategy are just examples to keep the code and short and clean. All we do is generate signals when we have a close above or below a Simple Moving Average. You can easily swap this out for any complex signal of your choice by editing the Longand Short boolean variables.

Pandas proved to be the trickier part for the author to implement. As such, I am sure there must be more efficient ways of coding this section. However, it appears to work (Thanks to google and stack overflow) and that is the main thing! Here we attempt to extract the date of each long and short signal and then pull xnumber of subsequent close prices. Once we have the close prices, we can then calculate the running PNL for each bar and insert it into new dataframe for plotting.

Results

Running the script with the default settings and example data will output a chart that looks like this:

Analysis over 200 days

The results are fascinating! So what we see is that generally long signals were more profitable than short signals over the test period. Personally, I think this makes sense as we know that the EURO has generally risen against the USD over the long term. Furthermore, the drops in the EURO have tended to be faster moves in comparison with the periods of appreciation. Finally, we are using a long look back on the SMA which means the results span 200 days. In other words, each signal is getting a benefit from the longer term trend as we move towards the second hundred bar.

EUR/USD Since 2003

So what happens if we reduce the period of analysis to just 20 days? Well, this happens:

Analysis over 20 days

I will leave you to come to your own conclusion on this chart. Like a candlestick chart, everyone will see something different! That is the beauty of this game.

Moving onward, I would be interested to see how more complex signals perform over the shorter term and will be definitely swapping out the SMA in my own tests.