Im trying to make an automated trailing stop, here is what I have so far:
- Code: Select all
if(OrdersTotal() == 1){
if(OrderSelect(0, SELECT_BY_POS, MODE_TRADES) == true) {
if(OrderType()==OP_BUY)
{
if (Bid > OrderOpenPrice()) {
double NewLongSL = Bid - Trailing_Stop*Point;
if (NewLongSL > OrderStopLoss()) {
OrderModify(OrderTicket(),OrderOpenPrice(), NewLongSL,0,0,Blue);
}else {
Print("nope" + Bid);
}
}
}
if(OrderType()==OP_SELL)
{
if (Ask < OrderOpenPrice()) {
double NewShortSL = Ask + Trailing_Stop*Point;
if (NewShortSL < OrderStopLoss()) {
OrderModify(OrderTicket(),OrderOpenPrice(), NewShortSL,0,0,Blue);
}else {
Print("nope" + Ask);
}
}
}
}
}
it is compiling all right and I can run it in a strategy tester.
The issue is that it behaves strange, when a long or short is opened this function is adjusting SL by moving to up or down by 1, even when the price is not changing. This is what it looks like:
I have no idea what is going on here, so any help would be appreciated.
Thanks in advance guys!