This commit is contained in:
2022-07-15 19:17:55 +09:00
parent 37840311e0
commit 3bb4ae2926
12 changed files with 779 additions and 0 deletions

3
logic/__init__.py Normal file
View File

@@ -0,0 +1,3 @@
from logic.mm import MM
from logic.hige import Hige
from logic.ema_chicken import EMA_Chicken

66
logic/ema_chicken.py Normal file
View File

@@ -0,0 +1,66 @@
import asyncio
class EMA_Chicken:
def __init__(self, logger, pair, candle, lot, limit_rate):
self._logger = logger
self._pair = pair
self._candle = candle
self._remain = 0
self._total = 0
self._lot = lot
self._limit_rate = limit_rate
self._total = 0
asyncio.create_task(self._main())
async def _main(self):
self._logger.info("started")
while True:
await self._pair.ticker.wait()
if len(self._candle.values) == 0:
continue
price = self._pair.ticker.price
ema = self._candle.EMA(200)
if price > ema:
if self._remain < self._lot*3:
buy = await self._pair.buy_market(self._lot)
await asyncio.sleep(0.5)
await buy.update()
outgo = buy.price * self._lot
self._total -= outgo
self._remain += self._lot
self._logger.info(f"<BUY> {self._lot} / {outgo} ({self._total})")
limit_price = buy.price*(1+self._limit_rate)
stop_price = buy.price*(1-self._limit_rate)
sell = await self._pair.sell_limit(self._lot, limit_price)
while True:
await asyncio.sleep(0.5)
await sell.update()
if sell.done:
amount = sell.amount - sell.remain
income = sell.price * amount
self._total += income
self._remain -= amount
if amount > 0:
self._logger.info(f"[WIN] <SELL> {amount} / {income} ({self._total})")
break
if self._pair.ticker.price < stop_price:
await sell.cancel()
sell = await self._pair.sell_market(self._lot)
await asyncio.sleep(0.5)
await sell.update()
amount = sell.amount
income = sell.price * sell.amount
self._total += income
self._remain -= amount
if amount > 0:
self._logger.info(f"[LOSE] <SELL> {amount} / {income} ({self._total})")
break

76
logic/hige.py Normal file
View File

@@ -0,0 +1,76 @@
import asyncio
class Hige:
def __init__(self, logger, pair, lot, delta):
self._logger = logger
self._pair = pair
self._lot = lot
self._delta = delta
self._remain = 0
self._sell_price = 0
self._buy_price = 0
self._sell = None
self._buy = None
self._total = 0
asyncio.create_task(self._main())
async def _main(self):
self._logger.info("started")
while True:
pair = self._pair
depth = pair.depth
if len(depth.bids) == 0 or len(depth.asks) == 0:
await depth.wait()
continue
expected_sell_price = depth.asks[0][0] + self._delta
expected_buy_price = depth.bids[0][0] - self._delta
if self._sell is not None:
await self._sell.update()
if self._sell.done:
amount = self._sell.amount - self._sell.remain
income = amount * self._sell.price
self._remain -= amount
self._total += income
self._sell = None
if amount > 0:
self._logger.info("<SELL> {amount} / {income} ({self._total})")
continue
elif self._sell_price != expected_sell_price:
await self._sell.cancel()
continue
elif self._remain > pair.order_unit:
amount = min(self._lot, self._remain)
self._sell_price = expected_sell_price
self._sell = await pair.sell_limit(amount, self._sell_price)
if self._buy is not None:
await self._buy.update()
if self._buy.done:
amount = self._buy.amount - self._buy.remain
outgo = amount * self._buy.price
self._total -= outgo
self._remain += amount
self._buy = None
if amount > 0:
self._logger.info("<BUY> {amount} / {outgo} ({self._total})")
continue
elif self._buy_price != expected_buy_price:
await self._buy.cancel()
continue
elif self._remain < self._lot*3:
self._buy_price = expected_buy_price
self._buy = await pair.buy_limit(self._lot, self._buy_price)
await asyncio.sleep(5)
async def _reset_order(order, price):
if not order.done and order.price != price:
await order.cancel()

106
logic/mm.py Normal file
View File

@@ -0,0 +1,106 @@
import asyncio
class MM:
def __init__(self, logger, pair, lot, epsilon):
self._logger = logger
self._pair = pair
self._lot = lot
self._remain = 0
self._epsilon = epsilon
self._buy_price = None
self._sell_price = None
self._buy = None
self._sell = None
self._total = 0
asyncio.create_task(self._main())
async def _main(self):
self._logger.info("started")
while True:
pair = self._pair
depth = pair.depth
await depth.wait()
if len(depth.bids) == 0 or len(depth.asks) == 0:
continue
ask = depth.asks[0][0]
bid = depth.bids[0][0]
enough_spread = (ask-bid) > self._epsilon*2
# calculate best amount to buy/sell
pos = self._remain / self._lot - 2
buy_amount = _quad( pos) * self._lot
sell_amount = _quad(-pos) * self._lot
if self._sell is not None:
# check current SELL order
await asyncio.sleep(0.5)
await self._sell.update()
# get highest ask
if ask == self._sell_price and abs(depth.asks[0][1]-self._sell.remain) < pair.price_unit:
ask = depth.asks[1][0]
if self._sell.done:
amount = self._sell.amount - self._sell.remain
if amount > 0:
income = amount*self._sell.price
self._total += income
self._logger.info(f"<SELL> {amount} / {income} ({self._total})")
self._remain -= amount
self._sell = None
elif abs(self._sell_price-(ask-self._epsilon)) >= pair.price_unit:
try:
await self._sell.cancel()
except Exception:
pass
elif enough_spread and sell_amount >= pair.order_unit:
# order SELL
self._sell_price = ask-self._epsilon
self._sell = await self._order_sell(sell_amount)
if self._buy is not None:
# check current BUY order
await asyncio.sleep(0.5)
await self._buy.update()
# get lowest bid
if bid == self._buy_price and abs(depth.bids[0][1]-self._buy.remain) < pair.price_unit:
bid = depth.bids[1][0]
if self._buy.done:
amount = self._buy.amount - self._buy.remain
if amount > 0:
outgo = amount*self._buy.price
self._total -= outgo
self._logger.info(f"<BUY> {amount} / {outgo} ({self._total})")
self._remain += amount
self._buy = None
elif abs(self._buy_price-(bid+self._epsilon)) >= pair.price_unit:
try:
await self._buy.cancel()
except Exception:
pass
elif enough_spread and buy_amount > pair.order_unit:
# order BUY
self._buy_price = bid+self._epsilon
self._buy = await self._order_buy(buy_amount)
async def _order_sell(self, amount):
return await self._pair.sell_limit(amount, self._sell_price, True)
async def _order_buy(self, amount):
return await self._pair.buy_limit(amount, self._buy_price, True)
# https://kijitora-2018.hatenablog.com/entry/2018/12/23/102913
def _quad(x):
if x < -1:
return 1
if x <= 1:
return -1/4 * (x+1)**2 + 1
return 0