How to avoid mixed (Buy/Sell) positions?

 

I've a major problem with My EA as detailed below; please help and advice with partially written CODE as possible.

Introduction:

  • My EA is instructed to start by certain neutral conditions, till only one of the GoLG (buy) or GoSH (sell) condition(s) is met.
  • The last OrderType() should be stored in order to be used as a controller for the next order (I mean the last RUNNING position and not the last closed one in history).
  • In case of, the LastOrderType() == OP_BUY/OP_SELL, My EA should be prepared for KeepLG/KeepSH if the GoLG/GoSH condition is met for the next order.
My Problem:

  • Upon trend reversal; suppose that, there're a set of positions (all are in the same direction buy/sell).
  • My EA should CloseALL positions upon receiving the SIGNAL_CLOSE. (Nothing is perfect)
  • What actually happening is, My EA open a reverse order but closes only some of the original orders.

How to instruct My EA to make sure that, CloseALL of the concurrent orders upon the reversal signal, and hence to avoid mixed positions?

I've tried the following code as a solution, But I found it results in OrderClose Error 138 (ERR_REQUOTE) although this loop should continue till killing all the positions at the market price.

if(LG_EXT)
          for(int nLG = 0; nLG < OrdersTotal(); nLG ++)
         {OrderSelect(nLG, SELECT_BY_POS, MODE_TRADES);
          if(OrderType() == OP_BUY  && OrderSymbol() == Symbol()) TakeAction = SIGNAL_CLOSEBUY; }

if(SH_EXT)
          for(int nSH = 0; nSH < OrdersTotal(); nSH ++)
         {OrderSelect(nSH, SELECT_BY_POS, MODE_TRADES);
          if(OrderType() == OP_SELL && OrderSymbol() == Symbol()) TakeAction = SIGNAL_CLOSESELL;}

I can see the OrderCloseBy() can help, but I'm not sure how to manage its parameters and get it loop incorporated to close the (n) Orders at all.

bool OrderCloseBy(int ticket, int opposite, color Color=CLR_NONE)
Closes an opened order by another opposite opened order. If the function succeeds, the return value is true. If the function fails, the return value is false. To get the detailed error information, call GetLastError().
ticket - Unique number of the order ticket.
opposite - Unique number of the opposite order ticket.
Color - Color of the closing arrow on the chart. If the parameter is missing or has CLR_NONE value closing arrow will not be drawn on the chart.

PLEASE ADVICE!

 

It's hard to see the issue with your code that closes orders if you don't show it . . .

If you are closing Orders you MUST count down, not up . . . Loops and Closing or Deleting Orders

 

Great thanks for you RaptorUK,

Your detailed explanation is very simple and i guess this is the good reason for my amazing tests.

I was wondering, how come that; the equity is reaching 12-20x times of the balance and sudden extreme losses later on.

 
If you are referring to the tester's graph. It could be due to the fact that you're testing a no-stop-loss system. You either ran out of money or the test stopped when one bad order was still losing.
Reason: