1

I want to open orders by orders whenever a certain trading condition is met. I want to open pending orders in previous highest and lowest price.

So, I tried like this:

     upper=iHigh(Symbol(),Period(),iHighest(Symbol(),Period(),MODE_HIGH,periods,0));
lower=iLow(Symbol(),Period(),iLowest(Symbol(),Period(),MODE_LOW,periods,0)); 

   Comment("Upper: ",upper,"\nLower: ",lower);

       int openCount=0;
       int openpendCount=0;

    for( int i = OrdersTotal()-1; i >= 0 ; i--)
     {
       if (OrderSelect(i, SELECT_BY_POS) &&         // Only my orders :
           OrderMagicNumber() == 0         &&         // my magic number
           OrderSymbol()      == _Symbol)             // and my symbol
        {
          if(OrderType() == OP_SELL && OrderType() ==OP_BUY)                  // count market orders
             openCount++;
             
          if(OrderType() == OP_SELLLIMIT && OrderType() ==OP_BUYLIMIT)                  // count market orders
             openpendCount++;
        }
     }
    if(openCount ==0 &&  openpendCount == 0 )
        {      OrderSend(Symbol(), OP_SELLLIMIT,1,Ask+(upper-Ask), 3,0,0,"Sub Buy", MAGIC,0, Blue);
               OrderSend(Symbol(), OP_BUYLIMIT,1,Ask-(Bid-lower), 3,0,0,"Sub Buy", MAGIC,0, Blue);
       }

But no success,

Q: How can I make multiple new orders at the same time, and no more new orders when trade condition meet.

enter image description here

not2qubit
  • 14,531
  • 8
  • 95
  • 135
linlin
  • 25
  • 1
  • 7

1 Answers1

1

There are a number of problems with your code and it is difficult to know all the problems as you have not included your full code. However, try the following, it addresses the main issues I can see which are.

  • You should only check once on the close of each new bar
  • You need to check for successful selection of orders before working with them
  • Do not check for a magic number of 0, this indicates manually placed orders
  • You are mixing up && and || when checking your order types
  • You are placing your orders at strange levels. You need to set them at the previously found levels (adjusted for spread for buy orders).
int MAGIC=123;
datetime TimeBar;
int periods=50;

int start()
{

   if(TimeBar==Time[0]) return(0);

   double upper=iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, periods, 0));
   double lower=iLow (NULL, 0, iLowest (NULL, 0, MODE_LOW , periods, 0)); 
   Comment("Upper: ",upper,"\r\nLower: ",lower);
   
   int openOrders=0;
   int limitOrders=0;

   for(int i=OrdersTotal()-1; i>=0; i--)
   {
      if(OrderSelect(i, SELECT_BY_POS))
      {
          if(OrderMagicNumber()!=MAGIC || OrderSymbol()!=Symbol()) continue;
          if(OrderType()==OP_SELL || OrderType()==OP_BUY) openOrders++;
          if(OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYLIMIT) limitOrders++;
      }
   }

   if(openOrders==0 && limitOrders==0)
   {
      int res;
      res=OrderSend(Symbol(), OP_SELLLIMIT, 1, upper, 3, 0, 0, "Sub Sell", MAGIC, 0, clrBlue);
      res=OrderSend(Symbol(), OP_BUYLIMIT,  1, lower+(Ask-Bid), 3, 0, 0, "Sub Buy",  MAGIC, 0, clrBlue);
   }

   TimeBar=Time[0];
return(0);
}

You will also need to address the problem that the EA could potentially try to open an order within MarketInfo(Symbol(), MODE_STOPLEVEL) of the current price which would cause the order to be rejected by the trade server.

It is also good practice to check your OrderSend for errors.

PaulB
  • 1,262
  • 1
  • 6
  • 17
  • I would even go as far to say that you **have to always check for errors** in all operations when sending orders, as I mentioned [here](https://stackoverflow.com/questions/67525423/my-mql4-order-is-sent-fine-but-then-nothing-happens-and-no-order-was-sent/67612351#comment119507996_67525423). – not2qubit May 20 '21 at 00:14
  • As I said in my answer, it is good practice to check `OrderSend` for errors but that was not really the point of the question. It would certainly be a next step in developing the code. – PaulB May 20 '21 at 07:48