How to pyramid in a Rotation Strategy?
Creation Date: 3/29/2010 12:42 PM

#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:

#### Cone

#2
First a little cleanup -
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:
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!

#### 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:

#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?

#### 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:

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.