pandas-technical-indicators icon indicating copy to clipboard operation
pandas-technical-indicators copied to clipboard

Calculation for the Stochastic Oszillator is wrong.

Open HermanoCrespo opened this issue 7 years ago • 5 comments

SOk = pd.Series((df['Close'] - df['Low']) / (df['High'] - df['Low']), name='SO%k')

is not the correct calculation of the stochastics %K

See here: http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:stochastic_oscillator_fast_slow_and_full

%K = (Current Close - Lowest Low)/(Highest High - Lowest Low) * 100 %D = 3-day SMA of %K

Lowest Low = lowest low for the look-back period Highest High = highest high for the look-back period %K is multiplied by 100 to move the decimal point two places

HermanoCrespo avatar Jun 01 '18 03:06 HermanoCrespo

@HermanoCrespo , thanks for the contribution! Do you think you could submit a PR for this ? I'm currently swamped and might not have the time to update the function in the next days.

deepbrook avatar Jun 07 '18 12:06 deepbrook

I agree with @HermanoCrespo and the link is a very good reference. The usual lookback period for this is 14 periods (ie days, weeks, months) and the smoothing function is typically a 3 or 5 period simple moving average of %k. Can be described as (14,3,SMA) or(14,5,SMA). Should pass the lookback period and the smoothing period as parameters to the function for maximum flexibility. I am new to python but I might take a crack at it as I am converting my excel macro that does this calculation to python.

KeithLW avatar Jun 13 '18 01:06 KeithLW

I tried to post my stochastic version. I am new to GitHub and don't know if I did it correct. I think I have a fork under my name with the proposed function. If I am to just paste it here let me know. It didn't look like the correct format when I just pasted it here.

KeithLW avatar Jun 16 '18 19:06 KeithLW

https://help.github.com/articles/creating-a-pull-request/

Have a look here!

deepbrook avatar Jun 18 '18 11:06 deepbrook

@HermanoCrespo This sentence

%K = (Current Close - Lowest Low)/(Highest High - Lowest Low) * 100

it should not be

'%K = (Current Close - Lowest Low)*100/(Highest High - Lowest Low) '

I'm sorry if I are wrong. Best regards

akitxu avatar Nov 07 '21 21:11 akitxu