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.

82 lines
2.4 KiB

from quart import Blueprint, current_app, jsonify
from totrader.models import *
from totrader.tasks import trader
bp = Blueprint('api', 'api', url_prefix='/api/v1')
@bp.route('/get_ticker_data')
async def get_ticker_data():
ticker = Ticker.select().order_by(Ticker.date.desc()).limit(1).first()
return jsonify({
'price': ticker.current_price,
'volume': ticker.volume,
'bid': ticker.bid,
'ask': ticker.ask,
'spread_sats': ticker.spread_sats,
'spread_btc': ticker.spread_btc,
'spread_perc': ticker.spread_perc,
'date': ticker.date
})
@bp.route('/get_balances')
async def get_balances():
base = Balance.select().where(
Balance.currency == trader.base_currency
).order_by(Balance.date.desc()).limit(1).first()
trade = Balance.select().where(
Balance.currency == trader.trade_currency
).order_by(Balance.date.desc()).limit(1).first()
if not base or not trade:
return jsonify({})
return jsonify({
base.currency: {
'total': base.total,
'available': base.available
},
trade.currency: {
'total': trade.total,
'available': trade.available
}
})
@bp.route('/get_bitcoin_price')
async def get_bitcoin_price():
btc = BitcoinPrice.select().order_by(BitcoinPrice.date.desc()).limit(1).first()
return jsonify({
'price': btc.price
})
@bp.route('/get_orders')
async def get_orders():
data = {}
for order in Order.filter(Order.active == True).order_by(Order.date.desc()):
data[order.uuid] = {
'trade_pair': order.trade_pair,
'trade_type': order.trade_type,
'buy': order.buy,
'quantity': order.quantity,
'price': order.price,
'uuid': order.uuid,
'active': order.active,
'cancelled': order.cancelled,
'date': order.date,
}
return jsonify(data)
@bp.route('/get_trade_history')
async def get_trade_history():
data = {}
for trade in Trade.select().order_by(Trade.date.desc()):
data[trade.id] = {
'trade_pair': trade.trade_pair,
'trade_type': trade.trade_type,
'buy': trade.buy,
'quantity': trade.quantity,
'price': trade.price,
'date': trade.date
}
return jsonify(data)