Create an Entry Stop Order

A regular entry order opens a position when the specified market condition is met.

A netting entry order closes all positions for the specified instrument and account which are in the direction (buy or sell) opposite to the direction of the entry order.

A stop entry order with a sell direction is filled when the market is below the rate specified in the order.

A stop entry order with a buy direction is filled when the market is above the rate specified in the order.

Please note that if hedging is disabled for the account, the order, first, closes existing opposite positions for the same account and instrument and only then opens a new position in the remaining amount.

Valuemap field

Datatype

Description

Command

string

The command. Must be CreateOrder.

OrderType

string

The type of the order. Must be SE.

OfferID

string

The identifier of the instrument the order should be placed for. The value must be obtained from the "Offers" table, the OfferID column.

AcctID

string

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.

BuySell

string

The order direction.

The value must be B - for buy orders and S - for sell orders.

Quantity

number

The quantity of the instrument to be bought or sold. The quantity is expressed in contracts and must be divisible by the value of the lot size (see baseUnitSize trading option in the following call: core.host:execute("getTradingProperty", ...)).

The quantity must not be specified in case the value of the NetQtyFlag field is y.

NetQtyFlag

string

The net quantity flag. The value can be either y or n. The value is optional. The default value is n.

An entry order in the netting mode does not create any positions but 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.

Rate

number

The rate at which the order must be filled.

The sell order rate must be below the market. The buy order rate must be above the market.

TrailUpdatePips

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.

GTC

string

Time-In-Force value.

Can be GTC (Good-Till-Cancelled) or DAY (Good-Till-End-Of-Day).

The value is optional.

Conditional orders are GTC orders by default.

CustomID

string

The custom identifier of the order. This value will be populated into all order-related QTXT columns of the following tables: "Orders", "Trades", and "Closed Trades".

ContingencyID

string

Specify the identifier of the existing contingency group to include the order into the that group. Don't specify the parameter if the order must not be included into any contingency group.

You can also create a pair of stop and limit orders for the trade using the same command. Please refer to Attach Stop and/or Limit Orders to the Command for details.

Note: If-Then Orders. If the order is a sell stop entry order with the order amount equal to zero. This order can be created below the market, and will be triggered when the market crosses the order rate down. Such order does not create or close any trades. If the order is used as a root order of the OTO, such order will activate all other orders included into the OTO group, so, you can use it for conditional activation of other orders.

See Limit Entry order for example on how to create a regular entry stop order.

Example: Create and Maintain Net Entry Stop at Zero-Profit Point of All Positions on Chosen Account/Instrument [hide]

   function Init()
       strategy:name("Sample");
       strategy:description("");
 
       strategy.parameters:addGroup("Account");
       strategy.parameters:addString("Account", "Choose Account", "", "");
       strategy.parameters:setFlag("Account", core.FLAG_ACCOUNT);
   end
 
   local OfferID;
   local AccountID;
 
   function Prepare(onlyName)
       local name;
       name = profile:id();
       instance:name(name);
 
       if onlyName then
           return ;
       end
 
       AccountID = instance.parameters.Account;
       OfferID = core.host:findTable("offers"):find("Instrument", instance.bid:instrument()).OfferID;
 
   end
 
   local executing = false;
 
   function Update()
       if not(core.host:execute("isTableFilled", "trades")) then
           return ;
       end
 
       if executing then
           return ;
       end
 
       -- find average weighted price of all long and short positions on
       -- chosen instrument/amount
       local enum, row;
       local longCount, shortCount;
       local sumLongAmount, sumLongPrices;
       local sumShortAmount, sumShortPrices;
 
       longCount = 0;
       sumLongAmount = 0;
       sumLongPrices = 0;
 
       shortCount = 0;
       sumShortAmount = 0;
       sumShortPrices = 0;
 
       enum = core.host:findTable("trades"):enumerator();
       row = enum:next();
       while row ~= nil do
           if row.AccountID == AccountID and
              row.OfferID == OfferID then
               if row.BS == "B" then
                   sumLongAmount = sumLongAmount + row.Lot;
                   sumLongPrices = sumLongPrices + row.Lot * row.Open;
                   longCount = longCount + 1;
               else
                   sumShortAmount = sumShortAmount + row.Lot;
                   sumShortPrices = sumShortPrices + row.Lot * row.Open;
                   shortCount = shortCount + 1;
               end
           end
           row = enum:next();
       end
 
       if longCount > 0 then
           setOrUpdateNetStop("S", sumLongAmount, sumLongPrices);
       end
 
       if shortCount > 0 then
           setOrUpdateNetStop("B", sumShortAmount, sumShortPrices);
       end
   end
 
   function setOrUpdateNetStop(side, amount, prices)
       -- average weighed open price
       local avgPrice = prices / amount;
 
       if side == "S" then
           -- price must be below the market for sell stop
           if avgPrice >= instance.bid[NOW] then
               return ;
           end
       else
           -- price must be above the market for buy stop
           if avgPrice <= instance.ask[NOW] then
               return ;
           end
       end
 
       -- try to find the net stop
       local enum, row;
 
           local enum, row;
       local order = nil;
       local rate;
 
       local enum, row;
 
       enum = core.host:findTable("orders"):enumerator();
       row = enum:next();
       while (row ~= nil) do
           if row.OfferID == OfferID and
              row.AccountID == AccountID and
              row.BS == side and
              row.NetQuantity and
              row.Type == "SE" then
               order = row.OrderID;
               rate = row.Rate;
           end
           row = enum:next();
       end
 
       if order == nil then
           valuemap = core.valuemap();
           valuemap.Command = "CreateOrder";
           valuemap.OrderType = "SE";
           valuemap.OfferID = OfferID;
           valuemap.AcctID = AccountID;
           valuemap.NetQtyFlag = "y";
           valuemap.Rate = avgPrice;
           valuemap.BuySell = side;
           executing = true;
           success, msg = terminal:execute(200, valuemap);
           if not(success) then
               executing = false;
               terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "Failed create stop " .. msg, instance.bid:date(NOW));
           end
       else
           if math.abs(avgPrice - rate) >= instance.bid:pipSize() then
               -- stop exists
               valuemap = core.valuemap();
               valuemap.Command = "EditOrder";
               valuemap.OfferID = OfferID;
               valuemap.AcctID = AccountID;
               valuemap.OrderID = order;
               valuemap.Rate = avgPrice;
               executing = true;
               success, msg = terminal:execute(200, valuemap);
               if not(success) then
                   executing = false;
                   terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "Failed change stop " .. msg, instance.bid:date(NOW));
               end
           end
       end
 
   end
 
   function AsyncOperationFinished(id, success, msg)
       if id == 200 then
           executing = false;
           if not(success) then
               terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "Failed create/change stop " .. msg, instance.bid:date(NOW));
           else
               terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "Create/change stop order" .. msg, instance.bid:date(NOW));
           end
       end
   end

back