-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSI_ret_v14.cs
618 lines (589 loc) · 18.2 KB
/
RSI_ret_v14.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#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_v14 : Strategy //Ver 10 - Gold Optimized, but works for everything // Version 11 - W/ time intervals coded in .
{
#region Variables
// Wizard generated variables
private int entryLevelDif = 0;
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;
private int longRsiPeriod =9;
private int longRsiLength = 15;
private int unifiedLongerTRSI = 0;
private int turn15minIO = 1;
private int instantIOstr = 1;
private int instantIOltr = 1;
private int instantIOroc = 1;
// private int startingHour = 12;/
// private int startingMinute = 21;
// private int startingSecond = 01;
// 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 Double[] rsiArray15;
private Double[] rocArray;
private bool Unified15min = false;
private bool IO15min = true;
//private int timeOff = 1;
private bool InstantSTR = true;
private bool InstantLTR = true;
private bool InstantROC = true;
#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()
{
//sets the exit on close status, think it is superseeded by options chosen on the form.
SetProfitTarget("", CalculationMode.Ticks, Profit);
if(TurnExitOn ==1){
ExitOnClose= true;
} else if(TurnExitOn ==0){
ExitOnClose = false;
}
//sets trail or fixed stop IO
if(TurnTrailOn == 1){
SetTrailStop("", CalculationMode.Ticks, TrailStopLoss, false);
} else {
SetStopLoss(CalculationMode.Ticks,StopLoss);
}
CalculateOnBarClose = false;
//creates an array to keep recent RSI values - needed for the instant update stuff
rsiArray = new Double[storePreviousRsiMax];
rsiArray15 = new Double[storePreviousRsiMax];
rocArray = new Double[storePreviousRsiMax];
// adds the longer period data to then calculate the longer period RSI -
// - LongRSILength - originally 15 min - accessible through BarsArray[]
Add(PeriodType.Minute, LongRsiLength);
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected void getRsiRecentHigh(){
// method to update and load new RSI value to the array
// working perfectly
int len = storePreviousRsiMax;
for (int i = len-1; i > 0; i--){
rsiArray[i] = rsiArray[i-1];
}
rsiArray[0] = RSI(RSIPeriod,3)[0];
}
protected void getRsi15RecentHigh(){ // how about lows ? arent those important ?
int len = storePreviousRsiMax;
for (int i = len-1; i > 0; i--){
rsiArray15[i] = rsiArray15[i-1];
}
rsiArray15[0] = RSI(BarsArray[1],LongRsiPeriod,3)[0];
}
protected void getRocRecentHigh(){
int len = storePreviousRsiMax;
for (int i = len-1; i > 0; i--){
rocArray[i] = rocArray[i-1];
}
rocArray[0] = ROC(ROCPeriod)[0];
}
protected String getRsiArrayContents(){
//method to print current array values, only necessary as a test, not currently used
String result = "";
for(int i = 0; i < storePreviousRsiMax; i++){
result = result + " , " + rsiArray[i];
}
return result;
}
protected Boolean checkRoundNum(){
//Method to check that current price is not near a round number,
//it only works for ticks that move @ 0.01 -- doesn't work for gold yet
if(TurnCheckRoundNumOn==0){
RoundTickDist = 0;
}
String check = Close[0].ToString();
int len = check.Length-2;
double units =100; //here
if(len >= 3){
units = double.Parse(check.Substring(len,2))*0.01/TickSize; // here 4? instead of 2, for it to work on gold
}
if (units >= 100 -RoundTickDist || units <= roundTickDist){
return false;
} else {
return true;
}
}
protected bool rocFilter(){
if(TurnRocFilterOn == 0){
ROCDif = 0;
}
double maxROC;
if(InstantROC){
maxROC = Math.Max(Math.Abs(rocArray[0]),Math.Abs(rocArray[0]));// this only gets the max between current and previous
} else {
maxROC = 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(){
// if recent trade was loss, dont take trades in that direction for time = LastStopMin
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;
}
//test whether the recent loss was a long or a short and return that value;
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 checkOverBoughtRsi15min(){
//checks longer time period RSI is overbought
if(RSI(BarsArray[1],LongRsiPeriod,3)[0]>67 || !IO15min){
//Log("15 min RSI is long",LogLevel.Information);
return true;
} else {
return false;
}
}
protected bool checkOverSoldRsi15min(){
//checks longer time period RSI is oversold
if(RSI(BarsArray[1],LongRsiPeriod,3)[0]<33 || !IO15min){
//Log("15 min RSI is long",LogLevel.Information);
return true;
} else {
return false;
}
}
*/
protected bool checkOverBoughtRsi15min(){
if(InstantLTR){
if(rsiArray15[0]>67 || !IO15min){
//Log("15 min RSI is long",LogLevel.Information);
return true;
} else {
return false;
}
} else {
if(RSI(BarsArray[1],LongRsiPeriod,3)[0]>67 || !IO15min){
//Log("15 min RSI is long",LogLevel.Information);
return true;
} else {
return false;
}
}
}
protected bool checkOverSoldRsi15min(){
if(InstantLTR){
if (rsiArray15[0]<33 || !IO15min){
//Log("15 min RSI is long",LogLevel.Information);
return true;
} else {
return false;
}
} else {
if (RSI(BarsArray[1],LongRsiPeriod,3)[0]<33 || !IO15min){
//Log("15 min RSI is long",LogLevel.Information);
return true;
} else {
return false;
}
}
}
protected bool unified15min(){
if(Unified15min){
if(checkOverBoughtRsi15min()
|| checkOverSoldRsi15min()){
return true;
} else {
return false;
}
}else {//if(!Unified15min){
return false;
}
}
protected override void OnBarUpdate()
{
//booleans
#region
if(InstantIOstr == 1 ){
InstantSTR = true;
} else {
InstantSTR = false;
}
if(InstantIOltr == 1 ){
InstantLTR = true;
} else {
InstantLTR = false;
}
if(InstantIOroc == 1 ){
InstantROC = true;
} else {
InstantROC = false;
}
if(Turn15minIO == 1){
IO15min = true;
} else {
IO15min = false;
// makes sure there is no confusion by the code when 15minIO is off
//but Unified has been left on by diregard.
UnifiedLongerTRSI = 0;
}
if(UnifiedLongerTRSI == 1){
Unified15min = true;
} else {
Unified15min = false;
}
#endregion
//Load rsi values to array
getRsiRecentHigh();
getRsi15RecentHigh();
getRocRecentHigh();
//Print(" ROC: "+Math.Abs(ROC(ROCPeriod)[0])+" ; ROC instant: "+Math.Abs(rocArray[0])+" ; RSI15: "+RSI(BarsArray[1],LongRsiPeriod,3)[0]+" ; RSI15instant: "+rsiArray15[0]);
//checks for width of the Bid/Ask delta, doesnt work on backtest
//---- need to create Bars based on highs and lows for it to work ----
if((GetCurrentAsk()-GetCurrentBid())<BidAsk*TickSize
&& checkRoundNum()
//&& checkLongRsi()
){//&& checkDay()){
itsOn = 1;
} else {
itsOn = 0;
}
if(CurrentBar < RSIPeriod || itsOn == 0){
//stops the next code
return;
}
//reset limits
belowLimit = 100;
aboveLimit = 0;
//Log(getRsiArrayContents(),LogLevel.Information);
//condition for oversold
//makes sure an oposite direction trade is not placed if the current position is negative
//-- avoiding stopping out to open a new position
Boolean shortIsPositive = true;
if (Position.MarketPosition == MarketPosition.Short){
if( Position.GetProfitLoss(Close[0], PerformanceUnit.Points)<0
&& IsPositiveOn == 1){
shortIsPositive = false;
}
}
//checks if RSI is under the Oversold level
//if(RSI(RSIPeriod,3)[0]<OverSoldLevel){
if(rsiArray[0]<OverSoldLevel){
//sets the below limit for the instant previous tick
//-- can have an IO to use the original RSI function[1]
//belowLimit = rsiArray[1];
if(InstantSTR){
belowLimit = rsiArray[1];
} else {
belowLimit = RSI(RSIPeriod,3)[1];
}
if (CrossAbove(RSI(RSIPeriod,3), belowLimit, 1)
&& (RSI(RSIPeriod,3)[0]-belowLimit>=entryLevelDif ) // min distance from previous value it should be to enter trade
&& checkRecentLossDirection() != "Long"
&& (Close[0] < this.longPriceDif|| this.longPriceDif == 0) // connected to recent loss direction
&& shortIsPositive
&& rocFilter()
&& ((!Unified15min && checkOverSoldRsi15min())
|| Unified15min && unified15min()) // ---- need checking that is working
//&& timeframe() //deactivated
)
{
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 = rsiArray[1];
if(rsiArray[0]>OverBoughtLevel){
if(InstantSTR){
aboveLimit = rsiArray[1];
} else {
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()
// && checkOverBoughtRsi15min()
&& ((!Unified15min && checkOverBoughtRsi15min())
|| Unified15min && unified15min()) // ---- need checking that is working
//&& timeframe()
)
{
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 LongRsiLength
{
get { return longRsiLength ; }
set { longRsiLength = Math.Max(0, 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); }
}
[GridCategory("Parameters")]
public int UnifiedLongerTRSI
{
get { return unifiedLongerTRSI; }
set { unifiedLongerTRSI = Math.Max(0, value); }
}
[GridCategory("Parameters")]
public int Turn15minIO
{
get { return turn15minIO; }
set { turn15minIO = Math.Max(0, value); }
}
[GridCategory("Parameters")]
public int LongRsiPeriod
{
get { return longRsiPeriod; }
set { longRsiPeriod = Math.Max(0, value); }
}
[GridCategory("Parameters")]
public int InstantIOstr
{
get { return instantIOstr; }
set { instantIOstr = Math.Max(0, value); }
}
[GridCategory("Parameters")]
public int InstantIOltr
{
get { return instantIOltr; }
set { instantIOltr = Math.Max(0, value); }
}
[GridCategory("Parameters")]
public int InstantIOroc
{
get { return instantIOroc; }
set { instantIOroc = Math.Max(0, value); }
}
#endregion
/*
[GridCategory("Parameters")]
public int StartingHour
{
get { return startingHour; }
set { startingHour = Math.Max(0, value); }
}
[GridCategory("Parameters")]
public int StartingMinute
{
get { return startingMinute; }
set { startingMinute = Math.Max(0, value); }
}
[GridCategory("Parameters")]
public int StartingSecond
{
get { return startingSecond; }
set { startingSecond = Math.Max(0, value); }
}
*/
}
}