How to pyramid in a Rotation Strategy?
Author: trader rog
Creation Date: 3/29/2010 12:42 PM
profile picture

trader rog

#1
Hi,

I am trying to backtest a strategy that adds to a position if the price is 1% below the original entry price. I would like to have up to 1 add-on per stock, and it would need to occur within 5 days of the original entry. Additionally, the entire position needs to be exited all at once. I have tried to code this using the pyramiding solution in Wealth-Lab Wiki as a guide, but am not sure how to proceed. Is this possible to do using WealthLab? I included the code below to help diagnose the problem. I sincerely appreciate any help or insight.

CODE:
Please log in to see this code.


profile picture

Cone

#2
First a little cleanup -
CODE:
Please log in to see this code.
This is peeking. The condition says, "buy today at the limit price of tomorrow's close." Surely you meant to write if( BuyAtLimit(bar + 1, Close[bar]).

There's really no trick to pyramiding in non-rotational scripts; you just add a Position whenever you want to; the ActivePositions.Count tells you how many Positions of the current symbol you have. Note, however, that each Position is a separate object - there is no "entire Position" object.

The difference in a script that trades multiple symbols is that you have to determine if a symbol even exists in ActivePositions. The simple function SymbolLastEntry takes care of that (see comments), and then it's a simple matter.

Here's the buy logic with the SymbolLastEntry function -

CODE:
Please log in to see this code.
What's not a simple (at least not obvious to me) matter is how exit both symbols using the reverse ActivePositions loop. There's a "chicken before the egg" condition to work through, plus you have to be careful how you "pick off" Positions in a list under enumeration (that's why the loop is in reverse). The problem is determining that multiple Positions of the same symbol need to be exited, but the time-based exit Position will be further down in the loop than the Position added later, which is processed first in the loop - a logic teaser for you!
profile picture

Cone

#3
Okay, I think I've got the answer - just save the symbols that were exited and process the ActivePositions again immediately after, checking if a same symbol was just exited.

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

trader rog

#4
Thank you Cone. That was extremely helpful! I am trying to limit the # of symbols to no more than 3 outstanding at one time. I added the activeLongs counter from my first post to your solution but I still get more than 3 symbols in open positions at any one time. Any idea what I'm overlooking here?
profile picture

Cone

#5
The problem is in the change in entry and exit in the classic rotation script. Your Positions exit only when they meet Cond1 or Cond2, but they don't get "rotated out" due to their rsi being greater than 3 other symbols. At the same time, new entries can be created due to the rsi criteria.

So, you can either:
1. add the rsi exit criteria, like in the original rotation script. This favors new entries (vs. existing Positions) based on rsi .

- or -

2. add another entry condition to take new Positions only if the number of "symbols" having Positions is less than 3, i.e., number. This method favors existing Positions vs. new Positions with lower rsi.

Assuming you want #2, do this -
CODE:
Please log in to see this code.

Finally, note that it's possible for this script to be holding fewer than number Positions since the exit logic can knock out the symbols that happen to have the lowest rsi. Conceivably you could have a bar here and there where there are 0 Positions.

-----
Edit:
I just realized that the ActiveSymbols() solution won't generate the proper exits in realtime since it requires that the Positions actually exit on the next bar; i.e., it needs to peek, though legally. Anyway, this can be worked out too, just with different logic that doesn't rely othe ActivePositions list.
profile picture

trader rog

#6
It seems to work perfectly. Thank you so much!

It's ok that there can be 0 positions. In fact, for a long strategy that can be desirable in a negative market environment.

Thanks again for all your help.
profile picture

Cone

#7
Again, the script is okay for backtesting, but a new design is required to replace ActiveSymbols() for proper Alerting. I'm swamped though, so you're on your own for a while ;)
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).