Sell trailingstop doesn't work

 

Hi Everyone

I'm brand new to MQL5.  I've managed to migrate all my code from MQL4 and it seems to be fine, but as for my trailingstops, the buy TS works fine, but the sell TS doesn't work at all.  Will someone please have a look at my code and let me know if there seems to be an obvious problem.  Thank you in advance.

//Include and variables are here 
//+------------------------------------------------------------------+
//| Function-event handler "tick"                                    |
//+------------------------------------------------------------------+
void OnTick(void){
//Trade logic is here
   
   CheckTrailingStopB();
   CheckTrailingStopS();
   }
//+------------------------------------------------------------------+
//| Trailing Buy                                                     |
//+------------------------------------------------------------------+
void CheckTrailingStopB(){
double TSTP=Trailing_Start*Pip,Trail=Trail_Stop*Pip;

   for(int i=PositionsTotal()-1;i>=0;i--){
      string symbol=PositionGetSymbol(i);
         if(_Symbol==symbol){
            if(PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_BUY){
            ulong PositionTicket=PositionGetInteger(POSITION_TICKET);
            double CurrentStopLoss=PositionGetDouble(POSITION_SL);
               if(Bid-OrderGetDouble(ORDER_PRICE_OPEN)>TSTP){
                  if(CurrentStopLoss<Bid-Trail){
               trade.PositionModify(PositionTicket,(Bid-Trail),0);
              } 
             }
            }
           }
          }
         }
//+------------------------------------------------------------------+
//| Trailing Sell                                                    |
//+------------------------------------------------------------------+
void CheckTrailingStopS(){
double TSTP=Trailing_Start*Pip,Trail=Trail_Stop*Pip;

   for(int i=PositionsTotal()-1;i>=0;i--){
      string symbol=PositionGetSymbol(i);
         if(_Symbol==symbol){
            if(PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL){
            ulong PositionTicket=PositionGetInteger(POSITION_TICKET);
            double CurrentStopLoss=PositionGetDouble(POSITION_SL);
               if(OrderGetDouble(ORDER_PRICE_OPEN)-Ask>TSTP){
                  if(CurrentStopLoss>Ask+Trail || CurrentStopLoss==0){
               trade.PositionModify(PositionTicket,(Ask+Trail),0);
              }
             }
            }
           }
          }
         }
 
trader3000:

Hi Everyone

I'm brand new to MQL5.  I've managed to migrate all my code from MQL4 and it seems to be fine, but as for my trailingstops, the buy TS works fine, but the sell TS doesn't work at all.  Will someone please have a look at my code and let me know if there seems to be an obvious problem.  Thank you in advance.

As it seems, you are referring to an order, but you have never selected an order as of the code you posted.

How do you do the order selection?

 
You also seem to be using Ask and Bid variables without declaring them or assigning a value.
 

Ok thank you guys, I appreciate your help.  The Buy side is working flawlessly, but the sell side does not want to adjust at all, and yet the code seems almost identical.  I have now tried to simplify things, by combining the two , but still only the buystop adjusts.  I declared Bid and Ask as follows:

//Include and other variables are here
double Ask,Bid;
//+------------------------------------------------------------------+
//| Function-event handler "tick"                                    |
//+------------------------------------------------------------------+
void OnTick(void){
Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
double SP=Ask-Bid;

//Trade logic is here
   
   CheckTrailingStop();
   }
//+------------------------------------------------------------------+
//| Trailing                                                         |
//+------------------------------------------------------------------+
void CheckTrailingStop(){
double TSTP=Trailing_Start*Pip,Trail=Trail_Stop*Pip;

   for(int i=PositionsTotal()-1;i>=0;i--){
      string symbol=PositionGetSymbol(i);
         if(_Symbol==symbol){
            if(PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_BUY){
            ulong PositionTicketB=PositionGetInteger(POSITION_TICKET);
            double CurrentStopLoss=PositionGetDouble(POSITION_SL);
               if(Bid-OrderGetDouble(ORDER_PRICE_OPEN)>TSTP){
                  if(CurrentStopLoss<Bid-Trail){
               trade.PositionModify(PositionTicketB,(Bid-Trail),0);
              } 
             }
            }
           if(PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL){
           ulong PositionTicketS=PositionGetInteger(POSITION_TICKET);
            double CurrentStopLoss=PositionGetDouble(POSITION_SL);
               if(OrderGetDouble(ORDER_PRICE_OPEN)-Ask>TSTP){
                  if(CurrentStopLoss>Ask+Trail || CurrentStopLoss==0){
               trade.PositionModify(PositionTicketS,(Ask+Trail),0);
              }
             }
            }
           }
          }
         }
Reason: