make more stable

This commit is contained in:
falsycat 2022-07-13 17:50:04 +09:00
parent becffcd1fb
commit fecec43e33
2 changed files with 31 additions and 144 deletions

View File

@ -23,17 +23,26 @@ def init():
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, "qtum_jpy", 5*60))
procs.append(Proc(pri, "qtum_jpy", 2000, algo.MA_Cross(algo.EMA(), "1h", 0.01), 5*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, "xrp_jpy", 5*60))
procs.append(Proc(pri, "xrp_jpy", 2000, algo.MA_Cross(algo.EMA(), "1h", 0.01), 5*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, "xlm_jpy", 5*60))
procs.append(Proc(pri, "xlm_jpy", 2000, algo.MA_Cross(algo.EMA(), "1h", 0.01), 5*60))
procs.append(Fetcher(pub, "matic_jpy", 5))
procs.append(Proc(pri, "matic_jpy", 1000, algo.MA_Cross(algo.EMA(), "1m", 0.01), 5))
procs.append(Fetcher(pub, "bat_jpy", 5*60))
procs.append(Proc(pri, "bat_jpy", 2000, algo.MA_Cross(algo.EMA(), "1h", 0.01), 5*60))
procs.append(Fetcher(pub, "xym_jpy", 5*60))
procs.append(Proc(pri, "xym_jpy", 2000, algo.MA_Cross(algo.EMA(), "1h", 0.01), 5*60))
procs.append(Fetcher(pub, "btc_jpy", 5*60))
procs.append(Proc(pri, "btc_jpy", 5000, algo.MA_Cross(algo.EMA(), "1h", 0.01), 5*60))
procs.append(Fetcher(pub, "eth_jpy", 5*60))
procs.append(Proc(pri, "eth_jpy", 5000, algo.MA_Cross(algo.EMA(), "1h", 0.01), 5*60))
return procs
@ -54,10 +63,8 @@ class Fetcher:
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]
st.price = st.candles["1h"][0][3]
global status
status[self._pair] = st
@ -85,21 +92,21 @@ class Fetcher:
class Proc:
def __init__(self, pri, pair, asset, algo, interval):
def __init__(self, pri, pair, lot, algo, interval):
self._pri = pri
self._pair = pair
self._last_update = 0
self._now = 0
self._bsi = 0
self._bsi = -1
self._algo = algo
self._interval = interval
self._last_order = None
self._buy_price = 0
self._asset = asset
self._lot = lot
def tick(self):
self._now = time.time()
@ -120,16 +127,16 @@ class Proc:
if bsi*pbsi < 0:
if bsi > 0:
try:
amount = self._asset/st.price
amount = self._lot/st.price
self._order_market(amount, "buy")
self._buy_price = st.price
print(f"[bitbank: {self._pair}] <BUY> +{amount} / -{self._asset}")
print(f"[bitbank: {self._pair}] <BUY> +{amount} / -{self._lot}")
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
amount = self._lot/self._buy_price
self._order_market(amount, "sell")
print(f"[bitbank: {self._pair}] <SELL> -{amount} / +{amount*st.price}")
except Exception as e:
@ -138,26 +145,3 @@ class Proc:
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}] <MARKET-SELL> amount: {amount}")
else:
raise Exception("tried 10 times.... X(")

111
main.py
View File

@ -11,114 +11,17 @@ alive = True
def main():
global alive
procs = [*bitbank.init()]
cnt = 0
while alive:
for i in range(len(procs)):
procs[i].tick()
time.sleep(0.5)
for i in range(10):
procs[cnt%len(procs)].tick()
cnt += 1
time.sleep(1)
def on_exit(sig, x):
global alive
alive = False
proc = multiprocessing.Process(target = main)
proc.start()
signal.signal(signal.SIGTERM, on_exit)
signal.pause()
proc.join()
# while True:
# print("start tick")
#
# # ---- get assets
# assets = pri.get_asset()["assets"]
# onhand = {}
# locked = {}
# for i in range(len(assets)):
# coin = assets[i]["asset"]
# onhand[coin] = float(assets[i]["onhand_amount"])
# locked[coin] = float(assets[i]["locked_amount"])
#
# # ---- get current price
# candles = pub.get_candlestick("btc_jpy", "1min", now.strftime("%Y%m%d"))["candlestick"][0]["ohlcv"];
# latest_candle = candles[len(candles)-1]
# begin = int(latest_candle[0])
# high = int(latest_candle[1])
# low = int(latest_candle[2])
# end = int(latest_candle[3])
#
# # ---- cancel existing orderes
# prev_orders = pri.get_active_orders("btc_jpy")["orders"]
# cancel_orders = []
# for i in range(len(prev_orders)):
# id = prev_orders[i]["order_id"]
# status = prev_orders[i]["status"]
# side = prev_orders[i]["side"]
# price = int(prev_orders[i]["price"])
#
# if status == "UNFILLED":
# diff = abs(end-price)/end
# if side == "buy":
# if diff < -0.0005:
# cancel_orders.append(str(id))
# elif side == "sell":
# if diff > 0.003:
# cancel_orders.append(str(id))
#
# if len(cancel_orders) > 0:
# pri.cancel_orders("btc_jpy", cancel_orders)
# print("cancelled", len(cancel_orders), "orders")
# if len(prev_orders)-len(cancel_orders) > 8:
# print("skip making an order because there are too many orderes")
# time.sleep(30)
# continue
#
# # ---- get transactions
# trans = pub.get_transactions("btc_jpy")["transactions"]
# buy_min = low
# sell_max = high
# for i in range(len(trans)):
# exec_at = datetime.datetime.fromtimestamp(trans[i]["executed_at"]/1000)
# side = trans[i]["side"]
# price = int(trans[i]["price"])
# if now-exec_at < datetime.timedelta(minutes=1):
# if side == "buy":
# buy_min = min(buy_min, price)
# elif side == "sell":
# sell_max = max(sell_max, price)
#
# # ---- make an order
# buy_coe = 0.5
# ben_coe = 0.8
#
# diff = (end-begin)/end
# if diff > 0.002:
# buy_coe -= clamp(100*diff/end, 0, 1)*0.2
# elif diff < -0.002:
# buy_coe += clamp(100*diff, 0, 1)*0.2
#
# print("buy-min:", buy_min, "| sell-max:", sell_max, "| diff:", sell_max-buy_min)
# diff = sell_max-buy_min
# avg = (sell_max+buy_min)/2
#
# buy_price = avg - diff*buy_coe*ben_coe
# sell_price = avg + diff*(1-buy_coe)*ben_coe
#
# buy_jpy = max((onhand["jpy"]-locked["jpy"])*0.1, (onhand["btc"]-locked["btc"])*end*0.1)
# buy_btc = buy_jpy/buy_price
# sell_jpy = buy_btc*sell_price
# sell_btc = buy_btc
#
# try:
# pri.order("btc_jpy", str(buy_price), str(buy_btc), "buy", "limit", True)
# print("[buy] btc:", buy_btc, "| jpy:", -buy_jpy, "| price:", buy_price)
# except Exception as e:
# print(e)
#
# try:
# pri.order("btc_jpy", str(sell_price), str(sell_btc), "sell", "limit", True)
# print("[sel] btc:", -sell_btc, "| jpy:", sell_jpy, "| price:", sell_price)
# except Exception as e:
# print(e)
#
# time.sleep(10)
main()