I have this following trail stop code which working perfectly. But i need some changes, basically what I need is assume I am setting Trail stop to be 10 Points. But I want the profit to be at least +N pips from open price.
That is ..
If i buy eur/usd at 1.3010, the price now is 1.3020, by right if I set 10 points for trail.
My s/l will be modified to 1.3010 (which is my open price). Is it possible to modify the code below so that it will only set trail stop when price is 1.3022 and s/l will be 10 points + N gap (which in this case I set as 2), prefer N to be a variable.
So when price is 1.3020, although I set 10 points, it will not set S/L to 1.3010 but wait till 10 Points + N Points (where in this case N is 2), means at least 1.3022 then It start set the trail stop to 1.3012
Thanks
externint TrailingStop=10;
externdouble StopLoss = 0;
double PointValue;
for (int i = 0; i < OrdersTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
//Calculate the point value in case there are extra digits in the quotesif (MarketInfo(OrderSymbol(), MODE_POINT) == 0.00001) PointValue = 0.0001;
elseif (MarketInfo(OrderSymbol(), MODE_POINT) == 0.001) PointValue = 0.01;
else PointValue = MarketInfo(OrderSymbol(), MODE_POINT);
//Normalize trailing stop value to the point valuedouble TSTP = TrailingStop * PointValue;
if (OrderType() == OP_BUY)
{
if (Bid - OrderOpenPrice() > TSTP)
{
if (OrderStopLoss() < Bid - TSTP)
{
if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP, OrderTakeProfit(), Red))
Print("Error setting Buy trailing stop: ", GetLastError());
}
}
elseif ((OrderStopLoss() != Bid - StopLoss * PointValue) && (StopLoss != 0))
if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - StopLoss * PointValue, OrderTakeProfit(), Red))
Print("Error setting Buy stop-loss: ", GetLastError());
}
elseif (OrderType() == OP_SELL)
{
if (OrderOpenPrice() - Ask > TSTP)
{
if ((OrderStopLoss() > Ask + TSTP) || (OrderStopLoss() == 0))
{
if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP, OrderTakeProfit(), Red))
Print("Error setting Sell trailing stop: ", GetLastError());
}
}
elseif ((OrderStopLoss() != Ask + StopLoss * PointValue) && (StopLoss != 0))
if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + StopLoss * PointValue, OrderTakeProfit(), Red))
Print("Error setting Sell stop-loss: ", GetLastError());
}
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Rgd My Trail Stop Code need some modification
Hi guys, thanks for reading.
I have this following trail stop code which working perfectly. But i need some changes, basically what I need is assume I am setting Trail stop to be 10 Points. But I want the profit to be at least +N pips from open price.
That is ..
If i buy eur/usd at 1.3010, the price now is 1.3020, by right if I set 10 points for trail.
My s/l will be modified to 1.3010 (which is my open price). Is it possible to modify the code below so that it will only set trail stop when price is 1.3022 and s/l will be 10 points + N gap (which in this case I set as 2), prefer N to be a variable.
So when price is 1.3020, although I set 10 points, it will not set S/L to 1.3010 but wait till 10 Points + N Points (where in this case N is 2), means at least 1.3022 then It start set the trail stop to 1.3012
Thanks