How to avoid duplicated orders?

 

My EA is instructed to open buy/sell order at specified level under certain conditions

But due to fluctuation and/or freezing movement about the same level, my EA executes additional orders repeatedly.

How to adjust my EA to open only one position at the specified level?

 
Either remember that you already did it . . . or before you place an order check the open orders and the recently closed orders for an order at the same entry.
 
OmegaFX:

My EA is instructed to open buy/sell order at specified level under certain conditions

But due to fluctuation and/or freezing movement about the same level, my EA executes additional orders repeatedly.

How to adjust my EA to open only one position at the specified level?

Personally I use ordercomment, i store Bars+MagicNumber+OrderType()+....

If comments match, when you scan ordercomments across all orders with all your variables, then don't enter!!

Some may say, that the broker can override the comment, but when that happens and if it ever, then let me know. I've been testing for a long long long time, real/demo, and have never had my comments overriden.

 
c0d3:

Personally I use ordercomment, i store Bars+MagicNumber+OrderType()+....

If comments match, when you scan ordercomments across all orders with all your variables, then don't enter!!

Some may say, that the broker can override the comment, but when that happens and if it ever, then let me know. I've been testing for a long long long time, real/demo, and have never had my comments overriden.

I don't think it's a question of if it happens. With so many people saying it happens I don't doubt that it does. The question becomes, do you think it's going to happen to you. Obviously, you don't believe the risk is high enough to avoid order-comments. Other people wouldn't be able to sleep at night with the remote possibility that ea could break due to comment override by broker.

Here's one link which seem convincing.

 
RaptorUK:
Either remember that you already did it . . . or before you place an order check the open orders and the recently closed orders for an order at the same entry.

I prefer the second option you've suggested.

To do so; I'd like to refer to your pretty topic "Loops and Closing or Deleting Orders" https://www.mql5.com/en/forum/139654

//--- Trade Entry Procedures
for ( int j = OrdersTotal()-1; j >= 0; j -- ) {
if ( LG_ENT_TEST )        TakeAction = SIGNAL_BUY;
if ( TakeAction == SIGNAL_BUY  && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount))) ) {
if ( !( ( OrderOpenPrice() + GAP * Point ) >= ACT_CC && ACT_CC >= ( OrderOpenPrice() - GAP * Point ) ) ) <<--- Is there already opened order (buy type) near the current price ACT_CC?
{
//--- Free Margin Module
if ( AccountFreeMargin() < (1000 * Lots) )
{ Print ( "We have no money. Free Margin = ", AccountFreeMargin() ); return(0);
}
if ( UseSL_Module ) SL_LEVEL = Ask - (StopLoss * Point);   else  SL_LEVEL = 0.0;
if ( UseTP_Module ) TP_LEVEL = Ask + (TakeProfit * Point); else  TP_LEVEL = 0.0;
Ticket = OrderSend ( Symbol(), OP_BUY , Lots, Ask, Slippage, SL_LEVEL, TP_LEVEL, "Buy(#" + LG_MagicNumber + ")", LG_MagicNumber, 0, DodgerBlue );
if ( Ticket > 0 ) {
if ( OrderSelect (Ticket, SELECT_BY_TICKET, MODE_TRADES) )
{ Print ( "BUY order opened : ", OrderOpenPrice() );
if ( SignalMail )
SendMail ( "[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr ( Ask, Digits ) + " Open Buy" );
}
else
{
Print ( "Error opening BUY order : ", GetLastError() );
}
}

Is there any interference for the checking out loop stated above?

Is the counting down more effective in this case also instead of the counting up loop?

Please review and comment

 
c0d3:Some may say, that the broker can override the comment, but when that happens and if it ever, then let me know.
Brokers can change comments, including complete replacement. Now you know. Depends on the broker. Some times EVERY order is modified.
 
OmegaFX:

I prefer the second option you've suggested.

To do so; I'd like to refer to your pretty topic "Loops and Closing or Deleting Orders" https://www.mql5.com/en/forum/139654

Is there any interference for the checking out loop stated above?

Is the counting down more effective in this case also instead of the counting up loop?

Please review and comment

I find trying to follow your code quite an effort due to lack of indenting . . .

4 lines down you use OrderOpenPrice() but I don't see an OrderSelect() above it, is there one ?

 
RaptorUK:

I find trying to follow your code quite an effort due to lack of indenting . . .

4 lines down you use OrderOpenPrice() but I don't see an OrderSelect() above it, is there one ?

Thanks for your reply, and so sorry for my late response.

Yes, you're right OrderSelect() is just missed out, but ignore that code (I don't like it anyway).

Below is the modification I've made to get the point and avoid opening duplicated orders at the same level within the said GAP (say 10 PIPs).

The main problem I've found with the second option suggested by you, i.e. checking out the opened orders is as following,

  • The checking out loop, should be terminated when the following condition is met, i.e.

( ( ACT_CC + GAP*Point >= OrderOpenPrice() ) && ( OrderOpenPrice() >= ACT_CC - GAP*Point ) ) == True // where ACT_CC is the current close price
  • On the other hand, I won't the loop to be terminated upon checking out only one side of the OrderType(), and hence the other type is missed out.
  • In other words, The loop should check both sides (buy and sell) and must be terminated partially when the buy/sell order is found (if any), and continue checking the other side for sell/buy order as well.
  • I got lost when I've tried to control that issue by using the ordinary single loop, that's why I split it into what I can call it dual-loop i.e. single loop for checking each order type as shown below,

same here, but only for sell orders,

Please review it, and help me getting them integrated in single loop normally if applicable.

 

Kindly check out my following code for defects,

//--- Check out the currently opened orders in order to avoid duplicates (I don't care about the orders history)
  //--- Trade Entry Procedures
  bool EXE_LG = True; // Initial trigger-value which allow sending buy order
  bool EXE_SH = True; // Initial trigger-value which allow sending sell order
  //--- Trade Entry Procedures (LG-MODE)
  for ( int kLG = OrdersTotal() -1; kLG >= 0; kLG -- )
    {
    if ( OrderSelect ( kLG, SELECT_BY_POS, MODE_TRADES ) == True ) 
      if ( OrderType() == OP_BUY  && OrderSymbol() == Symbol() && OrderMagicNumber() == LG_Magic )
        if ( DYN_XS + GAP*Point >= OrderOpenPrice() && OrderOpenPrice() >= DYN_XS - GAP*Point ) // if the order is found;
          break; // Terminate the loop, and
          EXE_LG = False; // Block sending any buy orders
    }
  //--- Trade Entry Procedures (SH-MODE)
  for ( int kSH = OrdersTotal() -1; kSH >= 0; kSH -- )
    {
    if ( OrderSelect ( kSH, SELECT_BY_POS, MODE_TRADES ) == True ) 
      if ( OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == SH_Magic )
        if ( DYN_XR + GAP*Point >= OrderOpenPrice() && OrderOpenPrice() >= DYN_XR - GAP*Point ) // if the order is found;
          break; // Terminate the loop, and
          EXE_SH = False; // Block sending any sell orders
    }
 
OmegaFX:

Kindly check out my following code for defects,


why ?? Is it wrong ?? Then what errors do you see....

Testing you can do yourself .....

The idea for checking looks OK depends on GAP

 
deVries:


why ?? Is it wrong ?? Then what errors do you see....

Testing you can do yourself .....

The idea for checking looks OK depends on GAP

Its attached and in action already,

but still duplicates the orders at the same price exactly ( extern int GAP = 10; // PIPs)

for sure, it gains huge profits when the market trends on my favor but on the other side extreme loss occurs when not.

I just want opening single order at the specified level no more.

Reason: