Sidechair

strategy.risk.max_intraday_loss()

Browse the reference

Stops trading for the rest of the day once the day's loss reaches a set amount.

Try it in the editor

strategy.risk.max_intraday_loss(value, type) caps the loss allowed within a single trading day. When the day's loss reaches the limit, the emulator cancels pending orders, closes open positions and places no new orders until the next trading day starts.

The loss is measured against the equity at the start of the day, as a percentage when type is strategy.percent_of_equity and as an amount in the account currency when it is strategy.cash.

Syntax

Pine Script
strategy.risk.max_intraday_loss(value: simple int/float, type: simple string, alert_message: simple string (optional)) -> void

Arguments

NameTypeDefaultDescription
valuesimple int/floatrequiredThe largest loss allowed in one trading day, in the unit chosen by type. Must be positive.
typesimple stringrequiredHow value is read: strategy.percent_of_equity for a percentage of equity, or strategy.cash for an amount in the account currency.
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. It needs an intraday chart to mean anything; on a daily chart each bar is a whole day.
  • Trading resumes on the next day, which is the difference from strategy.risk.max_drawdown.

Example

Pine ScriptTwo percent daily loss limitOpen in editor
//@version=6
strategy("Daily loss cap", overlay = true)
strategy.risk.max_intraday_loss(2, strategy.percent_of_equity)
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")
bgcolor(session.isfirstbar ? color.new(color.blue, 90) : na)

See also

Runs on Sidechair