Skip to content

Commit

Permalink
Fix balance series
Browse files Browse the repository at this point in the history
Previously not accumulating total amount of balance across all account.
Also added fiat getter from dashaboard
  • Loading branch information
Carlos Wu committed Sep 20, 2024
1 parent fa08ff8 commit 3ff034f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
runs-on: ubuntu-latest
services:
db:
image: mongo
image: mongo:7.0
options: >-
--health-cmd mongosh
--health-interval 10s
Expand Down
24 changes: 17 additions & 7 deletions api/account/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion api/autotrade/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
json_response_error,
json_response_message,
)
from tools.enum_definitions import AutotradeSettingsDocument

class AutotradeSettingsController(Database):
"""
Autotrade settings
"""

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
Expand Down
7 changes: 6 additions & 1 deletion api/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions api/tests/test_assets.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
9 changes: 9 additions & 0 deletions api/tools/enum_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 3ff034f

Please sign in to comment.