tmm/algorithm/wma.py
2022-07-13 08:27:16 +09:00

34 lines
840 B
Python

class WMA:
def __init__(self, unit, width):
self._unit = unit
self._width = width
self._period_short = 7
self._period_mid = 15
self._period_long = 60
self._posi_amp = 0.1
def judge(self, status):
cands = status.candles[self._unit]
short = self._wma(cands, self._period_short)
mid = self._wma(cands, self._period_mid)
long = self._wma(cands, self._period_long)
width = mid * self._width
posi = max(-1, min((short-long)/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
def _wma(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