Create a Stop Order

A stop order is used for limiting losses of the existing position when the market condition is met.

Stop orders can be created for existing trades as well as for existing entry orders. Stop orders created for entry orders remain inactive until the trade is created by the entry order.

Only one stop order can be attached to a position or an the entry order.

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

Please note that stop and limit orders cannot be attached to netting entry orders.

Parameter name

Datatype

Description

Command

const char *

The command. Must be CreateOrder.

OrderType

const char *

The type of the order. Must be S.

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.

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 equal to the size of the position (see the Amount column of the trade).

Rate

double

The rate at which the order must be filled.

The rate must be below the market for sell orders and above the market for buy orders.

To specify the stop order price, use:

  • Rate - if the stop order is created for the existing position.

  • Either Rate or a pair of PegType and PegOffset fields - if the stop order is created for an entry order.

PegType

const char *

"Pegged" means that the price is specified as an offset (in pips) against the market price.

The field specifies which price should be used to calculate the order price:

O

The open price of the related trade.

M

The close price (the current market price) of the related trade.

Please note that "pegged" orders are stored as "pegged" only while the order is attached to an entry order. Once the entry order gets filled and a position is opened, the peg price is automatically converted to a regular market price.

PegOffset

int

The offset to the price specified in the PegType field. The offset must be expressed in pips.

The offset must be positive for buy orders and negative for sell orders.

TrailStep

int

The trailing stop order follows the market in case the market moves in the direction opposite to that of the order (e.g. in the profitable direction for the position).

The value specifies the maximum change of the market price after which the rate of the order will be changed as well.

The value is expressed in the units of the minimum change of the price (see the Digits column of the Offers table).

For example, if the value of the Digits column is 3 and the value of this field is 5, the maximum change of the market price will be 5 * 10-3 = 0.005.

If the value is 1, it means that the dynamic trailing mode is used, i.e. the order rate is changed with every change of the market price. Please note that in some systems, only dynamic trailing mode is supported.

The value is optional. By default, this value is 0. 0 means no-trailing order.

TimeInForce

const char *

The Time In Force value.

Can be GTC (Good Till Cancelled) or DAY (Day Order).

The value is optional.

Stop orders are GTC 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 Stop 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::prepareParamsAndCallStopOrder(IO2GTradeRow * trade, int atMarket)
  {
      const char * tradeBuySell = trade->getBuySell();
      // Close order is opposite to the trade
      bool buyOrder = false;
      if (!strcmp (tradeBuySell, "S"))
          buyOrder = true;
 
      const char * tradeOfferID = trade->getOfferID();
      double curRate = 0;
      double stopRate = 0;
      double pointSize = mParams->mPointSize;
      if (buyOrder)
      {
          curRate = mParams->mAsk;
          stopRate = curRate + atMarket * pointSize;
 
      }
      else
      {
          curRate = mParams->mBid;
          stopRate = curRate - atMarket * pointSize;
      }
 
      createStopOrder(tradeOfferID,
                      trade->getAccountID(),
                      trade->getTradeID(),
                      trade->getAmount(),
                      stopRate,
                      buyOrder ? O2G2::Buy : O2G2::Sell);
  }
 
  void CreateOrderSample::createStopOrder(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::Stop);
      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 be = size of the position (Lot of the trade). Must be divisible by baseUnitSize
      valuemap->setDouble(Rate, rate);                // The rate at which the order must be filled (< market for "Sell" order, > market for "Buy" order)
      valuemap->setString(CustomID, "StopOrder");        // The custom identifier of the order
      O2G2Ptr<IO2GRequest> request = factory->createOrderRequest(valuemap);
      mSession->sendRequest(request);
  }

back