StDev of last month

Hi everyone,

I’m currently developing an algorithm that needs to calculate the standard deviation of the las month. To do that, what I have done so far is calculate the points when the month is changing and then calculate the number of days between those points.

eom = (m != Ref(m, 1)); // End of month
countDays = BarsSince( Ref( eom, -1 ) ) + 1; //1, 2, 3, 4…
DaysInMonth = valuewhen( eom, countDays, 1 ); //20, 21, 20, 22…

Now, I should calculate the StDev using the corresponding DaysInMonth for each month but DaysInMonth is an array and the function only allow us to use a number and then it returns back an array and I don’t know how to manage it.
Can anyone help me with this last step?

Thanks in advance

As it happens, @Tomasz wrote a post recently on this very subject explaining how to do it.

If you can code the underlying formula for Std Dev it will make the process more efficient. And given it’s publicly available, that’s easily done. But there’s also an example of using the inbuilt function in that post too.


Built-in StDev() supports variable periods out-of-the-box (version 6.19 or higher).

NewMonth = Month() != Ref( Month(), -1 );
bars = BarsSince( NewMonth ) + 1;

x = StDev( Close, bars );
1 Like

I was using 6.10. I’ve updated my version and it’s already working.