0
votes

I am working on a Project that requires me to place a BUYSTOP and a SELLSTOP pair of orders and then on the next bar if those orders are not triggered, then delete them and place fresh ones.

Here is my code:

if(logic == true && OrdersTotal() == 0)
  {bool res = OrderSend(....);}
if(OrdersTotal() != 0)
  {
  if(ordertype == OP_BUY || ordertype == OP_SELL)
     {
      bool del = OrderDelete(....);
     }
  }

This code is properly placing orders and deleting them as well when I am testing.

But when the EA is active on the live server, it does not open orders because the platform already has orders of other instruments open.

I'm sure there would be quite an easy way to get around this but since I am a novice, I'm not able to figure that out.

2

2 Answers

1
votes

it is not clear whether you use magic number and symbol check. you should check sth like

int _ordersTotal = OrdersTotal()-1;
for (int i = _ordersTotal; i >= 0; i--){
   if (OrderSymbol() != Symbol() || OrderMagicNumber() != magic) continue;
   ....
}

in different realization, i.e. you can create a function(String Symbol) that checks if you have some working orders placed of the indicated Symbol.

0
votes

Old proverb says:
Measure twice before cut once
There are actually four values ( three for pendings ) to check before OrderDelete()

As your definition states to handle { OP_{BUY|SELL}STOP } orders, there are following three items to check:

  • Symbol() match ( not causing unwanted side-effects by deleting other EA's or manual orders )
  • OrderType() match ( not ignoring the actual state { PENDING | AT_MARKET } and direction { BUY | SELL } of the order )
  • OrderMagicNumber() match ( not ignoring the UUID selector utility available to be set for each individual OrderSend() )

So, let's sketch the detection process:

int myEaContextAwareMagicNUMBER = ...;

for ( int ii  = OrdersTotal();
          ii >= 0;
          ii--
      )
      if OrderSelect( ii, SELECT_BY_POS, MODE_TRADES )
      {
          if (      OrderSymbol()       != _Symbol
             &&     OrderMagicNumber()  != myEaContextAwareMagicNUMBER
             &&     OrderOpenTime()     >= Time[1]                    // Prev. Bar 
             && !(  OrderType()         == OP_BUYSTOP
                 || OrderType()         == OP_SELLSTOP
                 )
             ) continue;                // __^ __^ __^ __^ __^ __^ loop for next test

          // -------------------------------------------+
          // FINALLY PROCESS THE MATCHING OrderDelete() |
          // -------------------------------------------+
             ...
             ..
             .
          // -------------------------------------------+
      }
      else Print( "WARN: OrderSelect() failed at db.POOL.SELECT(), RECORD_NUMBER == ", ii );

So how to delete un-triggered pendings is done.


Next comes the remark about

"... when the ea is active on the live server, it does not open orders because the platform already has orders of other instruments open."

There could hardly be any advice provided without delivering the exact { GetLastError() | _LastError } values.

Some Brokers for some account types do indeed restrict OrderSend() acceptance policies, and thus besides the GetLastError() value the respective Broker Terms and Conditions apply.

Do not hesitate to ask more & may enjoy other Questions/Answers in MQL4 domain.