Custom Standard Deviation

StrataSearch has many powerful features, with many techniques and approaches to explore. Here we discuss the things we've done, and the things we'd like to do.

Custom Standard Deviation

Postby thechartist13 » Sat Aug 17, 2013 8:03 pm

Hi Pete (Or anyone else who wants to read),

I'm trying to write a custom standard deviation formula that calculates the standard deviation for only positive/negative days.
I'll include my custom formula below, but it seems to be creating a graph that has many "holes and gaps" in it. I would understand this happening if the custom formula were returning undefined values but from what I gather I don't believe it should be. Here is the code:

days=parameter("Days");

psum=sum(if(close>open,power((close-open),2),0),days);
pdays=hadalert(close>open,days);
PDEV=sqrt(psum/pdays);


Just to quickly explain, the "psum" should just be the summation of all the squared positive daily changes and 0's for any negative days.

The pdays should be returning the number of days the close is greater than the open.

Finally, the standard deviation formula is the sum of the squared deviations, divided by the number of periods included.

I'm almost positive I've got my math right, but I can't seem to understand why I've got gaps and holes.

Thanks!

Joe
thechartist13
 
Posts: 22
Joined: Mon Oct 18, 2010 7:18 pm

Re: Custom Standard Deviation

Postby Overload » Sun Aug 18, 2013 9:00 am

The problem is actually in the power() formula, which does not handle negative numbers in the way that it probably should. So on days where your "close-open" is 0 or a positive number, it will work fine. But on days where that is negative, you are getting the gap.

To solve this, just wrap your "close-open" within the abs() formula to get the absolute value:

Code: Select all
days=parameter("Days");

psum=sum(if(close>open,power(abs(close-open),2),0),days);
pdays=hadalert(close>open,days);
PDEV=sqrt(psum/pdays);


This will work just fine as long as you're raising the power to an even number (2, 4, 6, 8, etc). If you were to raise to an odd number, a more complicated solution will be needed. But I will put this on the list so that negatives will have better handling in the future.

Pete
Overload
 
Posts: 2247
Joined: Wed Nov 30, 2005 12:14 pm


Return to General Discussion

cron