How to open new order immediately after last order closed

 

Hi I tried to open new order when last order is close. The last order can be closed by price hitting the SL or TP. To do that I used AnyBuyOrder() to check whether there's an open buy order or not,

bool AnyBuyOrders()
  {
   for(int i=OrdersTotal()-1; i >=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) &&
         OrderSymbol() == Symbol() &&
         OrderMagicNumber() == MagicNumber)
        {
         if(OrderType()==OP_BUY)
            return true;
        }
     }
   return false;
  }

if there isn't any open order then it will proceed to NewBuy() function and open new market order,

double lastBuyClosedPrice = 0;                          
void NewBuy()
  {
   if(!AnyBuyOrders())
     {
      for(int i=10; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) &&
            OrderSymbol() == Symbol() &&
            OrderMagicNumber() == MagicNumber)
           {
            if(OrderType()==OP_BUY)
               lastBuyClosedPrice = OrderClosePrice();
               Print("last buy closed price: ",lastBuyClosedPrice);
               break;
           }
        }
      MarketBuyOrder();
     }
  }

Since I used OrderClosePrice() as a new entry price, I thought it would be placed a new order after last order close but apparently it is not (screenshot attached). I looked on the journal tab that there's a lot of error 138, after looked in the internet the possible answer is that I'm not able to send new order through fast enough and suggesting to add slippage and it's a no go in my strategy. Is there another way to open new order at the last closed?

 
Luandre Ezra:

Hi I tried to open new order when last order is close. The last order can be closed by price hitting the SL or TP. To do that I used AnyBuyOrder() to check whether there's an open buy order or not,

if there isn't any open order then it will proceed to NewBuy() function and open new market order,

Since I used OrderClosePrice() as a new entry price, I thought it would be placed a new order after last order close but apparently it is not (screenshot attached). I looked on the journal tab that there's a lot of error 138, after looked in the internet the possible answer is that I'm not able to send new order through fast enough and suggesting to add slippage and it's a no go in my strategy. Is there another way to open new order at the last closed?

If you are buying at market, there is only one entry price that you can use and that is the current Ask.

Any other entry price will have to be done by pending orders or waiting until Ask is the price that you require.

You have been posting in this forum long enough and you should know all this by now.

Reason: