-
Notifications
You must be signed in to change notification settings - Fork 0
/
BollMean.cs
149 lines (138 loc) · 5.19 KB
/
BollMean.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
#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>
/// Mean Revertion strategy on boolean lines
/// </summary>
[Description("Mean Revertion strategy on boolean lines")]
public class BollMean : Strategy
{
#region Variables
// Wizard generated variables
private int bollBars = 125; // Default setting for BollBars
private double numberOfStd = 3.4; // Default setting for NumberOfStd
private double tickLimit = 43; // Default setting for TickLimit
private double tickStop = 19; // Default setting for TickStop
private int profitTarget = 0; // Default setting for limit position : open long tick differential and Default setting for open short tick differential
private double losstarget = 0.001; // define minimum standard deviation
private int positionSize = 3; // Default setting for PositionSize
//private double entryTarget = 0;
private double readStDev = 0; // make it a user input checks the std deviation value
// User defined variables (add any user defined variables below)
//private double limitDayLoss = -200;//-2000
//private double currentDayLoss = 0 ;
#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()
{
Add(Bollinger(NumberOfStd, BollBars));
SetProfitTarget("", CalculationMode.Ticks, TickLimit);
SetStopLoss("", CalculationMode.Ticks, TickStop, false);
CalculateOnBarClose = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
//currentDayLoss = GetAtmStrategyRealizedProfitLoss("id");
// if(currentDayLoss < limitDayLoss){
// positionSize = 0;
// AtmStrategyClose("idValue");
// }
readStDev = StdDev(bollBars)[0];
//Log("Current Standard dev = " + readStDev+ " at " + Time[0].ToString(),LogLevel.Information );
// Condition set 1
if (CrossBelow(Close, Bollinger(NumberOfStd, BollBars).Lower, 1)&& readStDev>=Losstarget)
{
EnterLongLimit(0,true,PositionSize, (Close[0] - 0.01*ProfitTarget) , "");
//entryTarget = Close[0] - 0.01*Losstarget;
//}
//if (entryTarget >= Close[0]){
//EnterLongStopLimit(PositionSize, ProfitTarget, Losstarget, "");
//EnterLong(PositionSize, "");
//entryTarget = 0;
//Log( "Created Long postition at " + DateTime.Now.ToString() + " " + Close[0].ToString(), NinjaTrader.Cbi.LogLevel.Alert);
}
if (CrossAbove(Close, Bollinger(NumberOfStd, BollBars).Upper, 1)&& readStDev>=Losstarget)
{
EnterShortLimit(0,true,PositionSize,(Close[0] + 0.01*profitTarget), "");
// Condition set 2
//if (CrossAbove(Close, Bollinger(NumberOfStd, BollBars).Upper, 1))
//{
// entryTarget = Close[0] + 0.01*profitTarget;
//}
//if(entryTarget <= Close[0]){
// EnterShort(PositionSize, "");
//entryTarget=0;
//Log( "Created Long postition at " + DateTime.Now.ToString() + " " + Close[0].ToString(), NinjaTrader.Cbi.LogLevel.Alert);
}
}
#region Properties
[Description("Number of previous bars to average")]
[GridCategory("Parameters")]
public int BollBars
{
get { return bollBars; }
set { bollBars = Math.Max(0, value); }
}
[Description("Number of Standard Deviation amplitude")]
[GridCategory("Parameters")]
public double NumberOfStd
{
get { return numberOfStd; }
set { numberOfStd = Math.Max(0, value); }
}
[Description("")]
[GridCategory("Parameters")]
public double TickLimit
{
get { return tickLimit; }
set { tickLimit = Math.Max(0, value); }
}
[Description("")]
[GridCategory("Parameters")]
public double TickStop
{
get { return tickStop; }
set { tickStop = Math.Max(0, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int ProfitTarget
{
get { return profitTarget; }
set { profitTarget = Math.Max(0, value); }
}
[Description("")]
[GridCategory("Parameters")]
public double Losstarget
{
get { return losstarget; }
set { losstarget = Math.Max(0, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int PositionSize
{
get { return positionSize; }
set { positionSize = Math.Max(0, value); }
}
#endregion
}
}