-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
UpFractal.mq4
297 lines (260 loc) · 8.09 KB
/
UpFractal.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
//+------------------------------------------------------------------+
//| UpFractal.mq4 |
//| Eng. Ahmad Lutfi |
//| https://www.lutfipro.com |
//+------------------------------------------------------------------+
#property copyright "Eng. Ahmad Lutfi"
#property link "https://www.lutfipro.com"
#property version "1.00"
#property strict
extern double TakeProfit = 30;
extern double StopLoss = 10;
extern double LotSize = 0.01;
extern int Slippage = 10;
int PipAdjust =1;
int buyticket = 0;
int sellticket =0;
double resistanceLine=0.0;
double supportLine = 0.0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int AdjustPips()
{
int pt = 1;
if(Digits==5 || Digits==3)
{
pt *=10;
}
return pt;
}
void InitVals()
{
PipAdjust = AdjustPips();
TakeProfit *= PipAdjust;
StopLoss *= PipAdjust;
Slippage *= PipAdjust;
}
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
if(IsNewCandle())
{
int Candle = 1;
//check last candle was an Upper Fractal
if(FractalDown(Candle))
{
//draw Support line
// supportLine = getLoPart(1);
supportLine = Low[Candle];
supportLine = NormalizeDouble(supportLine,Digits);
//TODO: set a pending Sell Order...
if(supportLine<Ask && supportLine<Bid)
{
//set a pending Sell Order!
setPendingSell(supportLine);
}
}
if(FractalUP(Candle))
{
//draw resistance line
//resistanceLine = getHiPart(); the easy Way!!
resistanceLine = High[Candle];
resistanceLine = NormalizeDouble(resistanceLine,Digits);
//TODO: set a pending BUY Order...
if(resistanceLine> Ask && resistanceLine > Bid)
{
//Set a Pending Buy!
setPendingBuy(resistanceLine);
}
}
}
}
//---
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool FractalUP(int Lookback=1)
{
bool IsUpFractal = EMPTY_VALUE;
// if(Lookback!=0)
// {
int Candle0 = Lookback-1;
int Candle1 = Lookback;
int Candle2 = Lookback + 1;
int Candle3 = Lookback + 2;
if( (getHiPart(Candle1)>getHiPart(Candle2) && getHiPart(Candle2)>getHiPart(Candle3) )
&& ( Open[Candle0] < getHiPart(Candle1 ) ) )
{
IsUpFractal = true;
}
//TODO: Place the Downfractal here and set upFlag to false;
if ( ((getLoPart(Candle1)<getLoPart(Candle2)) && (getLoPart(Candle2)<getLoPart(Candle3)) )
&& (Open[Candle0]> getLoPart(Candle1))
)
{
IsUpFractal = false;
}
// }
return IsUpFractal;
}
bool FractalDown(int Lookback =1)
{
bool IsDownFractal = EMPTY_VALUE;
// if(Lookback!=0)
// {
int Candle0 = Lookback-1;
int Candle1 = Lookback;
int Candle2 = Lookback + 1;
int Candle3 = Lookback + 2;
if ( ((getLoPart(Candle1)<getLoPart(Candle2)) && (getLoPart(Candle2)<getLoPart(Candle3)) )
&& (Open[Candle0]> getLoPart(Candle1))
)
{
IsDownFractal = true;
}
if( (getHiPart(Candle1)>getHiPart(Candle2) && getHiPart(Candle2)>getHiPart(Candle3) )
&& ( Open[Candle0] < getHiPart(Candle1 ) ) )
{
IsDownFractal = false;
}
// }
return IsDownFractal;
}
bool IsBullCandle(int lookback=1)
{
bool f = false;
if(Open[lookback]<Close[lookback])
{
f = true;
}
return f;
}
bool IsBearCandle(int lookback=1)
{
bool f = false;
if(Open[lookback] > Close[lookback])
{
f= true;
}
return f;
}
//+------------------------------------------------------------------+
double getHiPart(int Candle=1)
{
double HiPart = 0;
if(IsBullCandle(Candle))
{
//the below part is Open[Candle1]
HiPart = Close[Candle];
}
if(IsBearCandle(Candle))
{
HiPart = Open[Candle];
}
return NormalizeDouble(HiPart,Digits);
}
double getLoPart(int Candle=1)
{
double LoPart = 0;
if(IsBullCandle(Candle))
{
LoPart = Open[Candle];
}
if(IsBearCandle(Candle))
{
LoPart = Close[Candle];
}
return NormalizeDouble(LoPart,Digits);
}
void OrderEntry(int direction)
{
if(direction==0)
{
double tp=Ask+TakeProfit*Point;
double sl=Ask-StopLoss*Point;
if(OpenOrdersThisPair(Symbol())==0)
int buyticket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,0,0,NULL,/*MagicNumber*/0,0,Green);
if(buyticket>0)OrderModify(buyticket,OrderOpenPrice(),sl,tp,0,CLR_NONE);
}
if(direction==1)
{
double tp=Bid-TakeProfit*Point;
double sl=Bid+StopLoss*Point;
if(OpenOrdersThisPair(Symbol())==0)
int sellticket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,0,0,NULL,0/*MagicNumber*/,0,Red);
if(sellticket>0)OrderModify(sellticket,OrderOpenPrice(),sl,tp,0,CLR_NONE);
}
}
//+------------------------------------------------------------------+
//checks to see if any orders open on this currency pair.
//+------------------------------------------------------------------+
int OpenOrdersThisPair(string pair)
{
int total=0;
for(int i=OrdersTotal()-1; i >= 0; i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()== pair) total++;
}
return (total);
}
//+------------------------------------------------------------------+
//insuring its a new candle function
//+------------------------------------------------------------------+
bool IsNewCandle()
{
static int BarsOnChart=0;
if (Bars == BarsOnChart)
return (false);
BarsOnChart = Bars;
return(true);
}
//+------------------------------------------------------------------+
int setPendingBuy(double Price)
{
// int slippage = Slippage * PipAdjust;
double stop_loss = Price - (StopLoss * Point); //* PipAdjust;
double take_profit = Price +( TakeProfit * Point);// * PipAdjust;
int result =0;
if(Ask > Price)
{
result = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,Price,Slippage,stop_loss,take_profit,"ACandles_Long",0,0,CLR_NONE);
}
if(Ask < Price)
{
result = OrderSend(Symbol(),OP_BUYSTOP,LotSize,Price,Slippage,stop_loss,take_profit,"ACandles_Long",0,0,CLR_NONE);
}
return result;
}
int setPendingSell(double Price)
{
double stop_loss = Price + (StopLoss * Point); //* PipAdjust;
double take_profit = Price -( TakeProfit * Point);// * PipAdjust;
int result =0;
if(Bid > Price)
{
result = OrderSend(Symbol(),OP_SELLSTOP,LotSize,Price,Slippage,stop_loss,take_profit,"ACandles_Short",0,0,CLR_NONE);
}
if(Bid < Price)
{
result = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,Price,Slippage,stop_loss,take_profit,"ACandles_Short",0,0,CLR_NONE);
}
return result;
}