Cancel open orders when price invalidates trade.

 

Thanks for your assistance.

I placed orders to open short on the NZDUSD daily TF.

The entry was just below the bar low and I wanted my code to close the orders if price invalidated the trade by moving above the bar high.

It appeared that my code closed the orders before price moved above the bar high.

Below is a screen shot of where I think the orders were closed.

 

The red horizontal line shows the invalidation level.

The orange arrow shows the bar that price almost touched but didn't on the 15 min chart until later.

My code closed the orders at this time and I had to reopen the positions again which were soon closed again.

Here is some code:.

if(Direction=="BUY")TradeInvalidatedValue=Low[BarShift];
else TradeInvalidatedValue=High[BarShift];

if(Bid>TradeInvalidatedValue)
         {
         for(int i=OrdersTotal();OrdersTotal()>0;i--)
            {
            DeleteOrder=OrderDelete(TicketArray[i-1]);
            if(DeleteOrder==false)Print("An error occurred trying to delete an order: ",GetLastError());
            }//End for loop
         }//End if(Ask<TradeInvalidatedValue)

Did this happen because of the spread?

Should I amend the code something like..

if(Bid>TradeInvalidatedValue+(Spread/2))

Thanks,

Brad. 

 
Should I amend the code something like..

It is better you run and see what happens. Never asks unless it would not run like it should.


However, what I see here is more to logic solutions rather than problems.

 
brad:I placed orders to open short on the NZDUSD daily TF.
for(int i=OrdersTotal();OrdersTotal()>0;i--)
   {
   DeleteOrder=OrderDelete(TicketArray[i-1]);
  1. You can only delete pending orders. You don't check if the ticket has opened.
  2. You are looping on your array, but checking for the total number of open orders. They are not the same in the presence of other EAs on other charts and manual trading.
    for(int i=nTickets; i>0; i--)
Reason: