import datetime import json import python_bitbankcc import random import sys import time import algorithm as algo 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, "xlm_jpy", 10*60)) procs.append(Proc(pri, "xlm_jpy", 5000, algo.MA_Cross(algo.EMA(), "1h", 0.01), 10*60)) procs.append(Fetcher(pub, "btc_jpy", 10*60)) procs.append(Proc(pri, "btc_jpy", 5000, algo.MA_Cross(algo.EMA(), "1h", 0.01), 10*60)) procs.append(Fetcher(pub, "eth_jpy", 10*60)) procs.append(Proc(pri, "eth_jpy", 5000, algo.MA_Cross(algo.EMA(), "1h", 0.01), 10*60)) procs.append(Fetcher(pub, "matic_jpy", 5)) procs.append(Proc(pri, "matic_jpy", 1000, algo.MA_Cross(algo.EMA(), "1m", 0.01), 5)) return procs class Fetcher: def __init__(self, pub, pair, interval): self._interval = interval self._last_update = 0 self._pub = pub self._pair = pair self._candle_prev = {} def tick(self): now = time.time() if now-self._last_update < self._interval: return try: st = algo.Status() st.candles["1m"] = self._get_candle("1min", 60) st.candles["1h"] = self._get_candle("1hour", 60) st.price = st.candles["1m"][0][3] global status status[self._pair] = st except Exception as e: print("fetce error:", e) self._last_update = now def _get_candle(self, unit, n, t = datetime.datetime.now(tz=datetime.timezone.utc)): data = self._pub.get_candlestick( self._pair, unit, t.strftime("%Y%m%d")) data = data["candlestick"][0]["ohlcv"] ret = [] for i in range(min(n, 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])]) if len(ret) < n: t = t - datetime.timedelta(days=1) data = self._get_candle(unit, n-len(ret), t) ret = [*ret, *data] 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} / -{self._asset}") 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*st.price}") 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(")