-
Notifications
You must be signed in to change notification settings - Fork 6
/
Bollinger_Bands.Indicator.CS
90 lines (75 loc) · 2.94 KB
/
Bollinger_Bands.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
using System;
using System.Drawing;
using PowerLanguage.Function;
namespace PowerLanguage.Indicator
{
[SameAsSymbol(true)]
public class Bollinger_Bands : IndicatorObject
{
private AverageFC m_averagefc1;
private VariableSeries<Double> m_lowerband;
private VariableSeries<Double> m_upperband;
private IPlotObject Plot1;
private IPlotObject Plot2;
private IPlotObject Plot3;
public Bollinger_Bands(object ctx) :
base(ctx){
numdevsdn = -2;
numdevsup = 2;
length = 20;
}
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; }
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.Yellow,
Color.Empty, 0, 0, true));
Plot2 =
AddPlot(new PlotAttributes("LowerBand", 0, Color.Blue,
Color.Empty, 0, 0, true));
Plot3 =
AddPlot(new PlotAttributes("MidLine", 0, Color.Gray,
Color.Empty, 0, 0, true));
}
protected override void StartCalc(){
bollingerprice = Bars.Close;
testpriceuband = Bars.Close;
testpricelband = Bars.Close;
m_averagefc1.price = bollingerprice;
m_averagefc1.length = length;
}
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);
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");
}
}
}
}
}
}
}