tmm/algo/mm.py

71 lines
2.0 KiB
Python

# No copyright
import asyncio
import logging
class MM:
def __init__(self, player, pair, config):
self._player = player
self._pair = pair
self._lastBuy = ""
self._lastSell = ""
self._delta = float(config["delta"])
async def update(self):
lastBuyAgreed = (self._lastBuy is not None and
self._lastBuy not in self._player.orders)
lastSellAgreed = (self._lastSell is not None and
self._lastSell not in self._player.orders)
if not lastBuyAgreed and not lastSellAgreed:
return
cancel_orders = []
if not lastBuyAgreed and self._lastBuy is not None:
cancel_orders.append(self._lastBuy)
self._lastBuy = None
if not lastSellAgreed and self._lastSell is not None:
cancel_orders.append(self._lastSell)
self._lastSell = None
if len(cancel_orders) != 0:
await self._player.cancel(self._pair.names, cancel_orders)
sales = self._player.assets[self._pair.names[0]]
crncy = self._player.assets[self._pair.names[1]]
price = self._pair.ticker.price
lot = (sales.amount + crncy.amount / price) / 4
pos = sales.amount / lot - 2
buy_amount = _quad( pos) * lot
sell_amount = _quad(-pos) * lot
buy_price = price * (1 - self._delta);
sell_price = price * (1 + self._delta);
order_unit = pow(10, -sales.precision)
async def buy():
return (None if order_unit > buy_amount else
await self._player.orderLimitBuy(
self._pair.names, amount = buy_amount, price = buy_price))
async def sell():
return (None if order_unit > sell_amount else
await self._player.orderLimitSell(
self._pair.names, amount = sell_amount, price = sell_price))
orders = await asyncio.gather(buy(), sell())
self._lastBuy = orders[0]
self._lastSell = orders[1]
# 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