NexuScript 1.5
Strategies
Order calls and reads, in strategy scripts only. An order call only records what the script asks for; backtests read it, nothing is sent.
Asks for an order that buys, at market, at a limit or on a stop.
Syntax
order.buy(id::String, qty::Number = 1, limit::Number = automatic, stop::Number = automatic, take_profit::Number = automatic, stop_loss::Number = automatic, duration::String = "DAY")Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | String | required | Your name for the order, such as "Long". It must not be blank. Several orders may share an id, and order.cancel with that id cancels all of them. |
qty | Number | 1 | How many contracts to buy: a whole number, 1 or more. On a symbol traded in fractions it is a whole number of symbol.qty_step steps. |
limit | Number | automatic | A limit price: the order buys at this price or lower. With neither limit nor stop the order is at market. |
stop | Number | automatic | A stop price: the order wakes up when the price rises to it. With limit too, it becomes a stop-limit order. |
take_profit | Number | automatic | The price of a profit target placed once the entry fills. |
stop_loss | Number | automatic | The price of a protective stop placed once the entry fills. |
duration | String | "DAY" | How long the order works: "DAY" for the trading day, "GTC" until it fills or is cancelled. One of "DAY", "GTC". |
Details
An order call only records the order the script asks for on this bar. A backtest simulates it; nothing is sent to any broker from a script today. With neither limit nor stop it is a market order, with limit a limit order, with stop a stop order and with both a stop-limit order. take_profit and stop_loss form the bracket placed once the entry fills. It is an effect: it may run in any if, loop or function called from on bar, but not in setup or inside a higher block (NS230). Only one call of each kind and id counts per bar; a repeat on the same bar is ignored. A bar may make at most 100 order calls.
Units and time zones
Prices in the symbol's quote currency; quantity in contracts.
Missing values
A price that is given must be a real number: a missing one stops the script with NS452, so a missing limit never turns into a market order. Check prices with ismissing first.
Errors
- NS450the id is blank
- NS451the quantity is not a whole number of contracts, 1 or more (or of
symbol.qty_stepsteps) - NS452a price that is given is
missingor infinite - NS453more than 100 order calls on one bar
Example
let fast = ta.ema(close, 9)
let slow = ta.ema(close, 21)
let atr = ta.atr(14)
let up = ta.crossover(fast, slow)
if up && position.size == 0 && !ismissing(atr)
order.buy("Long", qty = 1, stop_loss = close - 2 * atr, take_profit = close + 3 * atr)
endSee also
Asks for an order that sells, at market, at a limit or on a stop.
Syntax
order.sell(id::String, qty::Number = 1, limit::Number = automatic, stop::Number = automatic, take_profit::Number = automatic, stop_loss::Number = automatic, duration::String = "DAY")Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | String | required | Your name for the order, such as "Long". It must not be blank. Several orders may share an id, and order.cancel with that id cancels all of them. |
qty | Number | 1 | How many contracts to sell: a whole number, 1 or more. On a symbol traded in fractions it is a whole number of symbol.qty_step steps. |
limit | Number | automatic | A limit price: the order sells at this price or higher. With neither limit nor stop the order is at market. |
stop | Number | automatic | A stop price: the order wakes up when the price falls to it. With limit too, it becomes a stop-limit order. |
take_profit | Number | automatic | The price of a profit target placed once the entry fills. |
stop_loss | Number | automatic | The price of a protective stop placed once the entry fills. |
duration | String | "DAY" | How long the order works: "DAY" for the trading day, "GTC" until it fills or is cancelled. One of "DAY", "GTC". |
Details
An order call only records the order the script asks for on this bar. A backtest simulates it; nothing is sent to any broker from a script today. It works like order.buy on the selling side: it can open a short or take profit on a long. It is an effect: it may run in any if, loop or function called from on bar, but not in setup or inside a higher block (NS230). Only one call of each kind and id counts per bar; a repeat on the same bar is ignored. A bar may make at most 100 order calls.
Units and time zones
Prices in the symbol's quote currency; quantity in contracts.
Missing values
A price that is given must be a real number: a missing one stops the script with NS452. Check prices with ismissing first.
Errors
- NS450the id is blank
- NS451the quantity is not a whole number of contracts, 1 or more (or of
symbol.qty_stepsteps) - NS452a price that is given is
missingor infinite - NS453more than 100 order calls on one bar
Example
if position.size > 0
order.sell("Target", qty = position.size, limit = position.avg + 20)
endSee also
Asks to cancel the working orders this script placed under one id.
Syntax
order.cancel(id::String)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | String | required | The id given to order.buy or order.sell. Every working order with that id is cancelled. |
Details
An order call only records the order the script asks for on this bar. A backtest simulates it; nothing is sent to any broker from a script today. It is an effect: it may run in any if, loop or function called from on bar, but not in setup or inside a higher block (NS230). Only one call of each kind and id counts per bar; a repeat on the same bar is ignored. A bar may make at most 100 order calls.
Missing values
It takes text, never a number, so it never sees missing.
Errors
- NS450the id is blank
- NS453more than 100 order calls on one bar
Example
let low10 = ta.lowest(low, 10)
if position.size != 0
order.cancel("Dip")
elseif !ismissing(low10)
order.buy("Dip", limit = low10)
endSee also
Asks to cancel every working order on the chart's symbol in the account.
Syntax
order.cancel_all()Details
An order call only records the order the script asks for on this bar. A backtest simulates it; nothing is sent to any broker from a script today. It is an effect: it may run in any if, loop or function called from on bar, but not in setup or inside a higher block (NS230). Only one call of each kind and id counts per bar; a repeat on the same bar is ignored. A bar may make at most 100 order calls.
Missing values
It takes no values, so it never sees missing.
Errors
- NS453more than 100 order calls on one bar
Example
let weak = close < ta.sma(close, 50)
if weak
order.cancel_all()
endSee also
Asks to close the whole position on the chart's symbol at market.
Syntax
order.close(id::String = "")Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | String | "" | NexuScript 1.5: close only when the position was opened by the entry with this id (order.buy("Long")); left out or "", close whatever is open. |
Details
An order call only records the order the script asks for on this bar. A backtest simulates it; nothing is sent to any broker from a script today. Working orders stay; pair it with order.cancel_all to leave nothing behind. With an id, nothing happens when the open position came from another entry. It is an effect: it may run in any if, loop or function called from on bar, but not in setup or inside a higher block (NS230). Only one call of each kind and id counts per bar; a repeat on the same bar is ignored. A bar may make at most 100 order calls.
Missing values
It takes only text, so it never sees missing.
Errors
- NS453more than 100 order calls on one bar
Example
let rsi = ta.rsi(close, 14)
if position.size != 0 && rsi > 75
order.close()
endSee also
Why the last order call of the previous run was refused, or empty text.
Syntax
order.last_error::StringReturns
String The reason the chart refused an order call in the previous run (the last such refusal), or "" when nothing was refused.
Details
Missing values
It is text, never missing; with nothing to report it is "".
Example
if order.last_error != ""
print("Order refused: $(order.last_error)")
endSee also
The net position on the chart's symbol.
Syntax
position.size::NumberReturns
Number Above 0 when long, below 0 when short, 0 when flat.
Details
Units and time zones
Contracts.
Missing values
It is never missing: with no position known it is 0.
Example
if position.size > 0
background(color.fade(color.green, 90))
elseif position.size < 0
background(color.fade(color.red, 90))
endSee also
The average price of the position on the chart's symbol.
Syntax
position.avg::NumberReturns
Number The average entry price; missing when flat.
Details
Units and time zones
The symbol's quote currency.
Missing values
It is missing when there is no position.
Example
plot("Entry price", position.avg, color = color.orange, style = "step")See also
The open profit or loss of the position on the chart's symbol.
Syntax
position.open_pnl::NumberReturns
Number The profit (above 0) or loss (below 0) not yet taken; 0 when flat.
Details
Units and time zones
The account's currency.
Missing values
It is missing when the chart does not know it; 0 when flat.
Example
plot("Open profit", position.open_pnl, style = "histogram", base = 0)See also
The account's balance.
Syntax
account.balance::NumberReturns
Number The balance, not counting open profit or loss.
Details
Units and time zones
In the account's currency.
Missing values
It is missing when the chart does not know the account's values, as in a backtest with no account.
Example
plot("Balance", account.balance)See also
The account's balance plus its open profit or loss.
Syntax
account.equity::NumberReturns
Number The equity of the account.
Details
Units and time zones
In the account's currency.
Missing values
It is missing when the chart does not know the account's values, as in a backtest with no account.
Example
let swing = account.equity - account.balance
plot("Equity swing", swing, style = "histogram", base = 0)See also
The account's profit or loss for the trading day.
Syntax
account.day_pnl::NumberReturns
Number The day's profit (above 0) or loss (below 0).
Details
Units and time zones
In the account's currency.
Missing values
It is missing when the chart does not know the account's values, as in a backtest with no account.
Example
if account.day_pnl < -500
order.close()
order.cancel_all()
endSee also
The open profit or loss across the whole account.
Syntax
account.open_pnl::NumberReturns
Number The profit or loss not yet taken on every open position.
Details
Units and time zones
In the account's currency.
Missing values
It is missing when the chart does not know the account's values, as in a backtest with no account.
Example
plot("Account open profit", account.open_pnl)See also
How much the account can still use to open trades.
Syntax
account.buying_power::NumberReturns
Number The buying power the chart reports.
Details
Units and time zones
In the account's currency.
Missing values
It is missing when the chart does not know the account's values, as in a backtest with no account.
Example
let roomy = account.buying_power > 5000
let up = ta.crossover(close, ta.sma(close, 20))
if roomy && up
order.buy("Long")
endSee also
The working orders on the chart's symbol, one named tuple each.
Syntax
orders::Array{(id = String, entry = String, side = String, type = String, qty = Number, price = Number, stop = Number)}Returns
Array{(id = String, entry = String, side = String, type = String, qty = Number, price = Number, stop = Number)} An array with one entry per working order. id is the order's own id, entry the script id it belongs to (a profit target or protective stop carries its entry's id), side is "buy" or "sell", type is "market", "limit", "stop" or "stop_limit", qty the size, price the limit price and stop the stop price.
Details
Units and time zones
qty in contracts; price and stop in the symbol's quote currency.
Missing values
price or stop is missing when the order has none. With no orders known the array is empty.
Example
for o in orders
if o.type == "limit"
marker(o.side, price = o.price, shape = "circle")
end
end
if length(orders) > 3
order.cancel_all()
endSee also
Asks for an order that only reduces the position: all of it or part, at market, at a limit or on a stop.
Syntax
order.exit(id::String, qty::Number = automatic, percent::Number = automatic, limit::Number = automatic, stop::Number = automatic)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | String | required | Your name for the exit, such as "TP half". Calling it again with the same id moves the working exit; once it has filled, the same id does nothing more until the position is flat again. |
qty | Number | automatic | How many contracts to take off. Leave it out to use percent. |
percent | Number | automatic | The share of the position to take off when the exit fills, above 0 and at most 100, rounded to whole contracts (at least one). 100 when neither qty nor percent is given. |
limit | Number | automatic | A limit price: a profit target. |
stop | Number | automatic | A stop price: a protective stop. With neither limit nor stop the exit is at market. |
Details
An order call only records the order the script asks for on this bar. A backtest simulates it. The exit sells a long or buys back a short and never opens a position; it is cancelled when the position goes flat. It is an effect: allowed in any if, loop or function called from on bar, never in setup or a higher block.
Units and time zones
Prices in the symbol's quote currency; quantity in contracts.
Missing values
A price that is given must be a real number: a missing one stops the script with NS452. Check prices with ismissing first.
Errors
- NS450the id is blank
- NS451both qty and percent are given, the percent is outside 0 to 100, or qty is not a whole number of contracts
- NS452a price that is given is
missingor infinite - NS453more than 100 order calls on one bar
Example
if position.size > 0 && !ismissing(position.avg)
order.exit("TP half", percent = 50, limit = position.avg * 1.015)
endSee also
How many bars the position has been open: 0 on the bar whose open filled the entry.
Syntax
position.bars::NumberReturns
Number A whole number of bars; missing when flat.
Details
Units and time zones
Bars.
Missing values
It is missing when there is no position, or when the chart does not know when it opened.
Example
let held = position.bars
if !ismissing(held) && held >= 32
order.close()
end