Create a Close Range Order

A close range order closes a position at the available market rate in case this rate is in the range specified in the command.

Please note that close orders must be permitted for the account in order to be used.

Parameter name

Datatype

Description

Command

string

The command. Must be CreateOrder.

OrderType

string

The type of the order. Must be CR.

OfferID

string

The identifier of the instrument the order should be placed for. The value must be obtained from the Offers table, the OfferID column.

AccountID

string

The identifier of the account the order should be placed for. The value must be obtained from the Accounts table, the AccountID column.

Please note that the account identifier may not necessarily be equal to the account name which is shown in the Trading Station application.

TradeID

string

The identifier of the trade to be closed. The value must be obtained from the Trades table, the TradeID column.

Do not fill this value for a net quantity order.

BuySell

string

The order direction.

The value must be B - for buy orders and S - for sell orders. The order direction must be opposite to the direction of the order which was used to create the position (see the BuySell column of the trade).

Amount

int

The amount of the order. In the case of FX instruments, the amount is expressed in the base currency of an instrument. In the case of CFD instruments, the amount is expressed in contracts. Must be divisible by the value of the lot size.

The amount must be less than or equal to the size of the position (see the Amount column of the trade).

Do not fill this value for a net quantity order.

NetQuantity

string

The net quantity flag. Use this parameter with the value y or Y only for a net quantity order.

An close order in the netting mode closes all positions which exist at the moment of the order filling and which are created for the order account and order instrument in the direction (see BuySell) opposite to the direction of the order.

RateMin

double

The minimum rate at which the order can be filled.

RateMax

double

The maximum rate at which the order can be filled.

TimeInForce

string

The Time In Force value.

Can be IOC (Immediate Or Cancel) or FOK (Fill Or Kill).

The value is optional.

Close Range orders are IOC orders by default.

ClientRate

double

The current price of the order instrument.

The value is optional. It is used for logging purposes.

CustomID

string

The custom identifier of the order. This value will be populated into these columns of the trading tables:

Table

Column

Orders

RequestTXT

Closed Trades

CloseOrderRequestTXT

The value is optional.

Create a Close Range 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 PrepareParamsAndCallRangeCloseOrder(O2GTradeRow tradeRow, int iAtMarket)
   {
       string sTradeBuySell = tradeRow.BuySell;
       // Close order is opposite to the trade
       bool bBuyOrder = (sTradeBuySell == Constants.Sell);
 
       // Get dRate, then calculate dRateMin and dRateMax
       string sTradeOfferID = tradeRow.OfferID;
       double dRate = 0;
       dRate = bBuyOrder ? mParams.Ask : mParams.Bid;
 
       double dPointSize = mParams.PointSize;
       double dRateMin = dRate - iAtMarket * dPointSize;
       double dRateMax = dRate + iAtMarket * dPointSize;
 
       CreateRangeCloseOrder(sTradeOfferID,
                tradeRow.AccountID,
                tradeRow.TradeID,
                tradeRow.Amount,
                dRateMin, dRateMax,
                bBuyOrder ? Constants.Buy : Constants.Sell);
   }
 
   public void CreateRangeCloseOrder(string sOfferID, string sAccountID, string sTradeID, int iAmount, double dRateMin, double dRateMax, string sBuySell)
   {
 
       O2GRequestFactory factory = mSession.getRequestFactory();
       if (factory == null)
           return;
       O2GValueMap valuemap = factory.createValueMap();
       valuemap.setString(O2GRequestParamsEnum.Command, Constants.Commands.CreateOrder);
       valuemap.setString(O2GRequestParamsEnum.OrderType, Constants.Orders.MarketCloseRange);
       valuemap.setString(O2GRequestParamsEnum.AccountID, sAccountID);
       valuemap.setString(O2GRequestParamsEnum.OfferID, sOfferID);                // The identifier of the instrument the order should be placed for.
       valuemap.setString(O2GRequestParamsEnum.TradeID, sTradeID);                // The identifier of the trade to be closed.
       valuemap.setString(O2GRequestParamsEnum.BuySell, sBuySell);                // The order direction (Constants.Buy for Buy, Constants.Sell for Sell). Must be opposite to the direction of the trade.
       valuemap.setInt(O2GRequestParamsEnum.Amount, iAmount);                    // The quantity of the instrument to be bought or sold. Must be <= size of the position (Lot of the trade). Must be divisible by baseUnitSize.
       valuemap.setDouble(O2GRequestParamsEnum.RateMin, dRateMin);                // The minimum dRate at which the order can be filled.
       valuemap.setDouble(O2GRequestParamsEnum.RateMax, dRateMax);                // The maximum dRate at which the order can be filled.
       valuemap.setString(O2GRequestParamsEnum.CustomID, "CloseRangeOrder");    // The custom identifier of the order.
       O2GRequest request = factory.createOrderRequest(valuemap);
       mSession.sendRequest(request);
   }

back