forked from calebsandfort/ninjatrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
THADiff.cs
176 lines (155 loc) · 5.57 KB
/
THADiff.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
a#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.DrawingTools;
#endregion
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class THADiff : Indicator
{
private Series<double> diffSeries;
private Brush BullishBrush = Brushes.DeepSkyBlue;
private Brush BearishBrush = Brushes.HotPink;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"THADiff";
Name = "THADiff";
Calculate = Calculate.OnBarClose;
IsOverlay = false;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
//See Help Guide for additional information.
IsSuspendedWhileInactive = true;
DiffMaLength = 15;
SmaThreshold = 0.4;
FastMaPeriod = 10;
SlowMaPeriod = 20;
AddPlot(Brushes.Orange, "DiffPlot");
Plots[0].Width= 2;
}
else if (State == State.Configure)
{
Stroke aStroke = new Stroke(Brushes.White, DashStyleHelper.Dash, 1);
AddLine(aStroke, SmaThreshold, "UpperThreshold");
AddLine(aStroke, -SmaThreshold, "LowerThreshold");
}
else if (State == State.DataLoaded)
{
diffSeries = new Series<double>(this);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < SlowMaPeriod) return;
double fastMa = EMA(FastMaPeriod)[0];
double slowMa = EMA(SlowMaPeriod)[0];
double diff = fastMa - slowMa;
diffSeries[0] = diff;
double diffMa = EMA(diffSeries, DiffMaLength)[0];
DiffPlot[0] = diff;
PlotBrushes[0][0] = (diff > SmaThreshold || diff < -SmaThreshold) ? (fastMa < slowMa ? BearishBrush : BullishBrush) : Brushes.White;
}
#region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="DiffMaLength", Order=1, GroupName="Parameters")]
public int DiffMaLength
{ get; set; }
[NinjaScriptProperty]
[Range(0, double.MaxValue)]
[Display(Name="SmaThreshold", Order=2, GroupName="Parameters")]
public double SmaThreshold
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="FastMaPeriod", Order=3, GroupName="Parameters")]
public int FastMaPeriod
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="SlowMaPeriod", Order=4, GroupName="Parameters")]
public int SlowMaPeriod
{ get; set; }
[Browsable(false)]
[XmlIgnore]
public Series<double> DiffPlot
{
get { return Values[0]; }
}
#endregion
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private THADiff[] cacheTHADiff;
public THADiff THADiff(int diffMaLength, double smaThreshold, int fastMaPeriod, int slowMaPeriod)
{
return THADiff(Input, diffMaLength, smaThreshold, fastMaPeriod, slowMaPeriod);
}
public THADiff THADiff(ISeries<double> input, int diffMaLength, double smaThreshold, int fastMaPeriod, int slowMaPeriod)
{
if (cacheTHADiff != null)
for (int idx = 0; idx < cacheTHADiff.Length; idx++)
if (cacheTHADiff[idx] != null && cacheTHADiff[idx].DiffMaLength == diffMaLength && cacheTHADiff[idx].SmaThreshold == smaThreshold && cacheTHADiff[idx].FastMaPeriod == fastMaPeriod && cacheTHADiff[idx].SlowMaPeriod == slowMaPeriod && cacheTHADiff[idx].EqualsInput(input))
return cacheTHADiff[idx];
return CacheIndicator<THADiff>(new THADiff(){ DiffMaLength = diffMaLength, SmaThreshold = smaThreshold, FastMaPeriod = fastMaPeriod, SlowMaPeriod = slowMaPeriod }, input, ref cacheTHADiff);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.THADiff THADiff(int diffMaLength, double smaThreshold, int fastMaPeriod, int slowMaPeriod)
{
return indicator.THADiff(Input, diffMaLength, smaThreshold, fastMaPeriod, slowMaPeriod);
}
public Indicators.THADiff THADiff(ISeries<double> input , int diffMaLength, double smaThreshold, int fastMaPeriod, int slowMaPeriod)
{
return indicator.THADiff(input, diffMaLength, smaThreshold, fastMaPeriod, slowMaPeriod);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.THADiff THADiff(int diffMaLength, double smaThreshold, int fastMaPeriod, int slowMaPeriod)
{
return indicator.THADiff(Input, diffMaLength, smaThreshold, fastMaPeriod, slowMaPeriod);
}
public Indicators.THADiff THADiff(ISeries<double> input , int diffMaLength, double smaThreshold, int fastMaPeriod, int slowMaPeriod)
{
return indicator.THADiff(input, diffMaLength, smaThreshold, fastMaPeriod, slowMaPeriod);
}
}
}
#endregion