OCO orders

How to create strategies and indicators
Message
Author
gidnoa
Posts: 9
Joined: Fri Aug 01, 2008 11:18 am

OCO orders

#1 Postby gidnoa » Fri Aug 01, 2008 11:38 am

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!

User avatar
Terranin
Site Admin
Posts: 833
Joined: Sat Oct 21, 2006 4:39 pm

Re: OCO orders

#2 Postby Terranin » Fri Aug 01, 2008 12:53 pm

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;
Hasta la vista
Mike

gidnoa
Posts: 9
Joined: Fri Aug 01, 2008 11:18 am

#3 Postby gidnoa » Fri Aug 01, 2008 1:03 pm

Gotit, Thanks a bunch for your quick reply!!!


Return to “FT API”

Who is online

Users browsing this forum: No registered users and 19 guests