Fxxtrader
Pine Script Rookie
Pine Script Rookie
Posts: 22
Joined: September 6th, 2020
Location: Toronto
Contact: Twitter TradingView Profile

Multiple Coefficient Correlation with Labels

Hello Coders/Traders

I added multiple Coefficient Correlation to chart, with labels to identify each symbol. However, label yloc is yloc.price. Label y.loc price value is not matching CC scale value, therefore I can't line up label to CC line.

Any suggestion to align both CC and label value to scale format?

Code: Select all

//** CAD - Coefficient Correlation of AUDCAD, NZDCAD, EURCAD, GBPCAD to USDCAD***

//@version=4
study(title="Correlation Coeff", shorttitle="CC", format=format.price, precision=3)
sym1 = input(title="Symbol", type=input.symbol, defval="OANDA:AUDCAD", confirm=true)
sym2 = input(title="Symbol", type=input.symbol, defval="OANDA:NZDCAD", confirm=true)
sym3 = input(title="Symbol", type=input.symbol, defval="OANDA:EURCAD", confirm=true)
sym4 = input(title="Symbol", type=input.symbol, defval="OANDA:GBPCAD", confirm=true)

src = input(close, title="Source")
length = input(20, minval=1)
res=timeframe.period

ovr1 = security(sym1, res, src)
ovr2 = security(sym2, res, src)
ovr3 = security(sym3, res, src)
ovr4 = security(sym4, res, src)
hline = 0.0

plot(correlation(src, ovr1, length), "AUDCAD", color=color.olive, style=plot.style_line, transp=0)
plot(correlation(src, ovr2, length), "NZDCAD", color=color.fuchsia, style=plot.style_line, transp=0)
plot(correlation(src, ovr3, length), "EURCAD", color=color.blue, style=plot.style_line, linewidth=2, transp=0)
plot(correlation(src, ovr4, length), "GBPCAD", color=color.green, style=plot.style_line, linewidth=2, transp=0)
plot(hline, title="Zero Line", color=color.white, style=plot.style_linebr, linewidth=2)

// Offset label
forward_Offset1 = input(1, title="Offset Label 1")
forward_Offset2 = input(5, title="Offset Label 2")
forward_Offset3 = input(10, title="Offset Label 3")
forward_Offset4 = input(15, title="Offset Label 4")

bar_Index_Duration = time - time[1]
lable_Forward_Offset1 = forward_Offset1 * bar_Index_Duration
lable_Forward_Offset2 = forward_Offset2 * bar_Index_Duration
lable_Forward_Offset3 = forward_Offset3 * bar_Index_Duration
lable_Forward_Offset4 = forward_Offset4 * bar_Index_Duration

//Plot Label
cclbl1 = label.new(x = time + lable_Forward_Offset1, y = close,  text = "AUDCAD", xloc = xloc.bar_time, yloc = yloc.price, color = color.olive, style = label.style_label_center, textcolor = color.white, size = size.small, textalign = text.align_center)
cclbl2 = label.new(x = time + lable_Forward_Offset2, y = close,  text = "NZDCAD", xloc = xloc.bar_time, yloc = yloc.price, color = color.fuchsia, style = label.style_label_down, textcolor = color.white, size = size.small, textalign = text.align_center)
cclbl3 = label.new(x = time + lable_Forward_Offset3, y = close,  text = "EURCAD", xloc = xloc.bar_time, yloc = yloc.price, color = color.blue, style = label.style_label_left, textcolor = color.white, size = size.small, textalign = text.align_center)
cclbl4 = label.new(x = time + lable_Forward_Offset4, y = close,  text = "GBPCAD", xloc = xloc.bar_time, yloc = yloc.price, color = color.green, style = label.style_label_up, textcolor = color.white, size = size.small, textalign = text.align_center)

label.delete(cclbl1[1])
label.delete(cclbl2[1])
label.delete(cclbl3[1])
label.delete(cclbl4[1])

//What are y.loc options for overlay=false?  y.loc for overlay=false, it can't be price, scale is not the same as price.  
Thank you.

Fxxtrader
Pine Script Rookie
Pine Script Rookie
Posts: 22
Joined: September 6th, 2020
Location: Toronto
Contact: Twitter TradingView Profile

Re: Multiple Coefficient Correlation with Labels

Okay, got the lines aligned with labels. I needed to separate and isolate the calculations, so I can then reference it as a y location in label.

I added this as it's own lines:

Code: Select all

cc1 = (correlation(src, ovr1, length))
cc2 = (correlation(src, ovr2, length))
cc3 = (correlation(src, ovr3, length))
cc4 = (correlation(src, ovr4, length))
cc5 = (correlation(src, ovr5, length))
cc6 = (correlation(src, ovr6, length))

Instead of:

Code: Select all

plot(correlation(src, ovr3, length), "EURCAD", color=color.blue, style=plot.style_line, linewidth=2, transp=0)
plot(correlation(src, ovr4, length), "GBPCAD", color=color.green, style=plot.style_line, linewidth=2, transp=0)
plot(correlation(src, ovr5, length), "CADJPY", color=color.navy, style=plot.style_line, linewidth=3, transp=0)
plot(correlation(src, ovr6, length), "CADCHF", color=color.maroon, style=plot.style_line, linewidth=3, transp=0)

Now I can plot labels with y location referring to the actual CC value: full script

Code: Select all

cclbl1 = label.new(x = time + lable_Forward_Offset1, y = cc1,  text = "AUDCAD", xloc = xloc.bar_time, yloc = yloc.price, color = color.olive, style = label.style_label_left, textcolor = color.white, size = size.small, textalign = text.align_center)
cclbl2 = label.new(x = time + lable_Forward_Offset2, y = cc2,  text = "NZDCAD", xloc = xloc.bar_time, yloc = yloc.price, color = color.aqua, style = label.style_label_left, textcolor = color.white, size = size.small, textalign = text.align_center)
cclbl3 = label.new(x = time + lable_Forward_Offset3, y = cc3,  text = "EURCAD", xloc = xloc.bar_time, yloc = yloc.price, color = color.blue, style = label.style_label_left, textcolor = color.white, size = size.small, textalign = text.align_center)
cclbl4 = label.new(x = time + lable_Forward_Offset4, y = cc4,  text = "GBPCAD", xloc = xloc.bar_time, yloc = yloc.price, color = color.green, style = label.style_label_center, textcolor = color.white, size = size.small, textalign = text.align_center)
cclbl5 = label.new(x = time + lable_Forward_Offset4, y = cc5,  text = "CADJPY", xloc = xloc.bar_time, yloc = yloc.price, color = color.navy, style = label.style_label_left, textcolor = color.white, size = size.tiny, textalign = text.align_center)
cclbl6 = label.new(x = time + lable_Forward_Offset4, y = cc6,  text = "CADCHF", xloc = xloc.bar_time, yloc = yloc.price, color = color.maroon, style = label.style_label_left, textcolor = color.white, size = size.tiny, textalign = text.align_center)

Now I have labels aligning with corresponding Coefficient correlation line

Code: Select all

//** CAD - Coefficient Correlation of AUDCAD, NZDCAD, EURCAD, GBPCAD to USDCAD***

//@version=4
study(title="Correlation Coeff", shorttitle="CC", format=format.inherit, precision=3)
sym1 = input(title="Symbol", type=input.symbol, defval="OANDA:AUDCAD", confirm=true)
sym2 = input(title="Symbol", type=input.symbol, defval="OANDA:NZDCAD", confirm=true)
sym3 = input(title="Symbol", type=input.symbol, defval="OANDA:EURCAD", confirm=true)
sym4 = input(title="Symbol", type=input.symbol, defval="OANDA:GBPCAD", confirm=true)
sym5 = input(title="Symbol", type=input.symbol, defval="OANDA:CADJPY", confirm=true)
sym6 = input(title="Symbol", type=input.symbol, defval="OANDA:CADCHF", confirm=true)

src = input(close, title="Source")
length = input(20, minval=1)
res=timeframe.period

ovr1 = security(sym1, res, src)
ovr2 = security(sym2, res, src)
ovr3 = security(sym3, res, src)
ovr4 = security(sym4, res, src)
ovr5 = security(sym5, res, src)
ovr6 = security(sym6, res, src)

cc1 = (correlation(src, ovr1, length))
cc2 = (correlation(src, ovr2, length))
cc3 = (correlation(src, ovr3, length))
cc4 = (correlation(src, ovr4, length))
cc5 = (correlation(src, ovr5, length))
cc6 = (correlation(src, ovr6, length))

hline = 0.0

plot(cc1, "AUDCAD", color=color.olive, style=plot.style_line, transp=0)
plot(cc2, "NZDCAD", color=color.aqua, style=plot.style_line, transp=0)
plot(correlation(src, ovr3, length), "EURCAD", color=color.blue, style=plot.style_line, linewidth=2, transp=0)
plot(correlation(src, ovr4, length), "GBPCAD", color=color.green, style=plot.style_line, linewidth=2, transp=0)
plot(correlation(src, ovr5, length), "CADJPY", color=color.navy, style=plot.style_line, linewidth=3, transp=0)
plot(correlation(src, ovr6, length), "CADCHF", color=color.maroon, style=plot.style_line, linewidth=3, transp=0)

plot(hline, title="Zero Line", color=color.white, style=plot.style_linebr, linewidth=2)

// Offset label
forward_Offset1 = input(1, title="Offset Label 1")
forward_Offset2 = input(3, title="Offset Label 2")
forward_Offset3 = input(5, title="Offset Label 3")
forward_Offset4 = input(7, title="Offset Label 4")
forward_Offset5 = input(9, title="Offset Label 5")
forward_Offset6 = input(11, title="Offset Label 6")

bar_Index_Duration = time - time[1]
lable_Forward_Offset1 = forward_Offset1 * bar_Index_Duration
lable_Forward_Offset2 = forward_Offset2 * bar_Index_Duration
lable_Forward_Offset3 = forward_Offset3 * bar_Index_Duration
lable_Forward_Offset4 = forward_Offset4 * bar_Index_Duration
lable_Forward_Offset5 = forward_Offset5 * bar_Index_Duration
lable_Forward_Offset6 = forward_Offset6 * bar_Index_Duration

//Plot Label
cclbl1 = label.new(x = time + lable_Forward_Offset1, y = cc1,  text = "AUDCAD", xloc = xloc.bar_time, yloc = yloc.price, color = color.olive, style = label.style_label_left, textcolor = color.white, size = size.small, textalign = text.align_center)
cclbl2 = label.new(x = time + lable_Forward_Offset2, y = cc2,  text = "NZDCAD", xloc = xloc.bar_time, yloc = yloc.price, color = color.aqua, style = label.style_label_left, textcolor = color.white, size = size.small, textalign = text.align_center)
cclbl3 = label.new(x = time + lable_Forward_Offset3, y = cc3,  text = "EURCAD", xloc = xloc.bar_time, yloc = yloc.price, color = color.blue, style = label.style_label_left, textcolor = color.white, size = size.small, textalign = text.align_center)
cclbl4 = label.new(x = time + lable_Forward_Offset4, y = cc4,  text = "GBPCAD", xloc = xloc.bar_time, yloc = yloc.price, color = color.green, style = label.style_label_center, textcolor = color.white, size = size.small, textalign = text.align_center)
cclbl5 = label.new(x = time + lable_Forward_Offset4, y = cc5,  text = "CADJPY", xloc = xloc.bar_time, yloc = yloc.price, color = color.navy, style = label.style_label_left, textcolor = color.white, size = size.tiny, textalign = text.align_center)
cclbl6 = label.new(x = time + lable_Forward_Offset4, y = cc6,  text = "CADCHF", xloc = xloc.bar_time, yloc = yloc.price, color = color.maroon, style = label.style_label_left, textcolor = color.white, size = size.tiny, textalign = text.align_center)

label.delete(cclbl1[1])
label.delete(cclbl2[1])
label.delete(cclbl3[1])
label.delete(cclbl4[1])
label.delete(cclbl5[1])
label.delete(cclbl6[1])
Thanks.

User avatar
Matthew
Site Admin
Site Admin
Posts: 92
Joined: July 1st, 2020
Location: Australia
Contact: Website Facebook Twitter TradingView Profile

Re: Multiple Coefficient Correlation with Labels

That's awesome! Well done Fxxtrader and thanks for sharing your detailed solution, I'm sure that will help others who run into similar issues in the future. Keep up the great work :)

Return to “Pine Script Q&A”