Algorithm to calculate stoch?

I am puzzled by how to calculate stochastic. This is the psuedocode I have:

stoch_fastK(list):
````{_, _, _, todaysClose} = list.first()
````low = min(list)
````high = max(list)
````((todayClose - low) / (high - low)) * 100

stoch(list, period, d, k):
````smoothK = for 1..k, 0, fun(kIteration, acc)
````````acc + stoch_fastK( sublist(list, kIteration, period)
````smoothK / k

stoch(last_20_datapoints_reverse_order, 14, 7, 2)

last_20_datapoints_reverse_order, means that the first member is todays close, the next is yesterdays, etc.

And my results are different from trading view. I am only looking at the smooth K line.

Can anyone help me complete this?
 
Ah I think I see atleast one mistake. The smooth K is over the period length. So to have a smooth k of 2 I need at least 14x2 datapoints, 20 is not enough?

So take the first 14 points calculate fast K, take the next 14 points, calculate fast K. Add both and / 2?
 
Back
Top