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

RATE_STOP

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 RATE_STOP or a pair of PEG_OFFSET_STOP and PEG_TYPE_STOP fields must be used to specify the associated stop order. If none of these fields are specified, the stop order will not be created.

PEG_TYPE_STOP

String

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

PEG_OFFSET_STOP

int

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

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

TRAIL_STEP_STOP

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.

RATE_LIMIT

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 RATE_LIMIT or a pair of PEG_OFFSET_LIMIT and PEG_TYPE_LIMIT fields must be used to specify the associated limit order. If none of these fields are specified, the limit order will not be created.

PEG_TYPE_LIMIT

String

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

PEG_OFFSET_LIMIT

int

The offset to the price specified in the PEG_TYPE_LIMIT 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 and Limit orders [hide]

 class ParamsFromLoginRules {
    String sAccountID;
    String sOfferID;
    int iBaseAmount;
    double dPointSize;
    double dAskRate;
    double dBidRate;
}
 
   private void prepareParamsAndCallEntryLimitOrderWithStopLimit(int iAmount, int iNumPoints, String sBuySell)
   {
       ParamsFromLoginRules params = new ParamsFromLoginRules();
       boolean bParamsOK = prepareParamsFromLoginRules(params);
       if (!bParamsOK)
            return;
 
       double dDistance = 10.0; // Distance in pips
       double dPegStop = 0;
       double dPegLimit = 0;
       double dOrderRate = 0;
       double dCurrentRate = 0;
       if (sBuySell.equals(Constants.Buy))
       {
           dCurrentRate = params.dAskRate;
           dOrderRate = dCurrentRate - params.dPointSize * iNumPoints;// The buy order rate must be below the market.
           dPegStop = -dDistance;
           dPegLimit = dDistance;
       }
       else// sBuySell = Constants.Sell
       {
           dCurrentRate = params.dBidRate;
           dOrderRate = dCurrentRate + params.dPointSize * (iNumPoints + dDistance);// The sell order rate must be above the market.
           dPegStop = dDistance;
           dPegLimit = -dDistance;
       }
       createEntryLimitOrderWithStopLimit(params.sOfferID, params.sAccountID, iAmount, dOrderRate, sBuySell, dPegStop, dPegLimit);
   }
 
   private void createEntryLimitOrderWithStopLimit(String sOfferID, String sAccountID, int iAmount, double dOrderRate, String sBuySell, double dPegStopOffset, double dPegLimitOffset)
   {
       O2GRequestFactory factory = mSession.getRequestFactory();
       if (factory == null) {
           return;
       }
       O2GValueMap valuemap = factory.createValueMap();
       valuemap.setString(O2GRequestParamsEnum.COMMAND, Constants.Commands.CreateOrder);
       valuemap.setString(O2GRequestParamsEnum.ORDER_TYPE, Constants.Orders.LimitEntry);
       valuemap.setString(O2GRequestParamsEnum.ACCOUNT_ID, sAccountID);                     // The identifier of the account the order should be placed for.
       valuemap.setString(O2GRequestParamsEnum.OFFER_ID, sOfferID);                         // The identifier of the instrument the order should be placed for.
       valuemap.setString(O2GRequestParamsEnum.BUY_SELL, sBuySell);                         // The order direction (Constants.Buy for buy, Constants.Sell for sell)
       valuemap.setDouble(O2GRequestParamsEnum.RATE, dOrderRate);                           // The rate at which the order must be filled (below current rate for Buy, above current rate for Sell)
       valuemap.setInt(O2GRequestParamsEnum.AMOUNT, iAmount);                               // The quantity of the instrument to be bought or sold.
       valuemap.setString(O2GRequestParamsEnum.PEG_TYPE_STOP, Constants.Peg.FromClose);     // The peg stop type
       valuemap.setDouble(O2GRequestParamsEnum.PEG_OFFSET_STOP, dPegStopOffset);
       valuemap.setString(O2GRequestParamsEnum.PEG_TYPE_LIMIT, Constants.Peg.FromOpen);     // The peg limit type
       valuemap.setDouble(O2GRequestParamsEnum.PEG_OFFSET_LIMIT, dPegLimitOffset);
       O2GRequest request = factory.createOrderRequest(valuemap);
       mSession.sendRequest(request);
   }
 
    private boolean prepareParamsFromLoginRules(ParamsFromLoginRules params)
    {
        boolean bOK = false;
        O2GResponseReaderFactory factory = mSession.getResponseReaderFactory();
        if (factory == null) {
            return false;
        }
        O2GLoginRules loginRules = mSession.getLoginRules();
        // get first account from login
        O2GResponse accountResponse = loginRules.getTableRefreshResponse(O2GTable.ACCOUNTS);
        O2GAccountsTableResponseReader accountsReader = factory.createAccountsTableReader(accountResponse);
        O2GAccountRow account = accountsReader.getRow(0);
        // get account id
        params.sAccountID = account.getAccountID();
        // get base amount
        params.iBaseAmount = account.getBaseUnitSize();
        params.sOfferID = "";
        params.dAskRate = 0;
        params.dBidRate = 0;
        params.dPointSize = 0;
        // Get offers for EUR/USD
        O2GResponse offersResponse = loginRules.getTableRefreshResponse(O2GTable.OFFERS);
        O2GOffersTableResponseReader offersReader = factory.createOffersTableReader(offersResponse);
        for (int i = 0; i < offersReader.size(); i++)
        {
            O2GOfferRow offer = offersReader.getRow(i);
            if (offer.getInstrument().equals("EUR/USD"))
            {
                params.sOfferID = offer.getOfferID();
                params.dAskRate = offer.getAsk();
                params.dBidRate = offer.getBid();
                params.dPointSize = offer.getPointSize();
                bOK = true;
                break;
            }
        }
        return bOK;
    }

back