/
AverageTrueRange.mqh
126 lines (112 loc) · 5.74 KB
/
AverageTrueRange.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
//+--------------------------------------------------------------------+
//| 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 AverageTrueRange : public Indicator
{
public:
AverageTrueRange(SlotTypes slotType);
virtual void Calculate(DataSet &dataSet);
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void AverageTrueRange::AverageTrueRange(SlotTypes slotType)
{
SlotType = slotType;
IndicatorName = "Average True Range";
WarningMessage = "";
IsAllowLTF = true;
ExecTime = ExecutionTime_DuringTheBar;
IsSeparateChart = true;
IsDiscreteValues = false;
IsDefaultGroupAll = false;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void AverageTrueRange::Calculate(DataSet &dataSet)
{
Data=GetPointer(dataSet);
MAMethod maMethod=(MAMethod) ListParam[1].Index;
int period=(int) NumParam[0].Value;
double level=Data.Point*NumParam[1].Value;
int previous=CheckParam[0].Checked ? 1 : 0;
int firstBar=period+previous+2;
double atr1[]; ArrayResize(atr1,Data.Bars); ArrayInitialize(atr1,0);
for(int bar=1; bar<Data.Bars; bar++)
{
atr1[bar]=MathMax(Data.High[bar],Data.Close[bar-1])-MathMin(Data.Low[bar],Data.Close[bar-1]);
}
double atr[]; MovingAverage(period,0,maMethod,atr1,atr);
ArrayResize(Component[0].Value,Data.Bars);
Component[0].CompName = "Average True Range";
Component[0].DataType = IndComponentType_IndicatorValue;
Component[0].FirstBar = firstBar;
ArrayCopy(Component[0].Value,atr);
ArrayResize(Component[1].Value,Data.Bars);
Component[1].FirstBar=firstBar;
ArrayResize(Component[2].Value,Data.Bars);
Component[2].FirstBar=firstBar;
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";
}
IndicatorLogic indLogic=IndicatorLogic_It_does_not_act_as_a_filter;
if(ListParam[0].Text=="ATR rises")
indLogic=IndicatorLogic_The_indicator_rises;
else if(ListParam[0].Text=="ATR falls")
indLogic=IndicatorLogic_The_indicator_falls;
else if(ListParam[0].Text=="ATR is higher than the Level line")
indLogic=IndicatorLogic_The_indicator_is_higher_than_the_level_line;
else if(ListParam[0].Text=="ATR is lower than the Level line")
indLogic=IndicatorLogic_The_indicator_is_lower_than_the_level_line;
else if(ListParam[0].Text=="ATR crosses the Level line upward")
indLogic=IndicatorLogic_The_indicator_crosses_the_level_line_upward;
else if(ListParam[0].Text=="ATR crosses the Level line downward")
indLogic=IndicatorLogic_The_indicator_crosses_the_level_line_downward;
else if(ListParam[0].Text=="ATR changes its direction upward")
indLogic=IndicatorLogic_The_indicator_changes_its_direction_upward;
else if(ListParam[0].Text=="ATR changes its direction downward")
indLogic=IndicatorLogic_The_indicator_changes_its_direction_downward;
NoDirectionOscillatorLogic(firstBar,previous,atr,level,Component[1],indLogic);
ArrayCopy(Component[2].Value,Component[1].Value);
}
//+------------------------------------------------------------------+