/
StandardDeviation.mqh
138 lines (121 loc) · 5.93 KB
/
StandardDeviation.mqh
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
//+--------------------------------------------------------------------+
//| Copyright: (C) 2016 Forex Software Ltd. |
//| Website: http://forexsb.com/ |
//| Support: http://forexsb.com/forum/ |
//| License: Proprietary under the following circumstances: |
//| |
//| This code is a part of Forex Strategy Builder. It is free for |
//| use as an integral part of Forex Strategy Builder. |
//| One can modify it in order to improve the code or to fit it for |
//| personal use. This code or any part of it cannot be used in |
//| other applications without a permission. |
//| The contact information cannot be changed. |
//| |
//| NO LIABILITY FOR CONSEQUENTIAL DAMAGES |
//| |
//| In no event shall the author be liable for any damages whatsoever |
//| (including, without limitation, incidental, direct, indirect and |
//| consequential damages, damages for loss of business profits, |
//| business interruption, loss of business information, or other |
//| pecuniary loss) arising out of the use or inability to use this |
//| product, even if advised of the possibility of such damages. |
//+--------------------------------------------------------------------+
#property copyright "Copyright (C) 2016 Forex Software Ltd."
#property link "http://forexsb.com"
#property version "2.1"
#property strict
#include <Forexsb.com/Indicator.mqh>
#include <Forexsb.com/Enumerations.mqh>
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class StandardDeviation : public Indicator
{
public:
StandardDeviation(SlotTypes slotType)
{
SlotType=slotType;
IndicatorName="Standard Deviation";
WarningMessage = "";
IsAllowLTF = true;
ExecTime = ExecutionTime_DuringTheBar;
IsSeparateChart = true;
IsDiscreteValues = false;
IsDefaultGroupAll = false;
}
virtual void Calculate(DataSet &dataSet);
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void StandardDeviation::Calculate(DataSet &dataSet)
{
Data=GetPointer(dataSet);
// Reading the parameters
MAMethod maMethod=(MAMethod) ListParam[1].Index;
BasePrice price=(BasePrice) ListParam[2].Index;
int period=(int) NumParam[0].Value;
double level=NumParam[1].Value;
int previous=CheckParam[0].Checked ? 1 : 0;
// Calculation
double adPrice[]; Price(price,adPrice);
double ma[]; MovingAverage(period,0,maMethod,adPrice,ma);
double stdv[]; ArrayResize(stdv,Data.Bars); ArrayInitialize(stdv,0);
int firstBar=period+previous+2;
for(int bar=period; bar<Data.Bars; bar++)
{
double dSum=0;
for(int index=0; index<period; index++)
{
double fDelta=(adPrice[bar-index]-ma[bar]);
dSum+=fDelta*fDelta;
}
stdv[bar]=MathSqrt(dSum/period);
}
// Saving the components
ArrayResize(Component[0].Value,Data.Bars);
Component[0].CompName = "Standard Deviation";
Component[0].DataType = IndComponentType_IndicatorValue;
Component[0].FirstBar = firstBar;
ArrayCopy(Component[0].Value,stdv);
ArrayResize(Component[1].Value,Data.Bars);
Component[1].FirstBar=firstBar;
ArrayResize(Component[2].Value,Data.Bars);
Component[1].FirstBar=firstBar;
// Sets the Component's type
if(SlotType==SlotTypes_OpenFilter)
{
Component[1].DataType = IndComponentType_AllowOpenLong;
Component[1].CompName = "Is long entry allowed";
Component[2].DataType = IndComponentType_AllowOpenShort;
Component[2].CompName = "Is short entry allowed";
}
else if(SlotType==SlotTypes_CloseFilter)
{
Component[1].DataType = IndComponentType_ForceCloseLong;
Component[1].CompName = "Close out long position";
Component[2].DataType = IndComponentType_ForceCloseShort;
Component[2].CompName = "Close out short position";
}
// Calculation of the logic
IndicatorLogic indLogic=IndicatorLogic_It_does_not_act_as_a_filter;
if(ListParam[0].Text=="Standard Deviation rises")
indLogic=IndicatorLogic_The_indicator_rises;
else if(ListParam[0].Text=="Standard Deviation falls")
indLogic=IndicatorLogic_The_indicator_falls;
else if(ListParam[0].Text=="Standard Deviation is higher than the Level line")
indLogic=IndicatorLogic_The_indicator_is_higher_than_the_level_line;
else if(ListParam[0].Text=="Standard Deviation is lower than the Level line")
indLogic=IndicatorLogic_The_indicator_is_lower_than_the_level_line;
else if(ListParam[0].Text=="Standard Deviation crosses the Level line upward")
indLogic=IndicatorLogic_The_indicator_crosses_the_level_line_upward;
else if(ListParam[0].Text=="Standard Deviation crosses the Level line downward")
indLogic=IndicatorLogic_The_indicator_crosses_the_level_line_downward;
else if(ListParam[0].Text=="Standard Deviation changes its direction upward")
indLogic=IndicatorLogic_The_indicator_changes_its_direction_upward;
else if(ListParam[0].Text=="Standard Deviation changes its direction downward")
indLogic=IndicatorLogic_The_indicator_changes_its_direction_downward;
NoDirectionOscillatorLogic(firstBar,previous,stdv,level,Component[1],indLogic);
ArrayCopy(Component[2].Value,Component[1].Value);
}
//+------------------------------------------------------------------+