Create a Market Order
A market order opens a position at the specified market rate in case such rate is available on the market.
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 |
OrderType |
string |
The type of the order. Must be |
OfferID |
string |
The identifier of the instrument the order should be placed for.
The value must be obtained from the "Offers" table, the |
AcctID |
string |
The identifier of the account the order should be placed for.
The value must be obtained from the "Accounts" table, the 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 |
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 |
Rate |
number |
The rate at which the order must be filled. |
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; 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; 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 = "O"; valuemap.OfferID = offer; valuemap.AcctID = account; valuemap.Quantity = amount; valuemap.Rate = instance.ask[NOW]; 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