Create a Close Order

A close order closes a position at the specified market rate in case such rate is available on the market.

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 C.

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.

A 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.

Rate

double

The rate at which the order must 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 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 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::prepareParamsAndCallMarketCloseOrder(IO2GTradeRow *trade)
  {
      const char * tradeBuySell = trade->getBuySell();
      // Close order is opposite to the trade
      bool buyOrder = false;
      if (!strcmp (tradeBuySell, O2G2::Sell))
          buyOrder = true;
 
      const char *tradeOfferID = trade->getOfferID();
      double rate = 0;
      // Ask price for Buy and Bid price for Sell
      rate = buyOrder ?  mParams->mAsk : mParams->mBid;
 
      createMarketCloseOrder(tradeOfferID,
                             trade->getAccountID(),
                             trade->getTradeID(),
                             trade->getAmount(),
                             rate,
                             buyOrder ? O2G2::Buy : O2G2::Sell);
  }
 
  void CreateOrderSample::createMarketCloseOrder(const char *offerID, const char *accountID, const char * tradeID, int amount, double rate, 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::MarketClose);
      valuemap->setString(AccountID, accountID);                // The identifier of the account the order should be placed for.
      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 <= to the size of the position (Lot column of the trade).
      valuemap->setDouble(Rate, rate);                          // The rate at which the order must be filled.
      valuemap->setString(CustomID, "CloseMarketOrder");        // The custom identifier of the order.
      O2G2Ptr<IO2GRequest> request = factory->createOrderRequest(valuemap);
      mSession->sendRequest(request);
  }

back