Advertisement
Guest User

Autocorrelation Function

a guest
Jan 25th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.34 KB | None | 0 0
  1. //@version=4
  2. study("Pig AutoCorrelation Function",precision=3)
  3. len=input(365,"Length")
  4. src=input(close)
  5. m = input(3,"Offset Multiple")
  6. w = input(4,"Histogram Width")
  7. number = input(true)
  8.  
  9. Detrend = input(title="Detrend", type=input.string, defval="Difference", options=["Difference","Log Return"])
  10. d = Detrend == "Difference" ? src-nz(src[1]) : Detrend == "Log Return" ? log(src/nz(src[1])): na
  11.  
  12. //cc
  13. c1 =correlation(d,nz(d[ 1]),len)
  14. c2 =correlation(d,nz(d[ 2]),len)
  15. c3 =correlation(d,nz(d[ 3]),len)
  16. c4 =correlation(d,nz(d[ 4]),len)
  17. c5 =correlation(d,nz(d[ 5]),len)
  18. c6 =correlation(d,nz(d[ 6]),len)
  19. c7 =correlation(d,nz(d[ 7]),len)
  20. c8 =correlation(d,nz(d[ 8]),len)
  21. c9 =correlation(d,nz(d[ 9]),len)
  22. c10=correlation(d,nz(d[10]),len)
  23. c11=correlation(d,nz(d[11]),len)
  24. c12=correlation(d,nz(d[12]),len)
  25. c13=correlation(d,nz(d[13]),len)
  26. c14=correlation(d,nz(d[14]),len)
  27. c15=correlation(d,nz(d[15]),len)
  28. c16=correlation(d,nz(d[16]),len)
  29. c17=correlation(d,nz(d[17]),len)
  30. c18=correlation(d,nz(d[18]),len)
  31. c19=correlation(d,nz(d[19]),len)
  32. c20=correlation(d,nz(d[20]),len)
  33. c21=correlation(d,nz(d[21]),len)
  34. c22=correlation(d,nz(d[22]),len)
  35. c23=correlation(d,nz(d[23]),len)
  36. c24=correlation(d,nz(d[24]),len)
  37. c25=correlation(d,nz(d[25]),len)
  38. c26=correlation(d,nz(d[26]),len)
  39. c27=correlation(d,nz(d[27]),len)
  40. c28=correlation(d,nz(d[28]),len)
  41. c29=correlation(d,nz(d[29]),len)
  42. c30=correlation(d,nz(d[30]),len)
  43. c31=correlation(d,nz(d[31]),len)
  44. c32=correlation(d,nz(d[32]),len)
  45. c33=correlation(d,nz(d[33]),len)
  46. c34=correlation(d,nz(d[34]),len)
  47. c35=correlation(d,nz(d[35]),len)
  48. c36=correlation(d,nz(d[36]),len)
  49. c37=correlation(d,nz(d[37]),len)
  50. c38=correlation(d,nz(d[38]),len)
  51. c39=correlation(d,nz(d[39]),len)
  52. c40=correlation(d,nz(d[40]),len)
  53. c41=correlation(d,nz(d[41]),len)
  54. c42=correlation(d,nz(d[42]),len)
  55. c43=correlation(d,nz(d[43]),len)
  56. c44=correlation(d,nz(d[44]),len)
  57. c45=correlation(d,nz(d[45]),len)
  58. c46=correlation(d,nz(d[46]),len)
  59. c47=correlation(d,nz(d[47]),len)
  60. c48=correlation(d,nz(d[48]),len)
  61. c49=correlation(d,nz(d[49]),len)
  62. c50=correlation(d,nz(d[50]),len)
  63.  
  64. //critical value
  65. CV1 = 1.96/sqrt(len)
  66. CV2 = -(CV1)
  67.  
  68. colorColumns = color.new(color.aqua,0)
  69. plot(c1 ,style=plot.style_histogram,offset=m*-50,show_last=1,title="lag 1" ,color=colorColumns,linewidth=w)
  70. plot(c2 ,style=plot.style_histogram,offset=m*-49,show_last=1,title="lag 2" ,color=colorColumns,linewidth=w)
  71. plot(c3 ,style=plot.style_histogram,offset=m*-48,show_last=1,title="lag 3" ,color=colorColumns,linewidth=w)
  72. plot(c4 ,style=plot.style_histogram,offset=m*-47,show_last=1,title="lag 4" ,color=colorColumns,linewidth=w)
  73. plot(c5 ,style=plot.style_histogram,offset=m*-46,show_last=1,title="lag 5" ,color=colorColumns,linewidth=w)
  74. plot(c6 ,style=plot.style_histogram,offset=m*-45,show_last=1,title="lag 6" ,color=colorColumns,linewidth=w)
  75. plot(c7 ,style=plot.style_histogram,offset=m*-44,show_last=1,title="lag 7" ,color=colorColumns,linewidth=w)
  76. plot(c8 ,style=plot.style_histogram,offset=m*-43,show_last=1,title="lag 8" ,color=colorColumns,linewidth=w)
  77. plot(c9 ,style=plot.style_histogram,offset=m*-42,show_last=1,title="lag 9" ,color=colorColumns,linewidth=w)
  78. plot(c10,style=plot.style_histogram,offset=m*-41,show_last=1,title="lag 10",color=colorColumns,linewidth=w)
  79. plot(c11,style=plot.style_histogram,offset=m*-40,show_last=1,title="lag 11",color=colorColumns,linewidth=w)
  80. plot(c12,style=plot.style_histogram,offset=m*-39,show_last=1,title="lag 12",color=colorColumns,linewidth=w)
  81. plot(c13,style=plot.style_histogram,offset=m*-38,show_last=1,title="lag 13",color=colorColumns,linewidth=w)
  82. plot(c14,style=plot.style_histogram,offset=m*-37,show_last=1,title="lag 14",color=colorColumns,linewidth=w)
  83. plot(c15,style=plot.style_histogram,offset=m*-36,show_last=1,title="lag 15",color=colorColumns,linewidth=w)
  84. plot(c16,style=plot.style_histogram,offset=m*-35,show_last=1,title="lag 16",color=colorColumns,linewidth=w)
  85. plot(c17,style=plot.style_histogram,offset=m*-34,show_last=1,title="lag 17",color=colorColumns,linewidth=w)
  86. plot(c18,style=plot.style_histogram,offset=m*-33,show_last=1,title="lag 18",color=colorColumns,linewidth=w)
  87. plot(c19,style=plot.style_histogram,offset=m*-32,show_last=1,title="lag 19",color=colorColumns,linewidth=w)
  88. plot(c20,style=plot.style_histogram,offset=m*-31,show_last=1,title="lag 20",color=colorColumns,linewidth=w)
  89. plot(c21,style=plot.style_histogram,offset=m*-30,show_last=1,title="lag 21",color=colorColumns,linewidth=w)
  90. plot(c22,style=plot.style_histogram,offset=m*-29,show_last=1,title="lag 22",color=colorColumns,linewidth=w)
  91. plot(c23,style=plot.style_histogram,offset=m*-28,show_last=1,title="lag 23",color=colorColumns,linewidth=w)
  92. plot(c24,style=plot.style_histogram,offset=m*-27,show_last=1,title="lag 24",color=colorColumns,linewidth=w)
  93. plot(c25,style=plot.style_histogram,offset=m*-26,show_last=1,title="lag 25",color=colorColumns,linewidth=w)
  94. plot(c26,style=plot.style_histogram,offset=m*-25,show_last=1,title="lag 26",color=colorColumns,linewidth=w)
  95. plot(c27,style=plot.style_histogram,offset=m*-24,show_last=1,title="lag 27",color=colorColumns,linewidth=w)
  96. plot(c28,style=plot.style_histogram,offset=m*-23,show_last=1,title="lag 28",color=colorColumns,linewidth=w)
  97. plot(c29,style=plot.style_histogram,offset=m*-22,show_last=1,title="lag 29",color=colorColumns,linewidth=w)
  98. plot(c30,style=plot.style_histogram,offset=m*-21,show_last=1,title="lag 30",color=colorColumns,linewidth=w)
  99. plot(c31,style=plot.style_histogram,offset=m*-20,show_last=1,title="lag 31",color=colorColumns,linewidth=w)
  100. plot(c32,style=plot.style_histogram,offset=m*-19,show_last=1,title="lag 32",color=colorColumns,linewidth=w)
  101. plot(c33,style=plot.style_histogram,offset=m*-18,show_last=1,title="lag 33",color=colorColumns,linewidth=w)
  102. plot(c34,style=plot.style_histogram,offset=m*-17,show_last=1,title="lag 34",color=colorColumns,linewidth=w)
  103. plot(c35,style=plot.style_histogram,offset=m*-16,show_last=1,title="lag 35",color=colorColumns,linewidth=w)
  104. plot(c36,style=plot.style_histogram,offset=m*-15,show_last=1,title="lag 36",color=colorColumns,linewidth=w)
  105. plot(c37,style=plot.style_histogram,offset=m*-14,show_last=1,title="lag 37",color=colorColumns,linewidth=w)
  106. plot(c38,style=plot.style_histogram,offset=m*-13,show_last=1,title="lag 38",color=colorColumns,linewidth=w)
  107. plot(c39,style=plot.style_histogram,offset=m*-12,show_last=1,title="lag 39",color=colorColumns,linewidth=w)
  108. plot(c40,style=plot.style_histogram,offset=m*-11,show_last=1,title="lag 40",color=colorColumns,linewidth=w)
  109. plot(c41,style=plot.style_histogram,offset=m*-10,show_last=1,title="lag 41",color=colorColumns,linewidth=w)
  110. plot(c42,style=plot.style_histogram,offset=m*-9 ,show_last=1,title="lag 42",color=colorColumns,linewidth=w)
  111. plot(c43,style=plot.style_histogram,offset=m*-8 ,show_last=1,title="lag 43",color=colorColumns,linewidth=w)
  112. plot(c44,style=plot.style_histogram,offset=m*-7 ,show_last=1,title="lag 44",color=colorColumns,linewidth=w)
  113. plot(c45,style=plot.style_histogram,offset=m*-6 ,show_last=1,title="lag 45",color=colorColumns,linewidth=w)
  114. plot(c46,style=plot.style_histogram,offset=m*-5 ,show_last=1,title="lag 46",color=colorColumns,linewidth=w)
  115. plot(c47,style=plot.style_histogram,offset=m*-4 ,show_last=1,title="lag 47",color=colorColumns,linewidth=w)
  116. plot(c48,style=plot.style_histogram,offset=m*-3 ,show_last=1,title="lag 48",color=colorColumns,linewidth=w)
  117. plot(c49,style=plot.style_histogram,offset=m*-2 ,show_last=1,title="lag 49",color=colorColumns,linewidth=w)
  118. plot(c50,style=plot.style_histogram,offset=m*-1 ,show_last=1,title="lag 50",color=colorColumns,linewidth=w)
  119.  
  120. colorPlotShape = color.new(color.white,50)
  121. plotshape(number?c5 :na,text="0" ,textcolor=colorPlotShape,style=shape.arrowdown,color=colorPlotShape,location=location.top,size=size.tiny,show_last=1,offset=m*-51)
  122. plotshape(number?c5 :na,text="5" ,textcolor=colorPlotShape,style=shape.arrowdown,color=colorPlotShape,location=location.top,size=size.tiny,show_last=1,offset=m*-46)
  123. plotshape(number?c10:na,text="10",textcolor=colorPlotShape,style=shape.arrowdown,color=colorPlotShape,location=location.top,size=size.tiny,show_last=1,offset=m*-41)
  124. plotshape(number?c15:na,text="15",textcolor=colorPlotShape,style=shape.arrowdown,color=colorPlotShape,location=location.top,size=size.tiny,show_last=1,offset=m*-36)
  125. plotshape(number?c20:na,text="20",textcolor=colorPlotShape,style=shape.arrowdown,color=colorPlotShape,location=location.top,size=size.tiny,show_last=1,offset=m*-31)
  126. plotshape(number?c25:na,text="25",textcolor=colorPlotShape,style=shape.arrowdown,color=colorPlotShape,location=location.top,size=size.tiny,show_last=1,offset=m*-26)
  127. plotshape(number?c30:na,text="30",textcolor=colorPlotShape,style=shape.arrowdown,color=colorPlotShape,location=location.top,size=size.tiny,show_last=1,offset=m*-21)
  128. plotshape(number?c35:na,text="35",textcolor=colorPlotShape,style=shape.arrowdown,color=colorPlotShape,location=location.top,size=size.tiny,show_last=1,offset=m*-16)
  129. plotshape(number?c40:na,text="40",textcolor=colorPlotShape,style=shape.arrowdown,color=colorPlotShape,location=location.top,size=size.tiny,show_last=1,offset=m*-11)
  130. plotshape(number?c45:na,text="45",textcolor=colorPlotShape,style=shape.arrowdown,color=colorPlotShape,location=location.top,size=size.tiny,show_last=1,offset=m*-6)
  131. plotshape(number?c45:na,text="50",textcolor=colorPlotShape,style=shape.arrowdown,color=colorPlotShape,location=location.top,size=size.tiny,show_last=1,offset=m*-1)
  132.  
  133. hline(0)
  134. hline(CV1)
  135. hline(CV2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement