The strategy.risk.max_intraday_loss()
function is a critical tool for setting a cap on the losses a strategy can incur within a single trading day. This tutorial explores how to utilize this function effectively in Pine Script.
Syntax and Arguments
The strategy.risk.max_intraday_loss()
function is designed to limit intraday losses either in monetary terms or as a percentage of the strategy’s equity. Its syntax is as follows:
strategy.risk.max_intraday_loss(value, type, alert_message) → void
- value (int/float): This mandatory argument specifies the maximum loss limit. It can be defined in the base currency of the account or as a percentage of the strategy’s maximum intraday equity. For percentage-based limits, the value must be within the 0 to 100 range.
- type (string): Also mandatory, this argument determines the nature of the
value
parameter. It can either bestrategy.percent_of_equity
for percentage-based limits orstrategy.cash
for absolute cash limits. - alert_message (string): An optional parameter that allows for the customization of alert messages when the specified loss limit is breached.
Examples
Example 1: Setting Maximum Intraday Loss as a Percentage of Equity
In this example, the strategy limits its maximum intraday loss to 10% of its equity.
//@version=5 strategy("Intraday Loss Limit Example: Percent of Equity", overlay = false, default_qty_type = strategy.percent_of_equity, default_qty_value = 100) // Input for maximum intraday loss percentage. maxLossPercentage = input.float(10, title="Max Loss Percentage") // Setting the maximum intraday loss. strategy.risk.max_intraday_loss(maxLossPercentage, strategy.percent_of_equity) // Entry condition. if bar_index == 0 strategy.entry("Short Entry", strategy.short) // Tracking and plotting equity change percentage. equityStartOfDay = ta.valuewhen(ta.change(dayofweek) > 0, strategy.equity, 0) equityChangePercent = 100 * ((strategy.equity - equityStartOfDay) / strategy.equity) plot(equityChangePercent, title="Equity Change Percentage") hline(-maxLossPercentage, title="Max Loss Threshold")
Example 2: Setting Maximum Intraday Loss in Absolute Cash Value
Here, the strategy limits the maximum intraday loss to a cash value of 5 units of the base currency.
//@version=5 strategy("Intraday Loss Limit Example: Cash Value", overlay = false) // Input for maximum intraday loss in cash. cashLossLimit = input.float(5, title="Cash Loss Limit") // Setting the maximum intraday loss in cash. strategy.risk.max_intraday_loss(cashLossLimit, strategy.cash) // Entry condition. if bar_index == 0 strategy.entry("Short Entry", strategy.short) // Tracking and plotting price change. priceStartOfDay = ta.valuewhen(ta.change(dayofweek) > 0, open, 0) priceChange = (close - priceStartOfDay) plot(priceChange, title="Price Change") hline(cashLossLimit, title="Cash Loss Limit")
Walkthrough of Code
- Initialization of the Strategy:
- Similar to the first example, but the strategy is named “Intraday Loss Limit Example: Cash Value” and does not specify default quantity types or values since this approach focuses on cash values.
- Setting Up the Input for Maximum Cash Loss:
cashLossLimit = input.float(5, title="Cash Loss Limit")
: Creates an input field for specifying the maximum loss in cash value. The default is set to 5 units of the base currency.
- Applying the Maximum Intraday Loss Limit:
- Uses
strategy.risk.max_intraday_loss(cashLossLimit, strategy.cash)
to apply the cash value limit to intraday losses.
- Uses
- Entry Condition and Price Change Tracking:
- Similar to the first example, this code includes an entry condition for a short position at the start of the chart.
- It calculates the absolute price change from the beginning of the day and plots this alongside the cash loss limit.
Key Features and Takeaways
- The
strategy.risk.max_intraday_loss()
function is pivotal for intraday trading strategies to manage risk effectively by setting limits on potential losses. - It supports both percentage-based and absolute cash value limits, offering flexibility in risk management strategies.
- Including an
alert_message
parameter can enhance monitoring and reaction capabilities during live trading. - This function ensures that trading strategies adhere to predefined risk thresholds, thereby helping to preserve capital and sustain trading operations over the long term.
By integrating the strategy.risk.max_intraday_loss()
function into your Pine Script strategies, you can implement robust risk management rules that safeguard against significant intraday losses.