NKactive

Untitled

Aug 15th, 2023
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.33 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. // © Ontra02
  3.  
  4. //@version=5
  5. indicator("Correlation Coefficient", shorttitle = "CC", format = format.price, overlay = true, precision = 5)
  6. //General Inputs
  7. Daily = input.bool(true,"Use 1D Chart", tooltip = "Please use the Daily Chart for Correct Calculation", group = "Daily Chart")
  8. TableOn = input.bool(true, "Show Table", "All Calculations get Shown in the Table", group = "Full Table")
  9. // This was broken so I removed and fixed. See below. NK.active
  10. // Tableposition = input.string(position.bottom_right, "Full Table", options = [position.bottom_center, position.bottom_left, position.bottom_right, position.middle_center,
  11. // position.middle_left, position.middle_right, position.top_center, position.top_left, position.top_right], group = "Full Table" ,tooltip = "Where on the Chart the Table gets shown")
  12. // I tried this NK.active
  13. //Tableposition = input.string
  14. // (
  15. // position =position.bottom_right,
  16. // "Full Table",
  17. // options = [position.top_left, position.top_center, position.top_right, position.middle_left, position.middle_center, position.middle_right, position.bottom_left, position.bottom_center, position.bottom_right],
  18. // group = "Full Table" ,
  19. // tooltip = "Where on the Chart the Table gets shown"
  20. // )
  21. Tableposition = input.string(position.bottom_right, "Full Table", options = [position.top_left, position.top_center, position.top_right, position.middle_left, position.middle_center, position.middle_right, position.bottom_left, position.bottom_center, position.bottom_right], group = "Full Table" ,tooltip = "Where on the Chart the Table gets shown")
  22. //
  23. TableAvgON = input.bool(false, "Show Average", "Only show Average of the Implied Trend", group = "AVG Table")
  24. TableAvgposition = input.string(position.top_right, "Implied Trend AVG", options = [position.bottom_center, position.bottom_left, position.bottom_right, position.middle_center,
  25. position.middle_left, position.middle_right, position.top_center, position.top_left, position.top_right], group = "AVG Table", tooltip = "Where on the Chart the Table gets shown")
  26. //Symbol Inputs
  27. symbolInput1 = input.symbol("DXY", "Symbol", confirm = true)
  28. symbolInput2 = input.symbol("oil", "Symbol 2 ", confirm = true)
  29. symbolInput3 = input.symbol("XAUUSD", "Symbol 3 ", confirm = true)
  30. symbolInput4 = input.symbol("NDX", "Symbol 4 ", confirm = true)
  31. symbolInput5 = input.symbol("SP500", "Symbol 5 ", confirm = true)
  32. symbolInput6 = input.symbol("VIX", "Symbol 6 ", confirm = true)
  33. symbolInput7 = input.symbol("US10", "Symbol 7", confirm = true)
  34.  
  35. //Source and Length Inputs
  36. sourceInput = input.source(close, "Source")
  37. lengthInput1 = input.int(15, "Length (Days)", confirm = true)
  38. lengthInput2 = input.int(30, "Length (Days)", confirm = true)
  39. lengthInput3 = input.int(90, "Length (Days)", confirm = true)
  40. lengthInput4 = input.int(120, "Length (Days)", confirm = true)
  41.  
  42. //Barcolor Change
  43. Barcolorchange = input.bool(false, "Change Color of Bar", "Change the Color of a Bar to Red and Green if the Overalll Implied Average Positive or Negative is")
  44.  
  45. // This takes into account additional settings enabled on chart, e.g. divident adjustment or extended session
  46. adjustedSeries1 = ticker.modify(symbolInput1)
  47. adjustedSeries2 = ticker.modify(symbolInput2)
  48. adjustedSeries3 = ticker.modify(symbolInput3)
  49. adjustedSeries4 = ticker.modify(symbolInput4)
  50. adjustedSeries5 = ticker.modify(symbolInput5)
  51. adjustedSeries6 = ticker.modify(symbolInput6)
  52. adjustedSeries7 = ticker.modify(symbolInput7)
  53.  
  54. //RequestData
  55. requestedData1 = request.security(adjustedSeries1, timeframe.period, sourceInput)
  56. requestedData2 = request.security(adjustedSeries2, timeframe.period, sourceInput)
  57. requestedData3 = request.security(adjustedSeries3, timeframe.period, sourceInput)
  58. requestedData4 = request.security(adjustedSeries4, timeframe.period, sourceInput)
  59. requestedData5 = request.security(adjustedSeries5, timeframe.period, sourceInput)
  60. requestedData6 = request.security(adjustedSeries6, timeframe.period, sourceInput)
  61. requestedData7 = request.security(adjustedSeries7, timeframe.period, sourceInput)
  62.  
  63. [supertrend, direction] = ta.supertrend(3,10)
  64.  
  65. //Request data for Implied Trend
  66. requestedData11 = request.security(adjustedSeries1, timeframe.period, direction)
  67. requestedData12 = request.security(adjustedSeries2, timeframe.period, direction)
  68. requestedData13 = request.security(adjustedSeries3, timeframe.period, direction)
  69. requestedData14 = request.security(adjustedSeries4, timeframe.period, direction)
  70. requestedData15 = request.security(adjustedSeries5, timeframe.period, direction)
  71. requestedData16 = request.security(adjustedSeries6, timeframe.period, direction)
  72. requestedData17 = request.security(adjustedSeries7, timeframe.period, direction)
  73.  
  74. //Correlation Calculation for 15D
  75. correlation1 = ta.correlation(sourceInput, requestedData1, lengthInput1)
  76. correlation2 = ta.correlation(sourceInput, requestedData2, lengthInput1)
  77. correlation3 = ta.correlation(sourceInput, requestedData3, lengthInput1)
  78. correlation4 = ta.correlation(sourceInput, requestedData4, lengthInput1)
  79. correlation5 = ta.correlation(sourceInput, requestedData5, lengthInput1)
  80. correlation6 = ta.correlation(sourceInput, requestedData6, lengthInput1)
  81. correlation7 = ta.correlation(sourceInput, requestedData7, lengthInput1)
  82.  
  83. //Correlation Calculation for 30D
  84. correlation11 = ta.correlation(sourceInput, requestedData1, lengthInput2)
  85. correlation12 = ta.correlation(sourceInput, requestedData2, lengthInput2)
  86. correlation13 = ta.correlation(sourceInput, requestedData3, lengthInput2)
  87. correlation14 = ta.correlation(sourceInput, requestedData4, lengthInput2)
  88. correlation15 = ta.correlation(sourceInput, requestedData5, lengthInput2)
  89. correlation16 = ta.correlation(sourceInput, requestedData6, lengthInput2)
  90. correlation17 = ta.correlation(sourceInput, requestedData7, lengthInput2)
  91.  
  92. //Correlation Calculation for 90D
  93. correlation21 = ta.correlation(sourceInput, requestedData1, lengthInput3)
  94. correlation22 = ta.correlation(sourceInput, requestedData2, lengthInput3)
  95. correlation23 = ta.correlation(sourceInput, requestedData3, lengthInput3)
  96. correlation24 = ta.correlation(sourceInput, requestedData4, lengthInput3)
  97. correlation25 = ta.correlation(sourceInput, requestedData5, lengthInput3)
  98. correlation26 = ta.correlation(sourceInput, requestedData6, lengthInput3)
  99. correlation27 = ta.correlation(sourceInput, requestedData7, lengthInput3)
  100.  
  101. //Correlation Calculation for 120D
  102. correlation31 = ta.correlation(sourceInput, requestedData1, lengthInput4)
  103. correlation32 = ta.correlation(sourceInput, requestedData2, lengthInput4)
  104. correlation33 = ta.correlation(sourceInput, requestedData3, lengthInput4)
  105. correlation34 = ta.correlation(sourceInput, requestedData4, lengthInput4)
  106. correlation35 = ta.correlation(sourceInput, requestedData5, lengthInput4)
  107. correlation36 = ta.correlation(sourceInput, requestedData6, lengthInput4)
  108. correlation37 = ta.correlation(sourceInput, requestedData7, lengthInput4)
  109.  
  110. //Draw table to Chart
  111. //MyTable = table.new(Tableposition, 10, 10, frame_color = color.navy, frame_width= 3, border_color = color.navy, border_width = 3, bgcolor = color.silver)
  112. MyTable = table.new(Tableposition, 10, 10, frame_color = color.navy, frame_width= 3, border_color = color.navy, border_width = 3, bgcolor = color.silver)
  113. if barstate.islast // added this to make table more efficient as only ned to calculate it on last bar NK.active
  114. table.cell(MyTable, 0, 1, " Asset Correlation " )
  115. table.cell(MyTable, 1, 1, text = str.tostring(lengthInput1))
  116. table.cell(MyTable, 2, 1, text = str.tostring(lengthInput2))
  117. table.cell(MyTable, 3, 1, text = str.tostring(lengthInput3))
  118. table.cell(MyTable, 4, 1, text = str.tostring(lengthInput4))
  119. table.cell(MyTable, 5, 1, "Average")
  120. table.cell(MyTable, 6, 1, "L/S")
  121. table.cell(MyTable, 7, 1, "Implied Trend")
  122. table.delete(MyTable[1])
  123.  
  124. if not TableOn
  125. table.delete(MyTable)
  126.  
  127. //Draw Table for Average to the Chart
  128. TableAvg = table.new(TableAvgposition , 10, 10, frame_color = color.navy, frame_width= 3, border_color = color.navy, border_width = 3, bgcolor = color.silver)
  129. table.cell(TableAvg,0,1,"Average Implied Trend")
  130. table.delete(TableAvg[1])
  131.  
  132. if not TableAvgON
  133. table.delete(TableAvg)
  134. //Cells for Symbols
  135. table.cell(MyTable, 0, 2, text = symbolInput1)
  136. table.cell(MyTable, 0, 3, text = symbolInput2)
  137. table.cell(MyTable, 0, 4, text = symbolInput3)
  138. table.cell(MyTable, 0, 5, text = symbolInput4)
  139. table.cell(MyTable, 0, 6, text = symbolInput5)
  140. table.cell(MyTable, 0, 7, text = symbolInput6)
  141. table.cell(MyTable, 0, 8, text = symbolInput7)
  142.  
  143. //Cells for Correlation 15D (rounded to 3 decimal places)
  144. table.cell(MyTable, 1, 2, text = str.tostring(math.round(correlation1 ,3)), bgcolor = correlation1 > 0 ? color.green : color.red)
  145. table.cell(MyTable, 1, 3, text = str.tostring(math.round(correlation2 ,3)), bgcolor = correlation2 > 0 ? color.green : color.red)
  146. table.cell(MyTable, 1, 4, text = str.tostring(math.round(correlation3 ,3)), bgcolor = correlation3 > 0 ? color.green : color.red)
  147. table.cell(MyTable, 1, 5, text = str.tostring(math.round(correlation4 ,3)), bgcolor = correlation4 > 0 ? color.green : color.red)
  148. table.cell(MyTable, 1, 6, text = str.tostring(math.round(correlation5 ,3)), bgcolor = correlation5 > 0 ? color.green : color.red)
  149. table.cell(MyTable, 1, 7, text = str.tostring(math.round(correlation6 ,3)), bgcolor = correlation6 > 0 ? color.green : color.red)
  150. table.cell(MyTable, 1, 8, text = str.tostring(math.round(correlation7 ,3)), bgcolor = correlation7 > 0 ? color.green : color.red)
  151.  
  152. //Cells for Correlation 30D (rounded to 3 decimal places)
  153. table.cell(MyTable, 2, 2, text = str.tostring(math.round(correlation11 ,3)), bgcolor = correlation11 > 0 ? color.green : color.red)
  154. table.cell(MyTable, 2, 3, text = str.tostring(math.round(correlation12 ,3)), bgcolor = correlation12 > 0 ? color.green : color.red)
  155. table.cell(MyTable, 2, 4, text = str.tostring(math.round(correlation13 ,3)), bgcolor = correlation13 > 0 ? color.green : color.red)
  156. table.cell(MyTable, 2, 5, text = str.tostring(math.round(correlation14 ,3)), bgcolor = correlation14 > 0 ? color.green : color.red)
  157. table.cell(MyTable, 2, 6, text = str.tostring(math.round(correlation15 ,3)), bgcolor = correlation15 > 0 ? color.green : color.red)
  158. table.cell(MyTable, 2, 7, text = str.tostring(math.round(correlation16 ,3)), bgcolor = correlation16 > 0 ? color.green : color.red)
  159. table.cell(MyTable, 2, 8, text = str.tostring(math.round(correlation17 ,3)), bgcolor = correlation17 > 0 ? color.green : color.red)
  160.  
  161. //Cels for Correlation 90D (rounded to 3 decimal places)
  162. table.cell(MyTable, 3, 2, text = str.tostring(math.round(correlation21 ,3)), bgcolor = correlation21 > 0 ? color.green : color.red)
  163. table.cell(MyTable, 3, 3, text = str.tostring(math.round(correlation22 ,3)), bgcolor = correlation22 > 0 ? color.green : color.red)
  164. table.cell(MyTable, 3, 4, text = str.tostring(math.round(correlation23 ,3)), bgcolor = correlation23 > 0 ? color.green : color.red)
  165. table.cell(MyTable, 3, 5, text = str.tostring(math.round(correlation24 ,3)), bgcolor = correlation24 > 0 ? color.green : color.red)
  166. table.cell(MyTable, 3, 6, text = str.tostring(math.round(correlation25 ,3)), bgcolor = correlation25 > 0 ? color.green : color.red)
  167. table.cell(MyTable, 3, 7, text = str.tostring(math.round(correlation26 ,3)), bgcolor = correlation26 > 0 ? color.green : color.red)
  168. table.cell(MyTable, 3, 8, text = str.tostring(math.round(correlation27, 3)), bgcolor = correlation27 > 0 ? color.green : color.red)
  169.  
  170. //Cels for Correlation 120D (rounded to 3 decimal places)
  171. table.cell(MyTable, 4, 2, text = str.tostring(math.round(correlation31 ,3)), bgcolor = correlation31 > 0 ? color.green : color.red)
  172. table.cell(MyTable, 4, 3, text = str.tostring(math.round(correlation32 ,3)), bgcolor = correlation32 > 0 ? color.green : color.red)
  173. table.cell(MyTable, 4, 4, text = str.tostring(math.round(correlation33 ,3)), bgcolor = correlation33 > 0 ? color.green : color.red)
  174. table.cell(MyTable, 4, 5, text = str.tostring(math.round(correlation34 ,3)), bgcolor = correlation34 > 0 ? color.green : color.red)
  175. table.cell(MyTable, 4, 6, text = str.tostring(math.round(correlation35 ,3)), bgcolor = correlation35 > 0 ? color.green : color.red)
  176. table.cell(MyTable, 4, 7, text = str.tostring(math.round(correlation36 ,3)), bgcolor = correlation36 < 0 ? color.green : color.red)
  177. table.cell(MyTable, 4, 8, text = str.tostring(math.round(correlation37, 3)), bgcolor = correlation37 > 0 ? color.green : color.red)
  178.  
  179. //Variables for Average
  180. Average1 = (correlation1 + correlation11 + correlation21 + correlation31) / 4
  181. Average2 = (correlation2 + correlation12 + correlation22 + correlation32) / 4
  182. Average3 = (correlation3 + correlation13 + correlation23 + correlation33) / 4
  183. Average4 = (correlation4 + correlation14 + correlation24 + correlation34) / 4
  184. Average5 = (correlation5 + correlation15 + correlation25 + correlation35) / 4
  185. Average6 = (correlation6 + correlation16 + correlation26 + correlation36) / 4
  186. Average7 = (correlation7 + correlation17 + correlation27 + correlation37) / 4
  187.  
  188. //Cells for Correlation Average of 15D, 30D, 90D, 120D
  189. table.cell(MyTable, 5, 2, text = str.tostring(math.round(Average1, 3)), bgcolor = Average1 > 0 ? color.green : color.red)
  190. table.cell(MyTable, 5, 3, text = str.tostring(math.round(Average2, 3)), bgcolor = Average2 > 0 ? color.green : color.red)
  191. table.cell(MyTable, 5, 4, text = str.tostring(math.round(Average3, 3)), bgcolor = Average3 > 0 ? color.green : color.red)
  192. table.cell(MyTable, 5, 5, text = str.tostring(math.round(Average4, 3)), bgcolor = Average4 > 0 ? color.green : color.red)
  193. table.cell(MyTable, 5, 6, text = str.tostring(math.round(Average5, 3)), bgcolor = Average5 > 0 ? color.green : color.red)
  194. table.cell(MyTable, 5, 7, text = str.tostring(math.round(Average6, 3)), bgcolor = Average6 > 0 ? color.green : color.red)
  195. table.cell(MyTable, 5, 8, text = str.tostring(math.round(Average7, 3)), bgcolor = Average7 > 0 ? color.green : color.red)
  196.  
  197. //Calculations for Implied Trend
  198. L_or_S11 = 0
  199. L_or_S12 = 0
  200. L_or_S13 = 0
  201. L_or_S14 = 0
  202. L_or_S15 = 0
  203. L_or_S16 = 0
  204. L_or_S17 = 0
  205.  
  206.  
  207. if requestedData11 > 0
  208. L_or_S11 := -1
  209.  
  210. if requestedData12 > 0
  211. L_or_S12 := -1
  212.  
  213. if requestedData13 > 0
  214. L_or_S13 := -1
  215.  
  216. if requestedData14 > 0
  217. L_or_S14 := -1
  218.  
  219. if requestedData15 > 0
  220. L_or_S15 := -1
  221.  
  222. if requestedData16 > 0
  223. L_or_S16 := -1
  224.  
  225. if requestedData17 > 0
  226. L_or_S17 := -1
  227.  
  228. //For Short Trend
  229. if requestedData11 < 0
  230. L_or_S11 := 1
  231.  
  232. if requestedData12 < 0
  233. L_or_S12 := 1
  234.  
  235. if requestedData13 < 0
  236. L_or_S13 := 1
  237.  
  238. if requestedData14 < 0
  239. L_or_S14 := 1
  240.  
  241. if requestedData15 < 0
  242. L_or_S15 := 1
  243.  
  244. if requestedData16 < 0
  245. L_or_S16 := 1
  246.  
  247. if requestedData17 < 0
  248. L_or_S17 := 1
  249.  
  250. //Cells for Long or Short
  251. table.cell(MyTable, 6, 2, text = str.tostring(L_or_S11 > 0 ? "L" : "S"), bgcolor = requestedData11 < 0 ? color.green : color.red)
  252. table.cell(MyTable, 6, 3, text = str.tostring(L_or_S12 > 0 ? "L" : "S"), bgcolor = requestedData12 < 0 ? color.green : color.red)
  253. table.cell(MyTable, 6, 4, text = str.tostring(L_or_S13 > 0 ? "L" : "S"), bgcolor = requestedData13 < 0 ? color.green : color.red)
  254. table.cell(MyTable, 6, 5, text = str.tostring(L_or_S14 > 0 ? "L" : "S"), bgcolor = requestedData14 < 0 ? color.green : color.red)
  255. table.cell(MyTable, 6, 6, text = str.tostring(L_or_S15 > 0 ? "L" : "S"), bgcolor = requestedData15 < 0 ? color.green : color.red)
  256. table.cell(MyTable, 6, 7, text = str.tostring(L_or_S16 > 0 ? "L" : "S"), bgcolor = requestedData16 < 0 ? color.green : color.red)
  257. table.cell(MyTable, 6, 8, text = str.tostring(L_or_S17 > 0 ? "L" : "S"), bgcolor = requestedData17 < 0 ? color.green : color.red)
  258.  
  259. //Cells for Implied Trend
  260. table.cell(MyTable, 7, 2, text = str.tostring(math.round(Average1 * L_or_S11, 3)), bgcolor = (Average1 * L_or_S11) > 0 ? color.green : color.red)
  261. table.cell(MyTable, 7, 3, text = str.tostring(math.round(Average2 * L_or_S12, 3)), bgcolor = (Average2 * L_or_S12) > 0 ? color.green : color.red)
  262. table.cell(MyTable, 7, 4, text = str.tostring(math.round(Average3 * L_or_S13, 3)), bgcolor = (Average3 * L_or_S13) > 0 ? color.green : color.red)
  263. table.cell(MyTable, 7, 5, text = str.tostring(math.round(Average4 * L_or_S14, 3)), bgcolor = (Average4 * L_or_S14) > 0 ? color.green : color.red)
  264. table.cell(MyTable, 7, 6, text = str.tostring(math.round(Average5 * L_or_S15, 3)), bgcolor = (Average5 * L_or_S15) > 0 ? color.green : color.red)
  265. table.cell(MyTable, 7, 7, text = str.tostring(math.round(Average6 * L_or_S16, 3)), bgcolor = (Average6 * L_or_S16) > 0 ? color.green : color.red)
  266. table.cell(MyTable, 7, 8, text = str.tostring(math.round(Average7 * L_or_S17, 3)), bgcolor = (Average7 * L_or_S17) > 0 ? color.green : color.red)
  267.  
  268. //Average of all Implied Trends.
  269. Average = ((Average1 * L_or_S11) + (Average2 * L_or_S12) + (Average3 * L_or_S13) + (Average4 * L_or_S14) + (Average5 * L_or_S15) + (Average6 * L_or_S16) + (Average7 * L_or_S17)) / 7
  270.  
  271. table.cell(MyTable, 7, 9, text = str.tostring(math.round(Average, 3)), bgcolor = (Average > 0 ? color.green : color.red))
  272. table.cell(TableAvg, 0, 2, text = str.tostring(math.round(Average, 3)), bgcolor = (Average > 0 ? color.green : color.red))
  273.  
  274. //Change color of Bar depending on Direction of Macro Correlation Implied Trend Average
  275. barcolor(not Barcolorchange ? na : Average > 0 ? color.green : color.red)
  276.  
  277. plot(Average, "Average history", color.white)
Add Comment
Please, Sign In to add comment