Using single symbol equity curve in a multi symbol backtest
Author: mikesblack
Creation Date: 8/3/2009 10:55 AM
profile picture

mikesblack

#1
I was hoping I could devise a way to use performance from individual symbols in a multi- symbol strategy to enhance the performance of the entire strategy. e.g. Let's say, I'm trading the S and P 100 and only want to trade symbols that are in perhaps 10%( or 20.day) drawdowns if traded on their own. Is there a way this can be done?
profile picture

Eugene

#2
What is the drawdown of an individual symbol?
profile picture

mikesblack

#3
It's a way to filter for under performing stocks in the portfolio using a particular strategy. The strategy would reference each stock's performance in in raw profit mode.

The idea is for the system to buy (or add positions) to the drawdown(assuming a reversion to the mean).
profile picture

Cone

#4
Drawdown is relative to the last peak in Portfolio Equity. So does that mean that individual symbol drawdown is relative to the peak gain for trading only that symbol? Also, portfolio drawdown needs a reference level, there is no such thing as drawdown in RP mode.

So what is it, exactly, and how do you calculate it?
profile picture

mikesblack

#5
Indeed- The equity peak draw down from trading a single symbol.
profile picture

mikesblack

#6
Not only equity draw down, but also time in draw down.
profile picture

Eugene

#7
This should be possible, although the implementation will have to wait. Problem is, when working on this Interacting with Portfolio Equity article, the code wasn't designed with individual symbol equity in mind. The routines execute a multi-symbol backtest, and your task requires some code library modification. I added such an overload to Community.Components, allowing to get the results of trading on a single symbol, but it will surely take time for testing and updating the KB article before actually releasing it.
profile picture

Eugene

#8
Mike, I've added an overload in Community.Components 2009.09 to run a Strategy on a single symbol and extract its results in the manner of the KB article, but I really don't have the time and desire to finish and support it. The syntax is:
CODE:
Please log in to see this code.

Example (taken from the KB article):
CODE:
Please log in to see this code.

Modify the similar line where you find it (as can be seen, a Bars object should be passed e.g. current symbol)
profile picture

mikesblack

#9
Thanks Eugene.
profile picture

johnls101

#10
Hi Eugene,
I note that about 4 years have passed since the last entry to this topic.
Has there been any further refinement for calculating single symbol equity as part of a multi symbol strategy? I appreciate that it is imperfect but one could perhaps utilise raw profit mode as suggested by Mike above so that equity could be calculated on the fly for each symbol and returned much like any other indicator. Is this easier to do now? As an example I would like to test a scenario where trades were not permitted on any individual stock after the theoretical equity curve for that stock dropped below 15% of its maximum until such time that the equity curve returned back to and exceeded the previous maximum.
profile picture

Eugene

#11
Hi John,

There wasn't any movement in this direction. I have nothing to suggest with regard to calculating single symbol equity as part of a MSB. However, obtaining the theoretical equity curve for a stock might be possible if you follow suggestion in my post #8. This means that the equity is obtained as if the Strategy was executed on that symbol alone in portfolio backtest mode.
profile picture

johnls101

#12
Hi Eugene

I do need to still go through the Interacting with Portfolio Equity article yet and I will do so soon enough.

However do you mind if I mind if I have a go at what may be a relatively simple approach to creating a theoretical equity curve indicator?

I have not worked this out fully but I was trying to use the "NetProfitAsOfBar" example provided in the Quick Ref. What I have done is to take the difference between the equity at one bar from the bar behind it. The idea is to create a cumulative sum of that with the exception of the equity difference at the exit bar which should be zero. The problem with doing what I have below is that the equity at the exit bar gets subtracted from zero leaving no net gain and I am back to the same series as before.

How do I make the last difference value at the exit bar equal to zero (top histogram) such that I have a more meaningful equity curve? Sorry, my coding is still limiting me.

CODE:
Please log in to see this code.
profile picture

Cone

#13
Don't neglect reading the Remarks in the QuickRef. In summary, NetProfitAsOfBar is of no use to you in a Strategy unless your Position sizing happens to be 1-share/contract fixed.
profile picture

johnls101

#14
Thanks for that Cone. I appreciate that I can only deal with one share or contract at a time and that this approach is limited. However, I am trying to create a theoretical single symbol equity curve (akin to the green shaded area seen in the "equity curve" tab) before the "real" backtest equity data are assembled.

I have tried to make the series that is to be cumulatively summed have the last difference in each trade equal to zero. When it is not zero, it generally reflects that the trade has finished. If the trade was a winning one, for example, the final profit at that bar is subtracted from zero and I am left with a cumulative sum of that series that returns to zero rather than reflecting cumulative equity for that share (see the window that is fourth from the top - blue line).

I have tried the following, but have done something wrong as hPftSerdiff and modifieddiff end up being exactly the same as each other. My intention is for modifieddiff to be just like hPftSerdiff with the exception of the last bar of each trade, when modifieddiff should be zero rather than hPftSerdiff which is not. My belief is that if I can force the difference at the last bar of each trade to be zero, I will end up with the symbol theoretical equity when I cumulatively sum the difference series. I am sure that there is something minor that I have got wrong, but my coding knowledge is still evolving.

Can you see where I have gone wrong?

CODE:
Please log in to see this code.
profile picture

Eugene

#15
QUOTE:
I have tried the following, but have done something wrong as hPftSerdiff and modifieddiff end up being exactly the same as each other. My intention is for modifieddiff to be just like hPftSerdiff with the exception of the last bar of each trade,


Well, in this case it's not "each trade" but exactly the LastPosition:

CODE:
Please log in to see this code.


Unless LastPosition (and only it) is exited precisely on the last bar, modifieddiff is always equal to hPftSerdiff. Without analyzing the logic of your code, your intention was probably to establish some sort of loop by Positions?
profile picture

johnls101

#16
I have struggled with this for a couple of hours but can't get it to work. I want modifieddiff for all bars in all positions to equal hPftSerdiff with the exception of the exit bar of all positions, in which case I want modifieddiff to equal zero. I probably do want to loop through each position, but I am lost as to how to do this.
profile picture

Eugene

#17
As you state that you're trying to create a theoretical single symbol equity curve before the "real" backtest equity data are assembled, the proper way to do it is follow my suggestion in post #8.
profile picture

johnls101

#18
Ok. I'll go over that. Thanks.
profile picture

johnls101

#19
I managed to work out the correct code for my idea that involves looping through positions - your suggestion. I have included the code below for the sake of completeness and it does produce a single symbol equity curve. However, as you implied, it is not useful as each trade has to have been taken before the equity curve can be generated and it cannot be accessed in hindsight. It is impossible as far as I can see, to feed the equity curve back into the previous code to influence whether or not to take a position or not in the first place.

You are right about doing things the proper way and my approach does not work.

CODE:
Please log in to see this code.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).