Maurizio-Ciullo

Indicatore Trailing Stop Alerts

Jan 7th, 2022 (edited)
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // Β© ZenAndTheArtOfTrading
  3.  
  4. //This script is designed as an ATR-based trailing stop tool to assist in managing open positions.
  5. //Once you're involved in a profitable trade, if you add this script to your chart you'll be prompted to select a bar to begin trailing from.
  6. //You can then adjust the candle lookback distance for swing high/lows (7 by default), the ATR multiplier (1.0 by default), and the direction to trail (Long/Short).
  7. //You can also adjust the ATR period in the settings menu if you want to (14 period by default).
  8. //Once the script is added to your chart, it will begin drawing your trailing stop and you can then set up alerts.
  9.  
  10. //Alert Options:
  11. //Any alert() function call: Will trigger an alert for both conditions (trailing stop updated, trailing stop hit)
  12. //Alert Conditions: Trailing Stop Update will trigger whenever the stop is updated, Trailing Stop Hit will trigger whenever the stop is hit.
  13.  
  14. //Note: the alerts will only fire once per bar close and the trailing stop will not update on realtime bars.
  15.  
  16. // @version=5
  17. indicator("Trailing Stop Alerts", overlay=true)
  18.  
  19. // Get user input
  20. trailType           = input.string(title="Trail Type", defval="Long", options=["Long", "Short"], confirm=true)
  21. structureLookback   = input.int(title="Lookback", defval=7, confirm=true)
  22. atrLength           = input.int(title="ATR Length", defval=14)
  23. multiplier          = input.float(title="ATR Multiplier", defval=1.0, confirm=true)
  24. barTime             = input.time(title="Bar Time", defval=timestamp("01 Jan 2021 13:30 +0000"), confirm=true)
  25.  
  26. // Get the current ATR
  27. atr = ta.atr(atrLength) * multiplier
  28.  
  29. // Declare trailing variables
  30. var trailPrice = 0.0
  31. t_trailPrice = trailType == "Long" ? ta.lowest(low, structureLookback) - atr : ta.highest(high, structureLookback) + atr
  32. alertType = -1
  33.  
  34. // Check for trailing stop update
  35. if time >= barTime and barstate.isconfirmed
  36.     // Trail long stop
  37.     if (t_trailPrice > trailPrice or trailPrice == 0.0) and trailType == "Long"
  38.         trailPrice := t_trailPrice
  39.         // Trigger alert
  40.         alertType := 1
  41.         alert(message="Trailing Stop updated for " + syminfo.tickerid + ": " + str.tostring(trailPrice, "#.#####"), freq=alert.freq_once_per_bar_close)
  42.     // Trail short stop
  43.     if (t_trailPrice < trailPrice or trailPrice == 0.0) and trailType == "Short"
  44.         trailPrice := t_trailPrice
  45.         // Trigger alert
  46.         alertType := 1
  47.         alert(message="Trailing Stop updated for " + syminfo.tickerid + ": " + str.tostring(trailPrice, "#.#####"), freq=alert.freq_once_per_bar_close)
  48.        
  49. // If long stop is hit, reset trail stop
  50. if trailPrice != 0.0 and low <= trailPrice and trailType == "Long"
  51.     trailPrice := na
  52.     // Trigger alert
  53.     alertType := 2
  54.     alert(message="Trailing Stop hit for " + syminfo.tickerid, freq=alert.freq_once_per_bar_close)
  55.  
  56. // If short stop is hit, reset trail stop
  57. if trailPrice != 0.0 and high >= trailPrice and trailType == "Short"
  58.     trailPrice := na
  59.     // Trigger alert
  60.     alertType := 2
  61.     alert(message="Trailing Stop hit for " + syminfo.tickerid, freq=alert.freq_once_per_bar_close)
  62.  
  63. // Draw data to chart
  64. plot(trailPrice != 0 ? trailPrice : na, color=color.red, title="Trailing Stop")
  65.  
  66. // Trigger alert conditions
  67. alertcondition(alertType == 1, "Trailing Stop Update", "Trailing Stop updated for {{ticker}}: {{plot_0}}")
  68. alertcondition(alertType == 2, "Trailing Stop Hit", "Trailing Stop hit for {{ticker}}")
Add Comment
Please, Sign In to add comment