Create a Close Market Order

A close market order closes a position at any currently available market rate.

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

Please note that netting close order (see NetQuantity below) can be executed even if close orders are disabled.

Parameter name

Datatype

Description

Command

const char *

The command. Must be CreateOrder.

OrderType

const char *

The type of the order. Must be CM.

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.

TimeInForce

const char *

The Time In Force value.

Can be GTC (Good Till Cancelled), IOC (Immediate Or Cancel), FOK (Fill Or Kill), or DAY (Day Order).

The value is optional.

Close Market 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 Market 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::prepareParamsAndCallTrueMarketCloseOrder(IO2GTradeRow *trade)
  {
      //The order direcion of the close order must be opposite to the direction of the trade.
      const char * tradeBuySell = trade->getBuySell();
      if (!strcmp(tradeBuySell, "B"))                    // trade BuySell="B" => order BuySell = "S"
          createTrueMarketCloseOrder(trade->getOfferID(), trade->getAccountID(), trade->getTradeID(), trade->getAmount(), "S");
      else                                            // trade BuySell="S" => order BuySell = "B"
          createTrueMarketCloseOrder(trade->getOfferID(), trade->getAccountID(), trade->getTradeID(), trade->getAmount(), "B");
  }
 
  void CreateOrderSample::createTrueMarketCloseOrder(const char *offerID, const char *accountID, const char * tradeID, int amount, 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::TrueMarketClose);
      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->setInt(Amount, amount);                        // The quantity of the instrument to be bought or sold. Must be <= to the size of the position ("Trades" table, Lot column).
      valuemap->setString(BuySell, buySell);                    // The order direction: "B" for Buy, "S" for Sell. Must be opposite to the direction of the trade.
      valuemap->setString(CustomID, "CloseTrueMarketOrder");  // The custom identifier of the order.
 
      O2G2Ptr<IO2GRequest> request = factory->createOrderRequest(valuemap);
      mSession->sendRequest(request);
  }

back