-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathRenkoStrategy.cs
244 lines (210 loc) · 6.18 KB
/
RenkoStrategy.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
#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;
using System.Globalization;
#endregion
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class RenkoStrategy : Strategy
{
private bool[] EntryBarArray;
private bool[] ExitBarArray;
private bool adhoc;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"The Renko Strategy as published in December 2019 Technical Analysis of Stocks and Commodities article titled 'Using Renko Charts' by John Devcic";
Name = "RenkoStrategy";
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 = 20;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
StopLossValue = 10;
EntryStrength = 2;
ExitStrength = 3;
AllowLong = true;
AllowShort = false;
UseExitStrength = false;
UseTrailStop = true;
}
else if (State == State.Configure)
{
EntryBarArray = new bool[EntryStrength];
for(int i = 0; i < EntryStrength; i++)
EntryBarArray[i] = false;
ExitBarArray = new bool[ExitStrength];
for(int i = 0; i < ExitStrength; i++)
ExitBarArray[i] = false;
if(UseTrailStop)
SetTrailStop(CalculationMode.Ticks, StopLossValue);
}
}
protected override void OnBarUpdate()
{
if(BarsPeriod.BarsPeriodType != BarsPeriodType.Renko)
{
Draw.TextFixed(this, "NinjaScriptInfo", "The RenkoStrategy must be ran on a Renko chart.", TextPosition.BottomRight);
return;
}
if(CurrentBar < EntryStrength || CurrentBar < ExitStrength)
return;
if(Position.MarketPosition == MarketPosition.Flat)
{
for(int i = 0; i < EntryStrength; i++)
EntryBarArray[i] = false;
for(int i = EntryStrength-1; i >=0; i--)
{
if(Close[i] > Open[i])
{
EntryBarArray[i] = true;
}
else
{
EntryBarArray[i] = false;
}
}
adhoc = true;
foreach(bool b in EntryBarArray)
{
adhoc = adhoc && b;
}
if(adhoc && AllowLong)
{
EnterLong();
}
else
{
for(int i = 0; i < EntryStrength; i++)
EntryBarArray[i] = false;
for(int i = EntryStrength-1; i >=0; i--)
{
if(Close[i] < Open[i])
{
EntryBarArray[i] = true;
}
else
{
EntryBarArray[i] = false;
}
}
adhoc = true;
foreach(bool b in EntryBarArray)
{
adhoc = adhoc && b;
}
if(adhoc && AllowShort)
{
EnterShort();
}
}
}
for(int i = 0; i < ExitStrength; i++)
ExitBarArray[i] = false;
if(Position.MarketPosition == MarketPosition.Long && BarsSinceEntryExecution() >= 1)
{
for(int i = ExitStrength-1; i >=0; i--)
{
if(Close[i] < Open[i])
{
ExitBarArray[i] = true;
}
}
adhoc = true;
foreach(bool b in ExitBarArray)
{
adhoc = adhoc && b;
}
if(adhoc && UseExitStrength)
{
ExitLong();
}
}
else if(Position.MarketPosition == MarketPosition.Short && BarsSinceEntryExecution() >= 1)
{
for(int i = ExitStrength-1; i >=0; i--)
{
if(Close[i] > Open[i])
{
ExitBarArray[i] = true;
}
}
adhoc = true;
foreach(bool b in ExitBarArray)
{
adhoc = adhoc && b;
}
if(adhoc && UseExitStrength)
{
ExitShort();
}
}
}
#region Properties
[NinjaScriptProperty]
[Range(0, double.MaxValue)]
[Display(Name="Stop Loss Value", Description="The value for the stop loss order.", Order=0, GroupName="Stop Loss Setup")]
public double StopLossValue
{ get; set; }
[NinjaScriptProperty]
[Range(0, int.MaxValue)]
[Display(Name="Strength for entry", Description="The number of subseaquent up or down bars needed for an entry.", Order=1, GroupName="Parameters")]
public int EntryStrength
{ get; set; }
[NinjaScriptProperty]
[Range(0, int.MaxValue)]
[Display(Name="Strength for exit", Description="The number of subseaquent up or down bars needed for an exit.", Order=2, GroupName="Parameters")]
public int ExitStrength
{ get; set; }
[NinjaScriptProperty]
[Display(Name="Use Exit Strength", Description="Enable Exit Strength Exits", Order=3, GroupName="Parameters")]
public bool UseExitStrength
{ get; set; }
[NinjaScriptProperty]
[Display(Name="Use Trail Stop", Description="Enable Trail Stop Exits", Order=4, GroupName="Parameters")]
public bool UseTrailStop
{ get; set; }
[NinjaScriptProperty]
[Display(Name="Allow Long Trades", Description="Enable Long Trades", Order=5, GroupName="Parameters")]
public bool AllowLong
{ get; set; }
[NinjaScriptProperty]
[Display(Name="Allow Short Trades", Description="Enable Short Trades", Order=6, GroupName="Parameters")]
public bool AllowShort
{ get; set; }
#endregion
}
}