-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathVWAPAnticipatedMove.cs
326 lines (274 loc) · 10.3 KB
/
VWAPAnticipatedMove.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#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.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
/// <summary>
/// Depends on the VWAP8 indicator which is available via community indicators
/// </summary>
public class VWAPAnticipatedMove : Strategy
{
private VWAP8 vwap;
double priorAveragePrice = 0;
double buffer = 0;
double calDays = 30;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"This strategy leverages the VIX to calculated an expected move range up to 4 standard deviations and trades accordingly. ";
Name = "VWAPAnticipatedMove";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = false;
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 = 32;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = false;
exitLongSD05 = true;
exitShortSD05 = true;
profitTaker = 25;
stopLoss = 1024;
AddPlot(Brushes.Red, "SD0"); //0
AddPlot(Brushes.Red, "SD1"); //1
AddPlot(Brushes.Red, "SD2"); //2
AddPlot(Brushes.Red, "SD3"); //3
AddPlot(Brushes.Red, "SD4"); //4
AddPlot(Brushes.LimeGreen, "SD-0"); //5
AddPlot(Brushes.LimeGreen, "SD-1"); //6
AddPlot(Brushes.LimeGreen, "SD-2"); //7
AddPlot(Brushes.LimeGreen, "SD-3"); //8
AddPlot(Brushes.LimeGreen, "SD-5"); //9
}
else if (State == State.Configure)
{
AddDataSeries("VX 12-20", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last); //1
SetStopLoss(CalculationMode.Ticks, stopLoss);
SetProfitTarget(CalculationMode.Ticks, profitTaker);
}
else if (State == State.DataLoaded)
{
vwap = VWAP8();
vwap.Plots[0].Brush = Brushes.White;
VOL().Panel = 2;
//add the indicators
AddChartIndicator(vwap);
AddChartIndicator(CamarillaPivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20));
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < (BarsRequiredToTrade))
return;
if(BarsPeriod.BarsPeriodType != BarsPeriodType.Renko)
{
Draw.TextFixed(this, "NinjaScriptInfo", "The RenkoStrategy must be ran on a Renko chart.", TextPosition.BottomRight);
return;
}
double cR1 = 0;
double cR3 = 0;
double cR4 = 0;
double cS1 = 0;
double cS3 = 0;
double cS4 = 0;
// Evaluates that this is a valid pivot point value
if (CamarillaPivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).S4.IsValidDataPoint(0))
{
// Prints the current pivot point value
cR1 = CamarillaPivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R1[0];
cR3 = CamarillaPivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R3[0];
cR4 = CamarillaPivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R4[0];
cS1 = CamarillaPivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).S1[0];
cS3 = CamarillaPivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).S3[0];
cS4 = CamarillaPivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).S4[0];
}
// Resets the stop loss to the original value when all positions are closed
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, stopLoss*4);
SetProfitTarget(CalculationMode.Ticks, profitTaker*4);
}
// If a position is open, allow for stop loss modification to breakeven
else if (Position.Quantity > 0)
{
// Once the price is greater than entry price+profit taker, set stop loss
if (priorAveragePrice != Position.AveragePrice)
{
if (Position.MarketPosition == MarketPosition.Long)
{
SetProfitTarget(CalculationMode.Price, Position.AveragePrice + profitTaker);
SetStopLoss(CalculationMode.Price, cS4);
}
if (Position.MarketPosition == MarketPosition.Short)
{
SetProfitTarget(CalculationMode.Price, Position.AveragePrice - profitTaker);
SetStopLoss(CalculationMode.Price, cR4);
}
priorAveragePrice = Position.AveragePrice;
}
}
//calculate the standard deviation
double sd = (vwap[0] * (Closes[1][0] / 100) * Math.Sqrt(calDays / 365)) / calDays;
Print("VX: " + Closes[1][0].ToString() + " SD: " + sd.ToString());
//calculate the offsets from the vwap
//above
Values[0][0] = vwap[0] + (sd * 0.5);
Values[1][0] = vwap[0] + sd;
Values[2][0] = vwap[0] + (sd * 2);
Values[3][0] = vwap[0] + (sd * 3);
Values[4][0] = vwap[0] + (sd * 4);
//below
Values[5][0] = vwap[0] - (sd * 0.5);
Values[6][0] = vwap[0] - sd;
Values[7][0] = vwap[0] - (sd * 2);
Values[8][0] = vwap[0] - (sd * 3);
Values[9][0] = vwap[0] - (sd * 4);
///********************************************************************************************************
//determine the long signals
///********************************************************************************************************
#region LongSignals
bool long0 = false;
bool long1 = false;
bool long2 = false;
bool long3 = false;
bool long4 = false;
if (Position.Quantity == 0
&& Close[0] < Values[5][0]
&& Close[0] > Values[6][0] && Close[0] < cR1
&& cS4 < Values[7][0])
{
long0 = true;
}
if (Position.Quantity == 1 && Close[0] < Values[6][0] )
{
long1 = true;
}
if (Position.Quantity == 2 && Close[0] < Values[7][0] )
{
long2 = true;
}
if (Position.Quantity == 3 && Close[0] < Values[8][0] )
{
long3 = true;
}
if (Position.Quantity >= 4 && Close[0] < Values[9][0] && CurrentBar > buffer )
{
long4 = true;
buffer = CurrentBar + 15;
}
//long exit logic
bool exitLong = false;
if (exitLongSD05 && Position.MarketPosition == MarketPosition.Long && Close[0] > Values[0][0]) exitLong = true;
if (!exitLongSD05 && Position.MarketPosition == MarketPosition.Long && Close[0] > vwap[0]) exitLong = true;
#endregion //LongSignals
///********************************************************************************************************
//determine the short signals
///********************************************************************************************************
#region ShortSignals
bool short0 = false;
bool short1 = false;
bool short2 = false;
bool short3 = false;
bool short4 = false;
if ((Position.Quantity == 0
&& Close[0] > Values[0][0]
&& Close[0] < Values[1][0]
&& Close[0] > cS1
&& cR4 > Values[2][0]
) ||
(exitLong
&& Close[0] > Values[0][0]
&& Close[0] < Values[1][0]
&& Close[0] > cS1
&& cR4 > Values[2][0]))
{
short0 = true;
}
if (Position.MarketPosition == MarketPosition.Short && Position.Quantity == 1 && Close[0] > Values[1][0] )
{
short1 = true;
}
if (Position.MarketPosition == MarketPosition.Short && Position.Quantity == 2 && Close[0] > Values[2][0] )
{
short2 = true;
}
if (Position.MarketPosition == MarketPosition.Short && Position.Quantity == 3 && Close[0] > Values[3][0] )
{
short3 = true;
}
if (Position.MarketPosition == MarketPosition.Short && Position.Quantity >= 4 && Close[0] > Values[4][0] && CurrentBar > buffer )
{
short4 = true;
buffer = CurrentBar + 15;
}
//short exit logic
bool exitShort = false;
if (exitShortSD05 && Position.MarketPosition == MarketPosition.Short && Close[0] < Values[5][0]) exitShort = true;
if (!exitShortSD05 && Position.MarketPosition == MarketPosition.Short && Close[0] < vwap[0]) exitShort = true;
#endregion //ShortSignals
//execute the entries
if ((Position.MarketPosition == MarketPosition.Flat || Position.MarketPosition == MarketPosition.Long) && (long0 || long1 || long2 || long3 || long4))
EnterLong();
if (exitLong)
ExitLong();
if ((Position.MarketPosition == MarketPosition.Flat || Position.MarketPosition == MarketPosition.Short) && (short0 || short1 || short2 || short3 || short4))
EnterShort();
if (exitShort)
ExitShort();
}
#region Properties
[NinjaScriptProperty]
[Display(Name="Exit Long SD+0.5", Description="Exit VWAP + 0.5 SD, else VWAP", Order=1, GroupName="Parameters")]
public bool exitLongSD05
{ get; set; }
[NinjaScriptProperty]
[Display(Name="Exit Short SD-0.5", Description="Exit VWAP - 0.5 SD, else VWAP", Order=2, GroupName="Parameters")]
public bool exitShortSD05
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Profit Taker Points", Description="Points to profit take at", Order=9, GroupName="Parameters")]
public int profitTaker
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Stop Loss Points", Description="Points to set stop loss at", Order=10, GroupName="Parameters")]
public int stopLoss
{ get; set; }
#endregion
}
}