-
Notifications
You must be signed in to change notification settings - Fork 15
/
AmroEA.mq4
335 lines (314 loc) · 11.9 KB
/
AmroEA.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//+------------------------------------------------------------------+
//| mm.mq4 |
//| Mohamed Mansour Beek. |
//| https://www.mansour.com |
//+------------------------------------------------------------------+
#property copyright "Mohamed Mansour Beek."
#property link "https://www.mansour.com"
#property version "1.00"
#property strict
//---- Dependencies
#import "stdlib.ex4"
string ErrorDescription(int e);
#import
extern int MagicNumber = 22445511;
#include "amro.mq4"
// External variables
extern string PS_Ex0 = ">> Lot Size TP SL";
extern double LotSize = 0.01;
extern int Max_Order = 1;
double RealPoint;
/******************************************************
- [x] Take order when Touch screen (Box)
- [x] One order / Square
- [x] Sl when Closed reverse => [ ] Then Take orders to make zero at next level
***************************************************/
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
EventSetTimer(60);
RealPoint = RealPipPoint(Symbol());
Signal = -1;
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Pip Point Function |
//+------------------------------------------------------------------+
double RealPipPoint(string Currency)
{
double CalcPoints;
CalcPoints = 0.001;
double CalcDigits = MarketInfo(Currency,MODE_DIGITS);
if(CalcDigits == 4 || CalcDigits == 5) CalcPoints = 0.00001;
return(CalcPoints);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
EventKillTimer();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
//Print( "Tick" );
int box = 0 ;
RefreshRates();
bool BarH = IsBarClosed(PERIOD_H1,0) ;
/******************* AT Touch ******************************/
RefreshRates();
LL[0] = HL[18];
HL[9] = LL[9];
if (Signal == OP_SELL){
for (int ii = 9;ii<19;ii++){
if((Bid < HL[ii]) && Bid > HL[ii+1]){
_sq = ii;
}
}
}
if (Signal == OP_BUY){
for (int ii = 0;ii<10;ii++){
if((Bid > LL[ii]) && Bid < LL[ii+1]){
_sq = ii;
}
}
}
/******************* AT Touch ******************************/
double open = iOpen(NULL,PERIOD_H1,0); //Curretn Open Price
if (Signal == OP_BUY){
if ((open > LL[1] ) && (Bid <= LL[1]) ){
if (Signal == OP_BUY && (_Buy[0] < Max_Order && _Buy[1] < Max_Order)){
Print( "Place Order Buy" );
if (OrderSend( Symbol(), OP_BUY, LotSize, Ask, 5, 0 ,LL[2] , "Buy Touch @ " + (string) _sq , MagicNumber,0, clrBlue) == -1)
{
Print("Error order Buy "+(string)ErrorDescription(GetLastError()) );
}
print();
}
}
}else if (Signal == OP_SELL){
if ((open < HL[10] ) && (Bid >= HL[10]) ){
if (Signal == OP_SELL && (_Sell[9] < Max_Order) && (_Sell[10] < Max_Order) ){
Print( "Place Order Sell" );
if (OrderSend( Symbol(), OP_SELL, LotSize, Bid, 5, 0, HL[11], "Sell Touch @ " + (string) _sq, MagicNumber,0, clrBrown) == -1)
{
Print("Error order Sell "+(string)ErrorDescription(GetLastError()) );
}
print();
}
}
}
Total_orders(BarH);
/****************** Normal Close ****************************/
if(BarH){
RefreshRates();
print();
open = iOpen(NULL,PERIOD_H1,1);
Amro();
_Update();
Total_orders(BarH);
if(Bid > LL[1]){
Signal = OP_BUY;
_Close(OP_SELL);
}else if(Bid < HL[10]){
Signal = OP_SELL;
_Close(OP_BUY);
}
for(int ii = 1 ; ii < 10; ii++){
if ((open > LL[ii-1] )&& (open < LL[ii]) && (Bid > LL[ii]) ){
Print( "SQ" , (string) ii );
_sq = ii;
if (Signal == OP_BUY && _Buy[ii] < Max_Order ){
Print( "Place Order Buy" );
if (OrderSend( Symbol(), OP_BUY, LotSize, Ask, 5, 0 ,LL[ii+1] , "Buy Close @ " + (string) _sq , MagicNumber,0, clrBlue) == -1)
{
Print("Error order sell "+(string)ErrorDescription(GetLastError()) );
//print();
}
}
print();
break;
}
}
for(int ii = 10 ; ii < 18; ii++){
if ( (open < HL[ii-1]) && (open > HL[ii]) && (Bid < HL[ii])){
Print( "SQ" , (string) ii );
_sq = ii;
if (Signal == OP_SELL && _Sell[ii] < Max_Order){
Print( "Place Order Sell" );
if (OrderSend( Symbol(), OP_SELL, LotSize, Bid, 5, 0, HL[ii+1], "Sell Close @ "+ (string) _sq, MagicNumber,0, clrRed) == -1)
{
Print("Error order Buy "+(string)ErrorDescription(GetLastError()) );
//print();
}
}
print();
break;
}
}
}
print();
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
}
//+------------------------------------------------------------------+
void print(bool trade = false){
Total_orders();
string s = "Status : \n "+
"SQ " + (string) _sq +
" SLt "+ (string) _Sell[_sq]+ " BLt "+ (string) _Buy[_sq] + "\n" +
" SLt "+ (string) _SLots+ " BLt "+ (string) _BLots+ "\n" +
" Pft "+ (string) _SProfit+ " pft "+ (string) _BProfit+ "\n" +
" Ttl "+ (string) profit ;
Comment(s);
}
//+------------------------------------------------------------------+
//| Clock function |
//+------------------------------------------------------------------+
bool IsBarClosed(int timeframe,int index)
{
static datetime lastbartime[3];
if(iTime(NULL,timeframe,1)==lastbartime[index]) // wait for new bar
return(false);
lastbartime[index]=iTime(NULL,timeframe,1);
return(true);
}
//+------------------------------------------------------------------+
//| Totals function |
//+------------------------------------------------------------------+
int _CountOrd, _Buy[30], _Sell[30],_sq;
double _SLots,_BLots;
double _SProfit, _BProfit;
double profit;
bool _q;
void Total_orders(bool P = false)
{
_CountOrd=0;
for (int i =0;i<30 ;i++){
_Buy[i]=0;_Sell[i]=0;
}
LL[0] = HL[18];
HL[9] = LL[9];
_SLots = 0.0; _BLots =0.0;
profit = 0.0;
_BProfit = 0.0; _SProfit = 0.0;
double max = 0;
double min = 1000;
RefreshRates();
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false) continue;
if(OrderSymbol()==Symbol()&&(OrderMagicNumber()==MagicNumber))
{
if(OrderType()==OP_BUY){
_CountOrd++;
_BLots += OrderLots();
_BProfit += OrderProfit()+OrderCommission()+OrderSwap();
for(int ii = 0 ; ii < 10; ii++){
if ((OrderOpenPrice() > LL[ii] )&& (OrderOpenPrice() < LL[ii+1])){
_Buy[ii]++;
}
}
}else if(OrderType()==OP_SELL){
_CountOrd++;
_SLots += OrderLots();
_SProfit += OrderProfit()+OrderCommission()+OrderSwap();
for(int ii = 9 ; ii < 18; ii++){
if ( (OrderOpenPrice() < HL[ii]) && (OrderOpenPrice() > HL[ii+1]) ){
_Sell[ii]++;
}
}
}
profit+=OrderProfit()+OrderCommission()+OrderSwap();
}
}
if(P){
Print("Total " + IntegerToString( _CountOrd)
+ " Buy " , _Buy[_sq] , " = " + (string) _BLots
+ " Sell " + IntegerToString(_Sell[_sq]) + " = " + (string) _SLots
+ " Profit " + (string) profit
);
}
}
//+-------------------------------------------------------------------+
//| Close Orders function |
//+-------------------------------------------------------------------+
void _Close(int direction){
double price = Ask;
if(direction == OP_SELL)
price = Ask;
else if(direction == OP_BUY)
price = Bid;
res = true;
string c;
// TP of all
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false) continue;
if(OrderSymbol()==Symbol() && OrderType()== direction &&(OrderMagicNumber()==MagicNumber)){
res = OrderClose(OrderTicket(),OrderLots(),price,3,clrBrown);
c = " Order #"+(string) OrderTicket()+" By Double rev profit: "+(string) OrderTakeProfit() +" Dir "+(string) direction + " Lots " +(string) OrderLots();
if(!res){
Print("Error in Closing _Close ALL . Error code=", ErrorDescription(GetLastError()) ,c);
print();
}else{
Print("Order Closed _Close ALL successfully." + c );
print();
}
Print(c);
}
}
}
//+-------------------------------------------------------------------+
//| Update function |
//+-------------------------------------------------------------------+
/*
break_even = MathAbs(RealPoint * profit / (_BLots - _SLots));
_BLots = (break_even * _SLots + RealPoint * profit)/break_even
*/
bool res;
void _Update(){
RefreshRates();
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false) continue;
if(OrderSymbol()==Symbol()&&(OrderMagicNumber()==MagicNumber))
{
if(OrderType()==OP_BUY){
for(int ii = 0 ; ii < 10; ii++){
if ((OrderOpenPrice() > LL[ii] )&& (OrderOpenPrice() < LL[ii+1]) && OrderTakeProfit() != LL[ii+1] ){
res=OrderModify(OrderTicket(),OrderOpenPrice(),0,LL[ii+1],0,Blue);
if(!res)
Print("Error in OrderModify OP_BUY . Error code=",ErrorDescription(GetLastError()));
else
Print("Order modified successfully. OP_BUY to LL " , ii+1);
}
}
}else if(OrderType()==OP_SELL){
for(int ii = 9 ; ii < 18; ii++){
if ( (OrderOpenPrice() < HL[ii]) && (OrderOpenPrice() > HL[ii+1]) && OrderTakeProfit() != HL[ii+1] ){
res=OrderModify(OrderTicket(),OrderOpenPrice(),0,HL[ii+1],0,Blue);
if(!res)
Print("Error in OrderModify OP_SELL . Error code=",ErrorDescription(GetLastError()));
else
Print("Order modified successfully. OP_SELL to HL " , ii+1);
}
}
}
}
}
}