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, stop and limit orders must be permitted for the account in order to be used (see also core.host:execute("getTradingProperty", ...) for details about permissions).

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

You can create either the regular stop and limit orders (for non-FIFO accounts) or ELS (entry limit-stop) orders for FIFO accounts. ELS is a pair of entry stop and entry limit orders which are joined into OCO (one-cancels-others) group, so when one of the order is executed, the other order is cancelled. Also, the orders only closes the existing opposite positions and never open the position. Also, the orders are cancelled when there is no opposite position is existing at the moment. However, because of the FIFO rule, these orders closes 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.

Valuemap field

Datatype

Description

EntryLimitStop

string

Set the value of this field to "Y" or "y" to create ELS limit/stop orders on a FIFO account.

Please note, that, unlike the regular stop and limit orders, each order in ELS orders batch has it's own request id. So, the terminal:execute() method will return all created request identifiers, separated by commas. The first is the request id of the main order (entry or market), and then limit order request id (if any), the stop order request id.

Examples: "REQ011,REQ012,REQ013" - both stop and limit orders are specified. "REQ011,REQ012" - only stop or limit order is specified.

The AsyncOperationFinished() method is called for each created order, so if you create entry and entry limit and stop ELS order, the AsyncOperationFinished() method will be called three times.

If you need to find an association between orders and trades, please use CustomID field of the request.

RateStop

number

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 PegPriceOffsetPipsStop 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

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.

PegPriceOffsetPipsStop

number

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

number

The trailing entry order follows the market in case the market moves in the direction opposite to that of the order (i.e. when the distance between the order and the current market price increases).

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

The value is expressed in pips.

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. See core.host:execute("getTradingProperty", ...) for details about permitted types of the trailing mode. In case only dynamic trailing stop is supported, any non-zero value of this parameter will cause creating a dynamic trailing stop.

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

RateLimit

number

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 PegPriceOffsetPipsLimit 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

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.

PegPriceOffsetPipsLimit

number

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 a Trade and Attach Stop and Limit Orders [hide]

   function Init()
       strategy:name("Sample");
       strategy:description("");
 
       strategy.parameters:addString("Account", "Account to trade", "", "");
       strategy.parameters:setFlag("Account", core.FLAG_ACCOUNT);
       strategy.parameters:addInteger("Amount", "Amount in lots to trade", "", 1, 1, 100);
       strategy.parameters:addString("BuySell", "Buy or Sell?", "", "B");
       strategy.parameters:addStringAlternative("BuySell", "Buy", "", "B");
       strategy.parameters:addStringAlternative("BuySell", "Sell", "", "S");
 
       strategy.parameters:addBoolean("SetLimit", "Set Limit Order", "", true);
       strategy.parameters:addInteger("Limit", "Set Limit Distance in pips", "", 30, 1, 10000);
       strategy.parameters:addBoolean("SetStop", "Set Stop Order", "", true);
       strategy.parameters:addInteger("Stop", "Set Stop Distance in pips", "", 30, 1, 10000);
       strategy.parameters:addBoolean("TrailingStop", "Set Trailing Stop Order", "", true);
   end
 
   local offer;
   local account;
   local amount;
   local BuySell;
   local canClose;
 
   function Prepare(onlyName)
       instance:name(profile:id() .. "(" .. instance.bid:instrument() .. ")");
 
       if onlyName then
           return ;
       end
 
       account = instance.parameters.Account;
       local lotSize = core.host:execute("getTradingProperty", "baseUnitSize", instance.bid:instrument(), account);
       amount = lotSize * instance.parameters.Amount;
       offer = core.host:findTable("offers"):find("Instrument", instance.bid:instrument()).OfferID;
       BuySell = instance.parameters.BuySell;
       canClose = core.host:execute("getTradingProperty", "canCreateMarketClose", instance.bid:instrument(), account);
   end
 
   local create = true;
   local trade = false;
   local requestId;
 
   function Update()
       if create then
           create = false;
 
           -- create order
           local valuemap = core.valuemap();
           valuemap.Command = "CreateOrder";
           valuemap.OrderType = "OM";
           valuemap.OfferID = offer;
           valuemap.AcctID = account;
           valuemap.Quantity = amount;
           valuemap.BuySell = BuySell;
 
           if instance.parameters.SetLimit then
               valuemap.PegTypeLimit = "M";
               if BuySell == "B" then
                   valuemap.PegPriceOffsetPipsLimit = instance.parameters.Limit;
               else
                   valuemap.PegPriceOffsetPipsLimit = -instance.parameters.Limit;
               end
           end
 
           if instance.parameters.SetStop then
               valuemap.PegTypeStop = "M";
               if BuySell == "B" then
                   valuemap.PegPriceOffsetPipsStop = -instance.parameters.Stop;
               else
                   valuemap.PegPriceOffsetPipsStop = instance.parameters.Stop;
               end
 
               if instance.parameters.TrailingStop then
                   valuemap.TrailStepStop = 1;
               end
           end
 
           if not(canClose) and (instance.parameters.SetStop or instance.parameters.SetLimit) then
               -- if regular s/l orders aren't allowed - create ELS order
               valuemap.EntryLimitStop = "Y";
           end
 
           local success, msg = terminal:execute(100, valuemap);
 
           if not (success) then
               terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "create order failed:" .. msg, instance.bid:date(NOW));
           else
               requestId = core.parseCsv(msg, ",")[0];
               terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "order sent:" .. requestId, instance.bid:date(NOW));
           end
 
       elseif not(trade) then
           -- check whether trade has been created
           local row;
           row = core.host:findTable("trades"):find("OpenOrderReqID", requestId);
           if row ~= nil then
               trade = true;
               terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "trade created:" .. row.TradeID, instance.bid:date(NOW));
           end
       end
   end
 
   function AsyncOperationFinished(cookie, success, msg)
       if cookie == 100 then
           if not(success) then
               terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "create order failed:" .. msg, instance.bid:date(NOW));
           else
               terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "order created:" .. msg, instance.bid:date(NOW));
           end
       end
   end

back