tmm/algorithm/ma.py

65 lines
1.4 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, width):
self._ma = ma
self._unit = unit
self._width = width
self._period_short = 5
self._period_mid = 21
self._period_long = 55
self._posi_amp = 0.1
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)
long = self._ma.calc(cands, self._period_long)
width = mid * self._width
posi = max(-1, min((short-long)/(2.5*width), 1)) * self._posi_amp
diff = short - mid
if diff < 0:
return max(-1, diff/width+posi) # sell
else:
return min(1, diff/width+posi) # buy