Pinescript v5 Strategy with dynamic trailing stop losses

When setting trailing stops, it’s important to make sure they are set with market conditions in mind.

This Pine Script strategy is a long-only strategy with dynamic trailing stop losses. Let’s break down what each part of the code does:

  1. strategy(“My Strategy”, overlay=true): This line sets the name of the strategy and specifies that it should be overlaid on the price chart.
  2. length = input(14, minval=1, title=”Length”): This line defines an input variable length with a default value of 14 and a minimum value of 1. It is used to determine the length of the highest high and lowest low calculations.
  3. src = close: This line assigns the closing price of each bar to the src variable.
  4. mult = input(3.0, title=”Multiplier”): This line defines an input variable mult with a default value of 3.0. It represents the multiplier used to calculate the trailing stop loss.
  5. highestHigh = ta.highest(src, length): This line calculates the highest high over the specified length period using the ta.highest function.
  6. lowestLow = ta.lowest(src, length): This line calculates the lowest low over the specified length period using the ta.lowest function.
  7. range = highestHigh – lowestLow: This line calculates the range by subtracting the lowest low from the highest high.
  8. avgRange = ta.sma(range, length): This line calculates the average range over the specified length period using the ta.sma function.
  9. longEntry = ta.crossover(close, highestHigh): This line checks if the current close price crosses above the highest high, indicating a long entry signal.
  10. longExit = ta.crossunder(close, highestHigh – mult * avgRange): This line checks if the current close price crosses below the trailing stop loss level, indicating a long exit signal.
  11. var float lastStopPriceLong = na: This line initializes a variable lastStopPriceLong to track the trailing stop loss price for long positions. na represents a value that is not available.
  12. if (longEntry): This line checks if there is a long entry signal.
  13. strategy.entry(“Long”, strategy.long): This line generates a long entry order using the strategy.entry function.
  14. lastStopPriceLong := highestHigh – mult * avgRange: This line sets the initial trailing stop loss price for long positions.
  15. else if (na(lastStopPriceLong)): This line checks if the lastStopPriceLong variable is not available.
  16. lastStopPriceLong := highestHigh – mult * avgRange: This line sets the initial trailing stop loss price for long positions when there is no existing value for lastStopPriceLong.
  17. lastStopPriceLong := max(lastStopPriceLong, close – mult * avgRange): This line updates the trailing stop loss price for long positions, taking the maximum value between the current trailing stop loss and the new calculated value.
  18. if (longExit): This line checks if there is a long exit signal.
  19. strategy.close(“Long”): This line generates a close order for the long position using the strategy.close function.
  20. lastStopPriceLong := na: This line resets the trailing stop loss price for long positions to a value that is not available.
  21. plot(lastStopPriceLong, title=”Trailing Stop Long”, color=color.red, linewidth=2): This line plots the trailing stop loss price on the chart for visualization purposes.

In summary, this strategy generates long entry signals when the close price crosses above the highest high and exits the long position when the close price crosses below the trailing stop loss level. The trailing stop loss is calculated based on the highest high, average range, and a multiplier


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Discover more from Enclave

Subscribe now to keep reading and get access to the full archive.

Continue reading