From e748cccbb7f6d2c0b37470f952f1d9853767400d Mon Sep 17 00:00:00 2001 From: Carlos Wu Fei Date: Mon, 8 Jan 2024 17:45:46 +0000 Subject: [PATCH] Fix volume ranking graph proportions --- api/account/assets.py | 24 ++++++++--- api/db.py | 1 - api/streaming/streaming_controller.py | 10 +---- web/src/components/BarChart.jsx | 5 ++- web/src/components/VolumesRanking.jsx | 58 ++++++++++++--------------- web/src/pages/dashboard/Dashboard.jsx | 28 +++++++------ 6 files changed, 64 insertions(+), 62 deletions(-) diff --git a/api/account/assets.py b/api/account/assets.py index ad6fb8e70..8109080dd 100644 --- a/api/account/assets.py +++ b/api/account/assets.py @@ -17,6 +17,7 @@ class Assets(BaseDeal): def __init__(self): self.db = setup_db() self.usd_balance = 0 + self.exception_list = [] def get_raw_balance(self, asset=None): """ @@ -346,20 +347,33 @@ def clean_balance_assets(self): """ data = self.signed_request(url=self.account_url) assets = [] - exception_list = ["USDT", "NFT", "BNB"] + self.exception_list = ["USDT", "NFT", "BNB"] active_bots = list(self.db.bots.find({"status": Status.active})) for bot in active_bots: quote_asset = bot["pair"].replace(bot["balance_to_use"], "") - exception_list.append(quote_asset) + self.exception_list.append(quote_asset) for item in data["balances"]: - if item["asset"] not in exception_list and float(item["free"]) > 0: + if item["asset"] not in self.exception_list and float(item["free"]) > 0: assets.append(item["asset"]) if len(assets) > 5: - self.transfer_dust(assets) - resp = json_response_message("Sucessfully cleaned balance.") + try: + self.transfer_dust(assets) + resp = json_response_message("Sucessfully cleaned balance.") + except BinanceErrors as error: + msg, code = error + if code == -5005: + for item in assets: + for string in msg: + if string in item: + self.exception_list = item + break + + self.clean_balance_assets() + else: + resp = json_response_error(f"Failed to clean balance: {error}") else: resp = json_response_error("Amount of assets in balance is low. Transfer not needed.") diff --git a/api/db.py b/api/db.py index 1727ff149..3836cb74a 100644 --- a/api/db.py +++ b/api/db.py @@ -11,5 +11,4 @@ def setup_db(): password=os.getenv("MONGO_AUTH_PASSWORD"), ) db = mongo[os.getenv("MONGO_APP_DATABASE")] - db.bots.create_index("id", unique=True) return db diff --git a/api/streaming/streaming_controller.py b/api/streaming/streaming_controller.py index a30a1ee9c..a5e53bc22 100644 --- a/api/streaming/streaming_controller.py +++ b/api/streaming/streaming_controller.py @@ -196,14 +196,7 @@ def update_order_data(self, result, db_collection: str = "bots"): if float(result["p"]) > 0: update["$set"]["orders.$.price"] = float(result["p"]) else: - total_qty = 0 - weighted_avg = 0 - for item in result["fills"]: - weighted_avg += float(item["price"]) * float(item["qty"]) - total_qty += float(item["qty"]) - - weighted_avg_price = weighted_avg / total_qty - result["p"] = weighted_avg_price + update["$set"]["orders.$.price"] = float(result["L"]) query = self.streaming_db[db_collection].update_one( {"orders": {"$elemMatch": {"order_id": order_id}}}, @@ -224,7 +217,6 @@ def on_user_data_message(self, socket, message): if "e" in res: if "executionReport" in res["e"]: - logging.info(f'executionReport {res}') query = self.update_order_data(res) if query.raw_result["nModified"] == 0: logging.debug( diff --git a/web/src/components/BarChart.jsx b/web/src/components/BarChart.jsx index 531c6f369..16d65b330 100644 --- a/web/src/components/BarChart.jsx +++ b/web/src/components/BarChart.jsx @@ -4,6 +4,7 @@ import { listCssColors } from "../validations"; function computerPercent(data) { const gainers = []; const losers = []; + for (let i = 0; i < data.gainers_percent.length; i++) { const totalCount = data.gainers_count[i] + data.losers_count[i]; const gainersCount = @@ -58,7 +59,7 @@ export default function BarChart({ data={[ { x: data.dates, - y: data.total_volume, + y: data.gainers_percent, type: "bar", marker: { color: listCssColors[8] }, name: line1name, @@ -66,7 +67,7 @@ export default function BarChart({ }, { x: data.dates, - y: data.total_volume, + y: data.losers_percent, type: "bar", marker: { color: listCssColors[2] }, name: line2name, diff --git a/web/src/components/VolumesRanking.jsx b/web/src/components/VolumesRanking.jsx index e62c4ee2a..316503d0d 100644 --- a/web/src/components/VolumesRanking.jsx +++ b/web/src/components/VolumesRanking.jsx @@ -19,47 +19,41 @@ const computeTotalVolume = (data) => { const average = (data) => { const total = data.reduce((acc, x) => { - return (acc + parseFloat(x.quoteVolume) + parseFloat(x.volume)); + return acc + parseFloat(x.quoteVolume) + parseFloat(x.volume); }, 0); return (total / data.length - 1).toLocaleString(); -} +}; export default function VolumesRankingCard({ data, title }) { const sortedData = computeTotalVolume(data); return (
-
- - - - {`Volume market average: ${average(data)}`} - {title} - - {sortedData.map((x, i) => ( - - - - - {x.symbol} - - - - - {(parseFloat(x.quoteVolume) + parseFloat(x.volume)).toLocaleString()} - - - - - ))} - - - - + + {`Volume market average: ${average(data)}`} + {title} + + {sortedData.map((x, i) => ( + + + + + {x.symbol} + + + + + {( + parseFloat(x.quoteVolume) + parseFloat(x.volume) + ).toLocaleString()} + + + + + ))} + +
); diff --git a/web/src/pages/dashboard/Dashboard.jsx b/web/src/pages/dashboard/Dashboard.jsx index 6dd498937..05a16ff85 100644 --- a/web/src/pages/dashboard/Dashboard.jsx +++ b/web/src/pages/dashboard/Dashboard.jsx @@ -363,19 +363,21 @@ class Dashboard extends React.Component { - {this.state.netWorth && ( - - )} - {this.state.dailyPnL && ( - - )} - - - - - {this.props.gainersLosersData && this.props.gainersLosersData.length > 0 && ( - - )} + + + {this.state.netWorth && ( + + )} + {this.props.gainersLosersData && this.props.gainersLosersData.length > 0 && ( + + )} + + + {this.state.dailyPnL && ( + + )} + +