-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMC Trader TriggerLines V2.1.mq4
312 lines (288 loc) · 11.4 KB
/
SMC Trader TriggerLines V2.1.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
//+------------------------------------------------------------------+
//| SMC Autotrader Momentum.mq4 |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
extern double TakeProfit = 50;
extern double Lots = 0.1;
extern double InitialStop = 30;
extern double TrailingStop = 20;
datetime BarTime;
static string DirTrgS = "None";
static string DirTrgM= "None";
static string DirTrgL = "None";
//#####################################################################
int init()
{
//----
BarTime=0;
//----
return(0);
}
//#####################################################################
int start()
{
int cnt,total,ticket,MinDist,tmp;
double Spread;
double ATR;
double StopMA;
double SetupHigh, SetupLow;
// int LSMA_Period = 25;
// int Rperiod = 25;
int length;
double lsma_length;
double sum[30];
double wt[30];
double lsma_ma[30];
double lengthvar;
double tmpTG;
int shift,i,j;
double lsma10, lsma25;
//############################################################################
if(Bars<100)
{Print("bars less than 100");
return(0);}
//exit if not new bar
if(BarTime == Time[0]) {return(0);}
//new bar, update bartime
BarTime = Time[0];
//########################################################################################
length = 15; //Hourly set to 15 bars
lsma_length = 15;
for(shift = 30; shift >= 0; shift--) // MAIN For Loop
{
sum[1] = 0;
for(i = length; i >= 1 ; i--) //LSMA loop
{
lengthvar = length + 1; //lengthvar = 21
lengthvar /= 3; //lengthvar = 7
tmpTG = 0;
tmpTG = ( i - lengthvar)*Close[length-i+shift]; //tmp = 20 - 7 * close[20-i+shift]
sum[1]+=tmpTG;
}
wt[shift] = sum[1]*6/(length*(length+1));
j = shift;
lsma_ma[shift] = wt[j+1] + (wt[j]-wt[j+1])* 2/(lsma_length+1);
}
DirTrgS = "None";
if(wt[1] > lsma_ma[1] && wt[2] > lsma_ma[2] ) DirTrgS = "Long"; //wt[2] < lsma_ma[2] &&
if(wt[1] < lsma_ma[1] && wt[2] < lsma_ma[2] ) DirTrgS = "Short"; // wt[2] > lsma_ma[2] &&
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
length = 60; //4Hourly = 15(hourly) *4
lsma_length = 60;
for(shift = 30; shift >= 0; shift--) // MAIN For Loop
{
sum[1] = 0;
for(i = length; i >= 1 ; i--) //LSMA loop
{
lengthvar = length + 1; //lengthvar = 21
lengthvar /= 3; //lengthvar = 7
tmpTG = 0;
tmpTG = ( i - lengthvar)*Close[length-i+shift]; //tmp = 20 - 7 * close[20-i+shift]
sum[1]+=tmpTG;
}
wt[shift] = sum[1]*6/(length*(length+1));
j = shift;
lsma_ma[shift] = wt[j+1] + (wt[j]-wt[j+1])* 2/(lsma_length+1);
}
DirTrgM = "None";
if(wt[1] > lsma_ma[1] && wt[2] > lsma_ma[2]) DirTrgM = "Long"; //2 days needed to confirm trend //wt[2] < lsma_ma[2] &&
if(wt[1] < lsma_ma[1] && wt[2] < lsma_ma[2]) DirTrgM = "Short"; //wt[2] > lsma_ma[2] &&
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
length = 100; // = Daily
lsma_length = 100;
for(shift = 30; shift >= 0; shift--) // MAIN For Loop
{
sum[1] = 0;
for(i = length; i >= 1 ; i--) //LSMA loop
{
lengthvar = length + 1; //lengthvar = 21
lengthvar /= 3; //lengthvar = 7
tmpTG = 0;
tmpTG = ( i - lengthvar)*Close[length-i+shift]; //tmp = 20 - 7 * close[20-i+shift]
sum[1]+=tmpTG;
}
wt[shift] = sum[1]*6/(length*(length+1));
j = shift;
lsma_ma[shift] = wt[j+1] + (wt[j]-wt[j+1])* 2/(lsma_length+1);
}
DirTrgL = "None";
if(wt[1] > lsma_ma[1] && wt[2] > lsma_ma[2]) DirTrgL = "Long"; //2 days needed to confirm trend //wt[2] < lsma_ma[2] &&
if(wt[1] < lsma_ma[1] && wt[2] < lsma_ma[2]) DirTrgL = "Short"; //wt[2] > lsma_ma[2] &&
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//#########################################################################################
//~~~~~~~~~~~~~~~~Miscellaneous setup stuff~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MinDist=MarketInfo(Symbol(),MODE_STOPLEVEL);
Spread=(Ask-Bid);
// use an indicator for data values
// ATR =iATR(NULL,0,10,0); // BE CAREFUL OF EFFECTING THE AUTO TRAIL STOPS
double TrendS=iMA(NULL,0,5,0,MODE_SMA,PRICE_CLOSE,0);
double TrendM=iMA(NULL,0,12,0,MODE_SMA,PRICE_CLOSE,0);
//#########################################################################################
//########################################################################################
//################## ORDER CLOSURE ###################################################
// If Orders are in force then check for closure against Technicals LONG & SHORT
//CLOSE LONG Entries
total=OrdersTotal();
if(total>0)
{
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
{
if(DirTrgS == "Short")
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close LONG position
}}
//CLOSE SHORT ENTRIES:
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_SELL && OrderSymbol()==Symbol()) // check for symbol
{
if(DirTrgS == "Long")
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close SHORT position
}}
} // for loop return
} // close 1st if
//################################################################################
//################## LOCK IN 50 points profit ###################################
if(0==1)
{
total=OrdersTotal();
if(total>0)
{
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_BUY && OrderSymbol()==Symbol()
//&& OrderOpen < Close[0]+50points
)
{
OrderModify(OrderTicket(),OrderOpenPrice(),0,OrderTakeProfit(),0,White);
}
}}
total=OrdersTotal();
if(total>0)
{
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_SELL && OrderSymbol()==Symbol())
{
OrderModify(OrderTicket(),OrderOpenPrice(),High[1],OrderTakeProfit(),0,White);
}
}}
}
//##############################################################################
//################## ORDER TRAILING STOP Adjustment #######################
//TRAILING STOP: LONG
if(0==1) //This is used to turn the trailing stop on & off
{
total=OrdersTotal();
if(total>0)
{
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_BUY && OrderSymbol()==Symbol()
&&
Bid-OrderOpenPrice()> (Point*TrailingStop)
&&
OrderStopLoss()<Bid-(Point*TrailingStop)
)
{OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,White);
return(0);}
}}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//TRAILING STOP: SHORT
total=OrdersTotal();
if(total>0)
{
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_SELL && OrderSymbol()==Symbol()
&&
OrderOpenPrice()-Ask > (Point*TrailingStop)
&&
OrderStopLoss() > Ask+(Point*TrailingStop)
)
{OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(Point*TrailingStop),OrderTakeProfit(),0,Yellow);
return(0);}
}}
} // end bracket for on/off switch
//##########################################################################################
//~~~~~~~~~~~ END OF ORDER Closure routines & Stoploss changes ~~~~~~~~~~~~~~~~~~~~~~~~~~~
//##########################################################################################
//~~~~~~~~~~~~START of NEW ORDERS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//######################### NEW POSITIONS ? ######################################
//Possibly add in timer to stop multiple entries within Period
// Check Margin available
// ONLY ONE ORDER per SYMBOL
// Loop around orders to check symbol doesn't appear more than once
// Check for elapsed time from last entry to stop multiple entries on same bar
if (0==1) // switch to turn ON/OFF history check
{
total=HistoryTotal();
if(total>0)
{
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY); //Needs to be next day not as below
if(OrderSymbol()==Symbol()&& CurTime()- OrderCloseTime() < (Period() * 60 )
)
{
return(0);
}}}}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
total=OrdersTotal();
if(total>0)
{
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol()) return(0);
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if(AccountFreeMargin()<(1000*Lots))
{Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//#########################################################################################
//ENTRY RULES: LONG
if(DirTrgS == "Long" &&
DirTrgM == "Long" &&
DirTrgL == "Long"
)
{Alert(Symbol()," Long");
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Trigger Long",16384,0,Orange); //Bid-(Point*(MinDist+2))
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//ENTRY RULES: SHORT //################################
if(DirTrgS == "Short" &&
DirTrgM == "Short" &&
DirTrgL == "Short"
)
{Alert(Symbol()," Short");
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"Trigger Short",16384,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
//#######################################################################################################
//####################################################################################
//############ End of PROGRAM #########################
return(0);
}