-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOneWay.mq4
313 lines (279 loc) · 10.8 KB
/
OneWay.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
//+------------------------------------------------------------------+
//| OneWay.mq4 |
//| Copyright 2015, Neko Prog |
//| https://www.mql5.com/en/users/megahentai |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, Neko Prog"
#property link "https://www.mql5.com/en/users/megahentai"
#property version "1.00"
#property strict
extern string TradingSettings = "==== Trade Settings ====";
extern double LotExponent = 0.1;
extern double MinLot = 0.01;
extern double MaxLot = 99;
extern double TakeProfit = 100;
extern double LayerAfter = 20;
extern ENUM_TIMEFRAMES TimeFrame = PERIOD_M5;
extern string StochSettings = "==== Stoch Settings ====";
extern int Kperiod = 9;
extern int Dperiod = 3;
extern int Slowing = 5;
extern string OtherSettings = "==== Other Settings ====";
extern int OrderMagic = 44342;
extern string Copyright = "==== \x00A9 2015 Neko Prog ====";
double lots,currLots,stoplevel,orderProfit,totalProfit,lastPrice;
string lastSignal,dispToday;
int totalOrder;
datetime currTime,prevTime;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//ObjectsDeleteAll(); // clear the chart graphical objects
//Comment(""); // clear the chart comments
return(0);
}
//+------------------------------------------------------------------+
//| Expert core function |
//+------------------------------------------------------------------+
void sendOrder(int orderPos,double orderLot,double orderPrice,double orderSL,double orderTP,color orderColor)
{
bool order;
order = OrderSend(Symbol(),orderPos,orderLot,orderPrice,3,orderSL,orderTP,"OneWay_nEk0_v1",OrderMagic,0,orderColor);
}
bool newTime()
{
currTime=iTime(Symbol(),TimeFrame,0);
if (prevTime!=currTime)
{
prevTime=currTime;
return true;
}
else {return false;}
}
void lotRegulator()
{
stoplevel = MarketInfo(Symbol(),MODE_STOPLEVEL);
lots = NormalizeDouble((AccountEquity()*LotExponent/10000),2);
if (lots<MinLot) lots = MinLot;
if (lots>MaxLot) lots = MaxLot;
}
int countOrder()
{
totalOrder = 0;
for (int i=0; i<OrdersTotal(); i++)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
if (OrderSymbol()!=Symbol()||OrderMagicNumber()!=OrderMagic) continue;
else {totalOrder++;}
}
}
return totalOrder;
}
double countProfit()
{
totalProfit = 0;
int tip;
for (int i=0; i<OrdersTotal(); i++)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
if (OrderSymbol()!=Symbol()||OrderMagicNumber()!=OrderMagic) continue;
else
{
tip=OrderType();
if (tip==0)
{
lastSignal = "buy";
if (lastPrice==0 || OrderOpenPrice()<lastPrice) lastPrice = OrderOpenPrice();
}
if (tip==1)
{
lastSignal = "sell";
if (lastPrice==0 || OrderOpenPrice()>lastPrice) lastPrice = OrderOpenPrice();
}
orderProfit = (OrderProfit()+OrderCommission()+OrderSwap())/OrderLots()/MarketInfo(OrderSymbol(),MODE_TICKVALUE);
totalProfit = totalProfit+orderProfit;
}
}
}
return NormalizeDouble(totalProfit,0);
}
string signal()
{
double prevMain,prevSignal,currMain,currSignal;
double prevUpperH1,currUpperH1,prevMidH1,currMidH1,prevLowerH1,currLowerH1;
double prevUpperH4,currUpperH4,prevMidH4,currMidH4,prevLowerH4,currLowerH4;
double H1Close,H4Close;
//H1 BB
H1Close = iClose(Symbol(),PERIOD_H1,1);
prevUpperH1 = iBands(Symbol(),PERIOD_H1,20,2,0,PRICE_CLOSE,MODE_UPPER,2);
prevMidH1 = iBands(Symbol(),PERIOD_H1,20,2,0,PRICE_CLOSE,MODE_MAIN,2);
prevLowerH1 = iBands(Symbol(),PERIOD_H1,20,2,0,PRICE_CLOSE,MODE_LOWER,2);
currUpperH1 = iBands(Symbol(),PERIOD_H1,20,2,0,PRICE_CLOSE,MODE_UPPER,1);
currMidH1 = iBands(Symbol(),PERIOD_H1,20,2,0,PRICE_CLOSE,MODE_MAIN,1);
currLowerH1 = iBands(Symbol(),PERIOD_H1,20,2,0,PRICE_CLOSE,MODE_LOWER,1);
//H4 BB
H4Close = iClose(Symbol(),PERIOD_H4,1);
prevUpperH4 = iBands(Symbol(),PERIOD_H4,20,2,0,PRICE_CLOSE,MODE_UPPER,2);
prevMidH4 = iBands(Symbol(),PERIOD_H4,20,2,0,PRICE_CLOSE,MODE_MAIN,2);
prevLowerH4 = iBands(Symbol(),PERIOD_H4,20,2,0,PRICE_CLOSE,MODE_LOWER,2);
currUpperH4 = iBands(Symbol(),PERIOD_H4,20,2,0,PRICE_CLOSE,MODE_UPPER,1);
currMidH4 = iBands(Symbol(),PERIOD_H4,20,2,0,PRICE_CLOSE,MODE_MAIN,1);
currLowerH4 = iBands(Symbol(),PERIOD_H4,20,2,0,PRICE_CLOSE,MODE_LOWER,1);
prevMain = iStochastic(Symbol(),TimeFrame,Kperiod,Dperiod,Slowing,MODE_SMA,0,MODE_MAIN,2);
prevSignal = iStochastic(Symbol(),TimeFrame,Kperiod,Dperiod,Slowing,MODE_SMA,0,MODE_SIGNAL,2);
currMain = iStochastic(Symbol(),TimeFrame,Kperiod,Dperiod,Slowing,MODE_SMA,0,MODE_MAIN,1);
currSignal = iStochastic(Symbol(),TimeFrame,Kperiod,Dperiod,Slowing,MODE_SMA,0,MODE_SIGNAL,1);
if (//Uptrend, look for buy only
currUpperH1>prevUpperH1 &&
currMidH1>prevMidH1 &&
H1Close>currMidH1 &&
currUpperH4>prevUpperH4 &&
currMidH4>prevMidH4 &&
H4Close>currMidH4 &&
//Oversell, Buy signal
currMain>prevMain &&
currMain>currSignal &&
currMain>=20 &&
currMain<=30
) return("buy");
if (//Downtrend, look for sell only
currLowerH1<prevLowerH1 &&
currMidH1<prevMidH1 &&
H1Close<currMidH1 &&
currLowerH4<prevLowerH4 &&
currMidH4<prevMidH4 &&
H4Close<currMidH4 &&
//Overbuy, Sell signal
currMain<prevMain &&
currMain<currSignal &&
currMain<=80 &&
currMain>=70
) return("sell");
else {return("no signal");}
}
void readDay()
{
switch(DayOfWeek())
{
case 0: dispToday="Sunday"; break;
case 1: dispToday="Monday"; break;
case 2: dispToday="Tuesday"; break;
case 3: dispToday="Wednesday"; break;
case 4: dispToday="Thursday"; break;
case 5: dispToday="Friday"; break;
case 6: dispToday="Saturday"; break;
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if (Period()==TimeFrame)
{
readDay();
lotRegulator();
// For debug purpose, can be commented out
Comment("Current Day : ",dispToday,
"\nBroker Time : ",TimeCurrent(),
"\nLocal Time : ",TimeLocal(),
"\nASK : ",NormalizeDouble(Ask,Digits),
"\nBID : ",NormalizeDouble(Bid,Digits),
"\nBalance : ",NormalizeDouble(AccountBalance(),2),
"\nEquity : ",NormalizeDouble(AccountEquity(),2),
"\nStop Level : ",stoplevel,
"\nSpread : ",MarketInfo(Symbol(),MODE_SPREAD),
"\nLots : ",lots,
"\nTotal Orders : ",countOrder(),
"\nProfit in Pips : ",countProfit(),
"\nTime Left : ",TimeToStr(Period()*60+Time[0]-TimeCurrent(),TIME_MINUTES|TIME_SECONDS) );
if (newTime())
{
// No order, send order
if (countOrder()==0)
{
lotRegulator();
currLots = lots;
if (signal()=="buy")
{
RefreshRates();
sendOrder(OP_BUY,currLots,Ask,0,0,clrBlue);
lastPrice = Ask;
lastSignal = "buy";
}
if (signal()=="sell")
{
RefreshRates();
sendOrder(OP_SELL,currLots,Bid,0,0,clrOrange);
lastPrice = Bid;
lastSignal = "sell";
}
}
// Have order, layer if price goes away
if (countOrder()>0 /*&& totalOrder<5*/)
{
if (countProfit()<TakeProfit)
{
if (lastSignal=="buy" && lastPrice>=Ask+LayerAfter*Point)
{
RefreshRates();
sendOrder(OP_BUY,currLots,Ask,0,0,clrBlue);
lastPrice = Ask;
}
if (lastSignal=="sell" && lastPrice<=Bid-LayerAfter*Point)
{
RefreshRates();
sendOrder(OP_SELL,currLots,Bid,0,0,clrOrange);
lastPrice = Bid;
}
}
}
}
// Have order, take profit
if (countOrder()>0)
{
if (countProfit()>=TakeProfit)
{
bool close;
int tip;
for (int i=0; i<OrdersTotal(); i++)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
if (OrderSymbol()!=Symbol()||OrderMagicNumber()!=OrderMagic) continue;
tip=OrderType();
//Buy
if (tip==0)
{
RefreshRates();
close = OrderClose(OrderTicket(),OrderLots(),Bid,3,clrGreen); //TP
lastPrice = Ask;
continue;
}
//Sell
if (tip==1)
{
RefreshRates();
close = OrderClose(OrderTicket(),OrderLots(),Ask,3,clrGreen); //TP
lastPrice = Bid;
continue;
}
}
}
}
}
}
else {Print("Trade run on ",EnumToString(TimeFrame)," timeframe only.");}
}
//+------------------------------------------------------------------+