Trailing Stop only working for BUY trades

 

Hello,


I've been banging my head against this ea for months. I even went to freelance and hired someone but he stopped responding about a week ago and i'm back to trying to do this on my own. The problem I currently have is that my incremental trail only works on buy trades. No modifications are taking place on my SELL trades at all. Doubtless its something an experienced coder could fix at a glace but i just don't get it. This is being used on a grid type EA so there is no initial stop loss but once it moves X ranges away it should place a stop loss and trail when a new level is reached.

void Modify(double sl)
{
        int ticket = OrderTicket();
        if (OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(sl, _Digits), OrderTakeProfit(), 0, clrNONE))
        {
                PrintFormat("SL modify OK !! Order %i !", ticket);
        }
        else
        {
                PrintFormat("Order %i NOT modified !", ticket);
        }
}


void Trail(int type, double sl)
{
        if (!incremental_trail || sl == 0) return;
        double sl_ = NormalizeDouble(sl, _Digits);
        for (i = 0; i < OrdersTotal(); i++)
        {
           if (OrderSelect(i, SELECT_BY_POS))
           {
              if (OrderMagicNumber() == magic && OrderSymbol() == _Symbol && OrderType() == type && (type == OP_SELL && OrderStopLoss() > sl_))
              {
                                Modify(sl_);
              }
              if (OrderMagicNumber() == magic && OrderSymbol() == _Symbol && OrderType() == type && ((type == OP_BUY && OrderStopLoss() < sl_)))
              {
                                Modify(sl_);
              }
           
           }
        }
}

Thanks in advance as always

 
dkamerond:

Hello,


I've been banging my head against this ea for months. I even went to freelance and hired someone but he stopped responding about a week ago and i'm back to trying to do this on my own. The problem I currently have is that my incremental trail only works on buy trades. No modifications are taking place on my SELL trades at all. Doubtless its something an experienced coder could fix at a glace but i just don't get it. This is being used on a grid type EA so there is no initial stop loss but once it moves X ranges away it should place a stop loss and trail when a new level is reached.

Thanks in advance as always

You didn't select any order here:

 int ticket = OrderTicket();

When you use SELECT_BY_POS you can't ignore the third parameter of the OrderSelect function.

if (OrderSelect(i, SELECT_BY_POS))

Should be

if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES))

Perhaps it worked for buy trade because the last selected order is your Buy trade.
 

 
Reza nasimi:

You didn't select any order here:

When you use SELECT_BY_POS you can't ignore the third parameter of the OrderSelect function.

if (OrderSelect(i, SELECT_BY_POS))

Should be

if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES))

Perhaps it worked for buy trade because the last selected order is your Buy trade.
 

The default is MODE_TRADES, so there i no need for the 3rd parameter unless you are working with MODE_HISTORY.

 
if (OrderMagicNumber() == magic && OrderSymbol() == _Symbol && OrderType() == type && (type == OP_SELL && (OrderStopLoss() > sl_ || OrderStopLoss()==0)))
 
Keith Watford:

The default is MODE_TRADES, so there i no need for the 3rd parameter unless you are working with MODE_HISTORY.

I didn't know this, 
thanks
Reason: