Trailing pending orders

 
#include  <Trade\Trade.mqh>

CTrade trade;

input int TrailingPoints = 1000;
input int TslTriggerPoints =2000;



int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

   
  }

void OnTick()
  {
   
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   
   MqlRates PriceInfo[];   
   ArraySetAsSeries(PriceInfo,true);   
   int PriceData=CopyRates(Symbol(),Period(),0,3,PriceInfo);
   
   double CurrentPrice = PositionGetDouble(POSITION_PRICE_CURRENT);
      
   if(PositionsTotal()<1)
     {
      
      trade.Buy(0.01,NULL,Ask,0,0,NULL);
     
     }

   double posOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);

   if(Ask < posOpenPrice - (TslTriggerPoints*_Point) && OrdersTotal()<1)
     {
      
      trade.BuyStop(0.01,Ask+(1000*_Point),_Symbol,0,0,ORDER_TIME_GTC,0,NULL);
     
     }
     
   double orderPrice = OrderGetDouble(ORDER_PRICE_OPEN);
   double orderTP = OrderGetDouble(ORDER_TP); 
   double orderSL = OrderGetDouble(ORDER_SL);

   
    for (int i = OrdersTotal()-1; i>=0; i--)
       {
         ulong orderTicket = OrderGetTicket(i);
         
         if (OrderSelect(orderTicket) && OrderGetString(ORDER_SYMBOL) == _Symbol)
            {
              ENUM_ORDER_TYPE orderType = (ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE);
              
            if (orderType == ORDER_TYPE_BUY_STOP)
              {
               double price = Ask + TrailingPoints*_Point;
               price = NormalizeDouble(price,_Digits);
                   
               if (price < orderPrice)
                 {
                  if (trade.OrderModify(orderTicket,price,orderSL,orderTP,ORDER_TIME_GTC,0))
                    {
                     Print(__FUNCTION__," > Order #",orderTicket, " was modified by the pending order trail machine...");
                          
                    }                      
                 } 
              
              }else if (orderType == ORDER_TYPE_SELL_STOP)
                      {
                       double price = Bid - TrailingPoints*_Point;
                       price = NormalizeDouble(price,_Digits);
                   
                       if (price > orderPrice)
                         {
                          if (trade.OrderModify(orderTicket,price,orderSL,orderTP,ORDER_TIME_GTC,0))
                            {
                             Print(__FUNCTION__," > Order #",orderTicket, " was modified by the pending order trail machine...");
                          
                            }                     
                         } 
                    
                      }
                                                            
            
            }
       }
   


  }


Hello,

I was trying to make program that trails pending orders in MT5. The idea is simple, let's say I enter a buy trade and if price goes 2000 pips in profit I want to place sell stop order 1000 pips under the current Ask price. I wrote this program but for some reason it won't place sell stop when price reaches wanted level. Can somebody please help me? 

Thank you! 

 
StevanV: I was trying to make program that trails pending orders in MT5. The idea is simple, let's say I enter a buy trade and if price goes 2000 pips in profit I want to place sell stop order 1000 pips under the current Ask price. I wrote this program but for some reason it won't place sell stop when price reaches wanted level. Can somebody please help me? 

Why?

Positions already have the concept of a stop-loss and a take-profit built into their functionality. You don't need to use an extra pending order for it.

Just simply update the position's stop-loss to reflect the trailing price.

Also, "buy" positions close at the Bid price, not the Ask price. So trailing the Ask price makes no sense for "buy" positions.

 
Fernando Carreiro #:

Why?

Positions already have the concept of a stop-loss and a take-profit built into their functionality. You don't need to use an extra pending order for it.

Just simply update the position's stop-loss to reflect the trailing price.

Also, "buy" positions close at the Bid price, not the Ask price. So trailing the Ask price makes no sense for "buy" positions.

Thank you for answering Fernando! 

Actually this is one part of my strategy, so it would be really nice if it could place that sell stop order. 

I made most of this program watching some tutorial on youtube and it works, but when I wanted to place pending order when price reaches certain level I couldn't find tutorial, so I tried to make it on my own. 

 
StevanV:

Hello,

I was trying to make program that trails pending orders in MT5. The idea is simple, let's say I enter a buy trade and if price goes 2000 pips in profit I want to place sell stop order 1000 pips under the current Ask price. I wrote this program but for some reason it won't place sell stop when price reaches wanted level. Can somebody please help me? 

Thank you! 

There is not even code to place a sell stop in what you posted !

Beside that there are a lot of errors in your code. For example you can't use OrderGetDouble(X) before selecting an order to work with.

 
StevanV: I was trying to make program that trails pending orders in MT5. T

There is no need to create pending orders in code.

  1. The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)

    Don't worry about it unless you're scalping M1 or trading news.

  2. Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.

 

Thank you all for answering in such a short time!

I see now that there are some errors, and that this code is not correct. In a hurry I didn't copy a whole program here, but I managed to find a solution that I was looking for on my own. 

Once again thank you all for answers and your time. 

 
William Roeder #:

There is no need to create pending orders in code.

  1. The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)

    Don't worry about it unless you're scalping M1 or trading news.

  2. Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.

Your answer in point (2) is really enlightening.

My previous EA that was have "Order" feature,  now changing to a certain trigger, to make the trade on the position at the right time.


And the results are indeed more efficient.

Thanks William

 
            if (orderType == ORDER_TYPE_BUY_STOP)
              {
               double price = Ask + TrailingPoints*_Point;

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using 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 / OrderClosePrice reaches it. To trigger close 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.)

    Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
    My GBPJPY shows average spread = 26 points, average maximum spread = 134.
    My EURCHF shows average spread = 18 points, average maximum spread = 106.
    (your broker will be similar).
              Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

Reason: