class KDJ:
history = []
code = ''
min = 99999.99
max = 0.0
k = 0.0
d = 0.0
rsv = 0.0
def __init__(self,code):
self.code = code
def checkMinAndMax(self,n):
min = 99999.9
max = 0.0
for item in self.history:
if item[3] < min:
min = item[3]
if item[4] > max:
max = item[4]
return [min,max]
def update(self,date,start,close,low,high,n):
self.history.append([date,start,close,low,high])
if len(self.history) < n:
self.min = min(self.min,low)
self.max = max(self.max,high)
return [date,self.k,self.d,0,start,close,low,high]
else:
if len(self.history) > n:
del self.history[0]
p = self.checkMinAndMax(n)
rsv = (close - p[0])/(p[1]-p[0])*100.0
oldk = self.k
oldd = self.d
self.k = (rsv + 2*oldk)/3.0
self.d = (self.k + 2*oldd)/3.0
self.k = min(100,max(self.k,0))
self.d = min(100,max(self.d,0))
j = 3*self.k - 2*self.d
j = min(100,max(j,0))
return [date,self.k,self.d,j,start,close,low,high]