-
Notifications
You must be signed in to change notification settings - Fork 6
/
Bollinger_Bands_Area.Indicator.CS
288 lines (240 loc) · 7.11 KB
/
Bollinger_Bands_Area.Indicator.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
using System;
using System.Drawing;
using System.Linq;
using System.Collections.Concurrent;
using System.Collections;
using PowerLanguage.Function;
using System.Collections.Generic;
namespace PowerLanguage.Indicator
{
[SameAsSymbol(true)]
public class Bollinger_Bands_Area : IndicatorObject, IChartCustomDrawer
{
public Bollinger_Bands_Area(object _ctx)
: base(_ctx)
{
numdevsdn = -2;
numdevsup = 2;
length = 20;
fillcolor = Color.FromArgb(128, 128, 255);
transparency = 85;
}
private AverageFC m_averagefc1;
private VariableSeries<Double> m_lowerband;
private VariableSeries<Double> m_upperband;
private IPlotObject Plot1;
private IPlotObject Plot2;
private IPlotObject Plot3;
private AreaValue m_IndicatorArea;
private bool isCSDRegistred;
private byte m_intensity; // 0-255
private ISeries<double> bollingerprice { get; set; }
private ISeries<double> testpriceuband { get; set; }
private ISeries<double> testpricelband { get; set; }
[Input]
public int length { get; set; }
[Input]
public double numdevsup { get; set; }
[Input]
public double numdevsdn { get; set; }
[Input]
public int displace { get; set; }
[Input]
public Color fillcolor { get; set; }
[Input]
public byte transparency
{
get
{
byte res = Convert.ToByte((float)m_intensity / 255.0 * 100.0);
return res;
}
set
{
try
{
m_intensity = Convert.ToByte(255.0 / 100.0 * (float)value);
}
catch (Exception)
{
transparency = 100;
}
}
}
protected override void Create()
{
m_averagefc1 = new AverageFC(this);
m_lowerband = new VariableSeries<Double>(this);
m_upperband = new VariableSeries<Double>(this);
Plot1 =
AddPlot(new PlotAttributes("UpperBand", 0, Color.Green, Color.Empty, 1, 0, true));
Plot2 =
AddPlot(new PlotAttributes("LowerBand", 0, Color.Red, Color.Empty, 1, 0, true));
Plot3 =
AddPlot(new PlotAttributes("MidLine", 0, Color.Gray, Color.Empty, 1, 0, true));
m_IndicatorArea = new AreaValue();
isCSDRegistred = false;
}
protected override void StartCalc()
{
if (!isCSDRegistred)
{
ChartCustomDraw.Register(this);
isCSDRegistred = true;
}
bollingerprice = Bars.Close;
testpriceuband = Bars.Close;
testpricelband = Bars.Close;
m_averagefc1.price = bollingerprice;
m_averagefc1.length = length;
}
protected override void StopCalc()
{
if (isCSDRegistred)
{
ChartCustomDraw.Unregister(this);
isCSDRegistred = false;
}
}
protected override void CalcBar()
{
var m_avg = m_averagefc1[0];
var m_sdev = bollingerprice.StandardDeviationCustom(length, 1);
m_upperband.Value = (m_avg + (numdevsup * m_sdev));
m_lowerband.Value = (m_avg + (numdevsdn * m_sdev));
if (((displace >= 0) || Bars.CurrentBar > Math.Abs(displace)))
{
Plot1.Set(displace, m_upperband.Value);
Plot2.Set(displace, m_lowerband.Value);
Plot3.Set(displace, m_avg);
m_IndicatorArea.SetValue("UpperBand", m_upperband.Value, Bars.TimeValue.ToBinary());
m_IndicatorArea.SetValue("LowerBand", m_lowerband.Value, Bars.TimeValue.ToBinary());
if ((displace <= 0))
{
if (this.CrossesOver(testpricelband, m_lowerband))
{
Alerts.Alert("Price crossing over lower price band");
}
else
{
if (this.CrossesUnder(testpriceuband, m_upperband))
{
Alerts.Alert("Price crossing under upper price band");
}
}
}
}
ChartCustomDraw.ReDraw();
}
#region IChartCustomDrawer
void IChartCustomDrawer.Draw(DrawContext context, EDrawPhases phase)
{
if (phase != EDrawPhases.Final) return;
RectangleF _dr = context.FullRect;
ChartPoint DrPleft = context.Environment.Point2ChartPoint(new PointF { X = _dr.X, Y = _dr.Y });
ChartPoint DrPRight = context.Environment.Point2ChartPoint(new PointF { X = _dr.Width, Y = _dr.Height });
if (DrPleft.Time.Ticks > DrPRight.Time.Ticks)
{
return;
}
Bar[] BarsToRedraw = null;
context.Environment.Bars.Get(DrPleft.Time, DrPRight.Time, out BarsToRedraw);
if (!object.Equals(BarsToRedraw, null))
{
int countBars = BarsToRedraw.Length;
if (countBars > 0)
{
AreaPainter p = new AreaPainter(fillcolor, (byte)(255 - m_intensity));
PointF[] pf = new PointF[countBars * 2];
int full = countBars * 2 - 1;
for (int i = 0, idx = 0; i < countBars; i++)
{
double high0 = m_IndicatorArea.GetValue("UpperBand", BarsToRedraw[i].Time.ToBinary());
double low0 = m_IndicatorArea.GetValue("LowerBand", BarsToRedraw[i].Time.ToBinary());
if (high0 < 0|| low0 < 0)
{
idx = 0;
full-=2;
pf = new PointF[full + 1];
continue;
}
Bar b = BarsToRedraw[i];
pf[idx] = context.Environment.ChartPoint2Point(new ChartPoint
{
Price = high0,
Time = b.Time
});
pf[full - idx] = context.Environment.ChartPoint2Point(new ChartPoint
{
Price = low0,
Time = b.Time
});
idx++;
}
p.PaintArea(context.graphics, pf);
}
}
context.DirtyRect = _dr;
}
#endregion
}
public class AreaValue
{
ConcurrentDictionary<String, ConcurrentDictionary<long /*Datetime*/, double>> m_Data;
public AreaValue()
{
m_Data = new ConcurrentDictionary<string,ConcurrentDictionary<long, double>>();
}
public void SetValue(String Name, double Value, long Time)
{
if(m_Data.ContainsKey(Name))
{
m_Data[Name][Time] = Value;
}
else
{
ConcurrentDictionary<long, double> data = new ConcurrentDictionary<long, double>();
while(!data.TryAdd(Time, Value))
{ }
while(!m_Data.TryAdd(Name, data))
{ }
}
}
public double GetValue(String Name, long Time)
{
try
{
if(m_Data[Name].ContainsKey(Time))
return m_Data[Name][Time];
return -1;
}
catch(System.Exception)
{
return -1;
}
}
public void Clear()
{
m_Data.Clear();
}
}
public class AreaPainter
{
byte m_Alpha; //0->255
Color m_FillColor;
Pen m_PainterPen;
SolidBrush m_SolidBrush;
public AreaPainter(Color color, byte Alphavalue)
{
m_FillColor = color;
m_Alpha = Alphavalue;
m_SolidBrush = new SolidBrush(Color.FromArgb(m_Alpha, m_FillColor));
m_PainterPen = new Pen(m_SolidBrush);
}
public void PaintArea(Graphics g, PointF[] p)
{
g.DrawPolygon(m_PainterPen, p);
g.FillPolygon(m_SolidBrush, p);
}
}
}