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

const char *

The command. Must be CreateOrder.

OrderType

const char *

The type of the order. Must be CR.

OfferID

const char *

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

AccountID

const char *

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

const char *

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

const char *

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

const char *

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

const char *

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

const char *

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.

Example: Create a Close Range order [hide]

  void CreateOrderSample::prepareParamsFromLoginRules(IO2GLoginRules *loginRules)
  {
      mParams = new OrderCreationParam();
 
      O2G2Ptr<IO2GResponseReaderFactory> factory = mSession->getResponseReaderFactory();
      // Gets first account from login.
      O2G2Ptr<IO2GResponse> accountsResponse = loginRules->getTableRefreshResponse(Accounts);
      O2G2Ptr<IO2GAccountsTableResponseReader> accountsReader = factory->createAccountsTableReader(accountsResponse);
      O2G2Ptr<IO2GAccountRow> account = accountsReader->getRow(0);
      // Store account id
      mParams->mAccountID = account->getAccountID();
      // Store base amount
      mParams->mBaseAmount = account->getBaseUnitSize();
      // Get offers for eur/usd
      O2G2Ptr<IO2GResponse> offerResponse = loginRules->getTableRefreshResponse(Offers);
      O2G2Ptr<IO2GOffersTableResponseReader> offersReader = factory->createOffersTableReader(offerResponse);
      for (int i = 0; i < offersReader->size(); i++)
      {
          O2G2Ptr<IO2GOfferRow> offer = offersReader->getRow(i);
          if (_stricmp(offer->getInstrument(), "EUR/USD") == 0)
          {
              mParams->mOfferID = offer->getOfferID();
              mParams->mAsk = offer->getAsk();
              mParams->mBid = offer->getBid();
              mParams->mPointSize = offer->getPointSize();
              break;
          }
      }
 
  }
 
  void CreateOrderSample::prepareParamsAndCallRangeCloseOrder(IO2GTradeRow * trade, int atMarket)
  {
      const char * tradeBuySell = trade->getBuySell();
      // Close order is opposite to the trade
      bool buyOrder = false;
      if (strcmp(tradeBuySell, O2G2::Buy) == 0)
          buyOrder = true;
 
      // Get rate, then calculate rateMin and rateMax
      const char * tradeOfferID = trade->getOfferID();
      double rate = 0;
      rate = buyOrder ? mParams->mAsk : mParams->mBid;
 
      double pointSize = mParams->mPointSize;
      double rateMin = rate - atMarket * pointSize;
      double rateMax = rate + atMarket * pointSize;
 
      createRangeCloseOrder(tradeOfferID,
                            trade->getAccountID(),
                            trade->getTradeID(),
                            trade->getAmount(),
                            rateMin, rateMax,
                            buyOrder ? O2G2::Sell : O2G2::Buy);
  }
 
  void CreateOrderSample::createRangeCloseOrder(const char *offerID, const char *accountID, const char *tradeID, int amount, double rateMin, double rateMax, const char * buySell)
  {
      using namespace O2G2;
      O2G2Ptr<IO2GRequestFactory> factory = mSession->getRequestFactory();
 
      O2G2Ptr<IO2GValueMap> valuemap = factory->createValueMap();
      valuemap->setString(Command, Commands::CreateOrder);
      valuemap->setString(OrderType, Orders::MarketCloseRange);
      valuemap->setString(AccountID, accountID);
      valuemap->setString(OfferID, offerID);                // The identifier of the instrument the order should be placed for.
      valuemap->setString(TradeID, tradeID);                // The identifier of the trade to be closed.
      valuemap->setString(BuySell, buySell);                // The order direction ("B" for Buy, "S" for Sell). Must be opposite to the direction of the trade.
      valuemap->setInt(Amount, amount);                    // 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(RateMin, rateMin);                // The minimum rate at which the order can be filled.
      valuemap->setDouble(RateMax, rateMax);                // The maximum rate at which the order can be filled.
      valuemap->setString(CustomID, "CloseRangeOrder");    // The custom identifier of the order.
      O2G2Ptr<IO2GRequest> request = factory->createOrderRequest(valuemap);
      mSession->sendRequest(request);
  }

back