-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSimple iMA Trailing.mq5
215 lines (213 loc) · 18.6 KB
/
Simple iMA Trailing.mq5
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
//+------------------------------------------------------------------+
//| Simple iMA Trailing.mq5 |
//| Copyright © 2020, Vladimir Karputov |
//| https://www.mql5.com/ru/market/product/43516 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property link "https://www.mql5.com/ru/market/product/43516"
#property version "1.000"
#property description "Trailing Stop in Points (1.00045-1.00055=10 points)"
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
//---
CPositionInfo m_position; // object of CPositionInfo class
CTrade m_trade; // object of CTrade class
CSymbolInfo m_symbol; // object of CSymbolInfo class
//--- input parameters
input group "Trailing"
input uint InpTrailingStop = 10; // Trailing Stop (min distance from price to Stop Loss)
input group "MA"
input int Inp_MA_ma_period = 12; // MA: averaging period
input int Inp_MA_ma_shift = 0; // MA: horizontal shift
input ENUM_MA_METHOD Inp_MA_ma_method = MODE_SMA; // MA: smoothing type
input ENUM_APPLIED_PRICE Inp_MA_applied_price = PRICE_CLOSE; // MA: type of price
input group "Additional features"
input bool InpPrintLog = true; // Print log
input ulong InpDeviation = 10; // Deviation
input ulong InpMagic = 11828516; // Magic number
//---
double m_trailing_stop = 0.0; // Trailing Stop -> double
int handle_iMA; // variable for storing the handle of the iMA indicator
datetime m_prev_bars = 0; // "0" -> D'1970.01.01 00:00';
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
ResetLastError();
if(!m_symbol.Name(Symbol())) // sets symbol name
{
Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
return(INIT_FAILED);
}
RefreshRates();
//---
m_trade.SetExpertMagicNumber(InpMagic);
m_trade.SetMarginMode();
m_trade.SetTypeFillingBySymbol(m_symbol.Name());
m_trade.SetDeviationInPoints(InpDeviation);
//---
m_trailing_stop = InpTrailingStop * m_symbol.Point();
//--- create handle of the indicator iMA
handle_iMA=iMA(m_symbol.Name(),Period(),Inp_MA_ma_period,Inp_MA_ma_shift,
Inp_MA_ma_method,Inp_MA_applied_price);
//--- if the handle is not created
if(handle_iMA==INVALID_HANDLE)
{
//--- tell about the failure and output the error code
PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
m_symbol.Name(),
EnumToString(Period()),
GetLastError());
//--- the indicator is stopped early
return(INIT_FAILED);
}
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- we work only at the time of the birth of new bar
datetime time_0=iTime(m_symbol.Name(),Period(),0);
if(time_0==m_prev_bars)
return;
m_prev_bars=time_0;
if(!RefreshRates())
{
m_prev_bars=0;
return;
}
Trailing();
}
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data |
//+------------------------------------------------------------------+
bool RefreshRates()
{
//--- refresh rates
if(!m_symbol.RefreshRates())
{
Print(__FILE__," ",__FUNCTION__,", ERROR: ","RefreshRates error");
return(false);
}
//--- protection against the return value of "zero"
if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
{
Print(__FILE__," ",__FUNCTION__,", ERROR: ","Ask == 0.0 OR Bid == 0.0");
return(false);
}
//---
return(true);
}
//+------------------------------------------------------------------+
//| Trailing |
//| InpTrailingStop: min distance from price to Stop Loss |
//+------------------------------------------------------------------+
void Trailing()
{
if(InpTrailingStop==0)
return;
double ma[];
ArraySetAsSeries(ma,true);
int start_pos=0,count=3;
if(!iGetArray(handle_iMA,0,start_pos,count,ma))
{
m_prev_bars=0;
return;
}
//---
for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions
if(m_position.SelectByIndex(i))
if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
{
double pos_price_current = m_position.PriceCurrent();
double pos_price_open = m_position.PriceOpen();
double stop_loss = m_position.StopLoss();
double take_profit = m_position.TakeProfit();
double ask = m_symbol.Ask();
double bid = m_symbol.Bid();
//---
if(m_position.PositionType()==POSITION_TYPE_BUY)
{
if(pos_price_current>pos_price_open+m_trailing_stop)
if(pos_price_current>ma[1])
if(stop_loss<ma[1])
if(!CompareDoubles(stop_loss,ma[1],Digits(),Point()))
{
if(!m_trade.PositionModify(m_position.Ticket(),m_symbol.NormalizePrice(ma[1]),take_profit))
if(InpPrintLog)
Print(__FILE__," ",__FUNCTION__,", ERROR: ","Modify BUY ",m_position.Ticket(),
" Position -> false. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
continue;
}
}
else
{
if(pos_price_current<pos_price_open-m_trailing_stop)
if(pos_price_current<ma[1])
if((stop_loss>ma[1]) || (stop_loss==0))
if(!CompareDoubles(stop_loss,ma[1],Digits(),Point()))
{
if(!m_trade.PositionModify(m_position.Ticket(),m_symbol.NormalizePrice(ma[1]),take_profit))
if(InpPrintLog)
Print(__FILE__," ",__FUNCTION__,", ERROR: ","Modify SELL ",m_position.Ticket(),
" Position -> false. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
}
}
}
}
//+------------------------------------------------------------------+
//| Compare doubles |
//+------------------------------------------------------------------+
bool CompareDoubles(double number1,double number2,int digits,double points)
{
if(MathAbs(NormalizeDouble(number1-number2,digits))<=points)
return(true);
else
return(false);
}
//+------------------------------------------------------------------+
//| Get value of buffers |
//+------------------------------------------------------------------+
bool iGetArray(const int handle,const int buffer,const int start_pos,
const int count,double &arr_buffer[])
{
bool result=true;
if(!ArrayIsDynamic(arr_buffer))
{
if(InpPrintLog)
PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);
return(false);
}
ArrayFree(arr_buffer);
//--- reset error code
ResetLastError();
//--- fill a part of the iBands array with values from the indicator buffer
int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
if(copied!=count)
{
//--- if the copying fails, tell the error code
if(InpPrintLog)
PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",
__FILE__,__FUNCTION__,count,copied,GetLastError());
//--- quit with zero result - it means that the indicator is considered as not calculated
return(false);
}
return(result);
}
//+------------------------------------------------------------------+