EA displays incorrect Stoploss

 

Hi All, The problem I encounter when drop my EA on USD/JPY & XAU/USD (2 symbols). On the USD/JPY it displays the stoploss of XAU/USD & then as price change's it displays the original stoploss (when trailing), so on & so forth.

My code below, I tried changing a few things but no luck & my order magic & order symbol are their. 

double TickValue=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE);
  
  double TrailWithLots=0;
  double AboveTrail=0;
  double NewTrailBuy=0;
  double NewTrailSell=0;
  bool Trailed=true;
  
  for(int l=OrdersTotal()-1;l>=0;l--)
  {
   if(OrderSelect(l,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_BUY)
   TickValue=OrderLots();
   AboveTrail=((TotalProfit()!=0)&&(TickValue!=0))*Pips();
   NewTrailBuy=OrderOpenPrice()+AboveTrail;
   double NewBuySL=Bid-TrailAmountSL*Pips();
   if(NewBuySL>NewTrailBuy&&(NewBuySL>OrderStopLoss()||OrderStopLoss()==0.0))
   bool TrailModBuy=OrderModify(OrderTicket(),OrderOpenPrice(),NewBuySL,OrderTakeProfit(),0,clrNONE);
  }
  
  for(int m=OrdersTotal()-1;m>=0;m--)
  {
   if(OrderSelect(m,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_SELL)
   TickValue=OrderLots();
   AboveTrail=((TotalProfit()!=0)&&(TickValue!=0))*Pips();
   NewTrailSell=OrderOpenPrice()-AboveTrail;
   double NewSellSL=Ask+TrailAmountSL*Pips();
   if(NewSellSL<NewTrailSell&&(NewSellSL<OrderStopLoss()||OrderStopLoss()==0.0))
   bool TrailModSell=OrderModify(OrderTicket(),OrderOpenPrice(),NewSellSL,OrderTakeProfit(),0,clrNONE);
  }

This is what I'm facing :(


 

 
Scalper8:

Hi All, The problem I encounter when drop my EA on USD/JPY & XAU/USD (2 symbols). On the USD/JPY it displays the stoploss of XAU/USD & then as price change's it displays the original stoploss (when trailing), so on & so forth.

My code below, I tried changing a few things but no luck & my order magic & order symbol are their. 

This is what I'm facing :(


 

If you had used the styler, you would see this

  for(int l=OrdersTotal()-1; l>=0; l--)
   {
    if(OrderSelect(l,SELECT_BY_POS,MODE_TRADES))
      if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
        if(OrderType()==OP_BUY)
          TickValue=OrderLots();
    AboveTrail=((TotalProfit()!=0)&&(TickValue!=0))*Pips();
    NewTrailBuy=OrderOpenPrice()+AboveTrail;
    double NewBuySL=Bid-TrailAmountSL*Pips();
    if(NewBuySL>NewTrailBuy&&(NewBuySL>OrderStopLoss()||OrderStopLoss()==0.0))
      bool TrailModBuy=OrderModify(OrderTicket(),OrderOpenPrice(),NewBuySL,OrderTakeProfit(),0,clrNONE);
   }

so that you can easily see that the only thing tied to 

if(OrderType()==OP_BUY)

is

TickValue=OrderLots();

You are missing braces

for(int l=OrdersTotal()-1; l>=0; l--)
   {
    if(OrderSelect(l,SELECT_BY_POS,MODE_TRADES))
      if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
        if(OrderType()==OP_BUY)
         {
          TickValue=OrderLots();
          AboveTrail=((TotalProfit()!=0)&&(TickValue!=0))*Pips();
          NewTrailBuy=OrderOpenPrice()+AboveTrail;
          double NewBuySL=Bid-TrailAmountSL*Pips();
          if(NewBuySL>NewTrailBuy&&(NewBuySL>OrderStopLoss()||OrderStopLoss()==0.0))
            bool TrailModBuy=OrderModify(OrderTicket(),OrderOpenPrice(),NewBuySL,OrderTakeProfit(),0,clrNONE);
         }
   }
 
Keith Watford #:

If you had used the styler, you would see this

so that you can easily see that the only thing tied to 

is

You are missing braces

I inserted braces but still get the incorrect Stoploss

for(int l=OrdersTotal()-1;l>=0;l--)
  {
   if(OrderSelect(l,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_BUY)
   {
   TickValue=OrderLots();
   AboveTrail=((TotalProfit()!=0)&&(TickValue!=0))*Pips();
   NewTrailBuy=OrderOpenPrice()+AboveTrail;
   double NewBuySL=Bid-TrailAmountSL*Pips();
   if(NewBuySL>NewTrailBuy&&(NewBuySL>OrderStopLoss()||OrderStopLoss()==0.0))
   bool TrailModBuy=OrderModify(OrderTicket(),OrderOpenPrice(),NewBuySL,OrderTakeProfit(),0,clrNONE);
   }
  }
  
  for(int m=OrdersTotal()-1;m>=0;m--)
  {
   if(OrderSelect(m,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_SELL)
   {
   TickValue=OrderLots();
   AboveTrail=((TotalProfit()!=0)&&(TickValue!=0))*Pips();
   NewTrailSell=OrderOpenPrice()-AboveTrail;
   double NewSellSL=Ask+TrailAmountSL*Pips();
   if(NewSellSL<NewTrailSell&&(NewSellSL<OrderStopLoss()||OrderStopLoss()==0.0))
   bool TrailModSell=OrderModify(OrderTicket(),OrderOpenPrice(),NewSellSL,OrderTakeProfit(),0,clrNONE);
   }
  }
 
Scalper8 #:

I inserted braces but still get the incorrect Stoploss

Please explain what you mean by "incorrect Stoploss".

Originally you were getting a value from XAUUSD being used for a USDJPY Sell Stop Order.

With the braces this is no longer possible. The code will only modify the SL on the chart symbol and only Buys or Sells. It will not modify  Sell Stop Orders.


You do need to address other issues with your code.

double TickValue=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE);

then later

TickValue=OrderLots();

Why? That makes no sense whatsoever!


double AboveTrail=0;

then

AboveTrail=((TotalProfit()!=0)&&(TickValue!=0))*Pips();

As TickValue is now OrderLots() (which can never be zero), this is the same as

AboveTrail=(TotalProfit()!=0 && OrderLots()!=0)*Pips();

ie.

double AboveTrail=(boolean_result_1 && boolean_result_2)*Pips();

double AboveTrail=(true or false)*Pips();

Again,  that makes no sense whatsoever!

 

I think it might be solved simpler for example: for(int l=OrdersTotal()-1; l>=0; l--)

{

    if(OrderSelect(l,SELECT_BY_POS,MODE_TRADES))

      if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)


if(OrderType==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT){
//code for buy trailing
        //set new sl properly and modify a trade with this new sl

}
else if(OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT){
//code for sell trailing

}

}

 
@Marzena Maria Szmit #:I think it might be solved simpler for example:
Please edit your post and use "</>" or Alt-S to properly insert your code. Don't just use plain text. It becomes unreadable.
 
Keith Watford #:

Please explain what you mean by "incorrect Stoploss".

Originally you were getting a value from XAUUSD being used for a USDJPY Sell Stop Order.

With the braces this is no longer possible. The code will only modify the SL on the chart symbol and only Buys or Sells. It will not modify  Sell Stop Orders.


You do need to address other issues with your code.

then later

Why? That makes no sense whatsoever!


then

As TickValue is now OrderLots() (which can never be zero), this is the same as

ie.

double AboveTrail=(boolean_result_1 && boolean_result_2)*Pips();

double AboveTrail=(true or false)*Pips();

Again,  that makes no sense whatsoever!

What I meant by  "incorrect Stoploss", for e.g. My EA is on XAU/USD & USD/JPY. USD/JPY opens a pending order with the stop loss of XAU/USD which is incorrect I want the stoploss of USD/JPY .

With that being said thanks for the heads up, problem is solved & my trailing works fine. 

for(int l=OrdersTotal()-1;l>=0;l--)
  {
   if(OrderSelect(l,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_BUY)
   {
   NewTrailBuy=OrderOpenPrice()+AboveTrail;
   double NewBuySL=Bid-TrailAmountSL*Pips();
   if(NewBuySL>NewTrailBuy&&(NewBuySL>OrderStopLoss()||OrderStopLoss()==0.0))
   bool TrailModBuy=OrderModify(OrderTicket(),OrderOpenPrice(),NewBuySL,OrderTakeProfit(),0,clrNONE);
   }
  }
  
  for(int m=OrdersTotal()-1;m>=0;m--)
  {
   if(OrderSelect(m,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_SELL)
   {
   NewTrailSell=OrderOpenPrice()-AboveTrail;
   double NewSellSL=Ask+TrailAmountSL*Pips();
   if(NewSellSL<NewTrailSell&&(NewSellSL<OrderStopLoss()||OrderStopLoss()==0.0))
   bool TrailModSell=OrderModify(OrderTicket(),OrderOpenPrice(),NewSellSL,OrderTakeProfit(),0,clrNONE);
   }
  }
 
Marzena Maria Szmit #:

I think it might be solved simpler for example:

if(OrderType==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT){
//code for buy trailing

What would be the point of using a trailing stop with pending orders??