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/deals/base.py b/api/deals/base.py index c6a32fe10..ed10d55f7 100644 --- a/api/deals/base.py +++ b/api/deals/base.py @@ -184,23 +184,17 @@ def save_bot_streaming(self): specifically for streaming saves """ - try: - - bot = encode_json(self.active_bot) - if "_id" in bot: - bot.pop("_id") - - response = self.db_collection.find_one_and_update( - {"id": self.active_bot.id}, - { - "$set": bot, - }, - return_document=ReturnDocument.AFTER, - ) + bot = encode_json(self.active_bot) + if "_id" in bot: + bot.pop("_id") - except Exception as error: - self.update_deal_logs(f"Failed to save bot during streaming updates: {error}") - raise StreamingSaveError(error) + response = self.db_collection.find_one_and_update( + {"id": self.active_bot.id}, + { + "$set": bot, + }, + return_document=ReturnDocument.AFTER, + ) return response @@ -213,19 +207,14 @@ def create_new_bot_streaming(self): specifically for streaming saves """ - try: - - bot = encode_json(self.active_bot) - if "_id" in bot: - bot.pop("_id") + bot = encode_json(self.active_bot) + bot.pop("id") + if "_id" in bot: + bot.pop("_id") - bot_response = self.db_collection.insert_one( - bot, - ) - - except Exception as error: - self.update_deal_logs(f"Failed to save bot during streaming updates: {error}") - raise StreamingSaveError(error) + bot_response = self.db_collection.insert_one( + bot, + ) return bot_response.inserted_id 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 (