import datetime import json import python_bitbankcc import random import sys import time import algorithm FETCH_INTERVAL = 5 status = {} def init(): try: sec = json.load(open("secret.json")) sec = sec["bitbank"] pub = python_bitbankcc.public() pri = python_bitbankcc.private(sec["key"], sec["secret"]) except Exception as e: print("failed to initialize bitbank features: ", e) return [] procs = [] 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", 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 class Fetcher: def __init__(self, pub, pair): self._last_update = 0 self._pub = pub self._pair = pair def tick(self): if time.time()-self._last_update < FETCH_INTERVAL: return try: st = algorithm.Status() st.candles["1m"] = self._get_candle("1min") st.candles["1h"] = self._get_candle("1hour") st.price = st.candles["1m"][0][3] global status status[self._pair] = st except Exception as e: print("fetce error:", e) return def _get_candle(self, unit): data = self._pub.get_candlestick( self._pair, unit, datetime.datetime.now(tz=datetime.timezone.utc).strftime("%Y%m%d")) data = data["candlestick"][0]["ohlcv"] ret = [] 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 class Proc: def __init__(self, pri, pair, asset, algo, interval): self._pri = pri self._pair = pair self._last_update = 0 self._now = 0 self._bsi = 0 self._algo = algo self._interval = interval self._last_order = None self._buy_price = 0 self._asset = asset def tick(self): self._now = time.time() if self._now-self._last_update > self._interval: self._update() self._last_update = self._now def _update(self): global status st = status[self._pair] pbsi = self._bsi bsi = self._algo.judge(status[self._pair]) if bsi != 0: self._bsi = bsi print(f"[bitbank: {self._pair}] BSI {pbsi} -> {bsi}") if bsi*pbsi < 0: if bsi > 0: try: amount = self._asset/st.price self._order_market(amount, "buy") self._buy_price = st.price print(f"[bitbank: {self._pair}] amount: {amount}") except Exception as e: print(f"[bitbank: {self._pair}] buy error", e) self._buy_price = 0 elif self._buy_price > 0: try: amount = self._asset/self._buy_price self._order_market(amount, "sell") print(f"[bitbank: {self._pair}] amount: {amount}") except Exception as e: print(f"[bitbank: {self._pair}] sell error", e) def _order_market(self, amount, sell_or_buy): 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 for i in range(10): amount = self._asset/price order = self._pri.order(self._pair, str(price), str(amount), sell_or_buy, "limit", True) time.sleep(.1) order = self._pri.get_order(self._pair, order["order_id"]) if order["status"] == "UNFILLED": return move = random.random()*10+1 if sell_or_buy == "sell": price = price-move else: price = price+move if sell_or_buy == "sell": self._pri.order(self._pair, None, str(amount), sell_or_buy, "market") print(f"[bitbank: {self._pair}] amount: {amount}") else: raise Exception("tried 10 times.... X(")