-
Notifications
You must be signed in to change notification settings - Fork 2
/
Martingale1.mq4
286 lines (245 loc) · 8.3 KB
/
Martingale1.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
//+------------------------------------------------------------------+
//| Reverse_Martingale.mq4 |
//| Copyright 2017, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
input double FixLot=0.01; //Fix Lot Size
input int StopLoss=50; //Stoploss In Pips
input int TakeProfit=30; //TakeProfit In Pips
input int MagicNumber=123; //Order Magic Number
input int Max_Orders=15; //Maximum Orders To Open
input long Spread=5; //Spread In Pips
input bool UseMartingale=true; //Use Martingale
input double MaxLot=0.64; //Maximum Lot size To Use
input double Multiplier=2; //Lot Multiplier
input string Moving_Average="=================";
input int MaPeriod=200;
input int MaShift=0;
input ENUM_MA_METHOD Ma_Method=MODE_SMA;
input ENUM_APPLIED_PRICE Apply_Price=PRICE_CLOSE;
datetime candletime=0;
double stoplevel,Upper_stoplevel,Lower_stoplevel;
datetime EaStartTime=TimeCurrent();
double lotsize;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
bool signal = false;
AddOrder(signal);
//--- New Candle Filter
bool IsNewBar=Time[0]>candletime;
//--- Spread Filter
bool IsSpreadGood=SymbolInfoInteger(_Symbol,SYMBOL_SPREAD)<(Spread*10);
if(IsSpreadGood==false) Print("Current Spread is greater than the spread put in EA inputs");
//--- Trading Rules
if(IsNewBar && IsSpreadGood && Count(0)+Count(1)<Max_Orders && signal==false)
{
if(Close[1]>MAvg(1)) {OpenOrder(0); candletime=Time[0];}
if(Close[1]<MAvg(1)) {OpenOrder(1); candletime=Time[0];}
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Open Orders Counting Function |
//+------------------------------------------------------------------+
int Count(int Type)
{
int count=0;
for(int i=0; i<=OrdersTotal()-1; i++)
{
bool Select=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(Select && OrderSymbol()==_Symbol && OrderMagicNumber()==MagicNumber && OrderType()==Type) count++;
}
return (count);
}
//+------------------------------------------------------------------+
//| Open Pending Orders Function |
//+------------------------------------------------------------------+
double OpenOrder(int Type , double lot =0)
{
double openprice=0; double stoploss=0; double takeprofit=0; double Lot=FixLot; double lots[]; LotSizeCalc(lots);
if(_Point==0.00001 || _Point==0.001)
{
stoploss=(StopLoss*10)*_Point;
takeprofit=(TakeProfit*10)*_Point;
}
if(_Point==0.0001 || _Point==0.01)
{
stoploss=(StopLoss)*_Point;
takeprofit=(TakeProfit)*_Point;
}
if(lots[losscnt()]>=MaxLot)
{
EaStartTime=TimeCurrent();
return 0;
}
if(Type==OP_BUY)
{
openprice=Ask;
if(StopLoss>0) stoploss=openprice-stoploss;
if(TakeProfit>0) takeprofit=openprice+takeprofit;
}
if(Type==OP_SELL)
{
openprice=Bid;
if(StopLoss>0) stoploss=openprice+stoploss;
if(TakeProfit>0) takeprofit=openprice-takeprofit;
}
if(Lot>PrevOrderLot()) lotsize=Lot;
if(lot>0)Lot = lot;
if(Lot>MaxLot) Lot=FixLot;
int Ticket=OrderSend(_Symbol,Type,Lot,openprice,300,stoploss,takeprofit,"Order Placed",MagicNumber,0,clrGreen);
return(Ticket);
}
//+------------------------------------------------------------------+
//| Indicator Moving Average Function |
//+------------------------------------------------------------------+
double MAvg(int shift)
{
double MA=iMA(_Symbol,0,MaPeriod,MaShift,Ma_Method,Apply_Price,shift);
return (MA);
}
//+------------------------------------------------------------------+
//| Martingale LotSize Calculation Function |
//+------------------------------------------------------------------+
void LotSizeCalc(double &arr[])
{
ArrayResize(arr,100,0);
arr[0]=FixLot;
for(int i=1;i<ArraySize(arr);i++)
{
arr[i]=arr[i-1]*Multiplier;
}
}
//+------------------------------------------------------------------+
//| Orders WinCount & LossCount Function |
//+------------------------------------------------------------------+
int losscnt()
{
int WinCount=0;
int Losscnt=0;
for(int Count=OrdersTotal()-1;Count>=0; Count--)
{
bool select=OrderSelect(Count,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderSymbol()==_Symbol && OrderType()<=1 && OrderOpenTime()>=EaStartTime)
{
if(OrderProfit()>0 && Losscnt==0) WinCount++;
else if(OrderProfit()<0 && WinCount==0) Losscnt++;
else break;
}
}
return(WinCount);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Open Orders Lot Selecting Function |
//+------------------------------------------------------------------+
double PrevOrderLot()
{
double lot=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
bool Select=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(Select && OrderSymbol()==_Symbol && OrderType()<=1 && OrderMagicNumber()==MagicNumber)
{
lot=OrderLots();
break;
}
}
return (lot);
}
struct COrders
{
int ticket;
double lots;
bool Istradeopen;
}orders[];
bool IsTicketAlreadyExist(int ticket)
{
bool signal = false;
for(int i=0;i<ArraySize(orders);i++)
{
if(ticket == orders[i].ticket)
{
signal=true;
break;
}
}
return(signal);
}
void AddOrder(bool &signal)
{
//static datetime timer=TimeCurrent();
signal=false;
for(int i=OrdersHistoryTotal()-1;i>=0;i--)
{
bool select = OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
if(select && OrderMagicNumber()==MagicNumber && OrderSymbol()==_Symbol// && OrderCloseTime()>=timer
&& OrderProfit()+OrderSwap()+OrderCommission()>0)
{
if(IsTicketAlreadyExist(OrderTicket())==false)
{
ArrayResize(orders,ArraySize(orders)+1,0);
orders[ArraySize(orders)-1].ticket=OrderTicket();
orders[ArraySize(orders)-1].lots=OrderLots();
orders[ArraySize(orders)-1].Istradeopen=false;
}
}
}
//---
if(ArraySize(orders)>0)
{
static datetime timecandle = Time[0];
bool IsNewBar = Time[0]>timecandle;
if(IsNewBar)
{
int index =-1;
double highest_lot = GetHighestOrderLotFromArray(index);
if(highest_lot >0)
{
if(Close[1]>MAvg(1)) OpenOrder(0,NormalizeDouble(highest_lot*Multiplier,2));
if(Close[1]<MAvg(1)) OpenOrder(1,NormalizeDouble(highest_lot*Multiplier,2));
orders[index].Istradeopen=true;
signal=true;
candletime=Time[0];
}
timecandle=Time[0];
}
}
}
double GetHighestOrderLotFromArray(int &index )
{
double h_lot =0;
for(int i=0;i<ArraySize(orders);i++)
{
if(orders[i].lots>h_lot && orders[i].Istradeopen==false)
{
h_lot=orders[i].lots;
index=i;
}
}
return(h_lot);
}