Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © Ontra02
- //@version=5
- indicator("Correlation Coefficient", shorttitle = "CC", format = format.price, overlay = true, precision = 5)
- //General Inputs
- Daily = input.bool(true,"Use 1D Chart", tooltip = "Please use the Daily Chart for Correct Calculation", group = "Daily Chart")
- TableOn = input.bool(true, "Show Table", "All Calculations get Shown in the Table", group = "Full Table")
- // This was broken so I removed and fixed. See below. NK.active
- // Tableposition = input.string(position.bottom_right, "Full Table", options = [position.bottom_center, position.bottom_left, position.bottom_right, position.middle_center,
- // 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")
- // I tried this NK.active
- //Tableposition = input.string
- // (
- // position =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"
- // )
- 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")
- //
- TableAvgON = input.bool(false, "Show Average", "Only show Average of the Implied Trend", group = "AVG Table")
- TableAvgposition = input.string(position.top_right, "Implied Trend AVG", options = [position.bottom_center, position.bottom_left, position.bottom_right, position.middle_center,
- 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")
- //Symbol Inputs
- symbolInput1 = input.symbol("DXY", "Symbol", confirm = true)
- symbolInput2 = input.symbol("oil", "Symbol 2 ", confirm = true)
- symbolInput3 = input.symbol("XAUUSD", "Symbol 3 ", confirm = true)
- symbolInput4 = input.symbol("NDX", "Symbol 4 ", confirm = true)
- symbolInput5 = input.symbol("SP500", "Symbol 5 ", confirm = true)
- symbolInput6 = input.symbol("VIX", "Symbol 6 ", confirm = true)
- symbolInput7 = input.symbol("US10", "Symbol 7", confirm = true)
- //Source and Length Inputs
- sourceInput = input.source(close, "Source")
- lengthInput1 = input.int(15, "Length (Days)", confirm = true)
- lengthInput2 = input.int(30, "Length (Days)", confirm = true)
- lengthInput3 = input.int(90, "Length (Days)", confirm = true)
- lengthInput4 = input.int(120, "Length (Days)", confirm = true)
- //Barcolor Change
- 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")
- // This takes into account additional settings enabled on chart, e.g. divident adjustment or extended session
- adjustedSeries1 = ticker.modify(symbolInput1)
- adjustedSeries2 = ticker.modify(symbolInput2)
- adjustedSeries3 = ticker.modify(symbolInput3)
- adjustedSeries4 = ticker.modify(symbolInput4)
- adjustedSeries5 = ticker.modify(symbolInput5)
- adjustedSeries6 = ticker.modify(symbolInput6)
- adjustedSeries7 = ticker.modify(symbolInput7)
- //RequestData
- requestedData1 = request.security(adjustedSeries1, timeframe.period, sourceInput)
- requestedData2 = request.security(adjustedSeries2, timeframe.period, sourceInput)
- requestedData3 = request.security(adjustedSeries3, timeframe.period, sourceInput)
- requestedData4 = request.security(adjustedSeries4, timeframe.period, sourceInput)
- requestedData5 = request.security(adjustedSeries5, timeframe.period, sourceInput)
- requestedData6 = request.security(adjustedSeries6, timeframe.period, sourceInput)
- requestedData7 = request.security(adjustedSeries7, timeframe.period, sourceInput)
- [supertrend, direction] = ta.supertrend(3,10)
- //Request data for Implied Trend
- requestedData11 = request.security(adjustedSeries1, timeframe.period, direction)
- requestedData12 = request.security(adjustedSeries2, timeframe.period, direction)
- requestedData13 = request.security(adjustedSeries3, timeframe.period, direction)
- requestedData14 = request.security(adjustedSeries4, timeframe.period, direction)
- requestedData15 = request.security(adjustedSeries5, timeframe.period, direction)
- requestedData16 = request.security(adjustedSeries6, timeframe.period, direction)
- requestedData17 = request.security(adjustedSeries7, timeframe.period, direction)
- //Correlation Calculation for 15D
- correlation1 = ta.correlation(sourceInput, requestedData1, lengthInput1)
- correlation2 = ta.correlation(sourceInput, requestedData2, lengthInput1)
- correlation3 = ta.correlation(sourceInput, requestedData3, lengthInput1)
- correlation4 = ta.correlation(sourceInput, requestedData4, lengthInput1)
- correlation5 = ta.correlation(sourceInput, requestedData5, lengthInput1)
- correlation6 = ta.correlation(sourceInput, requestedData6, lengthInput1)
- correlation7 = ta.correlation(sourceInput, requestedData7, lengthInput1)
- //Correlation Calculation for 30D
- correlation11 = ta.correlation(sourceInput, requestedData1, lengthInput2)
- correlation12 = ta.correlation(sourceInput, requestedData2, lengthInput2)
- correlation13 = ta.correlation(sourceInput, requestedData3, lengthInput2)
- correlation14 = ta.correlation(sourceInput, requestedData4, lengthInput2)
- correlation15 = ta.correlation(sourceInput, requestedData5, lengthInput2)
- correlation16 = ta.correlation(sourceInput, requestedData6, lengthInput2)
- correlation17 = ta.correlation(sourceInput, requestedData7, lengthInput2)
- //Correlation Calculation for 90D
- correlation21 = ta.correlation(sourceInput, requestedData1, lengthInput3)
- correlation22 = ta.correlation(sourceInput, requestedData2, lengthInput3)
- correlation23 = ta.correlation(sourceInput, requestedData3, lengthInput3)
- correlation24 = ta.correlation(sourceInput, requestedData4, lengthInput3)
- correlation25 = ta.correlation(sourceInput, requestedData5, lengthInput3)
- correlation26 = ta.correlation(sourceInput, requestedData6, lengthInput3)
- correlation27 = ta.correlation(sourceInput, requestedData7, lengthInput3)
- //Correlation Calculation for 120D
- correlation31 = ta.correlation(sourceInput, requestedData1, lengthInput4)
- correlation32 = ta.correlation(sourceInput, requestedData2, lengthInput4)
- correlation33 = ta.correlation(sourceInput, requestedData3, lengthInput4)
- correlation34 = ta.correlation(sourceInput, requestedData4, lengthInput4)
- correlation35 = ta.correlation(sourceInput, requestedData5, lengthInput4)
- correlation36 = ta.correlation(sourceInput, requestedData6, lengthInput4)
- correlation37 = ta.correlation(sourceInput, requestedData7, lengthInput4)
- //Draw table to Chart
- //MyTable = table.new(Tableposition, 10, 10, frame_color = color.navy, frame_width= 3, border_color = color.navy, border_width = 3, bgcolor = color.silver)
- MyTable = table.new(Tableposition, 10, 10, frame_color = color.navy, frame_width= 3, border_color = color.navy, border_width = 3, bgcolor = color.silver)
- if barstate.islast // added this to make table more efficient as only ned to calculate it on last bar NK.active
- table.cell(MyTable, 0, 1, " Asset Correlation " )
- table.cell(MyTable, 1, 1, text = str.tostring(lengthInput1))
- table.cell(MyTable, 2, 1, text = str.tostring(lengthInput2))
- table.cell(MyTable, 3, 1, text = str.tostring(lengthInput3))
- table.cell(MyTable, 4, 1, text = str.tostring(lengthInput4))
- table.cell(MyTable, 5, 1, "Average")
- table.cell(MyTable, 6, 1, "L/S")
- table.cell(MyTable, 7, 1, "Implied Trend")
- table.delete(MyTable[1])
- if not TableOn
- table.delete(MyTable)
- //Draw Table for Average to the Chart
- TableAvg = table.new(TableAvgposition , 10, 10, frame_color = color.navy, frame_width= 3, border_color = color.navy, border_width = 3, bgcolor = color.silver)
- table.cell(TableAvg,0,1,"Average Implied Trend")
- table.delete(TableAvg[1])
- if not TableAvgON
- table.delete(TableAvg)
- //Cells for Symbols
- table.cell(MyTable, 0, 2, text = symbolInput1)
- table.cell(MyTable, 0, 3, text = symbolInput2)
- table.cell(MyTable, 0, 4, text = symbolInput3)
- table.cell(MyTable, 0, 5, text = symbolInput4)
- table.cell(MyTable, 0, 6, text = symbolInput5)
- table.cell(MyTable, 0, 7, text = symbolInput6)
- table.cell(MyTable, 0, 8, text = symbolInput7)
- //Cells for Correlation 15D (rounded to 3 decimal places)
- table.cell(MyTable, 1, 2, text = str.tostring(math.round(correlation1 ,3)), bgcolor = correlation1 > 0 ? color.green : color.red)
- table.cell(MyTable, 1, 3, text = str.tostring(math.round(correlation2 ,3)), bgcolor = correlation2 > 0 ? color.green : color.red)
- table.cell(MyTable, 1, 4, text = str.tostring(math.round(correlation3 ,3)), bgcolor = correlation3 > 0 ? color.green : color.red)
- table.cell(MyTable, 1, 5, text = str.tostring(math.round(correlation4 ,3)), bgcolor = correlation4 > 0 ? color.green : color.red)
- table.cell(MyTable, 1, 6, text = str.tostring(math.round(correlation5 ,3)), bgcolor = correlation5 > 0 ? color.green : color.red)
- table.cell(MyTable, 1, 7, text = str.tostring(math.round(correlation6 ,3)), bgcolor = correlation6 > 0 ? color.green : color.red)
- table.cell(MyTable, 1, 8, text = str.tostring(math.round(correlation7 ,3)), bgcolor = correlation7 > 0 ? color.green : color.red)
- //Cells for Correlation 30D (rounded to 3 decimal places)
- table.cell(MyTable, 2, 2, text = str.tostring(math.round(correlation11 ,3)), bgcolor = correlation11 > 0 ? color.green : color.red)
- table.cell(MyTable, 2, 3, text = str.tostring(math.round(correlation12 ,3)), bgcolor = correlation12 > 0 ? color.green : color.red)
- table.cell(MyTable, 2, 4, text = str.tostring(math.round(correlation13 ,3)), bgcolor = correlation13 > 0 ? color.green : color.red)
- table.cell(MyTable, 2, 5, text = str.tostring(math.round(correlation14 ,3)), bgcolor = correlation14 > 0 ? color.green : color.red)
- table.cell(MyTable, 2, 6, text = str.tostring(math.round(correlation15 ,3)), bgcolor = correlation15 > 0 ? color.green : color.red)
- table.cell(MyTable, 2, 7, text = str.tostring(math.round(correlation16 ,3)), bgcolor = correlation16 > 0 ? color.green : color.red)
- table.cell(MyTable, 2, 8, text = str.tostring(math.round(correlation17 ,3)), bgcolor = correlation17 > 0 ? color.green : color.red)
- //Cels for Correlation 90D (rounded to 3 decimal places)
- table.cell(MyTable, 3, 2, text = str.tostring(math.round(correlation21 ,3)), bgcolor = correlation21 > 0 ? color.green : color.red)
- table.cell(MyTable, 3, 3, text = str.tostring(math.round(correlation22 ,3)), bgcolor = correlation22 > 0 ? color.green : color.red)
- table.cell(MyTable, 3, 4, text = str.tostring(math.round(correlation23 ,3)), bgcolor = correlation23 > 0 ? color.green : color.red)
- table.cell(MyTable, 3, 5, text = str.tostring(math.round(correlation24 ,3)), bgcolor = correlation24 > 0 ? color.green : color.red)
- table.cell(MyTable, 3, 6, text = str.tostring(math.round(correlation25 ,3)), bgcolor = correlation25 > 0 ? color.green : color.red)
- table.cell(MyTable, 3, 7, text = str.tostring(math.round(correlation26 ,3)), bgcolor = correlation26 > 0 ? color.green : color.red)
- table.cell(MyTable, 3, 8, text = str.tostring(math.round(correlation27, 3)), bgcolor = correlation27 > 0 ? color.green : color.red)
- //Cels for Correlation 120D (rounded to 3 decimal places)
- table.cell(MyTable, 4, 2, text = str.tostring(math.round(correlation31 ,3)), bgcolor = correlation31 > 0 ? color.green : color.red)
- table.cell(MyTable, 4, 3, text = str.tostring(math.round(correlation32 ,3)), bgcolor = correlation32 > 0 ? color.green : color.red)
- table.cell(MyTable, 4, 4, text = str.tostring(math.round(correlation33 ,3)), bgcolor = correlation33 > 0 ? color.green : color.red)
- table.cell(MyTable, 4, 5, text = str.tostring(math.round(correlation34 ,3)), bgcolor = correlation34 > 0 ? color.green : color.red)
- table.cell(MyTable, 4, 6, text = str.tostring(math.round(correlation35 ,3)), bgcolor = correlation35 > 0 ? color.green : color.red)
- table.cell(MyTable, 4, 7, text = str.tostring(math.round(correlation36 ,3)), bgcolor = correlation36 < 0 ? color.green : color.red)
- table.cell(MyTable, 4, 8, text = str.tostring(math.round(correlation37, 3)), bgcolor = correlation37 > 0 ? color.green : color.red)
- //Variables for Average
- Average1 = (correlation1 + correlation11 + correlation21 + correlation31) / 4
- Average2 = (correlation2 + correlation12 + correlation22 + correlation32) / 4
- Average3 = (correlation3 + correlation13 + correlation23 + correlation33) / 4
- Average4 = (correlation4 + correlation14 + correlation24 + correlation34) / 4
- Average5 = (correlation5 + correlation15 + correlation25 + correlation35) / 4
- Average6 = (correlation6 + correlation16 + correlation26 + correlation36) / 4
- Average7 = (correlation7 + correlation17 + correlation27 + correlation37) / 4
- //Cells for Correlation Average of 15D, 30D, 90D, 120D
- table.cell(MyTable, 5, 2, text = str.tostring(math.round(Average1, 3)), bgcolor = Average1 > 0 ? color.green : color.red)
- table.cell(MyTable, 5, 3, text = str.tostring(math.round(Average2, 3)), bgcolor = Average2 > 0 ? color.green : color.red)
- table.cell(MyTable, 5, 4, text = str.tostring(math.round(Average3, 3)), bgcolor = Average3 > 0 ? color.green : color.red)
- table.cell(MyTable, 5, 5, text = str.tostring(math.round(Average4, 3)), bgcolor = Average4 > 0 ? color.green : color.red)
- table.cell(MyTable, 5, 6, text = str.tostring(math.round(Average5, 3)), bgcolor = Average5 > 0 ? color.green : color.red)
- table.cell(MyTable, 5, 7, text = str.tostring(math.round(Average6, 3)), bgcolor = Average6 > 0 ? color.green : color.red)
- table.cell(MyTable, 5, 8, text = str.tostring(math.round(Average7, 3)), bgcolor = Average7 > 0 ? color.green : color.red)
- //Calculations for Implied Trend
- L_or_S11 = 0
- L_or_S12 = 0
- L_or_S13 = 0
- L_or_S14 = 0
- L_or_S15 = 0
- L_or_S16 = 0
- L_or_S17 = 0
- if requestedData11 > 0
- L_or_S11 := -1
- if requestedData12 > 0
- L_or_S12 := -1
- if requestedData13 > 0
- L_or_S13 := -1
- if requestedData14 > 0
- L_or_S14 := -1
- if requestedData15 > 0
- L_or_S15 := -1
- if requestedData16 > 0
- L_or_S16 := -1
- if requestedData17 > 0
- L_or_S17 := -1
- //For Short Trend
- if requestedData11 < 0
- L_or_S11 := 1
- if requestedData12 < 0
- L_or_S12 := 1
- if requestedData13 < 0
- L_or_S13 := 1
- if requestedData14 < 0
- L_or_S14 := 1
- if requestedData15 < 0
- L_or_S15 := 1
- if requestedData16 < 0
- L_or_S16 := 1
- if requestedData17 < 0
- L_or_S17 := 1
- //Cells for Long or Short
- table.cell(MyTable, 6, 2, text = str.tostring(L_or_S11 > 0 ? "L" : "S"), bgcolor = requestedData11 < 0 ? color.green : color.red)
- table.cell(MyTable, 6, 3, text = str.tostring(L_or_S12 > 0 ? "L" : "S"), bgcolor = requestedData12 < 0 ? color.green : color.red)
- table.cell(MyTable, 6, 4, text = str.tostring(L_or_S13 > 0 ? "L" : "S"), bgcolor = requestedData13 < 0 ? color.green : color.red)
- table.cell(MyTable, 6, 5, text = str.tostring(L_or_S14 > 0 ? "L" : "S"), bgcolor = requestedData14 < 0 ? color.green : color.red)
- table.cell(MyTable, 6, 6, text = str.tostring(L_or_S15 > 0 ? "L" : "S"), bgcolor = requestedData15 < 0 ? color.green : color.red)
- table.cell(MyTable, 6, 7, text = str.tostring(L_or_S16 > 0 ? "L" : "S"), bgcolor = requestedData16 < 0 ? color.green : color.red)
- table.cell(MyTable, 6, 8, text = str.tostring(L_or_S17 > 0 ? "L" : "S"), bgcolor = requestedData17 < 0 ? color.green : color.red)
- //Cells for Implied Trend
- 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)
- 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)
- 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)
- 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)
- 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)
- 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)
- 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)
- //Average of all Implied Trends.
- 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
- table.cell(MyTable, 7, 9, text = str.tostring(math.round(Average, 3)), bgcolor = (Average > 0 ? color.green : color.red))
- table.cell(TableAvg, 0, 2, text = str.tostring(math.round(Average, 3)), bgcolor = (Average > 0 ? color.green : color.red))
- //Change color of Bar depending on Direction of Macro Correlation Implied Trend Average
- barcolor(not Barcolorchange ? na : Average > 0 ? color.green : color.red)
- plot(Average, "Average history", color.white)
Add Comment
Please, Sign In to add comment