Create a Market Range Order (Using Price and Distance)

A market range order opens a position at the available market rate in case this rate is in the range specified in the command.

Please note that if hedging is disabled for the account, the command, 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 OR.

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

Rate

number

The center price of the price range in which the order can be filled.

AtMarket

number

The distance from the rate specified by the Rate field to the minimum and maximum prices of the range in which the order can be filled.

The value must be expressed in pips.

For example, if the pip of the instrument is 0.01 and Rate is equal to 95.12 and the AtMarket value is 20, the range will be from 94.92 to 95.32.

GTC

string

Time-In-Force value.

Can be IOC (Immediate-or-Cancel) or FOK (Fill-or-Kill).

The value is optional.

Market orders are IOC 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".

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.

Example: Create Open Market Order and then wait while trade appears [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);
   end
 
   local offer;
   local account;
   local amount;
   local pipsize;
 
   function Prepare()
       instance:name(profile:id() .. "(" .. instance.bid:instrument() .. ")");
 
       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;
       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";
           valuemap.OrderType = "OR";
           valuemap.OfferID = offer;
           valuemap.AcctID = account;
           valuemap.Quantity = amount;
           valuemap.Rate = instance.ask[NOW];
           valuemap.AtMarket = 10;
           valuemap.BuySell = "B";     -- use "S" to sell
 
           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