Correlation Percentile

Samus Aran

New member
hey everyone, I'm trying to make my own Correlation indicator with just adding as a modification the Mean & Percentile from the original indicator that ThinkorSwim provided . I got the mean to work, but I'm having trouble with the Percentile. I don't want the Percentile to be display on the graph; I want it display on the title of the indicator above its window, so I can get just coefficient Percentile of this instrument and compare the Percentile coefficient with other instruments.

sincerely

Code:
declare lower;

input length = 2000;
input correlationWithSecurity = "SPY";

plot Correlation = Correlation(close, close(correlationWithSecurity), length);
Correlation.SetDefaultColor(GetColor(5));

plot ratioAvg = ExpAverage(Correlation, length);

plot center = 0;

def currentCorrelation = Correlation(BarNumber(), close, length);

CorrelationPercentile = AsPercent((currentCorrelation / length));

nevermind the code above, let me reword my question: I would like to make a counter to count how many bars were under 0 and how many bars were above 0 on the Correlation axis of 0. And then compare the two percentages of above 0 & below 0 to see if these two instruments compared with each other are "for example 60%" negatively correlated or they are more positively correlated. Again I don't want it to be 60%, its just an example; I just want to find if most of the time if they are positively correlated or negatively correlated & what's that specific percentage.

Ty for the ones who decide to help me

Code:
declare lower;

input length = 14;
input length2 = 11682;

input correlationWithSecurity = "SPY";

plot Correlation =  Correlation(close, close(correlationWithSecurity), length);
Correlation.SetDefaultColor(GetColor(5));

plot ratioAvg = ExpAverage(Correlation, length);

plot center = 0;

rec postive = if Correlation > 0 then postive + 1 else 0;

rec negative = if Correlation < 0 then negative + 1 else 0;

def temp = Max ( postive,  negative);

plot PercentChg = 100 * ( temp / temp[length2] - 1);

Last edited:
@Samus Aran I don't really understand how counting the amount of bars above 0 will lead to correlation between 2 tickers. The Correlation study itself will tell you that from -1 to 1/ -100% to 100% based on some given length. So if length is 14 and study says .41 then correlation is 41% for the past 14 bars. Maybe I'm not getting it.

let me explain further, the correlation study just tells you what is the correlation for that candlestick at that given time "for example on Tuesday 2:00pm it had +0.05". That does not tell me if in its entirety is this instrument more negatively correlated or positively correlated . I would like to add up all the correlations at each candle stick and at the end I would like to determine that within a 6 month period which is the highest that the chart goes, how much in its entirety is this instrument more negatively correlated or positively correlated or they are exactly the same at 0.

entirety == 6 months

Not the exact question you're looking for?

87k+ Posts
364 Online

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?