Sizing and trade results
A strategy that sizes an entry from the setup's rule and the symbol's tick value, and one that reads the counts and profits of its own trades.
A strategy can ask the tester two kinds of question: how large an entry is (the setup's size rule, and the conversion of money between the currency of the symbol and the currency of the account) and how its trades went so far. The data is generated, so these programs show the calls, not a result worth trading.
Loss limit before entry#
A strategy that asks the setup for its quantity, states the money at risk in the account currency and skips entries that risk more than a limit.
Cuprel
// Check an entry against a loss limit before placing it. The setup's size rule gives the quantity, the symbol's tick value turns a stop distance into money,
// and sizing.to_account states that money in the account's currency (sizing.to_symbol goes the other way).
strategy "Loss limit before entry"
edition = 1
display = price
setup:
capital = 10000
size = sizing.lots(0.2)
input max_loss: float = 1.5, name = "Largest loss accepted", min = 0.1
input length: int = 20, name = "Length", min = 2
let basis = tech.sma(close, length)
let stop_distance = 2 * tech.atr(length)
let qty = sizing.default_qty(close)
let at_risk = sizing.to_account(qty * symbol.ticks(stop_distance) * symbol.tick_value)
if tech.crosses_above(close, basis) and at_risk <= max_loss
orders.enter("Long", side.long)
orders.exit("Long exit", "Long", stop = symbol.snap(close - stop_distance))
plot(basis, name = "Basis", color = color.orange)
plot(sizing.to_symbol(max_loss), name = "Limit in the symbol's currency", display = none)Trade tally#
A channel breakout strategy that reads its own trade counts, profits and position, and writes them into a table.
Cuprel
// A strategy that reads its own results: the counts of closed, winning, losing and open trades, the largest winning trade found with a loop over trades.closed.profit,
// the profit of the open trade, the net profit and the size of the position. The tally is written into a table on the last bar.
strategy "Trade tally"
edition = 1
display = price
setup:
capital = 10000
size = sizing.lots(0.1)
fn row(t: table, line: int, label: text, value: text)
table.cell(t, 0, line, label, h_align = left)
table.cell(t, 1, line, value)
input length: int = 24, name = "Channel length", min = 2
let upper = tech.highest(high, length)[1]
let lower = tech.lowest(low, length)[1]
if close > upper
orders.enter("Long", side.long)
if close < lower
orders.close("Long")
if bar.is_last
mut largest = 0.0
for i in 0 until trades.closed_count
largest = math.max(largest, trades.closed.profit(i))
let open_profit = if trades.open_count > 0 then trades.open.profit(0) else 0.0
let t = table.new(top_right, 2, 5, border_color = color.gray, border_width = 1)
row(t, 0, "Won / lost", f"{trades.won_count} / {trades.lost_count}")
row(t, 1, "Largest win", f"{largest:.2f}")
row(t, 2, "Open profit", f"{open_profit:.2f}")
row(t, 3, "Net profit", f"{stats.net_profit:.2f}")
row(t, 4, "Position", f"{position.size}")
plot(upper, name = "Upper", color = color.green)
plot(lower, name = "Lower", color = color.red)