Invalid Ticket
total=OrdersTotal(); for(int ct=total-1;ct>0;ct--) { OrderSelect(ct,SELECT_BY_POS,MODE_TRADES); if((OrderType()<=OP_SELL)==true) { OrderDelete(OrderTicket()); return(0); } }
Now I'm using magic number, it may not necessary but it's working.
total=OrdersTotal(); for(int mg=0;mg<total;mg++) { OrderSelect(mg,SELECT_BY_POS,MODE_TRADES); if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) { mag=OrderMagicNumber(); } } for(int ct=0;ct<total;ct++) { OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES); if(OrderMagicNumber()==mag) { if(OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT) { OrderDelete(OrderTicket()); } } }
Do You see main difference between your code and code by Rosh?
for(int ct=total-1; ct>=0; ct--)It is backward loop!
Thx
My idea is if there's an active buy/sell order, the EA will read it's magic number and delete the buystop/sellstop order with the same magic number.
I'm aware of it, but it simply didn't work. Maybe I wasn't make myself clear, so he didn't understand my problem.
Thx
If you ignore Rosh and Stringo advice, and you are deleting more than one pending
order during the loop, it will fail to close all the pending orders "sometimes"
I was just asking how to successfuly deleting the remaining pending order if one of the pending orders is triggered. I've tried Rosh's code, but somehow it didn't work, I don't know what's wrong. Maybe I should post all of my code to be examined but I prefer not to do that. However it's already solved, thanks for your attention and suggestion :)
I have the same OrderDelete issue: I have set a buystop and a sellstop and when one is triggered, I want the other one to be deleted. My code will delete the buystop if the sellstop is triggered, but the it will never delete the sellstop if the buystop is triggered. In other words, in NEVER deletes the sellstop. So, what have I missed?
//---- input parameters extern int ChannelSize=20; extern double StopLoss=120; extern double ProfitTarget=240; extern double LotSize=0.01; extern double LotSizeIncrease=0; //for later use... extern int Magic=2008; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { Comment("ChannelBreakOut"); return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { Comment(""); return(0); } int start() { PendingOrders(Magic); //COUNTS PENDING ORDERS DeletePendingOrders(Magic); //DELETES REMAINING PENDING ORDER AFTER BUYSTOP/SELLSTOP TRIGGERED int Total=OrdersTotal(); //COUNTS ALL ORDERS, OPEN AND PENDING if(Total==0) //IF NO ORDERS, THEN CREATE CHANNEL WITH BUYSTOP AND SELL STOP { OrderSend(Symbol(),OP_BUYSTOP,LotSize,Ask+(Point*ChannelSize/2),0,Ask+(Point*ChannelSize/2)-StopLoss*Point, Ask+(Point*ChannelSize/2)+ProfitTarget*Point,"",Magic,0,Green); OrderSend(Symbol(),OP_SELLSTOP,LotSize,Ask-(Point*ChannelSize/2),0,Ask-(Point*ChannelSize/2) +StopLoss*Point, Ask-(Point*ChannelSize/2)-ProfitTarget*Point,"",Magic,0,Green); } return(0); } int PendingOrders(int Magic) //COUNTS PENDING ORDERS { int c=0; int total = OrdersTotal(); for (int i=total-1; i >=0; i--) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if (OrderMagicNumber()==Magic && OrderSymbol()==Symbol() && (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP)) { c++; } } return(c); } int DeletePendingOrders(int Magic) //DEFINITION OF HOW TO DELETE THE PENDING ORDERS { if(PendingOrders(Magic)==1) { OrderSelect(0,SELECT_BY_POS,MODE_TRADES); if (OrderMagicNumber()==Magic && OrderSymbol()==Symbol() && (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP)) { OrderDelete(OrderTicket()); } } return(0); }
I'm writing an EA to put 2 pending order some pips (a lot more than broker's stop level) above and below the opening candle. I'm using the code below to delete the remaining pending order if one of them is triggered. But it doesn't work if the pending order was stop order, it's returning error 4108 (invalid ticket), but working fine if the pending order was limit order.
What's wrong with these code?
Thank you