forked from gjh33/SmartStrategies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseSmartStrategy.cs
177 lines (167 loc) · 6.6 KB
/
BaseSmartStrategy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript.Strategies;
#endregion
namespace NinjaTrader.NinjaScript.SmartStrategies
{
/// <summary>
/// The class to derive all Smart Strategies from!
/// </summary>
public abstract class BaseSmartStrategy : Strategy
{
[NinjaScriptProperty]
[Display(Name="Verbose", Order=1, GroupName="Smart Strategy Settings", Description = "When enabled, trades will print status updates and debug information")]
public bool Verbose { get; set; }
[NinjaScriptProperty]
[Display(Name="Ignore Historical Data", Order=2, GroupName="Smart Strategy Settings", Description = "When enabled, historical data won't call OnUpdate()")]
public bool IgnoreHistoricalData { get; set; }
/// <summary>
/// Callback for when an order is updated.
/// You can use this for your own order management.
/// </summary>
public event Action<Order> OrderUpdated;
/// <summary>
/// The correct bars in progress index to submit trades to.
/// when backtesting this will return the index of the 1 tick chart
/// when using realtime data this will submit to the main chart
/// </summary>
public int BarsInProgressIndex
{
get
{
return State == State.Historical ? 1 : 0;
}
}
/// <summary>
/// Called during the Configure state of the strategy
/// </summary>
protected virtual void Configure() { }
/// <summary>
/// Called on bar update.
/// This method won't be on any data series except the primary.
/// This method won't be called until min bars are on the chart.
/// This method won't be called until CurrentBars[0] >= 1
/// </summary>
protected virtual void OnUpdate() { }
/// <summary>
/// Called on script termination
/// </summary>
protected virtual void Cleanup() { }
/// <summary>
/// Called when you should set the default values of your variables
/// </summary>
protected virtual void SetDefaults() { }
/// <summary>
/// Called once all data has been loaded
/// </summary>
protected virtual void OnDataLoaded() { }
/// <summary>
/// Called when the script has been activated
/// </summary>
protected virtual void OnActivated() { }
/// <summary>
/// Called when the strategy begins processing historical data
/// </summary>
protected virtual void OnBeginHistoricalData() { }
/// <summary>
/// Called when the strategy begins processing realtime data
/// </summary>
protected virtual void OnBeginRealtimeData() { }
/// <summary>
/// Called when historical data is finished processing
/// </summary>
protected virtual void OnHistoricalDataTransition() { }
sealed protected override void OnStateChange()
{
switch (State)
{
case State.SetDefaults:
// All the NT Defaults are set here
Description = "A strategy built using the Smart Strategies framework!";
Name = "MyCustomSmartStrategy";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
IsUnmanaged = true;
// Smart Strategies don't currently support this, but will in the future
IsInstantiatedOnEachOptimizationIteration = true;
VPrint("LC: Setting Defaults");
SetDefaults();
break;
case State.Configure:
AddDataSeries(BarsPeriodType.Tick, 1);
VPrint("LC: Configuring");
Configure();
break;
case State.DataLoaded:
VPrint("LC: On Data Loaded");
OnDataLoaded();
break;
case State.Active:
VPrint("LC: On Activated");
OnActivated();
break;
case State.Terminated:
VPrint("LC: Cleanup");
Cleanup();
break;
case State.Historical:
VPrint("LC: On Begin Historical Data");
OnBeginHistoricalData();
break;
case State.Realtime:
VPrint("LC: On Begin Realtime Data");
OnBeginRealtimeData();
break;
case State.Transition:
VPrint("LC: On Transition");
OnHistoricalDataTransition();
break;
}
}
sealed protected override void OnBarUpdate()
{
if (CurrentBar <= BarsRequiredToTrade)
return;
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
if (IgnoreHistoricalData && State == State.Historical)
return;
OnUpdate();
}
sealed protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
{
if (OrderUpdated != null) OrderUpdated(order);
}
public void VPrint(object value)
{
if (Verbose) Print(string.Join("", "[", DateTime.Now.ToString("G"), "] ", value));
}
}
}