TrailingStop doesn't modify only Sell orders

 
void ATRTrailingStop()
  {
   double ATR = NormalizeDouble(iATR(Symbol(),ATRTRSPERIOD,ATR_Period,0)*Trailing_ATR_Multiplier,3);
   RefreshRates();
   for(int i=OrdersTotal()-1; i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
            if(OrderMagicNumber()==Magic)
               if(OrderType()==OP_BUY)
                 {
                  if(Bid-OrderOpenPrice()>= ATR)
                     if(OrderStopLoss()<GetStopLossBuy(Symbol(),ATR_Period))
                        bool SendTicket = OrderModify(OrderTicket(),OrderOpenPrice(),GetStopLossBuy(Symbol(),ATR_Period),OrderTakeProfit(),0,clrGreen);
                 }

      else if(OrderType()==OP_SELL) 
        {
         if(OrderOpenPrice()-Ask>=ATR)
            if(OrderStopLoss()>GetStopLossSell(Symbol(),ATR_Period))
               bool SendTicket = OrderModify(OrderTicket(),OrderOpenPrice(),GetStopLossSell(Symbol(),ATR_Period),0,clrRed);
        }
     }
  }

Hello everyone,

I'm new on mql and trying to develope myself. I've created my very first own EA and looking it's going well. 

I have a problem that I couldn't find a solution and  searched a lot about it..

TrailingStop works well on Buy trades however couldn't get any modify attemp on Sell positions. I don't know why..

and also I want to try  code that is below on the strategy tester but didn't get any reaction but in normal, it works.

void OnTimer()
{
 ATRTrailingStop();
}


I will appriciate if you help me,  Thank you for your interest.

 
  1. bool SendTicket = OrderModify(O

    OrderModify does not return a ticket.


  2. This is your code, properly indented. Select and filter, then work of the if( typeA ) else (typeB), You are processing all non-symbol non-magic sell trades together. @Maskin
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)
         && OrderSymbol()==Symbol()
         && OrderMagicNumber()==Magic
         && OrderType()==OP_BUY)
                     {
                      if(Bid-OrderOpenPrice()>= ATR)
                         if(OrderStopLoss()<GetStopLossBuy(Symbol(),ATR_Period))
                            bool SendTicket = OrderModify(OrderTicket(),OrderOpenPrice(),GetStopLossBuy(Symbol(),ATR_Period),OrderTakeProfit(),0,clrGreen);
                     }
    
          else if(OrderType()==OP_SELL) 
 
William Roeder:
  1. OrderModify does not return a ticket.


  2. This is your code, properly indented. Select and filter, then work of the if( typeA ) else (typeB), You are processing all non-symbol non-magic sell trades together. @Maskin

Thank you for your speedy answer ! I directly edited my code, however result is the same..

Updated version below here;

void ATRTrailingStop()
  {
   double ATR = NormalizeDouble(iATR(Symbol(),ATRTRSPERIOD,ATR_Period,0)*Trailing_ATR_Multiplier,3);
   RefreshRates();
   for(int i=OrdersTotal()-1; i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)
         && OrderSymbol() == Symbol()
         && OrderMagicNumber() == Magic
         && OrderType() == OP_BUY)
            {
            if(Bid-OrderOpenPrice()> ATR)
               if(OrderStopLoss()<GetStopLossBuy(Symbol(),ATR_Period))
                  bool ModifyTicket = OrderModify(OrderTicket(),OrderOpenPrice(),GetStopLossBuy(Symbol(),ATR_Period),OrderTakeProfit(),0,clrGreen);
            }

               else if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)
                     && OrderSymbol() == Symbol()
                     && OrderMagicNumber() == Magic
                     && OrderType() == OP_SELL) Sleep(100);
                 {
                  if(OrderOpenPrice()-Ask>ATR)
                     if(OrderStopLoss()>GetStopLossSell(Symbol(),ATR_Period))
                        bool ModifyTicket = OrderModify(OrderTicket(),OrderOpenPrice(),GetStopLossSell(Symbol(),ATR_Period),OrderTakeProfit(),0,clrRed);
                  }
                }
             }

  
double GetStopLossBuy(string Instrument,int ATRPeriod)
{
   double SLValue=Close[1]-iATR(Instrument,ATRTRSPERIOD,ATRPeriod,Shift)*Trailing_ATR_Multiplier;
   return SLValue;
}


double GetStopLossSell(string Instrument,int ATRPeriod)
{
   double SLValue=Close[1]+iATR(Instrument,ATRTRSPERIOD,ATRPeriod,Shift)*Trailing_ATR_Multiplier;
   return SLValue;
}
 
Maskin:

Thank you for your speedy answer ! I directly edited my code, however result is the same..

Updated version below here;

It simply works this way. The image is attached. Since I do not know the input values, it can only be written this way.

@ Maskin

dddd

#property copyright "Copyright 2021, HaskayaFx"
#property link      "https://www.haskayayazilim.net"
#property version   "1.00"
#property strict


input double Lots=0.02;

input int ATRTRSPERIOD,ATR_Period=14;
input double Trailing_ATR_Multiplier=1;
input int    Shift=1;
input string BuySellComment="MaskinEA";
input int Magic=99243;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  
  }




void OnTick()
  {
  int ticket;
        if(OrdersTotal()==0)
        ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,BuySellComment,Magic,0,clrBlue);
        // ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,2,0,0,NULL,0,0,clrRed);
        
        
     
        if(OrdersTotal()==0) ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,2,0,0,NULL,0,0,clrRed);
         
     
     ATRTrailingStop();
  }

//+------------------------------------------------------------------+


void ATRTrailingStop()
  {
  bool ModifyTicket;
   double ATR = NormalizeDouble(iATR(Symbol(),ATRTRSPERIOD,ATR_Period,0)*Trailing_ATR_Multiplier,3);
   RefreshRates();
   for(int i=OrdersTotal()-1; i>=0;i--)
     {
     bool tst=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         
    // Print("dd ",OrderSymbol()," ",Symbol()," ",OrderMagicNumber()," ",Magic," ",OrderType()," ",OP_BUY);
      if(
         OrderSymbol() == Symbol()
         && OrderMagicNumber() == Magic
         && OrderType() == OP_BUY)
            {
            Print("Burda BUY " ,Bid-OrderOpenPrice()," ATR ",ATR);
            if(Bid-OrderOpenPrice()> ATR)
               if(OrderStopLoss()<GetStopLossBuy(Symbol(),ATR_Period))
                {
                  ModifyTicket = OrderModify(OrderTicket(),OrderOpenPrice(),GetStopLossBuy(Symbol(),ATR_Period),OrderTakeProfit(),0,clrGreen);
                  Print("Modify BUY ",ModifyTicket);
                  }
            }

               else 
                
                if(  OrderSymbol() == Symbol()
                     && OrderMagicNumber() == Magic
                     && OrderType() == OP_SELL)
                      
                 {
                 Sleep(100);
                  if(OrderOpenPrice()-Ask>ATR)
                     if(OrderStopLoss()>GetStopLossSell(Symbol(),ATR_Period))
                        {
                         Print("Burda SELL  " ,OrderOpenPrice()-Ask," ATR ",ATR);
                        ModifyTicket = OrderModify(OrderTicket(),OrderOpenPrice(),GetStopLossSell(Symbol(),ATR_Period),OrderTakeProfit(),0,clrRed);
                  Print("Modify SELL ",ModifyTicket);
                  }
                  }
                }
             }

  
double GetStopLossBuy(string Instrument,int ATRPeriod)
{
   double SLValue=Close[1]-iATR(Instrument,ATRTRSPERIOD,ATRPeriod,Shift)*Trailing_ATR_Multiplier;
   return SLValue;
}


double GetStopLossSell(string Instrument,int ATRPeriod)
{
   double SLValue=Close[1]+iATR(Instrument,ATRTRSPERIOD,ATRPeriod,Shift)*Trailing_ATR_Multiplier;
   return SLValue;
}
 
Mehmet Bastem:

It simply works this way. The image is attached. Since I do not know the input values, it can only be written this way.

@ Maskin


Thank you Mehmet Bey,

I also tried that codes but undeclared identifier error has sent on print command of buy row. When I delete ModifyTicket to Text, code works but for only buy section. I couldn't understand why it does not work properly. I assume that first and second if statement before modify order always turn true. Still working on it. 

Reason: