- ago
I implemented a rotation-based strategy according to: https://www.wealth-lab.com/Discussion/Rotation-Strategy-Bi-Weekly-scale-9516

During debugging I found a strange behaviour of the following snippet:

CODE:
public class MainStrategy : UserStrategyBase    {       public MainStrategy()       {}       int preExecuteRun = 0;       public override void PreExecute(DateTime date, List<BarHistory> allBars)       {          WriteToDebugLog(preExecuteRun++, false);          WriteToDebugLog(date, false);       }       public override void Execute(BarHistory bars, int bar)       {       }    }


When I start test on 20 years DOW daily data source, I will get the following output (only the strange part):

QUOTE:
3270
25.08.2017 0:00:00
3271
28.08.2017 0:00:00
3272
29.08.2017 0:00:00
3273
30.08.2017 0:00:00
3274
31.08.2017 0:00:00
0
01.09.2017 0:00:00
1
05.09.2017 0:00:00
2
06.09.2017 0:00:00
3
07.09.2017 0:00:00
4
08.09.2017 0:00:00
5
11.09.2017 0:00:00


It looks like the preExecuteRun variable's value is reset. Can you please explain why this happens and how to avoid this?

Thank you!
0
83
Solved
4 Replies

Reply

Bookmark

Sort
Glitch8
 ( 10.33% )
- ago
#1
Yes, it's happening because each symbol gets its own instance of the Strategy class. PreExecute is executed once per group of symbols, but at some point one of the symbols is no longer a participant, so another instance takes over.

To avoid this, make the variable static.
0
Best Answer
- ago
#2
Thank you for the suggestion!

Another question, you told, that "each symol gets its own instance", but when I specify True as the second parameter for WriteToDebugLog, I can see only two symbols in the Debug log: BA and MTLQQ.20110331.C, why is it so?
0
Cone8
 ( 7.88% )
- ago
#3
If you don't groupBySymbol, then it just picks one for the output somewhat randomly.
Try this -
CODE:
         bool groupBySymbol = true;          WriteToDebugLog(preExecuteRun++, groupBySymbol);          WriteToDebugLog(date, groupBySymbol);
0
Glitch8
 ( 10.33% )
- ago
#4
It's because in some of the runs, MTLQQ.20110331.C was not present in the Participants list. PreExecute is executed in the context of one of the symbol Strategy instances in the Participants list.
0

Reply

Bookmark

Sort