Create OTO Orders

You can create two or more entry orders and then join them into OTO (one-triggers-others) using just one call.

The first of the orders specified is a root order of the OTO group. This is the only order which becomes active immediately. All other orders are inactive until the root order is executed.

To create an OTO order group:

1) Create an O2GValueMap and fill only one field:

Valuemap field

Datatype

Description

Command

string

The command. Must be CreateOTO.

The field is obligatory.

2) Create an O2GValueMap for each entry order you want to create and fill it as for a regular entry order.

3) Append each entry order valuemap to the main valuemap using the O2GValueMap.appendChild method. Always append the root order first. Instead of any of dependent orders you can also create another "CreateOTO" command. In this case the root order of this additional OTO will become a dependent order of the root OTO, so, you can create a tree of OTO orders of any level of hierarchy.

4) Pass the main valuemap table to the createOrderRequest method.

Limitations:

1) Only entry orders or other OTO groups can be joined into OTO. However, entry orders with attached stops/limits, either regular or ELS, can be joined into OTO.

2) All entry orders must be created for the same account.

Returns:

The createOrderRequest method will return an O2GRequest (the main request) containing child requests. If 3 entry orders are created, 3 child requests will be returned. Note that you will receive responses to the child requests and will not receive a response to the main request.

Example: Create an OTO order [hide]

    struct sOrderParams
    {
        public string offerID;
        public int amount;
        public double rate;
        public string buySell;
        public string orderType;
    };
 
    static bool PrepareParamsFromLoginRules(out string sAccountID, out string sOfferID, out int iBaseAmount, out double dPointSize, out double dAskRate, out double dBidRate)
    {
        bool bOK = false;
        O2GResponseReaderFactory factory = session.getResponseReaderFactory();
        if (factory == null)
            return bOK;
        O2GLoginRules loginRules = session.getLoginRules();
        // get first account from login
        O2GResponse accountResponse = loginRules.getTableRefreshResponse(O2GTable.Accounts);
        O2GAccountsTableResponseReader accountsReader = factory.createAccountsTableReader(accountResponse);
        O2GAccountRow account = accountsReader.getRow(0);
        // get account id
        sAccountID = account.AccountID;
        // get base amount
        iBaseAmount = account.BaseUnitSize;
        sOfferID = "";
        dAskRate = 0;
        dBidRate = 0;
        dPointSize = 0;
        // Get offers for EUR/USD
        O2GResponse offersResponse = loginRules.getTableRefreshResponse(O2GTable.Offers);
        O2GOffersTableResponseReader offersReader = factory.createOffersTableReader(offersResponse);
        for (int i = 0; i < offersReader.Count; i++)
        {
            O2GOfferRow offer = offersReader.getRow(i);
            if (String.Equals(offer.Instrument, "EUR/USD"))
            {
                sOfferID = offer.OfferID;
                dAskRate = offer.Ask;
                dBidRate = offer.Bid;
                dPointSize = offer.PointSize;
                bOK = true;
                break;
            }
        }
        return bOK;
    }
 
    static O2GValueMap CreateOrder(string sAccountID, string sOfferID, double dRate, int iAmount, string sBuySell, string sOrderType)
    {
        O2GRequestFactory factory = session.getRequestFactory();
        O2GValueMap valuemap = factory.createValueMap();
        valuemap.setString(O2GRequestParamsEnum.Command, Constants.Commands.CreateOrder);
        valuemap.setString(O2GRequestParamsEnum.OrderType, sOrderType);
        valuemap.setString(O2GRequestParamsEnum.AccountID, sAccountID);
        valuemap.setString(O2GRequestParamsEnum.OfferID, sOfferID);
        valuemap.setString(O2GRequestParamsEnum.BuySell, sBuySell);
        valuemap.setDouble(O2GRequestParamsEnum.Rate, dRate);
        valuemap.setInt(O2GRequestParamsEnum.Amount, iAmount);
 
        return valuemap;
    }
 
    // Helper method for creating valueMap for one order
    static  O2GValueMap CreateOrder(string sAccountID, sOrderParams orderParams)
    {
        return CreateOrder(sAccountID, orderParams.offerID, orderParams.rate, orderParams.amount, orderParams.buySell, orderParams.orderType);
    }
 
    static void PrepareAndCreateOTO()
    {
        string sAccountID, sOfferID;
        int iBaseAmount;
        double dPointSize, dAskRate, dBidRate;
        bool bParamsOK = PrepareParamsFromLoginRules(out sAccountID, out sOfferID, out iBaseAmount, out dPointSize, out dAskRate, out dBidRate);
        if (!bParamsOK)
        {
            return;
        }
        double dRate = dAskRate;
        sOrderParams[] orders = new sOrderParams[3];
        orders[0].amount = orders[1].amount = orders[2].amount = 10 * iBaseAmount;
        orders[0].offerID = orders[1].offerID = orders[2].offerID = sOfferID;
 
        orders[0].buySell = Constants.Buy;
        orders[0].orderType = Constants.Orders.LimitEntry;
        orders[0].rate = dRate - 50 * dPointSize;
 
        orders[1].buySell = Constants.Sell;
        orders[1].orderType = Constants.Orders.StopEntry;
        orders[1].rate = orders[0].rate - 10 * dPointSize;
 
        orders[2].buySell = Constants.Buy;
        orders[2].orderType = Constants.Orders.StopEntry;
        orders[2].rate = orders[0].rate + 20 * dPointSize;
 
        O2GValueMap primary = CreateOrder(sAccountID, orders[0]);
        O2GValueMap secondary1 = CreateOrder(sAccountID, orders[1]);
        O2GValueMap secondary2 = CreateOrder(sAccountID, orders[2]);
        CreateOTO(primary, secondary1, secondary2);
    }
 
    static void CreateOTO(O2GValueMap primary, O2GValueMap secondary1, O2GValueMap secondary2)
    {
        O2GRequestFactory factory = session.getRequestFactory();
        if (factory == null)
            return;
        // create OTO command
        O2GValueMap valuemap = factory.createValueMap();
        valuemap.setString(O2GRequestParamsEnum.Command, Constants.Commands.CreateOTO);
 
        // create primary order
        valuemap.appendChild(primary);
 
        // create 2 secondary orders
        valuemap.appendChild(secondary1);
        valuemap.appendChild(secondary2);
 
        // create request from valueMap
        responseListener.SendRequest(valuemap);
    }

back