This commit is contained in:
falsycat 2022-07-17 10:54:39 +09:00
parent 3bb4ae2926
commit 28c50d053b
4 changed files with 58 additions and 34 deletions

View File

@ -27,12 +27,12 @@ async def init(cli):
pair = Pair(cli, "btc_jpy")
logic.EMA_Chicken(
logging.getLogger("bitbank/btc/EMA_Chicken"),
pair, pair.candlestick_1m, 0.001, 0.01)
pair, pair.candlestick_1m, 0.001, 0.007)
pair = Pair(cli, "eth_jpy")
logic.MM(
logging.getLogger("bitbank/eth/MM"),
pair, 0.001, 1)
pair, 0.01, 1)
logic.EMA_Chicken(
logging.getLogger("bitbank/eth/EMA_Chicken"),
pair, pair.candlestick_1m, 0.01, 0.01)
@ -115,13 +115,22 @@ class Order(util.pair.Order):
self._id = id
async def update(self):
async with _k:
res = await self._cli.get(PRIVATE_API_ENTRYPOINT+"/user/spot/order", params={
"pair": self._pair.name,
"order_id": self._id,
})
res = await res.json()
_check_response(res)
if self.done: return
for i in range(5):
async with _k:
res = await self._cli.get(PRIVATE_API_ENTRYPOINT+"/user/spot/order", params={
"pair": self._pair.name,
"order_id": self._id,
})
res = await res.json()
if res["success"] == 1: break
# sometimes fails without a reason, so it does retry :(
if res["data"]["code"] != 50009 and str(res["data"]["code"]) in ERROR_CODES:
_check_response(res)
await asyncio.sleep(1)
if res["success"] != 1: raise Exception("failed to update")
res = res["data"]
self.amount = float(res["start_amount"])
self.remain = float(res["remaining_amount"])
@ -132,13 +141,35 @@ class Order(util.pair.Order):
res["status"] == "CANCELED_PARTIALLY_FILLED"
async def cancel(self):
async with _k:
res = await self._cli.post(PRIVATE_API_ENTRYPOINT+"/user/spot/cancel_order", data={
"pair": self._pair.name,
"order_id": self._id,
})
res = await res.json()
_check_response(res)
for i in range(5):
async with _k:
res = await self._cli.post(PRIVATE_API_ENTRYPOINT+"/user/spot/cancel_order", data={
"pair": self._pair.name,
"order_id": self._id,
})
res = await res.json()
if res["success"] == 1:
res = res["data"]
self.amount = float(res["start_amount"])
self.remain = float(res["remaining_amount"])
self.price = float(res["average_price"])
self.done = True
return
code = res["data"]["code"]
if code == 50027:
await self.update()
return
# sometimes fails without a reason, so it does retry :(
retry = \
code == 50009 or \
str(code) not in ERROR_CODES
if not retry:
_check_response(res)
await asyncio.sleep(1)
raise Exception("failed to cancel")
class Ticker(util.ticker.Ticker):

View File

@ -34,7 +34,7 @@ class EMA_Chicken:
outgo = buy.price * self._lot
self._total -= outgo
self._remain += self._lot
self._logger.info(f"<BUY> {self._lot} / {outgo} ({self._total})")
self._logger.info(f"<BUY> {self._lot} / {outgo} ({self._remain} / {self._total})")
limit_price = buy.price*(1+self._limit_rate)
stop_price = buy.price*(1-self._limit_rate)
@ -49,7 +49,7 @@ class EMA_Chicken:
self._total += income
self._remain -= amount
if amount > 0:
self._logger.info(f"[WIN] <SELL> {amount} / {income} ({self._total})")
self._logger.info(f"[WIN] <SELL> {amount} / {income} ({self._remain} / {self._total})")
break
if self._pair.ticker.price < stop_price:
@ -62,5 +62,5 @@ class EMA_Chicken:
self._total += income
self._remain -= amount
if amount > 0:
self._logger.info(f"[LOSE] <SELL> {amount} / {income} ({self._total})")
self._logger.info(f"[LOSE] <SELL> {amount} / {income} ({self._remain} / {self._total})")
break

View File

@ -41,7 +41,7 @@ class Hige:
self._total += income
self._sell = None
if amount > 0:
self._logger.info("<SELL> {amount} / {income} ({self._total})")
self._logger.info(f"<SELL> {amount} / {income} ({self._remain} / {self._total})")
continue
elif self._sell_price != expected_sell_price:
await self._sell.cancel()
@ -60,7 +60,7 @@ class Hige:
self._remain += amount
self._buy = None
if amount > 0:
self._logger.info("<BUY> {amount} / {outgo} ({self._total})")
self._logger.info(f"<BUY> {amount} / {outgo} ({self._remain} / {self._total})")
continue
elif self._buy_price != expected_buy_price:
await self._buy.cancel()

View File

@ -48,17 +48,14 @@ class MM:
if self._sell.done:
amount = self._sell.amount - self._sell.remain
self._remain -= amount
if amount > 0:
income = amount*self._sell.price
self._total += income
self._logger.info(f"<SELL> {amount} / {income} ({self._total})")
self._remain -= amount
self._logger.info(f"<SELL> {amount} / {income} ({self._remain} / {self._total})")
self._sell = None
elif abs(self._sell_price-(ask-self._epsilon)) >= pair.price_unit:
try:
await self._sell.cancel()
except Exception:
pass
await self._sell.cancel()
elif enough_spread and sell_amount >= pair.order_unit:
# order SELL
self._sell_price = ask-self._epsilon
@ -66,7 +63,6 @@ class MM:
if self._buy is not None:
# check current BUY order
await asyncio.sleep(0.5)
await self._buy.update()
# get lowest bid
@ -75,17 +71,14 @@ class MM:
if self._buy.done:
amount = self._buy.amount - self._buy.remain
self._remain += amount
if amount > 0:
outgo = amount*self._buy.price
self._total -= outgo
self._logger.info(f"<BUY> {amount} / {outgo} ({self._total})")
self._remain += amount
self._logger.info(f"<BUY> {amount} / {outgo} ({self._remain} / {self._total})")
self._buy = None
elif abs(self._buy_price-(bid+self._epsilon)) >= pair.price_unit:
try:
await self._buy.cancel()
except Exception:
pass
await self._buy.cancel()
elif enough_spread and buy_amount > pair.order_unit:
# order BUY
self._buy_price = bid+self._epsilon