CODESCRIPT

Risk

9 entries. What each one takes, what it returns, and a working example.

commission(value, type?)

Trade commission. type="percent" (default): value = percent × trade size. type="cash_per_order": value = fixed cash per order. type="cash_per_contract": value = cash per contract. Sets the backtest cost (default 0.04%).

Does not return a value; sets the backtest commission rate to this value.

value
number
Commission value. Percent when type="percent"; a cash amount for the cash types.
type
string
Optional. "percent" (default, percent × trade size) / "cash_per_order" (fixed cash/order) / "cash_per_contract" (cash/contract).

Note

Applied separately to each of entry and exit. "percent" value is a percent of the trade size; "cash_per_order" is a fixed cash amount per order; "cash_per_contract" is a cash amount per contract (unit). Give a value close to your real trading cost to keep results realistic.

CODESCRIPT
commission(0.1)
enterLong(crossover(close, sma(close, 20)))
exit(crossunder(close, sma(close, 20)))
plot(close)

Backtests a simple crossover strategy with 0.1% commission per trade.

CODESCRIPT
commission(2, "cash_per_order")
enterLong(crossover(close, sma(close, 20)))
exit(crossunder(close, sma(close, 20)))
plot(close)

A fixed 2-unit cash commission per order (not a percent) — a flat-fee broker model.

initialCapital(amount)

Backtest starting capital. Called with no argument it does not set anything — it READS the current starting capital.

Does not return a value; sets the backtest's starting capital (the equity scale).

amount
number
The starting capital (account size).

Note

Sets the scale of the equity curve and the absolute profit/loss figures; it does not affect percentage results.

CODESCRIPT
initialCapital(100000)
enterLong(crossover(close, sma(close, 20)))
exit(crossunder(close, sma(close, 20)))
plot(close)

Backtests the strategy with 100000 of starting capital.

isolated(leverage, marginPct)

ISOLATED margin: each trade uses marginPct% of equity as margin; liquidation only loses that margin (not the whole account). Limits risk — does NOT create edge. Aliases (named argument): kaldirac or lev work in place of leverage, marjYuzde in place of marginPct (e.g. isolated(kaldirac=10, marjYuzde=20) gives the exact same result as leverage/marginPct). If marginPct is omitted, it defaults to 100 (the full margin equals full equity; matches referans dil v6 margin_long/short).

Does not return a value; sets isolated margin mode.

leverage
number
Leverage multiplier. The named argument can also be given as kaldirac or lev (both equivalent).
marginPct
number
Percent of equity set aside as margin on each trade. The named argument can also be given as marjYuzde (equivalent). Defaults to 100 if omitted.

Note

Liquidation only takes the allocated margin, not the entire account. It limits risk; it does not create an advantage on its own.

CODESCRIPT
isolated(10, 20)

Switches the position to isolated margin and sets the leverage; a loss stays limited to the money assigned to that position and does not touch the rest of the account.

leverage(leverage, short_leverage?)

Futures leverage (CROSS): return and cost × x. WARNING: liquidation can zero the WHOLE account in one bad bar. Legit use: MODEST leverage on a proven edge. A second argument sets a SEPARATE leverage for short positions (omitted: both directions share one).

Does not return a value; sets the futures (cross) leverage.

leverage
number
Leverage multiplier (e.g. 3) (`x` is also accepted).
short_leverage
Optional. Separate leverage for short positions; falls back to the first value.

Note

Return and cost are multiplied by x. Warning: liquidation can wipe out the entire account on a single bad bar; use it moderately in a validated setup.

CODESCRIPT
leverage(3)

Sets the leverage for trades to three; the same money opens a position three times the size, and both gains and losses are tripled.

maxDrawdown(pct)

Max drawdown guard (halts new entries if exceeded).

Does not return a value; halts opening new positions once drawdown exceeds this percent.

pct
number
The maximum allowed drawdown percent.

Note

A protective check: once the drop from peak equity crosses the limit, new entries are blocked. It does not close an open position, it only stops new risk.

CODESCRIPT
maxDrawdown(20)
enterLong(crossover(close, sma(close, 20)))
exit(crossunder(close, sma(close, 20)))
plot(close)

Backtests the strategy with a guard that takes no new entries once drawdown exceeds 20%.

pyramiding(n)

At most n entries in the same direction.

Returns no value; it sets the cap on multi-entry scaled entries.

n
number
Maximum number of open entry parts allowed in the same direction.

Note

The default is 1 (a single entry portion). When n>1, a repeated entry signal in the same direction adds a new entry portion; once the cap is reached, no more are added.

When to use

For staged/scaled entry (pyramiding) strategies.

Limits

In this phase, multiple entry portions with leverage (lev>1) and an ATR stop are not supported. An entry portion cannot be added to a partially closed position.

CODESCRIPT
pyramiding(3)
enterLong(rsi(close,14) < 40, id='L')
exit(rsi(close,14) > 60)

Scaled entry of up to 3 entry legs while RSI is weak; positionSize +1→+2→+3.

CODESCRIPT
pyramiding(2)
enterLong(crossover(close, sma(close,10)))
exit(crossunder(close, sma(close,10)))

On the second cross within the same trend a second entry part is added (at most 2).

sizeByCash(cash)

Use a fixed cash amount per trade (quantity = amount / price). Switches sizing from %equity to cash-based.

Returns nothing. When called, sizing switches from the default %equity to cash-based; quantity taken = amount / price.

cash
number
Fixed cash amount to use on each trade. The named argument can also be given as `cash` (equivalent).

Note

If never called, the default %equity sizing is kept. defaultEntryQty takes a fixed quantity; sizeByCash instead divides a fixed amount by price to set the quantity. If margin is insufficient the trade is skipped.

CODESCRIPT
sizeByCash(1000)
enterLong(crossover(close, sma(close, 20)))
exit(crossunder(close, sma(close, 20)))
plot(close)

Uses 1000 of cash on each trade (quantity = 1000 / price).

sizeByEquity(pct)

Position size = % of equity.

Does not return a value; ties the position size to this fraction of equity.

pct
number
The percent of equity to use on each trade (0-100).

Note

The value is clamped to 0-100. To trade with a set percent of the account instead of a fixed lot — profit/loss scales by this fraction.

CODESCRIPT
sizeByEquity(50)
enterLong(crossover(close, sma(close, 20)))
exit(crossunder(close, sma(close, 20)))
plot(close)

Backtests the strategy using 50% of equity on each trade.

slippage(pct)

Slippage (%) — realistic fill cost.

Does not return a value; sets slippage to this value (added to the trade cost).

pct
number
Slippage per trade (one side, percent).

Note

Models the order filling slightly worse than the expected price. Makes results more realistic in thin or fast markets.

CODESCRIPT
slippage(0.05)
enterLong(crossover(close, sma(close, 20)))
exit(crossunder(close, sma(close, 20)))
plot(close)

Backtests the strategy with 0.05% slippage per trade.