Create OCO Orders
You can create two or more entry orders and then join them into OCO using just one call.
To create an OCO order:
1) Create an O2GValueMap
and specify only one parameter:
Parameter name |
Datatype |
Description |
Command |
string |
The command. Must be 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 value map to the main value map using the O2GValueMap.appendChild
method.
4) Pass the main IO2GValueMap
to the O2GRequestFactory.createOrderRequest
method.
Limitations:
1) Only entry orders can be joined into OCO. However, Entry orders with attached stop limits, either regular or ELS can be joined into OCO.
2) All entry orders must be created for the same account.
Returns:
The createOrderRequest method will return the list of request IDs for each entry order created. So, if 3 entry orders are created - 3 request IDs separated by commas (,) will be returned. Moreover, if any of the orders is an ELS order, 3 request IDs will be returned per each ELS order inside the OCO.
Example: Create an OCO order [hide]
public void PrepareParamsFromLoginRules(O2GLoginRules loginRules) { mParams = new OrderCreationParameters(); O2GResponseReaderFactory factory = mSession.getResponseReaderFactory(); if (factory == null) return; // Gets first account from login. O2GResponse accountsResponse = loginRules.getTableRefreshResponse(O2GTable.Accounts); O2GAccountsTableResponseReader accountsReader = factory.createAccountsTableReader(accountsResponse); O2GAccountRow account = accountsReader.getRow(0); // Store account id mParams.AccountID = account.AccountID; // Store base iAmount mParams.BaseAmount = account.BaseUnitSize; // Get offers for eur/usd O2GResponse offerResponse = loginRules.getTableRefreshResponse(O2GTable.Offers); O2GOffersTableResponseReader offersReader = factory.createOffersTableReader(offerResponse); for (int i = 0; i < offersReader.Count; i++) { O2GOfferRow offer = offersReader.getRow(i); if (string.Compare(offer.Instrument, "EUR/USD", true) == 0) { mParams.OfferID = offer.OfferID; mParams.Ask = offer.Ask; mParams.Bid = offer.Bid; mParams.PointSize = offer.PointSize; break; } } } public void PrepareParamsAndCallOCOOrders(string sOfferID, string sAccountID, int iAmount, double dPointSize, string sBuySell) { bool bBuy = (sBuySell == Constants.Buy); double dRate = bBuy ? mParams.Ask : mParams.Bid; double dAmendment = 5 * dPointSize; if (bBuy) dRate -= dAmendment; else dRate += dAmendment; CreateOCO(sOfferID, sAccountID, iAmount, dRate, sBuySell, 3); } public void CreateOCO(string sOfferID, string sAccountID, int iAmount, double dRate, string sBuySell, int iOrdersCount) { O2GRequestFactory factory = mSession.getRequestFactory(); if (factory == null) return; O2GValueMap mainValueMap = factory.createValueMap(); mainValueMap.setString(O2GRequestParamsEnum.Command, Constants.Commands.CreateOCO); for (int i = 0; i < iOrdersCount; i++) { O2GValueMap valuemap = factory.createValueMap(); valuemap.setString(O2GRequestParamsEnum.Command, Constants.Commands.CreateOrder); valuemap.setString(O2GRequestParamsEnum.OrderType, Constants.Orders.LimitEntry); valuemap.setString(O2GRequestParamsEnum.AccountID, sAccountID); // The identifier of the account the order should be placed for. valuemap.setString(O2GRequestParamsEnum.OfferID, sOfferID); // The identifier of the instrument the order should be placed for. valuemap.setString(O2GRequestParamsEnum.BuySell, sBuySell); // The order direction (Constants.Buy for buy, Constants.Sell for sell) valuemap.setDouble(O2GRequestParamsEnum.Rate, dRate); // The dRate at which the order must be filled (below current dRate for Buy, above current dRate for Sell) valuemap.setInt(O2GRequestParamsEnum.Amount, iAmount); // The quantity of the instrument to be bought or sold. valuemap.setString(O2GRequestParamsEnum.CustomID, string.Format("OCO_LimitEntry_N{0}", i + 1)); // The custom identifier of the order. mainValueMap.appendChild(valuemap); } O2GRequest request = factory.createOrderRequest(mainValueMap); for (int i = 0; i < request.ChildrenCount; i++) { O2GRequest childRequest = request.getChildRequest(i); mActions[childRequest.RequestID] = Action.DeleteOrder; } mSession.sendRequest(request); }