improve WMA algorithm

This commit is contained in:
falsycat 2022-07-13 08:27:16 +09:00
parent ecb9ad08e1
commit a5f951dea9
3 changed files with 41 additions and 27 deletions

View File

@ -1,22 +1,33 @@
class WMA:
def __init__(self, unit, period, width):
self._unit = unit
self._period = period
self._width = width
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(self._period-1, len(cands)-1)):
sum += cands[i+1][3] * (i+1)
for i in range(min(period, len(cands))):
sum += cands[i][3] * (i+1)
den += i+1
avg = sum / den
price = cands[0][3]
width = price * self._width
diff = price - avg
if diff < 0:
return max(-1, diff/width) # sell
else:
return min(1, diff/width) # buy
return sum / den

View File

@ -26,9 +26,9 @@ def init():
procs.append(Fetcher(pub, "btc_jpy"))
procs.append(Fetcher(pub, "eth_jpy"))
procs.append(Fetcher(pub, "matic_jpy"))
procs.append(Proc(pri, "btc_jpy", 5000, algorithm.WMA("1h", 8, 0.01), 10*60))
procs.append(Proc(pri, "eth_jpy", 5000, algorithm.WMA("1h", 8, 0.01), 10*60))
procs.append(Proc(pri, "matic_jpy", 5000, algorithm.WMA("1m", 8, 0.01), 5))
procs.append(Proc(pri, "btc_jpy", 5000, algorithm.WMA("1h", 0.01), 10*60))
procs.append(Proc(pri, "eth_jpy", 5000, algorithm.WMA("1h", 0.01), 10*60))
procs.append(Proc(pri, "matic_jpy", 5000, algorithm.WMA("1m", 0.01), 5))
return procs
@ -60,11 +60,11 @@ class Fetcher:
data = self._pub.get_candlestick(
self._pair,
unit,
datetime.datetime.now().strftime("%Y%m%d"))
datetime.datetime.now(tz=datetime.timezone.utc).strftime("%Y%m%d"))
data = data["candlestick"][0]["ohlcv"]
ret = []
for i in range(min(30, len(data))):
for i in range(min(60, len(data))):
j = len(data)-i-1
ret.append([float(data[j][0]), float(data[j][1]), float(data[j][2]), float(data[j][3])])
return ret
@ -82,7 +82,8 @@ class Proc:
self._algo = algo
self._interval = interval
self._buy_price = None
self._last_order = None
self._buy_price = 0
self._asset = asset
@ -98,7 +99,8 @@ class Proc:
pbsi = self._bsi
bsi = self._algo.judge(status[self._pair])
self._bsi = bsi
if bsi != 0:
self._bsi = bsi
print(f"[bitbank: {self._pair}] BSI {pbsi} -> {bsi}")
if bsi*pbsi < 0:
@ -109,18 +111,19 @@ class Proc:
self._buy_price = st.price
print(f"[bitbank: {self._pair}] <BUY> amount: {amount}")
except Exception as e:
self._buy_price = None
print(f"[bitbank: {self._pair}] buy error", e)
elif self._buy_price is not None:
self._buy_price = 0
elif self._buy_price > 0:
try:
amount = self._asset/self._buy_price
self._order_market(self._asset/self._buy_price, "sell")
self._order_market(amount, "sell")
print(f"[bitbank: {self._pair}] <SELL> amount: {amount}")
except Exception as e:
print(f"[bitbank: {self._pair}] sell error", e)
def _order_market(self, amount, sell_or_buy):
self._pri.order(self._pair, None, str(amount), sell_or_buy, "market")
order = self._pri.order(self._pair, None, str(amount), sell_or_buy, "market")
self._last_order = order
def _order_limit(self, price, sell_or_buy):
try_price = price

View File

@ -13,7 +13,7 @@ def main():
procs[i].tick()
time.sleep(1)
def on_exit(sig):
def on_exit(sig, x):
print(f"received {sig}")
proc = multiprocessing.Process(target = main)