Create an Entry Limit 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 limit entry order with a sell direction is filled when the market is above the rate specified in the order.

A limit entry order with a buy direction is filled when the market is below 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 LE.

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 above the market. The buy order rate must be below 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 limit entry order with the order amount equal to zero. This order can be created above the market, and will be triggered when the market crosses the order rate up. 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.

Create Entry Stop or Entry Limit order for chosen account/instrument on chosen distance to market. [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:addInteger("Distance", "Distance to market", "", 100, -1000, 1000);
       strategy.parameters:addString("BuySell", "Buy or Sell?", "", "B");
       strategy.parameters:addStringAlternative("BuySell", "Buy", "", "B");
       strategy.parameters:addStringAlternative("BuySell", "Sell", "", "S");
   end
 
   local offer;
   local account;
   local amount;
   local pipsize;
   local BuySell;
   local distance;
 
   function Prepare()
       instance:name(profile:id() .. "(" .. instance.bid:instrument() .. ")");
 
       account = instance.parameters.Account;
       BuySell = instance.parameters.BuySell;
       distance = instance.parameters.Distance;
 
       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;
       pipsize = instance.bid:pipSize();
 
   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";
 
           -- get the order type
           if BuySell == "B" and distance < 0 then
               valuemap.OrderType = "LE";
           elseif BuySell == "B" and distance > 0 then
               valuemap.OrderType = "SE";
           elseif BuySell == "S" and distance < 0 then
               valuemap.OrderType = "SE";
           elseif BuySell == "S" and distance > 0 then
               valuemap.OrderType = "LE";
           end
 
           if distance >= 0 then
               valuemap.Rate = instance.ask[NOW] + distance * pipsize;
           else
               valuemap.Rate = instance.bid[NOW] + distance * pipsize;
           end
 
           valuemap.OfferID = offer;
           valuemap.AcctID = account;
           valuemap.Quantity = amount;
           valuemap.BuySell = BuySell;
 
           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));
               trade = true;
           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