57 lines
1.1 KiB
Python
57 lines
1.1 KiB
Python
class SMA:
|
|
def __init__(self):
|
|
return
|
|
|
|
def calc(self, cands, period):
|
|
sum = 0
|
|
n = min(period, len(cands))
|
|
for i in range(n):
|
|
sum += cands[i][3]
|
|
return sum / n
|
|
|
|
class EMA:
|
|
def __init__(self):
|
|
return
|
|
|
|
def calc(self, cands, period):
|
|
n = min(period, len(cands))
|
|
ret = 0
|
|
for i in range(n):
|
|
ret += cands[i][3]
|
|
ret /= n
|
|
for i in range(n):
|
|
ret = (2/(1+n))*(cands[n-i-1][3] - ret) + ret
|
|
return ret
|
|
|
|
class WMA:
|
|
def __init__(self):
|
|
return
|
|
|
|
def calc(self, cands, period):
|
|
sum = 0
|
|
den = 0
|
|
for i in range(min(period, len(cands))):
|
|
sum += cands[i][3] * (i+1)
|
|
den += i+1
|
|
return sum / den
|
|
|
|
|
|
class MA_Cross:
|
|
def __init__(self, ma, unit):
|
|
self._ma = ma
|
|
self._unit = unit
|
|
|
|
self._period_short = 21
|
|
self._period_mid = 55
|
|
|
|
def judge(self, status):
|
|
cands = status.candles[self._unit]
|
|
short = self._ma.calc(cands, self._period_short)
|
|
mid = self._ma.calc(cands, self._period_mid)
|
|
|
|
diff = short - mid
|
|
if diff < 0:
|
|
return -1 # sell
|
|
else:
|
|
return 1 # buy
|