Good evening, I ask politely if you can have the code for Z-Score.
The formula is: (Close – Average close to n periods) / Standard Deviation to n periods.
The indicator should show the lines at +2 (green) and -2 (red) standard deviations and the 0 level (which is when the Close = Average Close).
Thanks in advance for the help
Yes of course. Z-score.. I used to work a lot with it, I’m a ‘mean reverting theory’ lover.. 🙂
n = 20
avg=average[n](close)
st = std[n](close)
zscore = (Close-avg)/st
RETURN zscore
You can combine different n time horizons to compound a more precise deviation for instance. What is the purpose of this request? 🙂
Good morning Nicolas, thanks for your response and your availability.
The purpose of my request is to use the indicator to see when the prices are very far from the average, and then locate the probable turning points. In my opinion it works well especially when there are divergences with prices.
I plan to also use it with other indicators … I’ll let you know.
Kindly ask you if you can draw horizontal lines to +2 (green) and a – 2 (red) to see immediately when prices exceed the standard deviation
What you are doing here is normalizing the bollinger bands indicator. Try to set a Bollinger bands on price and you’ll see the exact same informations (with n=20 of course). Though, Z-score is more convenient view I agree.
To add these 2 lines, replace the last line of code with this :
RETURN zscore, 2 coloured(0,255,0), -2 coloured(255,0,0)
Have a nice day.
Thank you very much Nicolas. I thank you also for your advice on BB. You’re really kind. If I let you know the news.Have a nice day