xmd79

The Echo Forecast [LUX]

Oct 4th, 2021
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. // This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
  2. // © LuxAlgo
  3.  
  4. //@version=4
  5. study("The Echo Forecast [LUX]","ECHO [LUX]",overlay=true,max_bars_back=1000,max_lines_count=200)
  6. length = input(50,'Evaluation Window',minval=0,maxval=200)
  7. fcast = input(50,'Forecast Window',minval=1,maxval=200)
  8.  
  9. fmode = input('Similarity','Forecast Mode',options=['Similarity','Dissimilarity'])
  10. cmode = input('Cumulative','Forecast Construction',options=['Cumulative','Mean','Linreg'])
  11. src = input(close)
  12.  
  13. fcast_col = input(#2157f3,'Forecast Style',inline='fcast_style',group='Style')
  14. fcast_style = input('· · ·','',options=['──','- - -','· · ·'],inline='fcast_style',group='Style')
  15.  
  16. show_area = input(true,'Show Area',inline='areas',group='Style')
  17. fcast_area = input(color.new(#ff5d00,50),'',inline='areas',group='Style')
  18. corr_area = input(color.new(#0cb51a,50),'',inline='areas',group='Style')
  19. eval_area = input(color.new(color.gray,50),'',inline='areas',group='Style')
  20. //----
  21. var lines = array.new_line(0)
  22. if barstate.isfirst
  23. for i = 0 to fcast-1
  24. array.push(lines,line.new(na,na,na,na))
  25. //----
  26. n = bar_index
  27. d = change(src)
  28.  
  29. top = highest(src,length+fcast*2)
  30. btm = lowest(src,length+fcast*2)
  31.  
  32. if barstate.islast
  33. float val = na
  34. k = 0
  35. A = array.new_float(0)
  36. X = array.new_int(0)
  37. for i = 0 to fcast*2+length
  38. array.push(A,src[i])
  39. if cmode == 'Linreg'
  40. array.push(X,n[i])
  41.  
  42. a = array.slice(A,0,fcast-1)
  43. for i = 0 to length-1
  44. b = array.slice(A,fcast+i,fcast*2+i-1)
  45. r = array.covariance(a,b)/(array.stdev(a)*array.stdev(b))
  46. if fmode == 'Similarity'
  47. val := r >= nz(val,r) ? r : val
  48. else
  49. val := r <= nz(val,r) ? r : val
  50. k := val == r ? i : k
  51.  
  52. prev = src
  53. current = src
  54.  
  55. for i = 0 to fcast-1
  56. e = d[fcast+k+(fcast-i-1)]
  57. if cmode == 'Mean'
  58. current := array.avg(a) + e
  59. else if cmode == 'Linreg'
  60. a = array.slice(A,0,fcast)
  61. x = array.slice(X,0,fcast)
  62. alpha = array.covariance(a,x)/array.variance(x)
  63. beta = array.avg(a) - alpha*array.avg(x)
  64. current := alpha*(n+i+1) + beta + e
  65. else
  66. current += e
  67.  
  68. l = array.get(lines,i)
  69. line.set_xy1(l,n+i,prev)
  70. line.set_xy2(l,n+i+1,current)
  71. line.set_color(l,fcast_col)
  72.  
  73. if fcast_style == '- - -'
  74. line.set_style(l,line.style_dashed)
  75. else if fcast_style == '· · ·'
  76. line.set_style(l,line.style_dotted)
  77.  
  78. prev := current
  79.  
  80. if show_area
  81. box.delete(box.new(n-length-fcast*2+1,top,n-fcast+1,btm,
  82. border_color=na,
  83. bgcolor=eval_area)[1])
  84. box.delete(box.new(n-fcast+1,top,n,btm,
  85. border_color=na,
  86. bgcolor=fcast_area)[1])
  87. box.delete(box.new(n-k-fcast*2+1,btm,n-k-fcast,top,
  88. border_color=na,
  89. bgcolor=corr_area)[1])
Advertisement
Add Comment
Please, Sign In to add comment