OCO Orders + Trailing Stop

 

Hello,

I want to apply a trailing stop to my oco ordes, could you help me please, because it doesn't work.


#include <Trade\trade.mqh>
CTrade trade;



void OnTick()
  {

   double Balance=AccountInfoDouble(ACCOUNT_BALANCE);

   double Equity=AccountInfoDouble(ACCOUNT_EQUITY);

   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);

   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);

   if(PositionsTotal()==0 && OrdersTotal()==0)
     {

      trade.BuyLimit(1,Ask+1*_Point,_Symbol,Ask-6*_Point,Ask+11*_Point, ORDER_TIME_GTC,0,0);

      trade.SellLimit(1,Bid-1*_Point,_Symbol,Bid+6*_Point,Bid-11*_Point, ORDER_TIME_GTC,0,0);

     }

   if(Balance!=Equity)
      {
      CancelOrder();
      }
      
   TrailingStopAsk(Ask);
   TrailingStopBid(Bid);
     

 }



void CancelOrder()
  {
   for(int i=OrdersTotal()-1; i>=0; i--)
     {

      ulong OrderTicket = OrderGetTicket(i);

      trade.OrderDelete(OrderTicket);

     }

  }
  
  
  
 void TrailingStopAsk(double Ask)
 {

   double SL_Ask = NormalizeDouble(Ask-3*_Point, _Digits);
   
   for (int i=PositionsTotal()-1; i>=0; i--)
   {
   
      string symbol=PositionGetSymbol(i);
      
      if (_Symbol==symbol)
      {
      
         ulong PositionTicket = PositionGetInteger(POSITION_TICKET);
         
         double CurrentSL = PositionGetDouble(POSITION_SL);
         
         if(CurrentSL<SL_Ask)
         {
         
         trade.PositionModify(PositionTicket,(CurrentSL+3*_Point),0);
        
         
         }
         
      
      }
   
   
   }

 }
 
  void TrailingStopBid(double Bid)
 {

   double SL_Bid = NormalizeDouble(Bid+3*_Point, _Digits);
   
   for (int i=PositionsTotal()-1; i>=0; i--)
   {
   
      string symbol=PositionGetSymbol(i);
      
      if (_Symbol==symbol)
      {
      
         ulong PositionTicket = PositionGetInteger(POSITION_TICKET);
         
         double CurrentSL = PositionGetDouble(POSITION_SL);
        
         
         if(CurrentSL<SL_Bid)
         {
         
         trade.PositionModify(PositionTicket,(CurrentSL-3*_Point),0);
        
         
         }
      
      }
   
   
   }

 }

Best regards

 
hackintosh:

Hello,

 it doesn't work.

Can you be a little bit more specific as to what does not work ? 

 
Marco vd Heijden:

Can you be a little bit more specific as to what does not work ? 

Hello Marco,

The trailing stop is not executed when this condition is valid

// Trailing stop for Bid

if(CurrentSL<SL_Bid)
         {
         
         trade.PositionModify(PositionTicket,(CurrentSL-3*_Point),0);
        
         
         }

// Trailing stop for Ask

if(CurrentSL<SL_Ask)
         {
         
         trade.PositionModify(PositionTicket,(CurrentSL-3*_Point),0);
        
         
         }

OCO orders works well, but once OCO Order is executed and when my trade is positive, the trailing stop doesn't executing, I can't understand why, the conditions are correct.


Best regards

 
trade.BuyLimit(1,Ask+1*_Point,_Symbol,Ask-6*_Point,Ask+11*_Point, ORDER_TIME_GTC,0,0);
You buy at the Ask and sell at the Bid.
  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Using the Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?
  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25
  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
 
William Roeder:
You buy at the Ask and sell at the Bid.
  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Using the Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?
  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25
  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)


Thank you William for your answer, 


When I change the code (see below) it works, there is only one problem, the trailing stop takes opposite direction ...

void OnTick()
  {

   double Balance=AccountInfoDouble(ACCOUNT_BALANCE);

   double Equity=AccountInfoDouble(ACCOUNT_EQUITY);

   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);

   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);

   if(PositionsTotal()==0 && OrdersTotal()==0)
     {

      trade.BuyStop(1,Ask+3*_Point,_Symbol,Ask-27*_Point,Ask+63*_Point, ORDER_TIME_GTC,0,0);

      trade.SellStop(1,Bid-3*_Point,_Symbol,Bid+27*_Point,Bid-63*_Point, ORDER_TIME_GTC,0,0);

     }

   if(Balance!=Equity)
      {
      CancelOrder();
      }
      
   TrailingStopAsk(Ask, Bid);
     

 }



void CancelOrder()
  {
   for(int i=OrdersTotal()-1; i>=0; i--)
     {

      ulong OrderTicket = OrderGetTicket(i);

      trade.OrderDelete(OrderTicket);

     }

  }
  
  
  
 void TrailingStopAsk(double Ask, double Bid)
 {

   double SL_Ask = NormalizeDouble(Ask-7*_Point, _Digits);
   
   double SL_Bid = NormalizeDouble(Bid+7*_Point, _Digits);
   
   for (int i=PositionsTotal()-1; i>=0; i--)
   {
   
      string symbol=PositionGetSymbol(i);
      
      if (_Symbol==symbol)
      {
      
         ulong PositionTicket = PositionGetInteger(POSITION_TICKET);
         
         double CurrentSL = PositionGetDouble(POSITION_SL);
         
         if(CurrentSL<SL_Ask)
         {
         
         trade.PositionModify(PositionTicket,(CurrentSL+3*_Point),0);
        
         
         }
         
         if(CurrentSL<SL_Bid)
         {
         
         trade.PositionModify(PositionTicket,(CurrentSL-3*_Point),0);
        
         
         }
         
      
      }
   
   
   }

 }

I tried to change the values +/- but still same problem ...


Best regards

 
hackintosh: I tried to change the values +/- but still same problem ...
         if(CurrentSL<SL_Ask){
            trade.PositionModify(PositionTicket,(CurrentSL+3*_Point),0);
         }
         if(CurrentSL<SL_Bid){
            trade.PositionModify(PositionTicket,(CurrentSL-3*_Point),0);
Move the SL to SL_Ask or SL_Bid depending on the position direction. You are trying to do both.
 
William Roeder:
Move the SL to SL_Ask or SL_Bid depending on the position direction. You are trying to do both.


Somebody can show me a simple example of Trailing Stop please ?

Best regards

Reason: