The prt STD formula is not the appropriate one
Forums › ProRealTime English forum › ProBuilder support › The prt STD formula is not the appropriate one
- This topic has 21 replies, 6 voices, and was last updated 5 years ago by Bard.
-
-
09/28/2017 at 6:42 AM #47583
Hi, I’m new, I’m writing, greeting and thanking everyone in advance.
As I wrote in the commentary in this link: https://www.prorealcode.com/documentation/std/
“The default function STD in Prorealtime it is in truth like =STDEV.P in Excel: this formula is not suitable for financial analysis because assumes that its arguments are the entire population. We need the code to replicate =STDEV.S because financial data represent a sample of the population. I tried to change the formula you gave to get STDEV.S, but Prorealtime does not even accept what you wrote, figure the rest…”
09/28/2017 at 7:52 AM #47588The difference between these 2 Excel formulas is the sample size you take to calculate the standard deviation. A ‘sample’ or the entire population is the same in our case, because the data array is populated within the last N periods. I also notice that Office support say : “For large sample sizes, STDEV.S and STDEV.P return approximately equal values”, which make sense since the population taken in their formulas is the same unlike 1 unit.
09/28/2017 at 8:00 AM #47590This is fine if the data set is large, for small data sets the question is a bit more delicate … Is it possible to have the code for PRT of = STDEV.S? Or is there some limitation for that calculation? Just know it.
Thank you very much Nicolas
09/28/2017 at 8:06 AM #47592To be precise: “A ‘sample’ or the entire population is the same in our case, because the data array is populated within the last N periods”
It is not the same: I have verified the result of PRT standard deviation with Excel = STDEV.S of the same data set, and of course the result is different, but is identical to =STDEV.P, just as obviously.
Thank you two twice Nicolas
09/28/2017 at 8:14 AM #4759309/28/2017 at 8:17 AM #4759409/28/2017 at 8:31 AM #47596A quick code snippet:
12345678910111213141516// Math Variance function.Per=20decay=1sum = 0ssum = 0Data = closefor i = 0 to Per-Decay dosum = sum+Data[i]ssum = ssum+square(Data[i])next// Standard Deviation functionMystd=sqrt((ssum * Per - sum * sum) / (Per * (Per - 1)))return Mystd coloured(0,0,255),std[Per] coloured(255,0,0)This code is a code I took from another trading platform to get the Standard Deviation value. We can spot a difference if we take or not the last data of the array (for which I introduce a variable named “decay” you can play with).
First picture with the decay (other platform), second one without it and the curves are similar, so I’m wondering now what is the correct one .. 🙂
09/28/2017 at 8:38 AM #4760209/28/2017 at 11:40 AM #47643@Nicolas: I had problems following your calculation for the sample standard deviation. I tried to code it myself but I got the same result.
Here is how I calculated the sample standard deviation:
Sample Standard Deviation1234567891011121314151617181920sum = 0numerator=0Data = close// caluculate sample meanfor i = 0 to Period-1 dosum = sum+Data[i]nextSampleMean=sum/period// calculate samplevariancefor i=0 to period-1 donumerator=numerator+square(data[i]-SampleMean)nextSampleVariance=numerator/(period-1)// calculated sample standard deviationMystd=sqrt(SampleVariance)return Mystd coloured(0,0,255)You introduced the decay variable but I think it is unnecessary because it is always decay=1 for calculating the std.s.
And I agree with Jim that for our use the sample standard deviation might be more appropriate.
09/28/2017 at 12:21 PM #47655I made the decay variable to quickly find how and why there was a difference between the STD instruction and the one from Excel.
So the problem is that PRT internal calculation of the Standard Deviation is done with 1 more bar than the set period, do you agree?
0 to 20-1 is still 20 periods while 0 to 20-0 is 21 periods.
09/28/2017 at 2:20 PM #47683Yes, correct. PRT uses the formula for the population standard deviation like Jim pointed out. The difference in the two formulas is basically only the “-1” in the denominator for the sample variance (you have 20-1=19 in the denominator instead of 20 for the population variance).
It’s only a very subtle difference. I remember that I also thought it is strange when I learned this back in university but well that’s how it is.
You can read about it and check the formulas here:
https://statistics.laerd.com/statistical-guides/measures-of-spread-standard-deviation.php
09/29/2017 at 7:49 AM #47745Hello Despair, thanks for the new code. Yes, the correct standard deviation requires only 1 degree of freedom. STD.P is considered a distorted/biased calculation when the sample we are analyzing is not the entire population of the phenomenon (like financial data). Try to build new Bollinger Bands with the correct standard deviation and compare two identical ts, one with the default BBs, one with the correct BBs: change everything, better, worse? This is not the point: it changes correctly, everything here. So my humble advice to the company is this: to change the PRT’s default STD function directly, so that all indicators using standard deviation (and in PRT are so many) become automatically corrected. PRT is a great technical analysis platform, really complete, beautiful to see, easy to plan (although I’m not capable of something) and I always seemed strange to have this error (I activated my first subscription about 10 years ago and after a while I left it for this little absurdity which, however, fails the results when you analyze a bit more in depth). I still find this stupid mistake today, while everything else is wonderful (some tips on how to improve TS reports I would have, but it does not matter right now): anyway, a stupendous platform. Resolve that crap please 😀
09/29/2017 at 8:09 AM #47752I just compared the bollinger bands between MT4 and PRT on EURUSD Daily timeframe, seems that the BB from MT4 is pretty much closer with the default PRT BBands (violet one), than the different other one we calculated together (the blue one).. I did not compare with other platforms though..
09/29/2017 at 8:15 AM #47755I personally prefer this to the standard BB:
https://www.prorealcode.com/prorealtime-indicators/exponential-volatility-bands/
09/29/2017 at 8:17 AM #47756Hi Nicolas, the static rules are clear and unique, I do not know if other platforms use them correctly or not, I just know that if I’m considering sapping one, I choose what at least in basic calculations behaves as it should. And you’ve been very helpful, and I still thank you, because now I can also program a non-biased Value At Risk on PRT, and it is not a small possibility 😉
-
AuthorPosts
Find exclusive trading pro-tools on