Advertisement
xmd79

True Range Moving Average Deviation

Sep 13th, 2023
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © gabasco
  3.  
  4. //@version=5
  5. indicator(title="True Range Moving Average Deviation", shorttitle="TRMAD", precision=2, timeframe="", overlay=false)
  6.  
  7. // 1. Input
  8. themeInput = input.string("Standard", title="Theme      ", options=["Standard", "Light"], inline = 'theme', group="General Settings", display=display.none)
  9. colorLineInput = input.color(color.new(color.black, 10), title= "", inline = 'theme', group="General Settings", display=display.none)
  10. obValueInput = input.float(3, title="Overbought", inline = 'ob', group="General Settings", display=display.none)
  11. obColorInput = input(#089981, title="", inline = 'ob', group="General Settings", display=display.none)
  12. osValueInput = input.float(-3, title="Oversold    ", inline = 'os', group="General Settings", display=display.none)
  13. osColorInput = input(#f23645, title="", inline = 'os', group="General Settings", display=display.none)
  14.  
  15. trTypeInput = input.string("SMA", title="TR Type", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="TR Settings", display=display.none)
  16. trLengthInput = input.int(14, "TR Length", minval=2, group="TR Settings", display=display.none)
  17.  
  18. maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings", display=display.none)
  19. maLengthInput = input.int(20, "MA Length", minval=2, group="MA Settings", display=display.none)
  20.  
  21.  
  22. // 2. Formula
  23. ma(source, length, type) =>
  24. switch type
  25. "SMA" => ta.sma(source, length)
  26. "EMA" => ta.ema(source, length)
  27. "SMMA (RMA)" => ta.rma(source, length)
  28. "WMA" => ta.wma(source, length)
  29. "VWMA" => ta.vwma(source, length)
  30.  
  31. // 3. Calculation
  32. atr = ma(ta.tr(true), trLengthInput, trTypeInput)
  33. distance = close - ma(close, maLengthInput, maTypeInput)
  34. trmad = nz(distance/atr)
  35.  
  36. // 4. Drawings
  37. // a. TRMAD
  38. trmadPlot = plot(trmad, "TRMAD", themeInput == "Standard" ? trmad > obValueInput ? obColorInput : trmad < osValueInput ? osColorInput : colorLineInput : colorLineInput, editable = false)
  39.  
  40. // b. Zero line
  41. zeroPlot = plot(0, editable=false, display=display.none)
  42. obPlot = plot(trmad > obValueInput ? trmad : na, editable=false, display=display.none)
  43. osPlot = plot(trmad < osValueInput ? trmad : na, editable=false, display=display.none)
  44.  
  45. // c. Fills
  46. // - Above/below zero line
  47. fill(zeroPlot, trmadPlot, themeInput == "Standard" ? trmad < 0 ? color.new(osColorInput,90) : color.new(obColorInput, 90) : chart.bg_color, title = "Area Fill")
  48.  
  49. // - Overbought / oversold zone
  50. fill(zeroPlot, obPlot, obValueInput * 1.5, obValueInput, top_color = themeInput == "Standard" ? color.new(obColorInput, 50) : na, bottom_color = themeInput == "Standard" ? color.new(obColorInput, 100) : na, title = "Overbought Gradient Fill")
  51. fill(zeroPlot, osPlot, osValueInput, osValueInput * 1.5, top_color = themeInput == "Standard" ? color.new(osColorInput, 100) : na, bottom_color = themeInput == "Standard" ? color.new(osColorInput, 0) : na, title = "Oversold Gradient Fill")
  52.  
  53. // d. Levels
  54. // - Hidden
  55. obLevel = plot(obValueInput, color = na, editable = false, display=display.none)
  56. midLevel = plot(0, color = na, editable = false, display=display.none)
  57. osLevel = plot(osValueInput, color = na, editable = false, display=display.none)
  58.  
  59. // - Displayed
  60. hline(obValueInput, 'Overbought')
  61. hline(0, 'Midline')
  62. hline(osValueInput, 'Oversold')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement