Create a Stop Order
A stop order is used for limiting losses of the existing position when the market condition is met.
Stop orders can be created for existing trades as well as for existing entry orders. Stop orders created for entry orders remain inactive until the trade is created by the entry order.
Only one stop order can be attached to a position or an the 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 |
||||
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. |
||||
TradeID |
string |
The identifier of the trade to be closed.
The value must be obtained from the "Trades" table, the |
||||
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 The quantity must be equal to the size of the position (see the |
||||
Rate |
number |
The rate at which the order must be filled. The rate must be below the market for sell orders and above the market for buy orders. To specify the stop order price, use:
|
||||
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:
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 The offset must be positive for buy orders and negative for sell orders. |
||||
TrailUpdatePips |
number |
The trailing stop order follows the market in case the market moves in the direction opposite to that of the order (e.g. in the profitable direction for the position). 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 The value is optional. By default, this value is 0. 0 means no-trailing order. |
||||
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 Stop 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 stop against market", "", 50, 1, 1000); strategy.parameters:addBoolean("Trailing", "Flag indicating whether the order must be trailing", "", true); 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 stopValue; local stopSide; if trade.BS == "B" then stopSide = "S"; stopValue = offer.Bid - instance.parameters.Distance * offer.PointSize; else stopSide = "B"; stopValue = offer.Ask + instance.parameters.Distance * offer.PointSize; end if trade.StopOrderID == "" or trade.StopOrderID == nil then valuemap = core.valuemap(); valuemap.Command = "CreateOrder"; valuemap.OrderType = "S"; valuemap.OfferID = trade.OfferID; valuemap.AcctID = trade.AccountID; valuemap.TradeID = trade.TradeID; valuemap.Quantity = trade.Lot; valuemap.Rate = stopValue; valuemap.BuySell = stopSide; if instance.parameters.Trailing then valuemap.TrailUpdatePips = 1; end 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 stop " .. msg, instance.bid:date(NOW)); end elseif math.abs(stopValue - trade.Stop) >= offer.PointSize then valuemap = core.valuemap(); valuemap.Command = "EditOrder"; valuemap.AcctID = trade.AccountID; valuemap.OrderID = trade.StopOrderID; valuemap.Rate = stopValue; if instance.parameters.Trailing then valuemap.TrailUpdatePips = 1; end 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 stop " .. 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 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