-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MA_Crossover.mq4
255 lines (219 loc) · 8.05 KB
/
MA_Crossover.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
//+------------------------------------------------------------------+
//| MA_Crossover.mq4 |
//| Copyright 2017, Eng. Ahmad Lutfi |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Eng. Ahmad Lutfi"
#property link "https://www.mql5.com"
#property version "1.20"
#property strict
extern int maPeriod1 = 37;
extern int maMode1 = 0;
extern int Shift1 = 0;
extern int Appliedto1=0;
extern int maPeriod2 = 122;
extern int maMode2 = 0;
extern int Shift2 = 0;
extern int Appliedto2=0;
extern double Lots = 1;
extern double LotMul = 1;
int Slippage=10;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
//Change NewBar DFunctions
datetime PreviousBar; // record the candle/bar time
bool NewBar() //Working!!!!
{
if(PreviousBar<Time[0])
{
PreviousBar = Time[0];
return(true);
}
else
{
return(false);
}
return(false); // in case if - else statement is not executed
}
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// if(NewBar())
//{
//---
static double size = Lots* LotMul;
//Print("lot Size ",size," lots ",Lots," LotMul ",LotMul);
static int ticket = 0;
double CurrentMa1 = iMA(Symbol(),PERIOD_CURRENT,maPeriod1,Shift1,maMode1,PRICE_TYPICAL,1);
double CurrentMa2 = iMA(Symbol(),PERIOD_CURRENT,maPeriod2,Shift2,maMode2,PRICE_TYPICAL,1);
// double LastMa1 = iMA(Symbol(),PERIOD_CURRENT,maPeriod1,0,MODE_SMA,PRICE_TYPICAL,2);
// double LastMa2 = iMA(Symbol(),PERIOD_CURRENT,maPeriod2,0,MODE_SMA,PRICE_TYPICAL,2);
//---
//if short Perod ma was below the fast Perod ma
//but now above the fast Perod ma
if(CurrentMa1 >= CurrentMa2)
{
//Close Open Buys & SELL
closeAllbuys();
if(OrdersTotal()<1)
{
ticket = OrderSend(Symbol(), OP_SELL, NormalizeDouble(LotMul,2),NormalizeDouble( Bid,Digits), 10, 0,0,NULL,0,0,Red); //1,Digits
Print("Sell Bid @",NormalizeDouble( Bid,Digits));
}
///
//ticket = OrderSend(Symbol(), OP_BUY, size/* NormalizeDouble( size,2)*/, NormalizeDouble(Ask*Point,Digits), 10,NULL, NULL,NULL,0,0,Blue); //1,Digits
}//END IF
///---
if(CurrentMa1 <= CurrentMa2)
{
//Close all Open Sells and BUY
closeAllSells();
if(OrdersTotal()<1)
{
ticket = OrderSend(Symbol(), OP_BUY, NormalizeDouble( LotMul,2), NormalizeDouble(Ask,Digits),10 ,0, 0,NULL,0,0,Blue); //1,Digits
Print("Buy Ask @",NormalizeDouble(Ask,Digits));
}
///
}
// }
///---
}
//+------------------------------------------------------------------+
bool closeAllbuys()
{
bool tikka = false; bool Ans= false;
//static int Slippage = 10;
if(OrdersTotal()>0)
{
for(int i = OrdersTotal()-1; i >= 0 ; i --) // <-- for loop to loop through all Orders . . COUNT DOWN TO ZERO !
{
if( OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderType() == OP_BUY && OrderSymbol() == Symbol() )
{
tryClose(OP_BUY);
Ans=true;
break;
}//END IF OrderType
}//END IF OrderSelect
else
{Print("Order Selection Error No. "+ IntegerToString(GetLastError()));
tikka = false;
}
if(!tikka)
{
Ans=false;
}
}//end for Loop
}//End ordersTotal
return(Ans);
}
//+------------------------------------------------------------------+
void tryClose(int Operation)//op: 0= buy, 1=sell; //function assumes order has already been selected!
{
// Print("entering close...");
static int number_try =2;
while(number_try>0)
{
Print("entering while...numtires= ",IntegerToString(number_try));
number_try--; //1
Sleep(500);
RefreshRates();
//---
bool result=false;//failed to close by default
if(Operation==1) //sell Order
{
Print("trying to close sell ");
result =OrderClose(OrderTicket(), OrderLots(),Ask, Slippage, Gold); //Ask for Closesell
}
if(Operation==0) //buy Order
{
Print("trying to close buy order ticket ="+OrderTicket()+"Bids=");
result =OrderClose(OrderTicket(), OrderLots(),Bid ,Slippage, Red); //Bid for Closebuy
}
//---
int error=0;
if(result != true)
{
error = GetLastError();
Print("Last Known Error: ",error);
}
if(result == true) //successful! true
{
number_try =2;
error =0;
break;
}
switch(error)
{
case 135://ERR_PRICE_CHANGED
case 136://ERR_OFF_QUOTES
case 137://ERR_BROKER_BUSY
case 138://ERR_REQUOTE
case 146:Sleep(1000);RefreshRates();/*i++;*/break;//ERR_TRADE_CONTEXT_BUSY
}
if(error==0)//no errors occured while closing this order
{
number_try=0; //get Out of the while loop!
Print("Order was Successfully Closed!");
Comment("Order was Successfully Closed!");
//or better
//return
//return true;
}
else
{
number_try++;//2
Print("Couldn't close order, retrying...");
Comment("Couldn't close order, retrying...");
}
}//end While!
}
//+------------------------------------------------------------------+
bool closeAllSells()
{
bool tikka = false; bool Ans= false;
//int error;
if(OrdersTotal()>0)
{
for(int i = OrdersTotal() - 1; i >= 0 ; i--) // <-- for loop to loop through all Orders . . COUNT DOWN TO ZERO !
{
if( OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderType() == OP_SELL && OrderSymbol() == Symbol() )
{
tryClose(OP_SELL);
Ans=true;
break;
}//END IF OrderType
}//END IF OrderSelect
else
{Print("Order Selecting Error No. "+ IntegerToString(GetLastError()));
tikka = false;
}
if(!tikka)
{
Ans=false;
}
}//end for Loop
}//End orders total
return(Ans);
}