Can you apply factors to
trade performance?

Posted on Sep 10, 2019 by Kris Longmore
1 Comment
95 Views

When tinkering with trading ideas, have you ever wondered whether a certain variable might be correlated with the success of the trade?

For instance, maybe you wonder if your strategy tends to do better when volatility is high?

In this case, you can get very binary feedback by, say, running backtests with and without a volatility filter.

But this can mask interesting insights that might surface if the relationship could be explored in more detail.

Zorro has some neat tools that allow us to associate data of interest with particular trading decisions, and then export that data for further analysis. Here’s how it works:

Zorro implements a TRADE struct for holding information related to a particular position. This struct is a data container which holds information about each trade throughout the life of our simulation. We can also add our own data to this struct via the TRADEVAR array, which we can populate with values associated with a particular trade.

Zorro stores this array, along with all the other information about each and every position, as members of the TRADE struct. We can access the TRADE struct members in two ways: inside a trade management function (TMF) and inside a trade enumeration loop.

Here’s an example of exporting the last estimated volatility at the time a position was entered, along with the return associated with that position (this is a simple, long only moving average cross over strategy, data is loaded from Alpha Vantage):

The general pattern for accomplishing this is:

  1. Define a meaningful name for the element of the TradeVar that we’ll use to hold our volatility data (line 5)
  2. Define a Trade Management Function to expose the TRADE struct and use it to assign our variable to our TradeVar (lines 7-12). A return value of 16 tells Zorro to run the TMF only when the position is entered and exited.
  3. Calculate the variable of interest in the Zorro script. Here we calculate the rolling 50-day standard deviation of returns (line 34).
  4. Pass the TMF and the variable of interest to Zorro’s enter function (line 38).
  5. In the EXITRUN (the last thing Zorro does after finishing a simulation), loop through all the positions using a trade enumeration loop and write the details, along with the volatility calculated just prior to entry, to a csv file.

Running this script results in a small csv file being written to Zorro’s Log folder. A sample of the data looks like this:

Once we’ve got that data, we can easily read it into our favourite data analysis tool for a closer look. Here, I’ll read it into R and use the tidyverse libraries to dig deeper. (This will be very cursory. You could and should go a lot deeper if this were a serious strategy.)

First, read the data in, and process it by adding a couple of columns that might be interesting:

If we head the resulting data frame, we find that it looks like this:

Sweet! Looks like we’re in business!

Now we can start to answer some interesting questions. First, is volatility at the time of entry related to the magnitude of the trade return? Intuitively we’d expect this to be the case, as higher volatility implies larger price swings and therefore larger absolute trade returns:

Nice! Just what we’d expect to see.

Does this relationship hold for each individual asset that we traded?

Looks like the relationship generally holds at the asset level, but note that we have a small sample size so take the results with a grain of salt:

Is volatility related to the actual trade return?

Looks like it might be. But this was a long-only strategy that made money in a period where everything went up, so I wouldn’t read too much into this without controlling for that effect.

Is there a significant difference in the entry volatility for winning and losing trades?

Finally, we can treat our volatility variable as a “factor” to which our trade returns are exposed. Is this factor useful in predicting trade returns?

First, we’ll need some functions for bucketing our trade results by factor quantile:

If we bucket our results by factor quantile, do any buckets account for significantly more profit and loss? Are there any other interesting relationships?

Looks like there might be something to that fifth quantile (but of course beware the small sample size).

We can retrieve the cutoff value for the fifth quantile by sorting our factor and finding the value four-fifths the length of the resulting vector:

Conclusion

There you have it. This was a simple example of exporting potentially relevant data from a Zorro simulation and reading it into a data analysis package for further research.

How might you apply this approach to more serious strategies? What data do you think is potentially relevant? Tell us your thoughts in the comments.

Want a broader understanding of algorithmic trading? See why it’s the only sustainable approach to profiting from the markets and how you can use it to your success inside the free Algo Basics PDF below….

Get the free PDF instantly

Want to see how we use algos to trade for a living — so you can too?

Discover how systematic retail traders generate profit long-term, and where to start:

Comment (1)

tovim
September 13, 2019 at 12:26 pm

Hi ,
I ‘m thinking like this ; 1-calculate the maximum favourable excursion minus maximum adverse excursion and high minus low for each bar.Then use mutual information to measure the dependence.2-Apply the same method for each closed trade.Is this possible?Does it make sense?

Leave a Comment