-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrailingStop.mq4
294 lines (240 loc) · 9.16 KB
/
TrailingStop.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
//+------------------------------------------------------------------+
//| TrailingStop.mq4 |
//| Copyright © 2006, http://www.forex-tsd.com |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright © Steve Wilson - Forex TSD Forum"
#property link "http://www.forex-tsd.com"
//+--------------------------------------------------------------------+
// This trailing stoploss is made of features found in other Experts. |
// 1. Trailing Stop Loss which is chart specific |
// 2. AllPositions - Trailing Stop Loss which is account specific |
// Attach to any chart and it will modify stop loss on all trades. |
// 3. ProfitTrailing - Modify only trades that are in profit |
// 4. Hedge Option - Does not open positions but will close all trades |
// if a specified target is reached on your trading account. |
// Activate it by giving a value to ProfitTarget. Disables stop |
// loss by setting value to 5000 |
//+--------------------------------------------------------------------+
extern bool AllPositions = false; // (True To modify all positions ) False(modify chart positions)
extern bool ProfitTrailing = True; //(To Trail only postions on profit not loss )
extern int TrailingStop=5; // Minimum is 5
extern int ProfitTarget=0; // Setting this value will calculate all open trades and close if profit reached.
extern int TrailingStep = 1;
extern bool UseSound = True;
extern string NameFileSound = "expert.wav";
bool runnable=true;
bool init=true;
bool result;
double pBid, pAsk, pp;
int i=0;
datetime timeprev=0;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
if (ProfitTarget>0)
{
TrailingStop=0;
ChartComment();
ClearStops();
CloseAllTrades();
}
if (AllPositions == false){
//+------------------------------------------------------------------+
//| Expert start function - Chart Specific |
//+------------------------------------------------------------------+
// Clean the chart
if (ProfitTarget==0)
CleanChart();
//Trailing Stop
TrailingAlls(TrailingStop);
//Close/Open
if(timeprev==Time[0])
return(0);
timeprev=Time[0];
return(0);
}
else
{
//+------------------------------------------------------------------+
//| Expert Start Function - All Positions |
//+------------------------------------------------------------------+
// Clean the chart
if (ProfitTarget==0)
CleanChart();
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
TrailingPositions();
}
}
return(0);
}
}
void TrailingAlls(int trail)
{
if (trail==0)
return;
double stopcrnt;
double stopcal;
int trade;
int trades=OrdersTotal();
for(trade=0;trade<trades;trade++)
{
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol())
continue;
pp = MarketInfo(OrderSymbol(), MODE_POINT);
//Long
if(OrderType()==OP_BUY)
{
pBid = MarketInfo(OrderSymbol(), MODE_BID);
if (!ProfitTrailing || (pBid-OrderOpenPrice())>TrailingStop*pp) {
if (OrderStopLoss()<pBid-(TrailingStop+TrailingStep-1)*pp) {
stopcrnt=OrderStopLoss();
stopcal=Bid-(trail*Point);
if(stopcrnt==0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Blue);
}
else
{
if(stopcal>stopcrnt)
{
OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Blue);
}
}
}
return;
}
}//Long
//Shrt
if(OrderType()==OP_SELL)
{
pAsk = MarketInfo(OrderSymbol(), MODE_ASK);
if (!ProfitTrailing || OrderOpenPrice()-pAsk>TrailingStop*pp) {
if (OrderStopLoss()>pAsk+(TrailingStop+TrailingStep-1)*pp || OrderStopLoss()==0) {
stopcrnt=OrderStopLoss();
stopcal=Ask+(trail*Point);
if(stopcrnt==0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Red);
}
else
{
if(stopcal<stopcrnt)
{
OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Red);
}
}
}
return;
}
}//Shrt
}//for
}
//+------------------------------------------------------------------+
//| Functions for All Positions |
//+------------------------------------------------------------------+
void TrailingPositions() {
// double pBid, pAsk, pp;
pp = MarketInfo(OrderSymbol(), MODE_POINT);
if (OrderType()==OP_BUY) {
pBid = MarketInfo(OrderSymbol(), MODE_BID);
if (!ProfitTrailing || (pBid-OrderOpenPrice())>TrailingStop*pp) {
if (OrderStopLoss()<pBid-(TrailingStop+TrailingStep-1)*pp) {
ModifyStopLoss(pBid-TrailingStop*pp);
return;
}
}
}
if (OrderType()==OP_SELL) {
pAsk = MarketInfo(OrderSymbol(), MODE_ASK);
if (!ProfitTrailing || OrderOpenPrice()-pAsk>TrailingStop*pp) {
if (OrderStopLoss()>pAsk+(TrailingStop+TrailingStep-1)*pp || OrderStopLoss()==0) {
ModifyStopLoss(pAsk+TrailingStop*pp);
return;
}
}
}
}
//+------------------------------------------------------------------+
//| Modify StopLoss |
//+------------------------------------------------------------------+
void ModifyStopLoss(double ldStopLoss) {
bool fm;
fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);
if (fm && UseSound) PlaySound(NameFileSound);
}
//+------------------------------------------------------------------+
void ChartComment()
{
string sComment = "";
string sp = "****************************\n";
string NL = "\n";
sComment = NL + sp;
sComment = sComment + "Current Profits (USD) = $" + DoubleToStr(AccountProfit(),2) + NL;
sComment = sComment + "Profit Target (USD) = $" + ProfitTarget + NL;
sComment = sComment + "Open Trades = " + OrdersTotal() + NL;
sComment = sComment + "Stop Loss Disabled = ("+ TrailingStop+")" + NL;
sComment = sComment + NL + sp;
Comment(sComment);
}
void CleanChart()
{
string sComment = "";
string sp = "****************************\n";
string NL = "\n";
sComment = sComment + NL;
sComment = sComment + NL;
sComment = sComment + NL;
sComment = sComment + NL;
sComment = sComment + NL;
sComment = sComment + NL;
sComment = sComment + NL;
Comment(sComment);
}
// Close all open trades when in profit
void CloseAllTrades(){
if (AccountProfit() >= ProfitTarget){
for (i=OrdersTotal()-1;i>=0;i--){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if (OrderType()==OP_SELL)result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,CLR_NONE);
if(result!=TRUE) Print("LastError = ", GetLastError());
if (OrderType()==OP_BUY)result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,CLR_NONE);
if(result!=TRUE) Print("LastError = ", GetLastError());
if (OrderType()==OP_BUYSTOP)result=OrderDelete(OrderTicket());
if(result!=TRUE) Print("LastError = ", GetLastError());
if (OrderType()==OP_SELLSTOP)result=OrderDelete(OrderTicket());
if(result!=TRUE) Print("LastError = ", GetLastError());
}
else Print( "Error when order select ", GetLastError());
}
}
}
// Function to clear all existing Stop Losses
void ClearStops(){
for (int i=0;i<OrdersTotal();i++){
if (OrderSelect(i,SELECT_BY_POS)){
OrderSelect(i,SELECT_BY_POS);
OrderModify(OrderTicket(),OrderOpenPrice(),0,OrderTakeProfit(),OrderExpiration(),Brown);
}
else Print( "Error when clearing Stops ", GetLastError());
}
return(0);
}