TrailingStop + Reverse Order

 

Hello All,


i have this simple code to create Trailing Stop. it works perfectly. But my ideas is to open a reverse order with double lots when SL is touched. I have made lot of test, but this seems not working


extern int MagicNumber=123;
extern double Lots =0.05;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   
    tStop(Symbol(),600,MagicNumber);
    
    ReverseBuy();
    
    ReverseSell();
    
  }
//+------------------------------------------------------------------+
void tStop(string symb,int stop, int MN)// Symbol + stop in pips + magic number
  {
   double bsl=NormalizeDouble(MarketInfo(symb,MODE_BID)-stop*MarketInfo(symb,MODE_POINT),MarketInfo(symb,MODE_DIGITS));
   double ssl=NormalizeDouble(MarketInfo(symb,MODE_ASK)+stop*MarketInfo(symb,MODE_POINT),MarketInfo(symb,MODE_DIGITS));

   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==MN)
            if(OrderSymbol()==symb)

               if(OrderType()==OP_BUY && (OrderStopLoss()<bsl || OrderStopLoss()==0))
                  if(OrderModify(OrderTicket(),OrderOpenPrice(),bsl,OrderTakeProfit(),0,clrNONE))
                    {
                     Print(symb+" Buy's Stop Trailled to "+(string)bsl);
                       }else{
                     Print(symb+" Buy's Stop Trail ERROR");
                    }

               if(OrderType()==OP_SELL && (OrderStopLoss()>ssl || OrderStopLoss()==0))
                  if(OrderModify(OrderTicket(),OrderOpenPrice(),ssl,OrderTakeProfit(),0,clrNONE))
                    {
                     Print(symb+" Sell's Stop Trailled to "+(string)ssl);
                       }else{
                     Print(symb+" Sell's Stop Trail ERROR");
                    }
     }
  } 
  
void ReverseBuy()
 {
  for (int b= OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
       if(OrderSymbol()==Symbol())
        if(OrderType()==OP_BUY)
         {
          if(OrderClosePrice() == OrderStopLoss())
           {
            Alert("StopLoss reached on " +Symbol() + " order is now reversed");
            int secondticket = OrderSend(_Symbol,OP_SELL,Lots*2,Bid,3,Bid+600*_Point,Bid-400*_Point,"Renko Sell Order",MagicNumber,0,Red);
            Sleep(60000);
           }
         }
     }
     return;
 }

void ReverseSell()
 {
  for (int b= OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
       if(OrderSymbol()==Symbol())
        if(OrderType()==OP_SELL)
         {
          if(OrderClosePrice() == OrderStopLoss())
           {
            Alert("StopLoss reached on " +Symbol() + " order is now reversed");
            int secondticket2 = OrderSend(_Symbol,OP_BUY,Lots*2,Ask,3,Ask-600*_Point,Ask+400*_Point,"Renko Buy Order",MagicNumber,0,Green);
            Sleep(60000);
           }
         }
     }
     return;
 }

I can't uderstand why, i have no errors in the code, no alert, what am i missing?

 
SIMONE MARELLI:

i have this simple code to create Trailing Stop. it works perfectly. But my ideas is to open a reverse order with double lots when SL is touched. I have made lot of test, but this seems not working

I can't uderstand why, i have no errors in the code, no alert, what am i missing?

if(OrderClosePrice() == OrderStopLoss())

You are checking open trades. If this condition is true, the trade is probably already closed, so wouldn't appear in the OrdersTotal() list.

 
Keith Watford:

You are checking open trades. If this condition is true, the trade is probably already closed, so wouldn't appear in the OrdersTotal() list.

So, maybe i had better to use OrderHistory instead of OrdersTotal? And check last trade?

 
SIMONE MARELLI:

So, maybe i had better to use OrderHistory instead of OrdersTotal? And check last trade?

Try it.

 
Keith Watford:

Try it.

I have made this test

extern int MagicNumber=123;
extern double Lots =0.05;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   
    tStop(Symbol(),600,MagicNumber);
    
    ReverseBuy();
    
    ReverseSell();
    
  }
//+------------------------------------------------------------------+
void tStop(string symb,int stop, int MN)// Symbol + stop in pips + magic number
  {
   double bsl=NormalizeDouble(MarketInfo(symb,MODE_BID)-stop*MarketInfo(symb,MODE_POINT),MarketInfo(symb,MODE_DIGITS));
   double ssl=NormalizeDouble(MarketInfo(symb,MODE_ASK)+stop*MarketInfo(symb,MODE_POINT),MarketInfo(symb,MODE_DIGITS));

   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==MN)
            if(OrderSymbol()==symb)

               if(OrderType()==OP_BUY && (OrderStopLoss()<bsl || OrderStopLoss()==0))
                  if(OrderModify(OrderTicket(),OrderOpenPrice(),bsl,OrderTakeProfit(),0,clrNONE))
                    {
                     Print(symb+" Buy's Stop Trailled to "+(string)bsl);
                       }else{
                     Print(symb+" Buy's Stop Trail ERROR");
                    }

               if(OrderType()==OP_SELL && (OrderStopLoss()>ssl || OrderStopLoss()==0))
                  if(OrderModify(OrderTicket(),OrderOpenPrice(),ssl,OrderTakeProfit(),0,clrNONE))
                    {
                     Print(symb+" Sell's Stop Trailled to "+(string)ssl);
                       }else{
                     Print(symb+" Sell's Stop Trail ERROR");
                    }
     }
  } 
  
void ReverseBuy()
 {
  for (int b= OrdersHistoryTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_HISTORY))
       if(OrderSymbol()==Symbol())
        if(OrderType()==OP_BUY)
         {
          if(OrderClosePrice() == OrderStopLoss())
           {
            Alert("StopLoss reached on " +Symbol() + " order is now reversed");
            int secondticket = OrderSend(_Symbol,OP_SELL,Lots*2,Bid,3,Bid+600*_Point,Bid-400*_Point,"Renko Sell Order",MagicNumber,0,Red);
            Sleep(60000);
           }
         }
     }
     return;
 }

void ReverseSell()
 {
  for (int b= OrdersHistoryTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_HISTORY))
       if(OrderSymbol()==Symbol())
        if(OrderType()==OP_SELL)
         {
          if(OrderClosePrice() == OrderStopLoss())
           {
            Alert("StopLoss reached on " +Symbol() + " order is now reversed");
            int secondticket2 = OrderSend(_Symbol,OP_BUY,Lots*2,Ask,3,Ask-600*_Point,Ask+400*_Point,"Renko Buy Order",MagicNumber,0,Green);
            Sleep(60000);
           }
         }
     }
     return;
 }

In strategy tester it looks like it's working!!! Now i will try on a demo, thanks!

 
if(OrderClosePrice() == OrderStopLoss())
  1. SL becomes a market order when hit and thus position closes with slippage. Unlikely to be true.
  2. Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 programming forum
 
SIMONE MARELLI:

I have made this test

In strategy tester it looks like it's working!!! Now i will try on a demo, thanks!

In your function for reversing there is no check for magic number.

You are not checking if it was the actual last trade.

You should find the last trade and then see whether it was a buy or a sell.

You need to check whether a reversal trade has already been opened based on that trade.

Reason: