Advertisement
xmd79

Fourier Transformed Money Flow Index

Dec 14th, 2023
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 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. // © profitprotrading
  3. //@version=5
  4. indicator(title="Fourier Transformed Money Flow Index", shorttitle="FMFI", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
  5.  
  6.  
  7. //Inputs
  8. N = input.int(3,"Fourier Period", group = "Fourier Transform")
  9. xval = input.source(hlc3,"Fourier Source", group = "Fourier Transform")
  10.  
  11.  
  12. //Discrete Fourier Transform
  13. DFT(x, y, Nx, _dir) =>
  14. float _arg = 0.0
  15. float _cos = 0.0
  16. float _sin = 0.0
  17. float xArr_i = 0.0
  18. float yArr_i = 0.0
  19. xArr = array.new_float(array.size(x))
  20. yArr = array.new_float(array.size(y))
  21.  
  22. for i = 0 to Nx - 1 by 1
  23. xArr_i := 0.0
  24. yArr_i := 0.0
  25. kx = float(i) / float(Nx)
  26. _arg := -_dir * 2 * math.pi * kx
  27. for k = 0 to Nx - 1 by 1
  28. _cos := math.cos(k * _arg)
  29. _sin := math.sin(k * _arg)
  30. xArr_i += array.get(x, k) * _cos - array.get(y, k) * _sin
  31. yArr_i += array.get(x, k) * _sin + array.get(y, k) * _cos
  32. yArr_i
  33. array.set(xArr, i, xArr_i)
  34. array.set(yArr, i, yArr_i)
  35.  
  36. if _dir == 1
  37. for i = 0 to Nx - 1 by 1
  38. array.set(x, i, array.get(xArr, i) / float(Nx))
  39. array.set(y, i, array.get(yArr, i) / float(Nx))
  40. else
  41. for i = 0 to Nx - 1 by 1
  42. array.set(x, i, array.get(xArr, i))
  43. array.set(y, i, array.get(yArr, i))
  44.  
  45.  
  46. //
  47.  
  48. x = array.new_float(N, 0.0)
  49. y = array.new_float(N, 0.0)
  50. for i = 0 to N - 1
  51. array.set(x, i, xval[i])
  52. array.set(y, i, 0.0)
  53.  
  54. DFT(x, y, N, 1)
  55.  
  56. mag = array.new_float(N, 0.0)
  57. for i = 0 to N - 1
  58. mag_i = math.sqrt(math.pow(array.get(x, i), 2) + math.pow(array.get(y, i), 2))
  59. array.set(mag, i, mag_i)
  60.  
  61. ft = array.get(mag,0)
  62.  
  63. //Money Flow Index
  64. length = input.int(title="Length", defval=14, minval=1, maxval=2000, group = "MFI")
  65. smooth = input.int(7, "Smoothing", group = "MFI")
  66. src = input(hlc3, "Source", group = "MFI")
  67. ori = input.bool(true, "Plot original MFI?")
  68. mf = ta.mfi(ft, length)
  69. mf := ta.ema(mf, smooth)
  70. short = (ta.crossunder(mf,mf[1]))
  71.  
  72. //Plotting
  73. plot(mf, "MF", color=mf > mf[1] ? #ffffff: color.rgb(128, 35, 95), linewidth = 4)
  74. plotchar(ta.crossover(mf,mf[1]) ? mf : na, char = "⊙", location = location.absolute, size = size.tiny, color = #ffffff, offset = -1)
  75. plotchar(ta.crossunder(mf,mf[1]) ? mf : na, char = "⦿", location = location.absolute, size = size.tiny, color = color.rgb(246, 93, 192), offset = -1)
  76. plotshape((ta.crossunder(mf,mf[1]) and mf > 80), style = shape.triangledown, color = color.rgb(201, 8, 8), location = location.top, size = size.tiny, offset = -1)
  77. plotshape((ta.crossover(mf,mf[1]) and mf < 20), style = shape.triangleup, color = color.rgb(0, 171, 11), location = location.bottom, size = size.tiny, offset = -1)
  78. mfx = plot(ori ? ta.mfi(src,length): na, color = color.rgb(0, 208, 255, 65))
  79.  
  80. ob = plot(80, display = display.none)
  81. ob1 = plot(100, display = display.none)
  82. ob2 = plot(65, display = display.none)
  83. os = plot(20, display = display.none)
  84. os1 = plot(0, display = display.none)
  85. os2 = plot(35, display = display.none)
  86. fill(ob, ob1, color = color.rgb(234, 0, 0, 75))
  87. fill(os, os1, color = color.rgb(0, 193, 6, 75))
  88. fill(ob2, ob, color =color.rgb(234, 0, 0, 85))
  89. fill(os, os2, color =color.rgb(0, 193, 6, 85))
  90.  
  91. //Alert Conditions
  92. alertcondition(ta.crossover(mf,mf[1]), title="Buy", message="{{exchange}}:{{ticker}}")
  93. alertcondition(ta.crossunder(mf,mf[1]), title="Sell", message="{{exchange}}:{{ticker}}")
  94. alertcondition((ta.crossover(mf,mf[1]) and mf < 20), title="Firm Buy", message="{{exchange}}:{{ticker}}")
  95. alertcondition((ta.crossunder(mf,mf[1]) and mf > 80), title="Firm Sell", message="{{exchange}}:{{ticker}}")
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement