Sidechair

strategy.risk.max_cons_loss_days()

Browse the reference

Stops the strategy after a set number of losing trading days in a row.

Try it in the editor

strategy.risk.max_cons_loss_days(count) counts trading days that ended with a loss. When count of them occur in a row, the emulator cancels pending orders, closes open positions and places no further orders.

A day that ends flat or in profit resets the count to zero. It is a way to switch a system off when conditions have changed against it rather than to guard a single bad day.

Syntax

Pine Script
strategy.risk.max_cons_loss_days(count: simple int, alert_message: simple string (optional)) -> void

Arguments

NameTypeDefaultDescription
countsimple intrequiredThe number of consecutive losing trading days that triggers the stop.
alert_messagesimple stringnaText sent with the alert when the rule stops the strategy. With na, the alert carries the default message.

Returns

Nothing. The rule stays in force for the whole run.

Remarks

  • Call it once, at the top level of the script. The count needs intraday data to separate days; on a daily or higher timeframe each bar is its own day.

Example

Pine ScriptStop after three losing daysOpen in editor
//@version=6
strategy("Losing streak stop", overlay = true)
strategy.risk.max_cons_loss_days(3, "Three losing days in a row")
fast = ta.sma(close, 10)
slow = ta.sma(close, 30)
if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
    strategy.close("Long")
plot(ta.sma(close, 30), "SMA 30", color.orange)

See also

Runs on Sidechair