add flair to logging and prep for passed starting bid amount

This commit is contained in:
lza_menace 2020-09-07 07:08:21 -07:00
parent bfc3e6b082
commit 7967949438

View file

@ -21,9 +21,10 @@ class Trader(TradeOgre):
base_currency = 'BTC' base_currency = 'BTC'
trade_currency = 'WOW' trade_currency = 'WOW'
trade_pair = f'{base_currency}-{trade_currency}' trade_pair = f'{base_currency}-{trade_currency}'
trade_amount = 100
def get_market_data(self): def get_market_data(self):
logging.info(f'Getting market data for trade pair {self.trade_pair}') logging.info(f'[MARKET] Getting market data for trade pair {self.trade_pair}')
res = self.get_trade_pair(self.trade_pair) res = self.get_trade_pair(self.trade_pair)
spread_btc = Decimal(res['ask']) - Decimal(res['bid']) spread_btc = Decimal(res['ask']) - Decimal(res['bid'])
spread_sats = float(spread_btc / Decimal(self.satoshi)) spread_sats = float(spread_btc / Decimal(self.satoshi))
@ -36,7 +37,6 @@ class Trader(TradeOgre):
def store_market_data(self): def store_market_data(self):
res = self.get_market_data() res = self.get_market_data()
logging.info(f'Storing market data for trade pair {self.trade_pair}')
t = Ticker( t = Ticker(
trade_pair=self.trade_pair, trade_pair=self.trade_pair,
initial_price=res['initialprice'], initial_price=res['initialprice'],
@ -51,11 +51,11 @@ class Trader(TradeOgre):
spread_perc=res['spread_perc'] spread_perc=res['spread_perc']
) )
t.save() t.save()
logging.info(f'Stored market data as ID {t.id}') logging.info(f'[MARKET] Stored market data as ID {t.id}')
return t return t
def store_balance(self, currency): def store_balance(self, currency):
logging.info(f'Storing balance for currency {currency}') logging.info(f'[BALANCE] Storing balance for currency {currency}')
res = self.get_balance(currency) res = self.get_balance(currency)
logging.debug(res) logging.debug(res)
b = Balance( b = Balance(
@ -64,7 +64,7 @@ class Trader(TradeOgre):
available=res['available'] available=res['available']
) )
b.save() b.save()
logging.info(f'Stored market data as ID {b.id}') logging.info(f'[BALANCE] Stored market data as ID {b.id}')
return b return b
def store_balances(self): def store_balances(self):
@ -73,38 +73,38 @@ class Trader(TradeOgre):
sleep(3) sleep(3)
def get_active_orders(self): def get_active_orders(self):
logging.info('Getting active orders in local database') logging.info('[ORDERS] Getting active orders in local database')
orders = Order.select().where(Order.active==True) orders = Order.select().where(Order.active==True)
logging.debug(f'Found {len(orders)} in database') logging.debug(f'Found {len(orders)} in database')
return orders return orders
def update_orders(self): def update_orders(self):
logging.info('Updating orders in local database against TradeOgre') logging.info('[ORDERS] Updating orders in local database against TradeOgre')
for order in self.get_active_orders(): for order in self.get_active_orders():
logging.debug(f'Checking order {order.uuid}') logging.info(f'Checking order {order.uuid}')
o = self.get_order(order.uuid) o = self.get_order(order.uuid)
logging.debug(f'Found order: {o}') logging.info(f'Found order: {o}')
if o['success'] is False: if o['success'] is False:
order.active = False order.active = False
order.save() order.save()
logging.debug(f'Order {order.uuid} no longer active on TradeOgre. Setting inactive.') logging.info(f'Order {order.uuid} no longer active on TradeOgre. Setting inactive.')
sleep(5) sleep(5)
def start_market_maker(self): def start_market_maker(self):
logging.info('Starting market maker') logging.info('[MARKET MAKER] Starting market maker')
latest = Ticker.select().order_by(Ticker.date.desc()).get() latest = Ticker.select().order_by(Ticker.date.desc()).get()
trade_type = 'market_maker' trade_type = 'market_maker'
if len(self.get_active_orders()) > 5: if len(self.get_active_orders()) > 5:
logging.info('Too many active orders in place. Skipping.') logging.info('[MARKET MAKER] Too many active orders in place. Skipping.')
return False return False
if latest.spread_sats > 4: if latest.spread_sats > 4:
bid_amount = uniform(100, 130) bid_amount = uniform(self.trade_amount, self.trade_amount + 30)
ask_amount = uniform(bid_amount - 6, bid_amount + 6) ask_amount = uniform(bid_amount - 6, bid_amount + 6)
bid_price = '{:.8f}'.format(latest.bid + self.satoshi) bid_price = '{:.8f}'.format(latest.bid + self.satoshi)
ask_price = '{:.8f}'.format(latest.ask - self.satoshi) ask_price = '{:.8f}'.format(latest.ask - self.satoshi)
logging.info(f'Submitting buy order for {bid_amount} at {bid_price} {self.trade_pair}') logging.info(f'[MARKET MAKER] Submitting buy order for {bid_amount} at {bid_price} {self.trade_pair}')
buy = self.submit_order('buy', self.trade_pair, bid_amount, bid_price) buy = self.submit_order('buy', self.trade_pair, bid_amount, bid_price)
logging.debug(buy) logging.debug(buy)
if buy['success']: if buy['success']:
@ -117,10 +117,10 @@ class Trader(TradeOgre):
uuid=buy['uuid'] uuid=buy['uuid']
) )
o.save() o.save()
logging.debug(f'Stored buy order as ID {o.id}') logging.info(f'[MARKET MAKER] Stored buy order as ID {o.id}')
sleep(3) sleep(3)
logging.info(f'Submitting sell order for {ask_amount} at {ask_price} {self.trade_pair}') logging.info(f'[MARKET MAKER] Submitting sell order for {ask_amount} at {ask_price} {self.trade_pair}')
sell = self.submit_order('sell', self.trade_pair, ask_amount, ask_price) sell = self.submit_order('sell', self.trade_pair, ask_amount, ask_price)
logging.debug(sell) logging.debug(sell)
if sell['success']: if sell['success']:
@ -133,9 +133,9 @@ class Trader(TradeOgre):
uuid=sell['uuid'] uuid=sell['uuid']
) )
o.save() o.save()
logging.debug(f'Stored sell order as ID {o.id}') logging.info(f'[MARKET MAKER] Stored sell order as ID {o.id}')
else: else:
logging.info(f'Not enough bid-ask spread ({latest.spread_sats} sats). Skipping market maker.') logging.info(f'[MARKET MAKER] Not enough bid-ask spread ({latest.spread_sats} sats). Skipping market maker.')
if __name__ == '__main__': if __name__ == '__main__':
@ -165,19 +165,22 @@ if __name__ == '__main__':
while True: while True:
t.store_market_data() t.store_market_data()
# update orders every 5 minutes # update orders every 3 minutes
if orders_counter == 5: if orders_counter == 3:
t.update_orders() t.update_orders()
logging.info('[ORDERS] Resetting orders counter')
orders_counter = 0 orders_counter = 0
# update balances every 2 minutes # update balances every 6 minutes
if balances_counter == 2: if balances_counter == 6:
t.store_balances() t.store_balances()
logging.info('[BALANCE] Resetting balances counter')
balances_counter = 0 balances_counter = 0
# start market makers every 3 minutes # start market makers every 2 minutes
if market_maker_counter == 3: if market_maker_counter == 2:
t.start_market_maker() t.start_market_maker()
logging.info('[MARKET MAKER] Resetting market maker counter')
market_maker_counter = 0 market_maker_counter = 0
orders_counter += 1 orders_counter += 1