-
Notifications
You must be signed in to change notification settings - Fork 11
/
@HeikenAshi.cs
380 lines (333 loc) · 13.2 KB
/
@HeikenAshi.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
//
// Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>.
// NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
//
#region Using declarations
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using System.Xml.Serialization;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
#endregion
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// HeikenAshi technique discussed in the article 'Using Heiken-Ashi Technique' in February 2004 issue of TASC magazine.
/// </summary>
[Description("HeikenAshi technique discussed in the article 'Using Heiken-Ashi Technique' in February 2004 issue of TASC magazine.")]
public class HeikenAshi : Indicator
{
#region Variables
private Color barColorDown = Color.Red;
private Color barColorUp = Color.Lime;
private SolidBrush brushDown = null;
private SolidBrush brushUp = null;
private Color shadowColor = Color.Black;
private Pen shadowPen = null;
private int shadowWidth = 1;
#endregion
/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
Add(new Plot(Color.Gray, PlotStyle.Line, "HAOpen"));
Add(new Plot(Color.Gray, PlotStyle.Line, "HAHigh"));
Add(new Plot(Color.Gray, PlotStyle.Line, "HALow"));
Add(new Plot(Color.Gray, PlotStyle.Line, "HAClose"));
PaintPriceMarkers = false;
CalculateOnBarClose = false;
PlotsConfigurable = false;
Overlay = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (Displacement + (CalculateOnBarClose ? 1 : 0) > 0 && CurrentBar > 0 && BarColorSeries[1] != Color.Transparent)
InitColorSeries();
BarColorSeries.Set(Math.Max(0, CurrentBar + Math.Max(0, Displacement) + (CalculateOnBarClose ? 1 : 0)), Color.Transparent);
CandleOutlineColorSeries.Set(Math.Max(0, CurrentBar + Math.Max(0, Displacement) + (CalculateOnBarClose ? 1 : 0)), Color.Transparent);
if (CurrentBar == 0)
{
HAOpen.Set(Open[0]);
HAHigh.Set(High[0]);
HALow.Set(Low[0]);
HAClose.Set(Close[0]);
return;
}
HAClose.Set((Open[0] + High[0] + Low[0] + Close[0]) * 0.25); // Calculate the close
HAOpen.Set((HAOpen[1] + HAClose[1]) * 0.5); // Calculate the open
HAHigh.Set(Math.Max(High[0], HAOpen[0])); // Calculate the high
HALow.Set(Math.Min(Low[0], HAOpen[0])); // Calculate the low
}
#region Properties
[Browsable(false)]
[XmlIgnore]
public DataSeries HAOpen
{
get { return Values[0]; }
}
[Browsable(false)]
[XmlIgnore]
public DataSeries HAHigh
{
get { return Values[1]; }
}
[Browsable(false)]
[XmlIgnore]
public DataSeries HALow
{
get { return Values[2]; }
}
[Browsable(false)]
[XmlIgnore]
public DataSeries HAClose
{
get { return Values[3]; }
}
[XmlIgnore]
[Description("Color of down bars.")]
[Category("Visual")]
[Gui.Design.DisplayNameAttribute("Down color")]
public Color BarColorDown
{
get { return barColorDown; }
set { barColorDown = value; }
}
/// <summary>
/// </summary>
[Browsable(false)]
public string BarColorDownSerialize
{
get { return Gui.Design.SerializableColor.ToString(barColorDown); }
set { barColorDown = Gui.Design.SerializableColor.FromString(value); }
}
/// <summary>
/// </summary>
[XmlIgnore]
[Description("Color of up bars.")]
[Category("Visual")]
[Gui.Design.DisplayNameAttribute("Up color")]
public Color BarColorUp
{
get { return barColorUp; }
set { barColorUp = value; }
}
/// <summary>
/// </summary>
[Browsable(false)]
public string BarColorUpSerialize
{
get { return Gui.Design.SerializableColor.ToString(barColorUp); }
set { barColorUp = Gui.Design.SerializableColor.FromString(value); }
}
/// <summary>
/// </summary>
[XmlIgnore]
[Description("Color of shadow line.")]
[Category("Visual")]
[Gui.Design.DisplayNameAttribute("Shadow color")]
public Color ShadowColor
{
get { return shadowColor; }
set { shadowColor = value; }
}
/// <summary>
/// </summary>
[Browsable(false)]
public string ShadowColorSerialize
{
get { return Gui.Design.SerializableColor.ToString(shadowColor); }
set { shadowColor = Gui.Design.SerializableColor.FromString(value); }
}
/// <summary>
/// </summary>
[Description("Width of shadow line.")]
[Category("Visual")]
[Gui.Design.DisplayNameAttribute("Shadow width")]
public int ShadowWidth
{
get { return shadowWidth; }
set { shadowWidth = Math.Max(value, 1); }
}
#endregion
#region Miscellaneous
private void InitColorSeries()
{
for (int i = 0; i <= CurrentBar + Displacement + (CalculateOnBarClose ? 1 : 0); i++)
{
BarColorSeries.Set(i, Color.Transparent);
CandleOutlineColorSeries.Set(i, Color.Transparent);
}
}
protected override void OnStartUp()
{
if (ChartControl == null || Bars == null)
return;
brushUp = new SolidBrush(barColorUp);
brushDown = new SolidBrush(barColorDown);
shadowPen = new Pen(shadowColor, shadowWidth);
}
protected override void OnTermination()
{
if (brushUp != null) brushUp.Dispose();
if (brushDown != null) brushDown.Dispose();
if (shadowPen != null) shadowPen.Dispose();
}
public override void GetMinMaxValues(ChartControl chartControl, ref double min, ref double max)
{
if (Bars == null || ChartControl == null)
return;
for (int idx = FirstBarIndexPainted; idx <= LastBarIndexPainted; idx++)
{
double tmpHigh = HAHigh.Get(idx);
double tmpLow = HALow.Get(idx);
if (tmpHigh != 0 && tmpHigh > max)
max = tmpHigh;
if (tmpLow != 0 && tmpLow < min)
min = tmpLow;
}
}
public override void Plot(Graphics graphics, Rectangle bounds, double min, double max)
{
if (Bars == null || ChartControl == null)
return;
int barPaintWidth = Math.Max(3, 1 + 2 * ((int)Bars.BarsData.ChartStyle.BarWidth - 1) + 2 * shadowWidth);
for (int idx = FirstBarIndexPainted; idx <= LastBarIndexPainted; idx++)
{
if (idx - Displacement < 0 || idx - Displacement >= BarsArray[0].Count || (!ChartControl.ShowBarsRequired && idx - Displacement < BarsRequired))
continue;
double valH = HAHigh.Get(idx);
double valL = HALow.Get(idx);
double valC = HAClose.Get(idx);
double valO = HAOpen.Get(idx);
int x = ChartControl.GetXByBarIdx(BarsArray[0], idx);
int y1 = ChartControl.GetYByValue(this, valO);
int y2 = ChartControl.GetYByValue(this, valH);
int y3 = ChartControl.GetYByValue(this, valL);
int y4 = ChartControl.GetYByValue(this, valC);
graphics.DrawLine(shadowPen, x, y2, x, y3);
if (y4 == y1)
graphics.DrawLine(shadowPen, x - barPaintWidth / 2, y1, x + barPaintWidth / 2, y1);
else
{
if (y4 > y1)
graphics.FillRectangle(brushDown, x - barPaintWidth / 2, y1, barPaintWidth, y4 - y1);
else
graphics.FillRectangle(brushUp, x - barPaintWidth / 2, y4, barPaintWidth, y1 - y4);
graphics.DrawRectangle(shadowPen, (x - barPaintWidth / 2) + (shadowPen.Width / 2), Math.Min(y4, y1), barPaintWidth - shadowPen.Width, Math.Abs(y4 - y1));
}
}
}
#endregion
}
}
#region NinjaScript generated code. Neither change nor remove.
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
public partial class Indicator : IndicatorBase
{
private HeikenAshi[] cacheHeikenAshi = null;
private static HeikenAshi checkHeikenAshi = new HeikenAshi();
/// <summary>
/// HeikenAshi technique discussed in the article 'Using Heiken-Ashi Technique' in February 2004 issue of TASC magazine.
/// </summary>
/// <returns></returns>
public HeikenAshi HeikenAshi()
{
return HeikenAshi(Input);
}
/// <summary>
/// HeikenAshi technique discussed in the article 'Using Heiken-Ashi Technique' in February 2004 issue of TASC magazine.
/// </summary>
/// <returns></returns>
public HeikenAshi HeikenAshi(Data.IDataSeries input)
{
if (cacheHeikenAshi != null)
for (int idx = 0; idx < cacheHeikenAshi.Length; idx++)
if (cacheHeikenAshi[idx].EqualsInput(input))
return cacheHeikenAshi[idx];
lock (checkHeikenAshi)
{
if (cacheHeikenAshi != null)
for (int idx = 0; idx < cacheHeikenAshi.Length; idx++)
if (cacheHeikenAshi[idx].EqualsInput(input))
return cacheHeikenAshi[idx];
HeikenAshi indicator = new HeikenAshi();
indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
#if NT7
indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
indicator.MaximumBarsLookBack = MaximumBarsLookBack;
#endif
indicator.Input = input;
Indicators.Add(indicator);
indicator.SetUp();
HeikenAshi[] tmp = new HeikenAshi[cacheHeikenAshi == null ? 1 : cacheHeikenAshi.Length + 1];
if (cacheHeikenAshi != null)
cacheHeikenAshi.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cacheHeikenAshi = tmp;
return indicator;
}
}
}
}
// This namespace holds all market analyzer column definitions and is required. Do not change it.
namespace NinjaTrader.MarketAnalyzer
{
public partial class Column : ColumnBase
{
/// <summary>
/// HeikenAshi technique discussed in the article 'Using Heiken-Ashi Technique' in February 2004 issue of TASC magazine.
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.HeikenAshi HeikenAshi()
{
return _indicator.HeikenAshi(Input);
}
/// <summary>
/// HeikenAshi technique discussed in the article 'Using Heiken-Ashi Technique' in February 2004 issue of TASC magazine.
/// </summary>
/// <returns></returns>
public Indicator.HeikenAshi HeikenAshi(Data.IDataSeries input)
{
return _indicator.HeikenAshi(input);
}
}
}
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// HeikenAshi technique discussed in the article 'Using Heiken-Ashi Technique' in February 2004 issue of TASC magazine.
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.HeikenAshi HeikenAshi()
{
return _indicator.HeikenAshi(Input);
}
/// <summary>
/// HeikenAshi technique discussed in the article 'Using Heiken-Ashi Technique' in February 2004 issue of TASC magazine.
/// </summary>
/// <returns></returns>
public Indicator.HeikenAshi HeikenAshi(Data.IDataSeries input)
{
if (InInitialize && input == null)
throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");
return _indicator.HeikenAshi(input);
}
}
}
#endregion