-
Notifications
You must be signed in to change notification settings - Fork 2
/
distance with trStop.mq4
310 lines (283 loc) · 19.5 KB
/
distance with trStop.mq4
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//+------------------------------------------------------------------+
//| distance with trStop.mq4 |
//| Copyright 2015, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//--- input parameters
input int Magic = 1; // expert id
input string TimeToSetOrders_1 = "02:00";
input string TimeToDelOrders_1 = "04:00";
input string TimeToSetOrders_2 = "09:00";
input string TimeToDelOrders_2 = "14:00";
input string TimeToSetOrders_3 = "00:00";
input string TimeToDelOrders_3 = "17:00";
input double Lot = 1; // lot
extern int StopLoss = 10;
extern int TakeProfit = 40;
extern int Distance = 8;
extern bool TrailingSwitcher = true; // Trailing Stop
extern int TrailingStop = 10; // Trailing Stop Distance
extern int TrailingStep = 50;
extern bool IRSI =false; // RSI Switcher
extern int RSIPeriod =8; //Period Days
extern int RSIStopBuy =65;// RSI Level for Stop buy
extern int RSIStopSell =35;// RSI Level for Stop sell
bool Time_check =false;
bool TradeB =false;
bool TradeS =false;
bool DelSellStop =false;
bool DelBuyStop =false;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- increment for terminals with 3 or 5 digits
if(Digits == 3 || Digits == 5)
{
Distance *= 10;
TrailingStop *= 10;
StopLoss *= 10;
TakeProfit *= 10;
}
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Активация трейлинг стоп
Trailing();
CountBuy();
CountSell();
CountBuyStop();
CountSellStop();
Checker();
}
//+------------------------------------------------------------------+
//Проверка количества открытых ордеров на покупку
//+------------------------------------------------------------------+
int CountBuy() //На покупку
{
int count=0;
for (int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol()==Symbol()&& OrderMagicNumber()==Magic && OrderType()== OP_BUY)
count++;
}
}
return(count);
}
//+------------------------------------------------------------------+
//Проверка количества открытых ордеров на продажу
//+------------------------------------------------------------------+
int CountSell() //На продажу
{
int count=0;
for (int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol()==Symbol()&& OrderMagicNumber()==Magic && OrderType()== OP_SELL)
count++;
}
}
return(count);
}
//+------------------------------------------------------------------+
//Проверка количества отложенных ордеров на покупку
//+------------------------------------------------------------------+
int CountBuyStop() //На покупку
{
int count=0;
for (int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol()==Symbol()&& OrderMagicNumber()==Magic && OrderType()== OP_BUYSTOP)
count++;
}
}
return(count);
}
//+------------------------------------------------------------------+
//Проверка количества отложенных ордеров на продажу
//+------------------------------------------------------------------+
int CountSellStop() //На продажу
{
int count=0;
for (int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol()==Symbol()&& OrderMagicNumber()==Magic && OrderType()== OP_SELLSTOP)
count++;
}
}
return(count);
}
//+------------------------------------------------------------------+
//Функция входа в рынок
//+------------------------------------------------------------------+
void Checker ()
{
string Timer = TimeToString(TimeCurrent(),TIME_MINUTES);
if((Timer == TimeToSetOrders_1 || Timer == TimeToSetOrders_2 || Timer == TimeToSetOrders_3) && (CountBuyStop() + CountSellStop()) ==0 )
{
TradeB = true;
if(IRSI)
{
if(iRSI (Symbol(),0,RSIPeriod,6,0)>RSIStopBuy)
{
TradeB =false;
}
}
Open_BYSTOP();
// Работа с ордером на продажу
TradeS = true;
if(IRSI)
{
if(iRSI (Symbol(),0,RSIPeriod,6,0)>RSIStopSell)
{
TradeS =false;
}
}
Open_SELLSTOP();
}
else
{
TradeB = false;
TradeS = false;
}
if(TimeToStr(TimeCurrent(), TIME_MINUTES)==TimeToDelOrders_1||TimeToStr(TimeCurrent(), TIME_MINUTES)==TimeToDelOrders_2||TimeToStr(TimeCurrent(), TIME_MINUTES)==TimeToDelOrders_3)
{
DelBuyStop = true;
DeleteBuyStop();
DelSellStop = true;
DeleteSellStop();
}
else
{
DelBuyStop = false;
DelSellStop = false;
}
}
//+------------------------------------------------------------------+
//Открытие ордера на покупку
//+------------------------------------------------------------------+
void Open_BYSTOP()
{
if(TradeB)
{
RefreshRates();
double Price = (Ask+Distance*Point);
double TP = (Ask+(Distance+TakeProfit)*Point);
double SL = (Bid-StopLoss*Point);
int ticket = OrderSend(Symbol(),OP_BUYSTOP,Lot,Price,5,SL,TP," ",Magic,0,clrGreen);
}
}
//+------------------------------------------------------------------+
//Открытие ордера на продажу
//+------------------------------------------------------------------+
void Open_SELLSTOP()
{
if(TradeS)
{
RefreshRates();
double Price = (Bid-Distance*Point);
double TP = (Bid-(Distance+TakeProfit)*Point);
double SL = (Ask+StopLoss*Point);
int ticket = OrderSend(Symbol(),OP_SELLSTOP,Lot,Price,5,SL,TP," ",Magic,0,clrBlue);
}
}
//+------------------------------------------------------------------+
// Удаление отложенных ордеров
//+------------------------------------------------------------------+
void DeleteBuyStop() //На покупку
{
if(DelBuyStop)
{
for (int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol()==Symbol()&& OrderMagicNumber()==Magic && OrderType()== OP_BUYSTOP)
int ticket=OrderDelete(OrderTicket(),clrRed);
}
}
}
}
//+------------------------------------------------------------------+
void DeleteSellStop() //На продажу
{
if (DelSellStop)
{
for (int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol()==Symbol()&& OrderMagicNumber()==Magic && OrderType()== OP_SELLSTOP)
int ticket=OrderDelete(OrderTicket(),clrRed);
}
}
}
}
//+------------------------------------------------------------------+
//Трейлинг стоп
//+------------------------------------------------------------------+
int Trailing()
{
if(TrailingSwitcher)
{
for( int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderMagicNumber()==Magic)
{
if (OrderType()==OP_BUY)
{
if (Bid-OrderOpenPrice()> TrailingStop*Point)
{
if (OrderStopLoss()<Bid-(TrailingStop+TrailingStep)*Point)
{
double SL=NormalizeDouble(Bid-TrailingStop*Point,Digits);
if(OrderStopLoss()!=StopLoss)
int result = OrderModify(OrderTicket(),OrderOpenPrice(),SL,0,0);
}
}
}
if(OrderType()==OP_SELL)
{
if(OrderOpenPrice()-Ask>TrailingStop*Point)
{
if (OrderStopLoss()>Ask+(TrailingStop+TrailingStep)*Point)
{
double SL=NormalizeDouble(Ask+TrailingStop*Point,Digits);
if(OrderStopLoss()!=StopLoss)
int result = OrderModify(OrderTicket(),OrderOpenPrice(),SL,0,0);
}
}
}
}
}
}
}
return(0);
}
//+------------------------------------------------------------------+