Attach Stop and/or Limit Orders to the Command

Orders which create a new position (OM, O, OR, SE and LE orders) may also have additional values in the value map, which forces creating associated stop and limit orders.

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.

You can create either regular stop and limit orders (for non-FIFO accounts) or ELS (entry stop limit) orders for FIFO accounts. ELS is a pair of entry stop and entry limit orders which are joined into OCO (one-cancel-other) group, so when one of the orders is executed, the other order is cancelled. Also, the orders only close the existing opposite positions and never open a position. Also, the orders are cancelled when there is no opposite position existing at the moment. However, because of the FIFO rule, these orders close the oldest positions first, not the position you have attached the orders to. So, in case you recognize the whole amount on particular instrument/account as one netting position, these orders works good enough. ELS orders cannot be used on non-FIFO accounts.

Parameter name

Datatype

Description

RateStop

double

The price level of the stop order.

The stop order must be below the trade price for the position created using a buy order (long position) and above the trade price for the position created using a sell order (short position).

Either RateStop or a pair of PegOffsetStop and PegTypeStop fields must be used to specify the associated stop order. If none of these fields are specified, the stop order will not be created.

PegTypeStop

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.

PegOffsetStop

int

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

The offset must be positive for a sell position and negative for a buy position.

TrailStepStop

int

The trailing stop order follows the market in case the market moves 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, that 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, it means a non-trailing order.

If the value is specified, the order type in the Orders table will be ST.

RateLimit

double

The price level of the limit order.

The limit order must be above the trade price for the position created using a buy order (long position) and below the trade price for the position created using a sell order (short position).

Either RateLimit or a pair of PegOffsetLimit and PegTypeLimit fields must be used to specify the associated limit order. If none of these fields are specified, the limit order will not be created.

PegTypeLimit

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.

PegOffsetLimit

int

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

The offset must be negative for a sell position and positive for a buy position.

Example: Create an Entry Order and attach Stop/Limit orders [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::prepareParamsAndCallEntryLimitOrderWithStopLimit(const char *offerID, const char *accountID, int amount, double pointSize, int numPoints, const char * buySell)
  {
      double distance = 10.0; // Distance in pips
      double pegStop = 0;
      double pegLimit = 0;
      double orderRate = 0;
      double curRate = 0;
      if (!strcmp(buySell, O2G2::Buy))
      {
          curRate = mParams->mAsk;
          orderRate = curRate - pointSize * numPoints;  // The buy order rate must be below the market.
          pegStop = -distance;
          pegLimit = distance;
      }
      else                        // BuySell = O2G2::Sell
      {
          curRate = mParams->mBid;
          orderRate = curRate + pointSize * (numPoints + distance);  // The sell order rate must be above the market.
          pegStop = distance;
          pegLimit = -distance;
      }
 
      createEntryLimitOrderWithStopLimit(offerID, accountID, amount, orderRate, buySell, pegStop, pegLimit);
  }
 
  void CreateOrderSample::createEntryLimitOrderWithStopLimit(const char *offerID, const char *accountID, int amount, double rate, const char * buySell, double stopOffset, double limitOffset)
  {
      using namespace O2G2;
      O2G2Ptr<IO2GRequestFactory> factory = mSession->getRequestFactory();
 
      O2G2Ptr<IO2GValueMap> valuemap = factory->createValueMap();
      valuemap->setString(Command, Commands::CreateOrder);
      valuemap->setString(OrderType, Orders::LimitEntry);
      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(BuySell, buySell);              // The order direction (O2G2::Buy for buy, O2G2::Sell for sell)
      valuemap->setDouble(Rate, rate);                    // The rate at which the order must be filled (below current rate for Buy, above current rate for Sell)
      valuemap->setInt(Amount, amount);                   // The quantity of the instrument to be bought or sold.
      valuemap->setString(CustomID, "LimitEntryOrderWithStopLimit"); // The custom identifier of the order.
      valuemap->setString(PegTypeStop, O2G2::Peg::FromClose);   // The peg offset type
      valuemap->setDouble(PegOffsetStop, stopOffset);
      valuemap->setString(PegTypeLimit, O2G2::Peg::FromOpen);     // The peg limit type
      valuemap->setDouble(PegOffsetLimit, limitOffset);
 
      O2G2Ptr<IO2GRequest> request = factory->createOrderRequest(valuemap);
      mSession->sendRequest(request);
 
      // Store request ID and set the order for further editing. After the order has been created,
      // the order rate and amount will be changed.
      mActions[request->getRequestID()] = editOrderAction;
  }

back