implement SMA, EMA in addition to WMA

This commit is contained in:
2022-07-13 11:17:28 +09:00
parent 3d2a454e96
commit becffcd1fb
5 changed files with 98 additions and 53 deletions

View File

@@ -1,2 +1,2 @@
from algorithm.wma import WMA
from algorithm.ma import SMA, WMA, EMA, MA_Cross
from algorithm.status import Status

64
algorithm/ma.py Normal file
View File

@@ -0,0 +1,64 @@
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

View File

@@ -1,33 +0,0 @@
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