Page 1 of 1

OCO order problem

Posted: Fri Jun 29, 2012 4:43 am
by will111
Hi, I am trying to code an OCO order 10pips above bar 1 high and 10 pips below bar 1 low. The pending orders go thru but it won't delete the other pending order after one order is executed. Could someone please help with my code below? Thanks!

//assign price
pBuy := High(1) + 10*Point;
pSell := Low(1) - 10*Point;


begin
SendPendingOrder(Symbol, op_BuyStop, 0.1, pBuy - 50*Point, pBuy + 60*Point, pBuy, '', 0, OrderHandle1);
SendPendingOrder(Symbol, op_SellStop, 0.1, pSell + 50*Point, pSell - 60*Point, pSell, '', 0, OrderHandle2);
end;

//OCO orders
if (OrderHandle1 <> -1) or (OrderHandle2 <> -1) then
begin
if OrderSelect(OrderHandle1, SELECT_BY_TICKET, MODE_TRADES) and (OrderType in [tp_BuyStop, tp_SellStop]) then
begin
DeleteOrder(OrderHandle2);
OrderHandle2 := -1;
end
else
if OrderSelect(OrderHandle2, SELECT_BY_TICKET, MODE_TRADES) and (OrderType in [tp_BuyStop, tp_SellStop]) then
begin
DeleteOrder(OrderHandle1);
OrderHandle1 := -1;
end;
end;

end;

Posted: Sat Jun 30, 2012 3:32 am
by FT Support
Hello,

Please explain why you select order by OrderHandle2 and then delete order by OrderHandle1

Posted: Sat Jun 30, 2012 4:54 am
by will111
Hi,

Thanks for your reply. Sorry I am new to programming.

I want to select orderHandle1 and if it's a buy or sell order that is executed then I want to delete the other order. When the executed order is closed I want to restart the criteria for executing the pending orders again.

I made some changes but still not quite working. Could you please tell me what changes to the code I need to make? Thanks!

begin
SendPendingOrder(Symbol, op_BuyStop, 0.1, pBuy - 50*Point, pBuy + 60*Point, pBuy, '', 0, OrderHandle1);
SendPendingOrder(Symbol, op_SellStop, 0.1, pSell + 50*Point, pSell - 60*Point, pSell, '', 0, OrderHandle2);
end;

//OCO orders
if (OrderHandle1 <> -1) and (OrderHandle2 <> -1) then
begin
if OrderSelect(OrderHandle1, SELECT_BY_TICKET, MODE_TRADES) and (OrderType in [tp_Buy, tp_Sell]) then
begin
DeleteOrder(OrderHandle2);
end
else
if OrderSelect(OrderHandle2, SELECT_BY_TICKET, MODE_TRADES) and (OrderType in [tp_Buy, tp_Sell]) then
begin
DeleteOrder(OrderHandle1);
end;
end;

//Restart orders
if OrderClosed(OrderHandle1) and OrderClosed(OrderHandle2) then
begin
OrderHandle1 := -1;
OrderHandle2 := -1;
end;

Posted: Sun Jul 01, 2012 9:09 am
by FT Support
Hello, i'm not sure if it will help but please try the code below

Code: Select all


begin
SendPendingOrder(Symbol, op_BuyStop, 0.1, pBuy - 50*Point, pBuy + 60*Point, pBuy, '', 0, OrderHandle1);
SendPendingOrder(Symbol, op_SellStop, 0.1, pSell + 50*Point, pSell - 60*Point, pSell, '', 0, OrderHandle2);
end;

//OCO orders
if (OrderHandle1 <> -1) and (OrderHandle2 <> -1) then
begin
if OrderSelect(OrderHandle1, SELECT_BY_TICKET, MODE_TRADES) then
 if (OrderType in [tp_Buy, tp_Sell]) then
begin
  DeleteOrder(OrderHandle2);
end
else
if OrderSelect(OrderHandle2, SELECT_BY_TICKET, MODE_TRADES) then 
if (OrderType in [tp_Buy, tp_Sell]) then
begin
  DeleteOrder(OrderHandle1);
end;
end;

//Restart orders
if OrderClosed(OrderHandle1) then
begin
OrderHandle1 := -1;
end;

if OrderClosed(OrderHandle2) then
begin
OrderHandle2 := -1;
end;


Posted: Sun Jul 01, 2012 9:22 am
by will111
This works! Thanks for your help!

Re: OCO order problem

Posted: Mon Feb 29, 2016 11:08 am
by themaxx
Could anyone please help me modify this code, to create a pending order script which opens a buy stop 5 pips above the current bar high, with the stoploss 5 pips below the current bar low?