-
Notifications
You must be signed in to change notification settings - Fork 1
/
AutoStopLoss.mq5
47 lines (44 loc) · 1.53 KB
/
AutoStopLoss.mq5
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
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
double stopLoss = 100 * _Point; // Stop loss in points
double takeProfit = 200 * _Point; // Take profit in points
int totalOrders = OrdersTotal();
for (int i = totalOrders - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderMagicNumber() == 0) // Check if the order was placed manually
{
double newSL, newTP;
if (OrderType() == ORDER_BUY)
{
newSL = OrderOpenPrice() - stopLoss;
newTP = OrderOpenPrice() + takeProfit;
}
else if (OrderType() == ORDER_SELL)
{
newSL = OrderOpenPrice() + stopLoss;
newTP = OrderOpenPrice() - takeProfit;
}
else
{
continue;
}
bool result = OrderModify(OrderTicket(), OrderOpenPrice(), newSL, newTP, 0);
if (result)
{
Print("Stop Loss and Take Profit set successfully.");
}
else
{
Print("Failed to set Stop Loss and Take Profit. Error: ", GetLastError());
}
break; // Stop after modifying the latest order
}
}
}
}
//+------------------------------------------------------------------+