-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MA_Crossover2.mq4
207 lines (166 loc) · 7.82 KB
/
MA_Crossover2.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
//+------------------------------------------------------------------+
//| MA_Crossover2.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.25"
#property strict
extern int maPeriod1 = 37;
extern int maPeriod2 = 122;
extern int maModeType1 = 0;
extern int maModeType2 = 0;
extern double Lots = 1;
extern double LotMul = 1;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
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
}
int OnInit()
{
//---
Print("Initialized!");
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(NewBar())
{
//---
Print("started");
static double size = Lots* LotMul;
//Print("lot Size ",size," lots ",Lots," LotMul ",LotMul);
static int ticket = 0;
double CurrentMa1 = iMA(Symbol(),PERIOD_CURRENT,maPeriod1,0,maModeType1,PRICE_TYPICAL,1);
double CurrentMa2 = iMA(Symbol(),PERIOD_CURRENT,maPeriod2,0,maModeType1,PRICE_TYPICAL,1);
double LastMa1 = iMA(Symbol(),PERIOD_CURRENT,maPeriod1,0,maModeType2,PRICE_TYPICAL,2);
double LastMa2 = iMA(Symbol(),PERIOD_CURRENT,maPeriod2,0,maModeType2,PRICE_TYPICAL,2);
//---
//if short Perod ma was below the fast Perod ma
//but now above the fast Perod ma
if(LastMa1 <= LastMa2 &&CurrentMa1 >= CurrentMa2)
{
//Close Open Sells
//Close Open Buys
Print("lets close all buys...");
closeAllbuys();
//
// closeAllSells();
//Open Sell
//ticket = openSell(Lots) ;
// if( OrdersTotal()<1)
// {
Print("Orders less than 1, check 4 Sell...");
// ticket = OrderSend(Symbol(), OP_BUY, LotMul, Ask, 10, NormalizeDouble(Bid-StopLoss*Point,Digits), NormalizeDouble(Bid+TakeProfit*Point,Digits), "Set by SimpleSystem");
///
// ticket = OrderSend(Symbol(), OP_SELL, NormalizeDouble(LotMul,2)/* size \ NormalizeDouble( size,2)*/,NormalizeDouble( Bid,Digits) /* NormalizeDouble( Bid*Point,Digits)*/, 10, NULL,NULL,NULL,0,0,Red); //1,Digits
// Print("Sell Bid @",NormalizeDouble( Bid,Digits));
///
ticket = OrderSend(Symbol(), OP_SELL, NormalizeDouble(LotMul,2)/* size \ NormalizeDouble( size,2)*/,NormalizeDouble( Bid,Digits) /* NormalizeDouble( Bid*Point,Digits)*/, 10, NULL,NULL,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
//}
//else{ ticket = -1;}
}//END IF
///---
if(LastMa1 >= LastMa2 &&
CurrentMa1 < CurrentMa2)
{
Print("lets close all Sells...");
closeAllSells();
//if( OrdersTotal()<1)
//{
Print("Orders less than 1, check 4 Buy...");
ticket = OrderSend(Symbol(), OP_BUY, NormalizeDouble( LotMul,2), NormalizeDouble(Ask,Digits) /* NormalizeDouble(Ask*Point,Digits)*/, 10,NULL, NULL,NULL,0,0,Blue); //1,Digits
Print("Buy Ask @",NormalizeDouble(Ask,Digits));
///
// }
//else{ ticket = -1;}
}
}
///---
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
bool closeAllbuys()
{
static int Slippage = 10; bool tikka = false; bool Ans= false;
int PositionIndex; // <-- this variable is the index used for the loop
int TotalNumberOfOrders; // <-- this variable will hold the number of orders currently in the Trade pool
TotalNumberOfOrders = OrdersTotal(); // <-- we store the number of Orders in the variable
if(TotalNumberOfOrders>0)
{
for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --) // <-- for loop to loop through all Orders . . COUNT DOWN TO ZERO !
{
if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue; // <-- if the OrderSelect fails advance the loop to the next PositionIndex
if( //OrderMagicNumber() == MagicNo && // <-- does the Order's Magic Number match our EA's magic number ?
OrderSymbol() == Symbol() // <-- does the Order's Symbol match the Symbol our EA is working on ?
&& OrderType() == OP_BUY ) // <-- Close Buy Orders //
if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), Slippage ) ) // <-- try to close the order
{
// Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() ); // <-- if the Order Close failed print some helpful information
tikka = false;break;
}
else{ tikka = true;}//Success in Closing A BUY Order
//bool here!
} // end of For loop
// if(!tikka)
// {
Ans = tikka; //THis ONLY works with just One Buy Order at a Time! otherwise, use dynamic array
// }
}
return (Ans);
}
//+------------------------------------------------------------------+
bool closeAllSells()
{
static int Slippage = 10; bool tikka = false; bool Ans= false;
int PositionIndex; // <-- this variable is the index used for the loop
int TotalNumberOfOrders; // <-- this variable will hold the number of orders currently in the Trade pool
TotalNumberOfOrders = OrdersTotal(); // <-- we store the number of Orders in the variable
if(TotalNumberOfOrders>0)
{
for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --) // <-- for loop to loop through all Orders . . COUNT DOWN TO ZERO !
{
if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue; // <-- if the OrderSelect fails advance the loop to the next PositionIndex
if( // OrderMagicNumber() == MagicNo // <-- does the Order's Magic Number match our EA's magic number ? &&
OrderSymbol() == Symbol() // <-- does the Order's Symbol match the Symbol our EA is working on ?
&& OrderType() == OP_SELL ) // <-- or is it a Sell Order ?
if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), Slippage ) ) // <-- try to close the order
{
// Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() ); // <-- if the Order Close failed print some helpful information
tikka = false;break;
}
else {tikka = true;}//Success in Closing A SELL Order
} // end of For loop
Ans = tikka; //THis ONLY works with just One Buy Order at a Time! otherwise, use dynamic array
}
return (Ans);
}
//+------------------------------------------------------------------+