Create a Limit Order

A limit order is used for locking in profit of the existing position when the market condition is met.

Limit orders can be created for existing trades as well as for existing entry orders. Limit orders created for entry orders remain inactive until the trade is created by the entry order.

Only one limit order can be attached to a position or an entry order.

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.

Valuemap field

Datatype

Description

Command

string

The command. Must be CreateOrder.

OrderType

string

The type of the order. Must be L.

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.

TradeID

string

The identifier of the trade to be closed. The value must be obtained from the "Trades" table, the TradeID column.

BuySell

string

The order direction.

The value must be B - for buy orders and S - for sell orders. The order direction must be opposite to the direction of the order which was used to create the position (see the BS column of the trade).

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 be equal to the size of the position (see the Lot column of the trade).

Rate

number

The rate at which the order must be filled.

The rate must be above the market for sell orders and below the market for buy orders.

To specify the limit order price, use:

  • Rate - if the limit order is created for the existing position.
  • Either Rate or a pair of PegType and PegPriceOffsetPips fields - if the limit order is created for an entry order.

PegType

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.

Please note that "pegged" orders are stored as "pegged" only while the order is attached to an entry order. Once the entry order gets filled and a position is opened, the peg price is automatically converted to a regular market price.

PegPriceOffsetPips

number

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

The offset must be negative for buy orders and positive for sell orders.

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

Example: Create or Change the Limit Order of Chosen Trade [hide]

   function Init()
       strategy:name("Sample");
       strategy:description("");
 
       strategy.parameters:addGroup("Trade");
       strategy.parameters:addString("Trade", "Choose Trade", "", "");
       strategy.parameters:setFlag("Trade", core.FLAG_TRADE);
       strategy.parameters:addInteger("Distance", "Distance in pips to set a limit against market", "", 50, 1, 1000);
   end
 
 
   function Prepare(onlyName)
       local name;
       name = profile:id();
       instance:name(name);
 
       if onlyName then
           return ;
       end
   end
 
   local executing = false;
   local done = false;
 
   function Update()
       if not(core.host:execute("isTableFilled", "trades")) then
           return ;
       end
 
       if not(core.host:execute("isTableFilled", "offers")) then
           return ;
       end
 
       if executing then
           return ;
       end
 
       if done then
           core.host:execute("stop");
           return ;
       end
 
       local trade, offer;
 
       trade = core.host:findTable("trades"):find("TradeID", instance.parameters.Trade);
 
       if trade == nil then
           terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "Trade " .. instance.parameters.Trade .. " disappear", instance.bid:date(NOW));
           core.host:execute("stop");
           return ;
       end
 
       offer = core.host:findTable("offers"):find("OfferID", trade.OfferID);
 
       local limitValue;
       local limitSide;
 
       if trade.BS == "B" then
           limitSide = "S";
           limitValue = offer.Bid + instance.parameters.Distance * offer.PointSize;
       else
           limitSide = "B";
           limitValue = offer.Ask - instance.parameters.Distance * offer.PointSize;
       end
 
       if trade.LimitOrderID == "" or trade.LimitOrderID == nil then
           valuemap = core.valuemap();
           valuemap.Command = "CreateOrder";
           valuemap.OrderType = "L";
           valuemap.OfferID = trade.OfferID;
           valuemap.AcctID = trade.AccountID;
           valuemap.TradeID = trade.TradeID;
           valuemap.Quantity = trade.Lot;
           valuemap.Rate = limitValue;
           valuemap.BuySell = limitSide;
           executing = true;
           success, msg = terminal:execute(200, valuemap);
           if not(success) then
               executing = false;
               done = true;
               terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "Failed create limit " .. msg, instance.bid:date(NOW));
           end
       elseif math.abs(limitValue - trade.Limit) >= offer.PointSize then
           valuemap = core.valuemap();
           valuemap.Command = "EditOrder";
           valuemap.AcctID = trade.AccountID;
           valuemap.OrderID = trade.LimitOrderID;
           valuemap.Rate = limitValue;
           executing = true;
           success, msg = terminal:execute(200, valuemap);
           if not(success) then
               executing = false;
               done = true;
               terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "Failed change limit " .. msg, instance.bid:date(NOW));
           end
       end
   end
 
   function AsyncOperationFinished(id, success, message)
       if id == 200 then
           executing = false;
           done = true;
           if not(success) then
               terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "Failed create/change limit " .. msg, instance.bid:date(NOW));
           else
               terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "Create/change limit order" .. msg, instance.bid:date(NOW));
           end
       end
   end

back