IsInstantiatedOnEachOptimizationIteration

View as Markdown

Definition

Determines if the strategy should be re-instantiated (re-created) after each optimization run when using the Strategy Analyzer Optimizer.

The default behavior is to re-instantiate the strategy for each optimization backtest run. However, the process of re-instantiating a strategy requires more time and computer resources to return results, which could impact the amount of time it takes to run an optimization. When false, the strategy is re-used to save time and computer resources. Under this design, internal properties are reset to default values after each iteration, but it is possible that user-defined properties and other custom resources may carry their state over from the previous iteration into a new backtest run. To take advantage of performance optimizations, developers may need to reset class level variables in the strategy otherwise unexpected results can occur.

If you choose to take advantage of the performance benefits during strategy optimization by setting the IsInstantiatedOnEachOptimizationIteration property to false, any objects you create in your code MUST be reset during the appropriate State within the OnStateChange() method. Please see the example below on “Manually resetting class level variables to take advantage of Strategy Analyzer optimizer performance benefits”.

Property Value

This property returns true if the strategy is not recycled; otherwise, false. Default set to true.

This property should ONLY be set from the OnStateChange() method during State.SetDefaults or State.Configure.

Syntax

IsInstantiatedOnEachOptimizationIteration

Examples

Using IsInstantiatedOnEachOptimizationIteration to reset class level variables

1// A custom trades dictionary is created when strategy is instantiated
2// since we later set "IsInstantiatedOnEachOptimizationIteration" to true,
3// we are guaranteed to start with a new object on each optimization run
4private Dictionary<datetime, string=""> myTrades = new Dictionary<datetime, string="">();
5protected override void OnStateChange()
6{
7if (State == State.SetDefaults)
8{
9 Name = "My Optimization Test 1";
10 Description = "Demonstrates using IsInstantiatedOnEachOptimizationIteration to reset a class level variable";
11 Fast = 10;
12 Slow = 25;
13 // setting to true so our custom trades dictionary is reset on each optimization run (comes with a performance penalty)
14 // This is the default behavior.
15 IsInstantiatedOnEachOptimizationIteration = true;
16}
17else if (State == State.Terminated)
18{
19 // Print the number of trades at the end of the optimization
20 if (myTrades != null)
21 {
22 // if we set "IsInstantiatedOnEachOptimizationIteration" to false (so not using the default of true), the values here would be unexpected
23 // since the custom trade dictionary was never explicitly reset at the end of each optimization
24 Print(myTrades.Count);
25 }
26}
27}
28protected override void OnBarUpdate()
29{
30if (CurrentBar < BarsRequiredToTrade)
31 return;
32if (CrossAbove(SMA(Fast), SMA(Slow), 1))
33{
34 EnterLong();
35 myTrades.Add(Time[0], "long");
36}
37else if (CrossBelow(SMA(Fast), SMA(Slow), 1))
38{
39 EnterShort();
40 myTrades.Add(Time[0], "short");
41}
42}
43[Range(1, int.MaxValue), NinjaScriptProperty]
44[Display(Name = "Fast", GroupName = "NinjaScriptStrategyParameters", Order = 0)]
45public int Fast
46{ get; set; }
47[Range(1, int.MaxValue), NinjaScriptProperty]
48[Display(Name = "Slow", GroupName = "NinjaScriptStrategyParameters", Order = 1)]
49public int Slow
50{ get; set; }

Manually resetting class level variables to take advantage of Strategy Analyzer optimizer performance benefits

1// A custom trades dictionary is declared when strategy is first optimized,
2// but not instantiated until later in State.DataLoaded,
3private Dictionary<datetime, string=""> myTrades;
4// examples of other fields which need to be reset
5private double myDouble;
6private bool myBool;
7private DateTime myDateTime;
8private Order myOrderObject;
9private Brush myBrushObject;
10private SMA mySMAIndicator;
11private Array myIntArray;
12private List<object> myList;
13private Series<double> mySeries;
14protected override void OnStateChange()
15{
16if (State == State.SetDefaults)
17{
18 Name = "My Optimization Test 2";
19 Description = "Demonstrates manually resetting a class level variable without re-instantiating the strategy";
20 Fast = 10;
21 Slow = 25;
22 // in this case, we do not need to re-instantiate the strategy after each optimization
23 // because we are explicitly resetting the custom trade dictionary in State.DataLoaded
24 // This design of re-using the strategy instance comes with performance benefits
25 **IsInstantiatedOnEachOptimizationIteration** = false;
26}
27else if (State == State.DataLoaded)
28{
29 // re-create custom trade dictionary on each optimization run
30 // we are guaranteed to start with a new object on each optimization run
31 if (myTrades != null)
32 myTrades.Clear();
33 else
34 myTrades = new Dictionary<datetime, string="">();
35 //Any strategy defaults which are maintained do not need to be reset if they are not mutable as the strategy runs.
36 //Any strategy state that would be mutable after State.SetDefaults needed to be reset for the next run.
37 myDouble = double.MinValue;
38 myBool = false;
39 myDateTime = DateTime.MinValue;
40 myOrderObject = null;
41 myBrushObject = null;
42 mySMAIndicator = SMA(14);
43 if (myIntArray != null)
44 Array.Clear(myIntArray, 0, myIntArray.Length);
45 else
46 myIntArray = new int[20];
47 if (myList != null)
48 myList.Clear();
49 else
50 myList = new List<object>();
51 mySeries = new Series<double>(this);
52}
53}
54protected override void OnBarUpdate()
55{
56if (CurrentBar < BarsRequiredToTrade)
57 return;
58if (CrossAbove(SMA(Fast), SMA(Slow), 1))
59{
60 EnterLong();
61 myTrades.Add(Time[0], "long");
62}
63else if (CrossBelow(SMA(Fast), SMA(Slow), 1))
64{
65 EnterShort();
66 myTrades.Add(Time[0], "short");
67}
68}
69[Range(1, int.MaxValue), NinjaScriptProperty]
70[Display(Name = "Fast", GroupName = "NinjaScriptStrategyParameters", Order = 0)]
71public int Fast
72{ get; set; }
73[Range(1, int.MaxValue), NinjaScriptProperty]
74[Display(Name = "Slow", GroupName = "NinjaScriptStrategyParameters", Order = 1)]
75public int Slow
76{ get; set; }