You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
totrader/totrader/tasks.py

120 lines
4.4 KiB

import logging
from os import getenv
from decimal import Decimal
from dotenv import load_dotenv
from totrader.tradeogre import TradeOgre
from totrader.models import *
class Trader(TradeOgre):
load_dotenv('.env')
satoshi = .00000001
base_currency = getenv('BASE_CURRENCY', 'BTC')
trade_currency = getenv('TRADE_CURRENCY', 'WOW')
trade_pair = f'{base_currency}-{trade_currency}'
trade_amount = float(getenv('TRADE_AMOUNT', 200))
spread_target = float(getenv('SPREAD_TARGET', 4))
amount_multiplier = float(getenv('AMOUNT_MULTIPLIER', 1.2))
active_order_limit = float(getenv('ACTIVE_ORDER_LIMIT', 10))
def store_ticker_data(self):
logging.info(f'[MARKET] Getting market data for trade pair {self.trade_pair}')
res = self.get_ticker(self.trade_pair)
spread_btc = Decimal(res['ask']) - Decimal(res['bid'])
spread_sats = float(spread_btc / Decimal(self.satoshi))
spread_perc = (spread_btc / Decimal(res['ask'])) * 100
res['spread_btc'] = spread_btc
res['spread_sats'] = spread_sats
res['spread_perc'] = spread_perc
logging.debug(res)
t = Ticker(
trade_pair=self.trade_pair,
initial_price=res['initialprice'],
current_price=res['price'],
high_price=res['high'],
low_price=res['low'],
volume=res['volume'],
bid=res['bid'],
ask=res['ask'],
spread_btc=res['spread_btc'],
spread_sats=res['spread_sats'],
spread_perc=res['spread_perc']
)
t.save()
logging.info(f'[MARKET] Stored ticker data: {t.id}')
def store_balances(self):
for currency in self.base_currency, self.trade_currency:
logging.info(f'[BALANCE] Storing balances for currency {currency}')
res = self.get_balance(currency)
logging.debug(res)
b = Balance(
currency=currency,
total=res['balance'],
available=res['available']
)
b.save()
logging.info(f'[BALANCE] Stored balances: {b.id}')
def get_active_orders(self):
logging.info('[ORDERS] Getting active orders in local database')
orders = Order.select().where(Order.active == True, Order.trade_pair == self.trade_pair)
logging.debug(f'Found {len(orders)} in database')
return orders
def reconcile_orders(self):
logging.info('[ORDERS] Reconciling orders on TradeOgre with local database')
to_orders = self.get_orders(self.trade_pair)
for order in to_orders:
if not Order.select().where(Order.uuid == order['uuid']).first():
o = Order(
trade_pair=order['market'],
trade_type='manual',
buy=order['type'] == 'buy',
quantity=float(order['quantity']),
price=float(order['price']),
uuid=order['uuid'],
date=datetime.utcfromtimestamp(order['date'])
)
o.save()
logging.info(f'[ORDERS] Saved order {order["uuid"]} to the database')
def store_orders(self):
logging.info('[ORDERS] Updating orders in local database against TradeOgre')
for order in self.get_active_orders():
logging.info(f'Checking order {order.uuid}')
o = self.get_order(order.uuid)
logging.info(f'Found order: {o}')
if o['success'] is False:
order.active = False
order.save()
logging.info(f'Order {order.uuid} no longer active on TradeOgre. Setting inactive.')
def store_trade_history(self):
logging.info('Updating trade history for the ticker')
for trade in self.get_history(self.trade_pair):
tr = Trade(
trade_pair=self.trade_pair,
trade_type=trade['type'],
buy=trade['type'] == 'buy',
quantity=float(trade['quantity']),
price=float(trade['price']),
date=datetime.utcfromtimestamp(trade['date'])
)
tr.save()
logging.info('Trade added to the database')
def store_bitcoin_price(self):
logging.info('Updating Bitcoin price')
r = self.get_bitcoin_price()
bp = BitcoinPrice(
price=float(r['price'])
)
bp.save()
trader = Trader()