Skip to content
Nexus Prop LLC

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.

order.buy

functionNexuScript 1.1

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

NameTypeDefaultDescription
idStringrequiredYour 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.
qtyNumber1How 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.
limitNumberautomaticA limit price: the order buys at this price or lower. With neither limit nor stop the order is at market.
stopNumberautomaticA stop price: the order wakes up when the price rises to it. With limit too, it becomes a stop-limit order.
take_profitNumberautomaticThe price of a profit target placed once the entry fills.
stop_lossNumberautomaticThe price of a protective stop placed once the entry fills.
durationString"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_step steps)
  • NS452a price that is given is missing or 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)
end

See also

order.sellorder.cancelposition.sizestrategy

order.sell

functionNexuScript 1.1

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

NameTypeDefaultDescription
idStringrequiredYour 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.
qtyNumber1How 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.
limitNumberautomaticA limit price: the order sells at this price or higher. With neither limit nor stop the order is at market.
stopNumberautomaticA stop price: the order wakes up when the price falls to it. With limit too, it becomes a stop-limit order.
take_profitNumberautomaticThe price of a profit target placed once the entry fills.
stop_lossNumberautomaticThe price of a protective stop placed once the entry fills.
durationString"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_step steps)
  • NS452a price that is given is missing or infinite
  • NS453more than 100 order calls on one bar

Example

if position.size > 0
    order.sell("Target", qty = position.size, limit = position.avg + 20)
end

See also

order.buyorder.closeposition.avg

order.cancel

functionNexuScript 1.1

Asks to cancel the working orders this script placed under one id.

Syntax

order.cancel(id::String)

Parameters

NameTypeDefaultDescription
idStringrequiredThe 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)
end

See also

order.cancel_allorder.buyorders

order.cancel_all

functionNexuScript 1.1

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()
end

See also

order.cancelorder.closeorders

order.close

functionNexuScript 1.1

Asks to close the whole position on the chart's symbol at market.

Syntax

order.close(id::String = "")

Parameters

NameTypeDefaultDescription
idString""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()
end

See also

order.cancel_allposition.size

order.last_error

valueNexuScript 1.1

Why the last order call of the previous run was refused, or empty text.

Syntax

order.last_error::String

Returns

String The reason the chart refused an order call in the previous run (the last such refusal), or "" when nothing was refused.

Details

It changes from run to run, so read it inside on bar, never in setup. It has no history: store it with let or state first if you need an earlier value.

Missing values

It is text, never missing; with nothing to report it is "".

Example

if order.last_error != ""
    print("Order refused: $(order.last_error)")
end

See also

order.buyprint

position.size

valueNexuScript 1.1

The net position on the chart's symbol.

Syntax

position.size::Number

Returns

Number Above 0 when long, below 0 when short, 0 when flat.

Details

It changes from run to run, so read it inside on bar, never in setup. It has no history: store it with let or state first if you need an earlier value.

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))
end

See also

position.avgposition.open_pnlorder.close

position.avg

valueNexuScript 1.1

The average price of the position on the chart's symbol.

Syntax

position.avg::Number

Returns

Number The average entry price; missing when flat.

Details

It changes from run to run, so read it inside on bar, never in setup. It has no history: store it with let or state first if you need an earlier value.

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

position.sizeposition.open_pnl

position.open_pnl

valueNexuScript 1.1

The open profit or loss of the position on the chart's symbol.

Syntax

position.open_pnl::Number

Returns

Number The profit (above 0) or loss (below 0) not yet taken; 0 when flat.

Details

It changes from run to run, so read it inside on bar, never in setup. It has no history: store it with let or state first if you need an earlier value.

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

position.sizeaccount.open_pnl

account.balance

valueNexuScript 1.1

The account's balance.

Syntax

account.balance::Number

Returns

Number The balance, not counting open profit or loss.

Details

It changes from run to run, so read it inside on bar, never in setup. It has no history: store it with let or state first if you need an earlier value.

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

account.equityaccount.day_pnl

account.equity

valueNexuScript 1.1

The account's balance plus its open profit or loss.

Syntax

account.equity::Number

Returns

Number The equity of the account.

Details

It changes from run to run, so read it inside on bar, never in setup. It has no history: store it with let or state first if you need an earlier value.

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

account.balanceaccount.open_pnl

account.day_pnl

valueNexuScript 1.1

The account's profit or loss for the trading day.

Syntax

account.day_pnl::Number

Returns

Number The day's profit (above 0) or loss (below 0).

Details

It changes from run to run, so read it inside on bar, never in setup. It has no history: store it with let or state first if you need an earlier value.

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()
end

See also

account.balanceorder.close

account.open_pnl

valueNexuScript 1.1

The open profit or loss across the whole account.

Syntax

account.open_pnl::Number

Returns

Number The profit or loss not yet taken on every open position.

Details

It changes from run to run, so read it inside on bar, never in setup. It has no history: store it with let or state first if you need an earlier value.

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

position.open_pnlaccount.equity

account.buying_power

valueNexuScript 1.1

How much the account can still use to open trades.

Syntax

account.buying_power::Number

Returns

Number The buying power the chart reports.

Details

It changes from run to run, so read it inside on bar, never in setup. It has no history: store it with let or state first if you need an earlier value.

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")
end

See also

account.balanceorder.buy

orders

valueNexuScript 1.1

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

It changes from run to run, so read it inside on bar, never in setup. It has no history: store it with let or state first if you need an earlier value.

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()
end

See also

order.cancelorder.cancel_alltuplefor

order.exit

functionNexuScript 1.5

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

NameTypeDefaultDescription
idStringrequiredYour 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.
qtyNumberautomaticHow many contracts to take off. Leave it out to use percent.
percentNumberautomaticThe 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.
limitNumberautomaticA limit price: a profit target.
stopNumberautomaticA 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 missing or 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)
end

See also

order.closeorder.buyposition.avg

position.bars

valueNexuScript 1.5

How many bars the position has been open: 0 on the bar whose open filled the entry.

Syntax

position.bars::Number

Returns

Number A whole number of bars; missing when flat.

Details

It changes from run to run, so read it inside on bar. It has no history: store it with let first.

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

See also

position.sizeorder.closebar.index