-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathModify_SL_TP.mq5
More file actions
88 lines (78 loc) · 5.66 KB
/
Modify_SL_TP.mq5
File metadata and controls
88 lines (78 loc) · 5.66 KB
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
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
CPositionInfo posi;
CTrade trade;
CSymbolInfo symb;
input double InpStoploss=0.0; //StopLoss Pips
input double InpTakeProfit=0.0;//TakeProfit Pips
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
double stoploss=0.0;
double takeprofit=0.0;
ulong slippage=3;
int pt=1;
symb.Name(Symbol());
if(!symb.RefreshRates())return;
if(symb.Digits()==5 || symb.Digits()==3) pt=10;
stoploss=InpStoploss*pt;
slippage = slippage * pt;
takeprofit = InpTakeProfit * pt;
trade.SetDeviationInPoints(slippage);
//---
double curBid = symb.Bid();
double slbuy=0.0,slsell=0.0,tpbuy=0.0,tpsell=0.0;
if(stoploss>0)
{
slbuy = curBid - stoploss*symb.Point();
slsell = curBid + stoploss*symb.Point();
}
if(takeprofit>0)
{
tpbuy = curBid + takeprofit*symb.Point();
tpsell = curBid - takeprofit*symb.Point();
}
ModifySLTP(slbuy,tpbuy,slsell,tpsell);
}
//+------------------------------------------------------------------+
void ModifySLTP(double slPriceBuy,double tpPriceBuy,double slPriceSell,double tpPriceSell)
{
//---
double sl=0.0 ,tp=0.0;
bool bslbuy = false,btpbuy = false, bslsell = false, btpsell = false;
if(slPriceBuy>0)bslbuy=true;
if(tpPriceBuy>0)btpbuy=true;
if(slPriceSell>0)bslsell=true;
if(tpPriceSell>0)btpsell=true;
if(!bslbuy && !btpbuy && !bslsell && !btpsell )
{
Print(__FUNCTION__,",No SL/TP need to be modified");
return;
}
for(int i=PositionsTotal()-1;i>=0;i--)
{
if(posi.SelectByIndex(i))
{
if(posi.Symbol()==Symbol() )
{
if(posi.PositionType()==POSITION_TYPE_BUY)
{
if(bslbuy)sl=slPriceBuy;else sl = posi.StopLoss();
if(btpbuy)tp=tpPriceBuy;else tp = posi.TakeProfit();
trade.PositionModify(posi.Ticket(),NormalizeDouble(sl,Digits()),NormalizeDouble(tp,Digits()));
}
if(posi.PositionType()==POSITION_TYPE_SELL)
{
if(bslsell)sl=slPriceSell;else sl = posi.StopLoss();
if(btpsell)tp=tpPriceSell;else tp = posi.TakeProfit();
trade.PositionModify(posi.Ticket(),NormalizeDouble(sl,Digits()),NormalizeDouble(tp,Digits()));
}
}
}
}
//---
}