Advertisement
Maurizio-Ciullo

ATR Revers System TheArtOfTrading

Oct 21st, 2022
1,217
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 / www.PineScriptMastery.com
  3. // @version=5
  4. strategy("ATR Reversion System",
  5.      overlay=true,
  6.      currency=currency.USD,
  7.      initial_capital=100000,
  8.      default_qty_type=strategy.percent_of_equity,
  9.      default_qty_value=100,
  10.      commission_type=strategy.commission.cash_per_order,
  11.      commission_value=9.95)
  12.  
  13. // Get user input
  14. i_EmaLongLength     = input.int(title="Long-term EMA", defval=200)
  15. i_EmaShortLength    = input.int(title="Short-term EMA Length", defval=20)
  16. i_ATRPeriod         = input.int(title="ATR Period", defval=5)
  17. i_ATRBand           = input.float(title="ATR Band Distance", defval=1)
  18. i_ATRStretch        = input.float(title="ATR Buy Stretch", defval=1)
  19. i_SellBand          = input.string(title="Sell At Band:", defval="Middle", options=["Top", "Middle", "Bottom"])
  20. i_SellSrc           = input.source(title="Sell Price Source", defval=high)
  21.  
  22. // Get indicator values
  23. emaLongTerm     = ta.ema(close, i_EmaLongLength)
  24. emaShortTerm    = ta.ema(close, i_EmaShortLength)
  25. atrValue        = ta.atr(i_ATRPeriod)
  26.  
  27. // Get ATR bands
  28. atrBandTop = emaShortTerm + (atrValue * i_ATRBand)
  29. atrBandBot = emaShortTerm - (atrValue * i_ATRBand)
  30.  
  31. // Define price stretch
  32. float buyLimitPrice = na
  33.  
  34. // Check setup conditions = bar close is below ATR band, above long-term EMA
  35. setupCondition = close < atrBandBot and low > emaLongTerm
  36.  
  37. // Clear any pending limit orders
  38. strategy.cancel_all()
  39.  
  40. // Enter trades on next bar after setup condition is met
  41. if setupCondition
  42.     buyLimitPrice := low - (atrValue * i_ATRStretch)
  43.     strategy.entry("Long", strategy.long, limit=buyLimitPrice)
  44.  
  45. // Get sell price
  46. sellPrice = switch i_SellBand
  47.     "Top"       => atrBandTop
  48.     "Middle"    => emaShortTerm
  49.     "Bottom"    => atrBandBot
  50.  
  51. // Exit trades
  52. if i_SellSrc >= sellPrice or close < emaLongTerm
  53.     strategy.close("Long", comment="Exit trade")
  54.  
  55. // Draw data to chart
  56. plot(emaLongTerm, "EMA Filter", color.red, 2)
  57. plot(emaShortTerm, "ATR Band Middle", color.blue)
  58. plot(atrBandBot, "ATR Band Bottom", color=color.green)
  59. plot(atrBandTop, "ATR Band Top", color=color.new(color.gray, 75))
  60. plot(setupCondition ? buyLimitPrice : na, "Buy Limit", color.lime, 1, plot.style_cross)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement