Adding a buy rule to a Rotation Strategy
Author: wmrgrove
Creation Date: 1/2/2013 3:48 PM
profile picture

wmrgrove

#1
I have a question on adding a buy rule to a rotation strategy. When using a data-series versus the actual coded buy-rule I get very different results. Attached is a basic roation strat where I added ;

DataSeries ATRnorm = ATRP.Series(Bars,80) -ATRP.Series(Bars,10) ; after the protected override void Execute()line

then ran the backtest with ; ( one of these enabled )

// if( ATRnorm [bar] > 0 ) or


// if( ATRP.Series(Bars,80)[bar] -ATRP.Series(Bars,10)[bar] > 0 )

before the buy rule BuyAtClose(bar+2);

Is the reason for different results a "context" issue, and will the actual coded 2nd line function correctly?

Can you tell me what is actually happening when the dataseries is used?

Thanks for any help! Strat code here;

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

Eugene

#2
QUOTE:
before the buy rule BuyAtClose(bar+2);

It's generally a mistake to pass "bar+N" where N > 1; the "bar" here should not exceed "bar+1".
QUOTE:
Attached is a basic roation strat where I added ;

DataSeries ATRnorm = ATRP.Series(Bars,80) -ATRP.Series(Bars,10) ; after the protected override void Execute()line

Where you added this line is the culprit. The DataSeries will always be equal to the DataSeries for the charted (clicked) symbol. So yes, this is a context issue.

If you want to add this extra rule for checking it on a by symbol basis before making an entry, expand the holder class and assign the value:

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

wmrgrove

#3
Eugene, thanks for the reply and Happy New Year to you and yours! Bill
profile picture

Eugene

#4
You're welcome Bill.
profile picture

crazyjava

#5
I'd like to continue the question since I am developing rotation strategy based on the sample code similar to the above.
There are several things really puzzled me.
1. If you write such a rotation strategy code and run on a portfolio with 10 stocks, the above code will be executed 10 times.
To test this, add the following line before the loop starts.
CODE:
Please log in to see this code.


I have to do the following to work around this and it is ugly.
CODE:
Please log in to see this code.

How to get around this in a nicer way?

2. Now the Context issue. Because the code will always select the first stock (data set) as its first default context, if the first stock has much less date/year coverage, it is really hard to get the remaining data sets data lined up from the earliest dates possible to present. I illustrate here:
S1 - First Stock. E.g. 1/1/2008 to present
S2 - 1/1/2006 to present
S3 - 1/1/2002 to present
SPY - 1/1/2001 to present (the longest dates coverage).
If you synchronize data, you will get the rest symbols starts from 1/1/2008.
If you SetContext("SPY", false);
Every time you RestoreConext(), it goes back to the context of S1. Because you need call it when But and Sell positions. The logic getting really messy. To create a dummy data set and put it as a first stock may work but not elegant.

For Rotation Strategy, it would be ideal have a way to stop auto run through the portfolio (or stop run at the first stock), and have a way to Set Default Context other than on the first stock.

For the above example, the rotation strategy can set default context on SPY (the longest coverage). The code can check at a given bar, if a symbol has data.

Let me know if you have a solution. Thanks.
profile picture

crazyjava

#6
I think I found the answer. I should select one symbol, e.g. SPY, and run the code. Then the code will not run 10 times (MSB mode?). In addition, the default context will be SPY. I definitely should read more on the forum and learn. Thanks.
profile picture

Eugene

#7
FAQ | Strategies and WealthScript > "A Rotation strategy isn't working like it should."
profile picture

crazyjava

#8
Thanks Eugene. It's well written there.
profile picture

Carova

#9
What is the proper way to define a complete DataSeries in a Rotation script? Is something like this possible? Would it be cached correctly?

Thanks!
Vince

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

Eugene

#10
Let's start with why are you trying to define a DataSeries?
profile picture

Carova

#11
Simple - ease of coding. Using "roc[bar]" instead of "ROC.Series(Bars.Close, Period)[bar]" makes little difference if only one item is being used. But if a relatively large number of items/locations need to be coded the simplified form is to be preferred to improve readability and comprehension.

Vince

profile picture

Eugene

#12
I can't agree with that. A few lines higher or lower won't make a difference to me. As long as the series is being assigned within Set*/RestoreContext, of course.

On the surface it looks like the placement might work (except the error i.e. "New" vs "new") but you should verify it for yourself.
profile picture

Carova

#13
I will verify, but I was wondering what needed to be done to ensure that the series will be cached properly?

Vince
profile picture

Cone

#14
Indicator caching is part of the indicator construction. You don't have to worry about it. You can delete 2 statements and just use this one -

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

Eugene

#15
Re:
CODE:
Please log in to see this code.

...from which he essentially is trying to walk away (see post #1) ;)
profile picture

Carova

#16
It appears that the structure that I suggested works correctly, including proper caching.

Thanks for the help!

Vince
profile picture

Carova

#17
I seem to be doing something wrong. I am looping through my positions and attempting to see how long the positions are being held.

CODE:
Please log in to see this code.


But it appears that the BarsHeld is not incrementing as expected.

QUOTE:
12/6/2002 12:00:00 AM
496
12/13/2002 12:00:00 AM
496
...
13
5/25/2012 12:00:00 AM
13
6/1/2012 12:00:00 AM
13


What am I doing incorrectly?

Vince
profile picture

Eugene

#18
Vince,

Please review the QuickRef for BarsHeld and you'll know what's going on here (and it's not rotation specific).

What will work is something like this:
CODE:
Please log in to see this code.
profile picture

Carova

#19
Oops! Thanks for the info. Problem resolved.

Vince
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).