-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSI_ret_v9_JustTue.cs
373 lines (354 loc) · 10.8 KB
/
RSI_ret_v9_JustTue.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// To localise the entry level of the RSI with respect to its extremes.
/// </summary>
[Description("To localise the entry level of the RSI with respect to its extremes.")]
public class RSI_ret_v9_JustTueOld : Strategy
{
#region Variables
// Wizard generated variables
private int entryLevelDif = 0; // Default setting for EntryLevelDif
private int stopLoss = 30; // Default setting for StopLoss
private int profit = 40; // Default setting for Profit
private int rSIPeriod = 11; // Default setting for RSIPeriod
private int roundTickDist = 0;
private int trailStopLoss = 0;
private int turnTrailOn = 0;
private int turnExitOn = 0;
private int lastStopMin = 141;
private int bidAsk = 3;
private int priceDif = 1;
private int isPositiveOn = 1;
private int rOCPeriod = 15;
private double rOCDif = 0.05;
private int overSoldLevel = 33;
private int overBoughtLevel = 67;
private int turnCheckRoundNumOn = 0;
private int turnRocFilterOn = 1;
private int turnLastStopMinOn = 0;
// User defined variables (add any user defined variables below)
private double aboveLimit = 0;
private double belowLimit = 100;
private int itsOn = 0;
private double longPriceDif =0;
private double shortPriceDif = 0;
private int storePreviousRsiMax = 5;
private Double[] rsiArray;
//private int timeOff = 1;
#endregion
/// <summary>
/// This method is used to configure the §strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
SetProfitTarget("", CalculationMode.Ticks, Profit);
if(TurnExitOn ==1){
ExitOnClose= true;
} else if(TurnExitOn ==0){
ExitOnClose = false;
}
if(TurnTrailOn == 1){
SetTrailStop("", CalculationMode.Ticks, TrailStopLoss, false);
} else {
SetStopLoss(CalculationMode.Ticks,StopLoss);
}
CalculateOnBarClose = false;
rsiArray = new Double[storePreviousRsiMax];
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected void getRsiRecentHigh(){
int len = storePreviousRsiMax;
for (int i = len-1; i > 0; i--){
rsiArray[i] = rsiArray[i-1];
}
rsiArray[0] = RSI(RSIPeriod,3)[0];
}
protected String getRsiArrayContents(){
String result = "";
for(int i = 0; i < storePreviousRsiMax; i++){
result = result + " , " + rsiArray[i];
}
return result;
}
protected Boolean checkRoundNum(){
if(TurnCheckRoundNumOn==0){
RoundTickDist = 0;
}
String check = Close[0].ToString();
int len = check.Length-2;
double units =100;
if(len >= 3){
units = double.Parse(check.Substring(len,2))*0.01/TickSize;
}
if (units >= 100 -RoundTickDist || units <= roundTickDist){
return false;
} else {
return true;
}
}
protected bool rocFilter(){
if(TurnRocFilterOn == 0){
ROCDif = 0;
}
double maxROC = Math.Max(Math.Abs(ROC(ROCPeriod)[0]),Math.Abs(ROC(ROCPeriod)[0]));
if(maxROC>ROCDif){
return true;
} else {
return false;
}
//Need to go through all the code and change double comparisons to something more exact
/*absolute max value of roc between previous and current bar
* is this less than optimized value?
* dont take the trade
* int ROC Period(already Defined)
* double Optimized value
*
*/
}
protected String checkRecentLossDirection(){
Boolean recentLoss = false;
// Check to make sure there is at least one trade in the collection
if (Performance.AllTrades.LosingTrades.Count > 0)
{
//references last trade
Trade lastTrade = Performance.AllTrades.LosingTrades[Performance.AllTrades.LosingTrades.Count -1 ];
//gets the difference in time between last losing and current time
TimeSpan losingTime =( Times[0][0].TimeOfDay - lastTrade.ExitExecution.Time.TimeOfDay );
//prints to log that difference
if(losingTime.TotalSeconds>0
&& losingTime.TotalSeconds < LastStopMin
&& TurnLastStopMinOn == 1){
recentLoss = true;
} else {
recentLoss = false;
}
if(recentLoss && lastTrade.Entry.MarketPosition == MarketPosition.Long){
this.longPriceDif=Close[0]-PriceDif*TickSize;
return "Long";
} else if(recentLoss && lastTrade.Entry.MarketPosition == MarketPosition.Short){
this.shortPriceDif=Close[0]+PriceDif*TickSize;
return "Short";
} else {
return "Nop";
}
}else {
return "Nop";
}
}
protected bool checkDay(){
if (Times[0][0].DayOfWeek == DayOfWeek.Tuesday){
//&& Times[0][0].Hour >= StartingHour && Times[0][0].Hour <= EndingHour){
return true;
} else {
return false;
}
}
protected override void OnBarUpdate()
{
if((GetCurrentAsk()-GetCurrentBid())<BidAsk*TickSize
&& checkRoundNum()
&& checkDay()){
itsOn = 1;
} else {
itsOn = 0;
}
if(CurrentBar < RSIPeriod || itsOn == 0){
return;
}
//reset limits
belowLimit = 100;
aboveLimit = 0;
//Load rsi values to array
//getRsiRecentHigh();
//Log(getRsiArrayContents(),LogLevel.Information);
//condition for oversold
Boolean shortIsPositive = true;
if (Position.MarketPosition == MarketPosition.Short){
if( Position.GetProfitLoss(Close[0], PerformanceUnit.Points)<0
&& IsPositiveOn == 1){
shortIsPositive = false;
}
}
if(RSI(RSIPeriod,3)[0]<OverSoldLevel){
belowLimit = RSI(RSIPeriod,3)[1];
if (CrossAbove(RSI(RSIPeriod,3), belowLimit, 1)
&& (RSI(RSIPeriod,3)[0]-belowLimit>=entryLevelDif )
&& checkRecentLossDirection() != "Long"
&& (Close[0] < this.longPriceDif|| this.longPriceDif == 0)
&& shortIsPositive
&& rocFilter())
{
this.longPriceDif = 0;
EnterLong(DefaultQuantity, "");
}
}
//condition for overbought
Boolean longIsPositive = true;
if (Position.MarketPosition == MarketPosition.Long){
if( Position.GetProfitLoss(Close[0], PerformanceUnit.Points)<0
&& IsPositiveOn == 1){
longIsPositive = false;
}
}
if(RSI(RSIPeriod,3)[0]>OverBoughtLevel){
aboveLimit = RSI(RSIPeriod,3)[1];
if (CrossBelow(RSI(RSIPeriod,3), aboveLimit, 1)
&& ( aboveLimit-RSI(RSIPeriod,3)[0]>=entryLevelDif)
&& checkRecentLossDirection() != "Short"
&& (Close[0] > this.shortPriceDif || this.shortPriceDif == 0)
&& longIsPositive
&& rocFilter())
{
this.shortPriceDif= 0;
EnterShort(DefaultQuantity, "");
}
}
}
#region Properties
[Description("Diferential from max/min to open position, in RSI percentage.")]
[GridCategory("Parameters")]
public int EntryLevelDif
{
get { return entryLevelDif; }
set { entryLevelDif = Math.Max(0, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int StopLoss
{
get { return stopLoss; }
set { stopLoss = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int Profit
{
get { return profit; }
set { profit = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int RSIPeriod
{
get { return rSIPeriod; }
set { rSIPeriod = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int RoundTickDist
{
get { return roundTickDist; }
set { roundTickDist = Math.Max(0, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int TrailStopLoss
{
get { return trailStopLoss; }
set { trailStopLoss = Math.Max(0, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int TurnTrailOn
{
get { return turnTrailOn; }
set { turnTrailOn = Math.Max(0, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int LastStopMin
{
get { return lastStopMin; }
set { lastStopMin = Math.Max(0, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int TurnExitOn
{
get { return turnExitOn; }
set { turnExitOn = Math.Max(0, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int PriceDif
{
get { return priceDif; }
set { priceDif = Math.Max(0, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int IsPositiveOn
{
get { return isPositiveOn; }
set { isPositiveOn = Math.Max(0, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int ROCPeriod
{
get { return rOCPeriod; }
set { rOCPeriod = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public double ROCDif
{
get { return rOCDif; }
set { rOCDif = Math.Max(0, value); }
}
[GridCategory("Parameters")]
public int BidAsk
{
get { return bidAsk; }
set { bidAsk = Math.Max(1, value); }
}
[GridCategory("Parameters")]
public int OverSoldLevel
{
get { return overSoldLevel; }
set { overSoldLevel = Math.Max(1, value); }
}
[GridCategory("Parameters")]
public int OverBoughtLevel
{
get { return overBoughtLevel; }
set { overBoughtLevel = Math.Max(1, value); }
}
[GridCategory("Parameters")]
public int TurnCheckRoundNumOn
{
get { return turnCheckRoundNumOn; }
set { turnCheckRoundNumOn = Math.Max(0, value); }
}
[GridCategory("Parameters")]
public int TurnLastStopMinOn
{
get { return turnLastStopMinOn; }
set { turnLastStopMinOn = Math.Max(0, value); }
}
[GridCategory("Parameters")]
public int TurnRocFilterOn
{
get { return turnRocFilterOn; }
set { turnRocFilterOn = Math.Max(0, value); }
}
#endregion
}
}