get pages and auto updating working

This commit is contained in:
lza_menace 2023-02-26 16:59:15 -08:00
parent 60a2998d80
commit f7740ba778
6 changed files with 86 additions and 33 deletions

View file

@ -1,6 +1,9 @@
import quart.flask_patch import quart.flask_patch
from quart import Quart from quart import Quart
from totrader.routes import meta, tasks, api
from totrader import filters
def create_app(): def create_app():
app = Quart(__name__) app = Quart(__name__)
@ -8,8 +11,6 @@ def create_app():
@app.before_serving @app.before_serving
async def startup(): async def startup():
from totrader.routes import meta, tasks, api
from totrader import filters
app.register_blueprint(meta.bp) app.register_blueprint(meta.bp)
app.register_blueprint(tasks.bp) app.register_blueprint(tasks.bp)
app.register_blueprint(api.bp) app.register_blueprint(api.bp)

View file

@ -23,16 +23,59 @@ async def get_ticker_data():
@bp.route('/get_balances') @bp.route('/get_balances')
async def 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({})
return jsonify({
base.currency: {
'total': base.total,
'available': base.available
},
trade.currency: {
'total': trade.total,
'available': trade.available
}
})
@bp.route('/get_bitcoin_price') @bp.route('/get_bitcoin_price')
async def get_bitcoin_price(): async def get_bitcoin_price():
return jsonify({}) btc = BitcoinPrice.select().order_by(BitcoinPrice.date.desc()).limit(1).first()
return jsonify({
'price': btc.price
})
@bp.route('/get_orders') @bp.route('/get_orders')
async def get_orders(): async def get_orders():
return jsonify({}) 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') @bp.route('/get_trade_history')
async def get_trade_history(): async def get_trade_history():
return jsonify({}) 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)

View file

@ -20,15 +20,15 @@ async def store_balances():
@bp.route('/store_orders') @bp.route('/store_orders')
async def store_orders(): async def store_orders():
current_app.add_background_task(trader.reconcile_orders) current_app.add_background_task(trader.reconcile_orders)
current_app.add_background_task(trader.update_orders) current_app.add_background_task(trader.store_orders)
return 'ok' return 'ok'
@bp.route('/store_trade_history') @bp.route('/store_trade_history')
async def store_trade_history(): async def store_trade_history():
current_app.add_background_task(trader.update_trade_history) current_app.add_background_task(trader.store_trade_history)
return 'ok' return 'ok'
@bp.route('/store_bitcoin_price') @bp.route('/store_bitcoin_price')
async def store_bitcoin_price(): async def store_bitcoin_price():
current_app.add_background_task(trader.update_bitcoin_price) current_app.add_background_task(trader.store_bitcoin_price)
return 'ok' return 'ok'

View file

@ -83,7 +83,7 @@ class Trader(TradeOgre):
o.save() o.save()
logging.info(f'[ORDERS] Saved order {order["uuid"]} to the database') logging.info(f'[ORDERS] Saved order {order["uuid"]} to the database')
def update_orders(self): def store_orders(self):
logging.info('[ORDERS] 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.info(f'Checking order {order.uuid}') logging.info(f'Checking order {order.uuid}')
@ -94,7 +94,7 @@ class Trader(TradeOgre):
order.save() order.save()
logging.info(f'Order {order.uuid} no longer active on TradeOgre. Setting inactive.') logging.info(f'Order {order.uuid} no longer active on TradeOgre. Setting inactive.')
def update_trade_history(self): def store_trade_history(self):
logging.info('Updating trade history for the ticker') logging.info('Updating trade history for the ticker')
for trade in self.get_history(self.trade_pair): for trade in self.get_history(self.trade_pair):
tr = Trade( tr = Trade(
@ -108,7 +108,7 @@ class Trader(TradeOgre):
tr.save() tr.save()
logging.info('Trade added to the database') logging.info('Trade added to the database')
def update_bitcoin_price(self): def store_bitcoin_price(self):
logging.info('Updating Bitcoin price') logging.info('Updating Bitcoin price')
r = self.get_bitcoin_price() r = self.get_bitcoin_price()
bp = BitcoinPrice( bp = BitcoinPrice(

View file

@ -35,9 +35,7 @@
// }).show(); // }).show();
render() { render() {
// fetch latest data for the UI
let updateAllData; let updateAllData;
// trigger background tasks to store data
let storeMarketData; let storeMarketData;
let storeBalances; let storeBalances;
let storeBitcoinPrice; let storeBitcoinPrice;
@ -49,12 +47,28 @@
fetch('{{ url_for("api.get_ticker_data") }}') fetch('{{ url_for("api.get_ticker_data") }}')
.then((response) => response.json()) .then((response) => response.json())
.then((res) => { .then((res) => {
console.log(res); document.getElementById('get_ticker_data').innerHTML = JSON.stringify(res);
});
fetch('{{ url_for("api.get_balances") }}')
.then((response) => response.json())
.then((res) => {
document.getElementById('get_balances').innerHTML = JSON.stringify(res)
});
fetch('{{ url_for("api.get_bitcoin_price") }}')
.then((response) => response.json())
.then((res) => {
document.getElementById('get_bitcoin_price').innerHTML = JSON.stringify(res)
});
fetch('{{ url_for("api.get_orders") }}')
.then((response) => response.json())
.then((res) => {
document.getElementById('get_orders').innerHTML = JSON.stringify(res)
});
fetch('{{ url_for("api.get_trade_history") }}')
.then((response) => response.json())
.then((res) => {
document.getElementById('get_trade_history').innerHTML = JSON.stringify(res)
}); });
fetch('{{ url_for("api.get_balances") }}');
fetch('{{ url_for("api.get_bitcoin_price") }}');
fetch('{{ url_for("api.get_orders") }}');
fetch('{{ url_for("api.get_trade_history") }}');
} }
if (this.state.looping) { if (this.state.looping) {
@ -78,7 +92,7 @@
console.log('storing trade history'); console.log('storing trade history');
fetch('{{ url_for("tasks.store_trade_history") }}'); fetch('{{ url_for("tasks.store_trade_history") }}');
}, 60000); }, 60000);
updateAllData = setInterval(getAllData(), 10000); updateAllData = setInterval(getAllData, 10000);
} }
return( return(
@ -88,7 +102,7 @@
<div> <div>
<p>market making is started</p> <p>market making is started</p>
<button onClick={() => { <button onClick={() => {
this.setState({looping: false}); this.setState({...this.state, looping: false});
clearInterval(storeMarketData); clearInterval(storeMarketData);
clearInterval(storeBalances); clearInterval(storeBalances);
clearInterval(storeBitcoinPrice); clearInterval(storeBitcoinPrice);
@ -101,28 +115,23 @@
<div> <div>
<p>market making is stopped</p> <p>market making is stopped</p>
<button onClick={() => { <button onClick={() => {
this.setState({looping: true}); this.setState({...this.state, looping: true});
getAllData(); getAllData();
}}>Start</button> }}>Start</button>
</div> </div>
)} )}
<p id="get_ticker_data"></p>
<p id="get_balances"></p>
<p id="get_bitcoin_price"></p>
<p id="get_orders"></p>
<p id="get_trade_history"></p>
</div> </div>
) )
} }
} }
// this.setState(prevState => {
// let jasper = { ...prevState.jasper }; // creating copy of state variable jasper
// jasper.name = 'someothername'; // store the name property, assign a new value
// return { jasper }; // return new object jasper object
// });
const domContainer = document.querySelector('#container'); const domContainer = document.querySelector('#container');
const root = ReactDOM.createRoot(domContainer); const root = ReactDOM.createRoot(domContainer);
root.render(e(App)); root.render(e(App));
// setInterval(function() {
// fetch('{{ url_for("tasks.store_ticker_data") }}');
// }, 10000)
</script> </script>

View file

@ -59,7 +59,7 @@ class TradeOgre(object):
return self.req(route, 'post', data) return self.req(route, 'post', data)
def get_bitcoin_price(self): def get_bitcoin_price(self):
url = 'https://api.coingecko.com/api/v3/coins/wownero' url = 'https://api.coingecko.com/api/v3/coins/bitcoin'
headers = {'accept': 'application/json'} headers = {'accept': 'application/json'}
data = { data = {
'localization': False, 'localization': False,