Trailing Stop and reversing

 

Hy Guys! I have made a simple code, and then i have added trailing stop. Works nice, but i was thinking to reverse the order when sl is touched. My ideas is this:


Trailing 


for (int b= OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
       if(OrderSymbol()==Symbol())
        if(OrderType()==OP_BUY)
         {
          if(OrderStopLoss() < Ask - (200*_Point))
           OrderModify(OrderTicket(),OrderOpenPrice(),Ask - (120*_Point),OrderTakeProfit(),0,Green);
         }
     }
     
    for (int b= OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
       if(OrderSymbol()==Symbol())
        if(OrderType()==OP_SELL)
         {
          if(OrderStopLoss() < Bid + (200*_Point))
           OrderModify(OrderTicket(),OrderOpenPrice(),Bid + (120*_Point),OrderTakeProfit(),0,Green);
         }
     }

Maybe i will add an option to start the trailing when order is 20 pips in profit. To reversing i have used same code, but on this way:

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);
           }
         }
     }
     
    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);
           }
         }
     }

But seems that something is wrong, i don't know what. Could someone help me?

 
SIMONE MARELLI:

Hy Guys! I have made a simple code, and then i have added trailing stop. Works nice, but i was thinking to reverse the order when sl is touched. My ideas is this:


Trailing 


Maybe i will add an option to start the trailing when order is 20 pips in profit. To reversing i have used same code, but on this way:

But seems that something is wrong, i don't know what. Could someone help me?

Hello,

OrderClosePrice() 

is only for history orders, not for current.

In your code, use it on open orders..

if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))

 
Nikolaos Pantzos:

Hello,

is only for history orders, not for current.

Oh, right! So, i had better to change on this way 

for (int b= OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
       if(OrderSymbol()==Symbol())
        if(OrderType()==OP_BUY)
         {
          if(Ask == 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);
           }
         }
     }
     
    for (int b= OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
       if(OrderSymbol()==Symbol())
        if(OrderType()==OP_SELL)
         {
          if(Bid == 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);
           }
         }
     }

This should be correct right?

 
Nikolaos Pantzos:

Hello,

is only for history orders, not for current.

In your code, use it on open orders..


That is not true. OrderClosePrice() is also used on MODE_TRADES.

In MODE_TRADES, OrderClosePrice() = Bid for Buy Order and Ask for Sell order.

Please get tested if you don't believe it.

 
Roberto Jacobs:

That is not true. OrderClosePrice() is also used on MODE_TRADES.

In MODE_TRADES, OrderClosePrice() = Bid for Buy Order and Ask for Sell order.

Please get tested if you don't believe it.

So, what do you think is the error?

 
  1.         if(OrderType()==OP_BUY){
              if(OrderStopLoss() < Ask - (200*_Point))
    You buy at the Ask and sell at the Bid. So for buy orders you pay the spread on open. For sell orders you pay the spread on close.
    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Not the Ask. Your SL is shorter by the spread and your TP would be longer. Don't you want the same/specified amount for 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.)

  2.           if(Ask == OrderStopLoss())
    
    Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 programming forum

  3. Once the market hits the SL, the order is closed. Your condition will never occur.

  4. You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type for close price. But if you potentially close multiple orders, you must call RefreshRates after the server call and before the next OrderSelect.
 
William Roeder:
  1. You buy at the Ask and sell at the Bid. So for buy orders you pay the spread on open. For sell orders you pay the spread on close.
    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Not the Ask. Your SL is shorter by the spread and your TP would be longer. Don't you want the same/specified amount for 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.)

  2. Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 programming forum

  3. Once the market hits the SL, the order is closed. Your condition will never occur.

  4. You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type for close price. But if you potentially close multiple orders, you must call RefreshRates after the server call and before the next OrderSelect.

My original code was made with OrderClosePrice(), but looks like is not working. So, for Trailing, i had better to change on this way?


 for (int b= OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
       if(OrderSymbol()==Symbol())
        if(OrderType()==OP_BUY)
         {
          if(OrderStopLoss() < Bid - (200*_Point))
           OrderModify(OrderTicket(),OrderOpenPrice(),Ask - (120*_Point),OrderTakeProfit(),0,Green);
         }
     }
     
    for (int b= OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
       if(OrderSymbol()==Symbol())
        if(OrderType()==OP_SELL)
         {
          if(OrderStopLoss() < Ask + (200*_Point))
           OrderModify(OrderTicket(),OrderOpenPrice(),Bid + (120*_Point),OrderTakeProfit(),0,Green);
         }
     }

This trailing is correct? 

 
SIMONE MARELLI:

So, what do you think is the error?

For Buy order, it must be Bid> = OrderStopLoss ()

For Sell order, it must Ask <= OrderStopLoss ()

Buy order: Ask == OrderStopLoss () or Sell order: Bid == OrderStopLoss (),

then the stop loss has not been reached by the price.

To suit your purpose, it's best to check after the order closes by stop loss, in MODE_HISTORY.

If OrderClosePrice()> = OrderStopLoss() (for buy order) or OrderClosePrice() <= OrderStopLoss() (for sell order).

 
Roberto Jacobs:

For Buy order, it must be Bid> = OrderStopLoss ()

For Sell order, it must Ask <= OrderStopLoss ()

Buy order: Ask == OrderStopLoss () or Sell order: Bid == OrderStopLoss (),

then the stop loss has not been reached by the price.

To suit your purpose, it's best to check after the order closes by stop loss, in MODE_HISTORY.

If OrderClosePrice()> = OrderStopLoss() (for buy order) or OrderClosePrice() <= OrderStopLoss() (for sell order).

So, guys you are all a bit confusing me. Let me know if i am right with everything:

//Reverse order for the BUY
    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);
           }
         }
     }
    
    //Reverse order for the SELL 
    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);
           }
         }
     }
    
    //Trailing Stop for the BUY 
    for (int b= OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
       if(OrderSymbol()==Symbol())
        if(OrderType()==OP_BUY)
         {
          if(Bid >= OrderStopLoss() + (200*_Point))
           OrderModify(OrderTicket(),OrderOpenPrice(),Ask - (120*_Point),OrderTakeProfit(),0,Green);
         }
     }
    
    //Trailing Stop for the SELL 
    for (int b= OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
       if(OrderSymbol()==Symbol())
        if(OrderType()==OP_SELL)
         {
          if(Ask <= OrderStopLoss() - (200*_Point))
           OrderModify(OrderTicket(),OrderOpenPrice(),Bid + (120*_Point),OrderTakeProfit(),0,Green);
         }
     }
 
SIMONE MARELLI:

So, guys you are all a bit confusing me. Let me know if i am right with everything:

That's all wrong.

You cannot check orders in mode_trades if the order is closed by stop loss.

What's more you want to modify an order that has closed at a stop loss, at a price of 200 points greater (buy) or smaller than order stop loss (sell) ?

Please learn more MQL4/MQL5 reference documents.

 
Roberto Jacobs:

That's all wrong.

You cannot check orders in mode_trades if the order is closed by stop loss.

What's more you want to modify an order that has closed at a stop loss, at a price of 200 points greater (buy) or smaller than order stop loss (sell) ?

Please learn more MQL4/MQL5 reference documents.

Nono, the ideas is to move sl when price is more than 20 pips in profit. That's for trailing, and trailing was working correctly. The only problem is the reversing. I just need to reverse the order at stoploss, I have made a test and it's not working, so I am looking for a better way
Reason: