reconcile orders, update dashboard, refactor db, refactor trade project

This commit is contained in:
lza_menace 2020-09-08 21:30:49 -07:00
parent 5d06b8ee23
commit 2a1d22646c
5 changed files with 104 additions and 25 deletions

2
db.py
View File

@ -52,7 +52,7 @@ class Order(Model):
buy = BooleanField() buy = BooleanField()
quantity = DoubleField() quantity = DoubleField()
price = DoubleField() price = DoubleField()
uuid = CharField() uuid = TextField()
active = BooleanField(default=True) active = BooleanField(default=True)
cancelled = BooleanField(default=False) cancelled = BooleanField(default=False)
date = DateTimeField(default=get_time) date = DateTimeField(default=get_time)

View File

@ -16,7 +16,7 @@
"gnetId": null, "gnetId": null,
"graphTooltip": 0, "graphTooltip": 0,
"id": 1, "id": 1,
"iteration": 1599453836661, "iteration": 1599544047350,
"links": [], "links": [],
"panels": [ "panels": [
{ {
@ -113,6 +113,11 @@
{ {
"alias": "B Sell Orders", "alias": "B Sell Orders",
"color": "#E0B400" "color": "#E0B400"
},
{
"alias": "C Fulfilled Orders",
"color": "rgb(0, 0, 0)",
"pointradius": 3
} }
], ],
"spaceLength": 10, "spaceLength": 10,
@ -242,6 +247,65 @@
"type": "expression" "type": "expression"
} }
] ]
},
{
"format": "table",
"group": [
{
"params": [
"$__interval",
"NULL"
],
"type": "time"
}
],
"hide": false,
"metricColumn": "uuid",
"rawQuery": false,
"rawSql": "SELECT\n $__timeGroupAlias(date,$__interval,NULL),\n uuid AS metric,\n avg(price) AS \"Fulfilled Orders\"\nFROM \"order\"\nWHERE\n $__timeFilter(date) AND\n active = 'false'\nGROUP BY 1,2\nORDER BY 1,2",
"refId": "C",
"select": [
[
{
"params": [
"price"
],
"type": "column"
},
{
"params": [
"avg"
],
"type": "aggregate"
},
{
"params": [
"Fulfilled Orders"
],
"type": "alias"
}
]
],
"table": "\"order\"",
"timeColumn": "date",
"timeColumnType": "timestamp",
"where": [
{
"name": "$__timeFilter",
"params": [],
"type": "macro"
},
{
"datatype": "bool",
"name": "",
"params": [
"active",
"=",
"'false'"
],
"type": "expression"
}
]
} }
], ],
"thresholds": [], "thresholds": [],
@ -366,13 +430,13 @@
], ],
"metricColumn": "none", "metricColumn": "none",
"rawQuery": false, "rawQuery": false,
"rawSql": "SELECT\n $__timeGroupAlias(date,$__interval),\n count(id) AS \"id\"\nFROM \"order\"\nWHERE\n $__timeFilter(date) AND\n active = 'false' AND\n cancelled = 'false'\nGROUP BY 1\nORDER BY 1", "rawSql": "SELECT\n $__timeGroupAlias(date,$__interval),\n count(price) AS \"price\"\nFROM \"order\"\nWHERE\n active = 'false' AND\n cancelled = 'false'\nGROUP BY 1\nORDER BY 1",
"refId": "A", "refId": "A",
"select": [ "select": [
[ [
{ {
"params": [ "params": [
"id" "price"
], ],
"type": "column" "type": "column"
}, },
@ -384,7 +448,7 @@
}, },
{ {
"params": [ "params": [
"id" "price"
], ],
"type": "alias" "type": "alias"
} }
@ -394,11 +458,6 @@
"timeColumn": "date", "timeColumn": "date",
"timeColumnType": "timestamp", "timeColumnType": "timestamp",
"where": [ "where": [
{
"name": "$__timeFilter",
"params": [],
"type": "macro"
},
{ {
"datatype": "bool", "datatype": "bool",
"name": "", "name": "",
@ -516,13 +575,13 @@
], ],
"metricColumn": "none", "metricColumn": "none",
"rawQuery": false, "rawQuery": false,
"rawSql": "SELECT\n $__timeGroupAlias(date,$__interval),\n count(id) AS \"id\"\nFROM \"order\"\nWHERE\n $__timeFilter(date) AND\n active = 'true' AND\n cancelled = 'false'\nGROUP BY 1\nORDER BY 1", "rawSql": "SELECT\n $__timeGroupAlias(date,$__interval),\n count(price) AS \"price\"\nFROM \"order\"\nWHERE\n active = 'true' AND\n cancelled = 'false'\nGROUP BY 1\nORDER BY 1",
"refId": "A", "refId": "A",
"select": [ "select": [
[ [
{ {
"params": [ "params": [
"id" "price"
], ],
"type": "column" "type": "column"
}, },
@ -534,7 +593,7 @@
}, },
{ {
"params": [ "params": [
"id" "price"
], ],
"type": "alias" "type": "alias"
} }
@ -544,11 +603,6 @@
"timeColumn": "date", "timeColumn": "date",
"timeColumnType": "timestamp", "timeColumnType": "timestamp",
"where": [ "where": [
{
"name": "$__timeFilter",
"params": [],
"type": "macro"
},
{ {
"datatype": "bool", "datatype": "bool",
"name": "", "name": "",
@ -841,7 +895,7 @@
"y": 14 "y": 14
}, },
"hiddenSeries": false, "hiddenSeries": false,
"id": 18, "id": 26,
"legend": { "legend": {
"alignAsTable": true, "alignAsTable": true,
"avg": true, "avg": true,
@ -866,7 +920,7 @@
"renderer": "flot", "renderer": "flot",
"repeat": null, "repeat": null,
"repeatDirection": "h", "repeatDirection": "h",
"repeatIteration": 1599453836661, "repeatIteration": 1599544047350,
"repeatPanelId": 12, "repeatPanelId": 12,
"scopedVars": { "scopedVars": {
"currency": { "currency": {
@ -1813,7 +1867,7 @@
] ]
}, },
"time": { "time": {
"from": "now-3h", "from": "now-2d",
"to": "now" "to": "now"
}, },
"timepicker": { "timepicker": {
@ -1833,5 +1887,5 @@
"timezone": "", "timezone": "",
"title": "Trading", "title": "Trading",
"uid": "1KF3M1DGz", "uid": "1KF3M1DGz",
"version": 20 "version": 28
} }

View File

@ -1,3 +1,4 @@
requests requests
peewee peewee
psycopg2-binary psycopg2-binary
python-dotenv

View File

@ -3,6 +3,7 @@
import logging import logging
from os import getenv from os import getenv
from argparse import ArgumentParser from argparse import ArgumentParser
from datetime import datetime
from decimal import Decimal from decimal import Decimal
from random import uniform from random import uniform
from time import sleep from time import sleep
@ -21,7 +22,7 @@ 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 trade_amount = 250
def get_market_data(self): def get_market_data(self):
logging.info(f'[MARKET] Getting market data for trade pair {self.trade_pair}') logging.info(f'[MARKET] Getting market data for trade pair {self.trade_pair}')
@ -78,6 +79,23 @@ class Trader(TradeOgre):
logging.debug(f'Found {len(orders)} in database') logging.debug(f'Found {len(orders)} in database')
return orders 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.filter(Order.uuid==order['uuid']):
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 update_orders(self): def update_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():
@ -155,6 +173,7 @@ if __name__ == '__main__':
exit() exit()
if args.update_orders: if args.update_orders:
t.reconcile_orders()
t.update_orders() t.update_orders()
exit() exit()
@ -168,8 +187,8 @@ if __name__ == '__main__':
except Exception as e: except Exception as e:
logging.info('[ERROR] Unable to store market data!', e) logging.info('[ERROR] Unable to store market data!', e)
# update orders every 3 minutes # update orders every 5 minutes
if orders_counter == 3: if orders_counter == 5:
try: try:
t.update_orders() t.update_orders()
logging.info('[ORDERS] Resetting orders counter') logging.info('[ORDERS] Resetting orders counter')

View File

@ -43,3 +43,8 @@ class TradeOgre(object):
def get_order(self, uuid): def get_order(self, uuid):
route = f'/account/order/{uuid}' route = f'/account/order/{uuid}'
return self.req(route) return self.req(route)
def get_orders(self, pair):
route = f'/account/orders'
data = {'market': pair}
return self.req(route, 'post', data)