From aada4b88de077e6713bcf44a013356d2a82f50ae Mon Sep 17 00:00:00 2001 From: Carlos Wu Date: Fri, 20 Sep 2024 20:44:23 +0100 Subject: [PATCH] Fix balance series Previously not accumulating total amount of balance across all account. Also added fiat getter from dashaboard --- .github/workflows/pr.yml | 2 +- api/account/assets.py | 24 +++++++++++++++++------- api/autotrade/controller.py | 3 ++- api/db.py | 7 ++++++- api/tests/test_assets.py | 2 -- api/tools/enum_definitions.py | 9 +++++++++ 6 files changed, 35 insertions(+), 12 deletions(-) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index cddc2f22..3958321f 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -18,7 +18,7 @@ jobs: runs-on: ubuntu-latest services: db: - image: mongo + image: :7.0 options: >- --health-cmd mongosh --health-interval 10s diff --git a/api/account/assets.py b/api/account/assets.py index 4f6f99ef..33afa4d8 100644 --- a/api/account/assets.py +++ b/api/account/assets.py @@ -52,7 +52,10 @@ def _check_locked(self, b): def store_balance(self) -> dict: """ - Alternative PnL data that runs as a cronjob everyday once at 12:00. Stores current balance in DB and estimated + Alternative PnL data that runs as a cronjob everyday once at 12:00. + This works outside of context. + + Stores current balance in DB and estimated total balance in fiat (USDC) for the day. Better than deprecated store_balance_snapshot @@ -61,21 +64,27 @@ def store_balance(self) -> dict: - the result of total_usdc is pretty much the same, the difference is in 0.001 USDC - however we don't need a loop and we decreased one network request (also added one, because we still need the raw_balance to display charts) """ - # Store balance works outside of context as cronjob + fiat = self.get_fiat_coin() wallet_balance = self.get_wallet_balance() - bin_balance = self.get_raw_balance() - rate = self.get_ticker_price('BTCUSDC') + itemized_balance = self.get_raw_balance() + + rate = self.get_ticker_price(f'BTC{fiat}') + + total_wallet_balance = 0 + for item in wallet_balance: + if item["balance"] and float(item["balance"]) > 0: + total_wallet_balance += float(item["balance"]) - total_wallet_balance = next((float(item["balance"]) for item in wallet_balance if float(item["balance"]) > 0), 0) total_usdc = total_wallet_balance * float(rate) - response = self.create_balance_series(bin_balance, round_numbers(total_usdc, 4)) + response = self.create_balance_series(itemized_balance, round_numbers(total_usdc, 4)) return response - def balance_estimate(self, fiat="USDC"): + def balance_estimate(self): """ Estimated balance in given fiat coin """ + fiat = self.get_fiat_coin() balances = self.get_raw_balance() total_fiat = 0 left_to_allocate = 0 @@ -261,6 +270,7 @@ def get_total_fiat(self, fiat="USDC"): float: total BTC estimated in the SPOT wallet then converted into USDC """ + fiat = self.get_fiat_coin() wallet_balance = self.get_wallet_balance() get_usdc_btc_rate = self.ticker(symbol=f"BTC{fiat}", json=False) total_balance = 0 diff --git a/api/autotrade/controller.py b/api/autotrade/controller.py index 8fecd79e..7d4e5968 100644 --- a/api/autotrade/controller.py +++ b/api/autotrade/controller.py @@ -7,6 +7,7 @@ json_response_error, json_response_message, ) +from tools.enum_definitions import AutotradeSettingsDocument class AutotradeSettingsController(Database): """ @@ -14,7 +15,7 @@ class AutotradeSettingsController(Database): """ def __init__( - self, document_id: Literal["test_autotrade_settings", "settings"] = "settings" + self, document_id: AutotradeSettingsDocument = AutotradeSettingsDocument.settings ): self.document_id = document_id self.db = self._db diff --git a/api/db.py b/api/db.py index b99019bb..c86ebf37 100644 --- a/api/db.py +++ b/api/db.py @@ -5,7 +5,7 @@ from tools.handle_error import encode_json from deals.models import DealModel from bots.schemas import BotSchema -from tools.enum_definitions import Status +from tools.enum_definitions import Status, AutotradeSettingsDocument def get_mongo_client(): client = MongoClient( @@ -41,6 +41,11 @@ class Database: """ _db = setup_db() + def get_fiat_coin(self): + document_id = AutotradeSettingsDocument.settings + settings = self._db.research_controller.find_one({"_id": document_id}) + return settings["balance_to_use"] + def save_bot_streaming(self, active_bot: BotSchema, db_collection_name: str="bots"): """ MongoDB query to save bot using Pydantic diff --git a/api/tests/test_assets.py b/api/tests/test_assets.py index 1f8c4fd3..e3a44002 100644 --- a/api/tests/test_assets.py +++ b/api/tests/test_assets.py @@ -1,8 +1,6 @@ from fastapi.testclient import TestClient import pytest from account.assets import Assets -from account.controller import AssetsController -from account.account import Account from main import app import mongomock diff --git a/api/tools/enum_definitions.py b/api/tools/enum_definitions.py index e13eb95b..01ab448f 100644 --- a/api/tools/enum_definitions.py +++ b/api/tools/enum_definitions.py @@ -152,3 +152,12 @@ class BinanceKlineIntervals(str, Enum): def __str__(self): return str(self.str) + +class AutotradeSettingsDocument(str, Enum): + # Autotrade settings for test bots + test_autotrade_settings = "test_autotrade_settings" + # Autotrade settings for real bots + settings = "settings" + + def __str__(self): + return str(self.str)