gidnoa wrote:Can someone please help with coding OCO orders?
I'm looking at something like buying 10 pips above 8am price or selling 10 pips below, if one order is executed the other should be canceled.
This is the part when i send pending orders:
Code: Select all
if (HourOf(time) = 8) then
begin
price1 := Ask +10*Point;
price2 := Bid - 10*point;
SendPendingOrder(Symbol, op_BuyStop, 0.1, price1 - 20*Point, price1 + 20*Point, price1, '', 0, OrderHandle);
SendPendingOrder(Symbol, op_SellStop, 0.1, price2 - 20*Point, price2 + 20*Point, price2, '', 0, OrderHandle);
end;
anyone can help with how i cancel the second order once one is opened?
Thanks!
You should save order handles, so use OrderHandle1 and OrderHandle2 as a global variables. Also check if these orders already set here:
Code: Select all
if (HourOf(time) = 8) and (OrderHandle1 = -1) and (OrderHandle2 = -1) then
Otherwise you will open a lot of orders during 8th hour.
Then when you already have orders, you need to monitor them by handles.
Code: Select all
if (OrderHandle1 <> -1) and (OrderHandle2 <> -1) then
begin
if OrderSelect(OrderHandle1, SELECT_BY_TICKET, MODE_TRADES) and (OrderType in [tp_Buy, tp_Sell]) then
CloseOrder(OrderHandle2)
else
if OrderSelect(OrderHandle2, SELECT_BY_TICKET, MODE_TRADES) and (OrderType in [tp_Buy, tp_Sell]) then
CloseOrder(OrderHandle1);
end;
when both orders closed or you do not need them any more set their handles to -1
Code: Select all
if OrderClosed(OrderHandle1) and OrderClose(OrderHandle2) then
begin
OrderHandle1 := -1;
OrderHandle2 := -1;
end;