From 8e22885be06dd73a51e84bdd8c3b9dfb22e1ecb5 Mon Sep 17 00:00:00 2001 From: Carlos Wu Date: Sat, 7 Dec 2024 13:03:31 +0000 Subject: [PATCH] Setup new top gainers endpoiont (#634) --- .dockerignore | 3 ++- api/apis.py | 2 +- api/autotrade/controller.py | 4 ++++ api/charts/controllers.py | 16 +++++++++++++++- api/charts/routes.py | 21 ++++++++++++++++++++- api/database/models/autotrade_table.py | 1 + api/tests/test_autotrade_settings.py | 2 ++ binquant | 2 +- 8 files changed, 46 insertions(+), 5 deletions(-) diff --git a/.dockerignore b/.dockerignore index 919108bf6..9ddf685de 100644 --- a/.dockerignore +++ b/.dockerignore @@ -16,4 +16,5 @@ db.dump __pycache__ kafka_data pg_data -!terminal/.env \ No newline at end of file +!terminal/.env +!.python-version diff --git a/api/apis.py b/api/apis.py index e5afe52c5..12a40dba2 100644 --- a/api/apis.py +++ b/api/apis.py @@ -148,7 +148,7 @@ def ticker_24(self, type: str = "FULL", symbol: str | None = None): params["symbol"] = symbol # mongo_cache = self.setup_mongocache() - # expire_after = 15m because candlesticks are 15m + # # because candlesticks are 15m # session = CachedSession('ticker_24_cache', backend=mongo_cache, expire_after=15) data = self.request(url=self.ticker24_url, params=params) return data diff --git a/api/autotrade/controller.py b/api/autotrade/controller.py index bf9e392b8..30bee09f4 100644 --- a/api/autotrade/controller.py +++ b/api/autotrade/controller.py @@ -74,3 +74,7 @@ def edit_settings(self, data): AsyncBaseProducer().update_required("UPDATE_AUTOTRADE_SETTINGS") self.session.close() return settings + + def get_fiat(self): + data = self.get_settings() + return data.fiat diff --git a/api/charts/controllers.py b/api/charts/controllers.py index 18392cfae..f3bc8aa30 100644 --- a/api/charts/controllers.py +++ b/api/charts/controllers.py @@ -116,7 +116,8 @@ class MarketDominationController(Database, BinbotApi): def __init__(self) -> None: super().__init__() self.collection = self.kafka_db.market_domination - self.autotrade_settings = AutotradeSettingsController().get_settings() + self.autotrade_db = AutotradeSettingsController() + self.autotrade_settings = self.autotrade_db.get_settings() def mkdm_migration(self): """ @@ -236,3 +237,16 @@ def get_market_domination(self, size=7): ] ) return list(result) + + def top_gainers(self): + """ + Get market top gainers of the day + + ATTENTION - This is a very heavy weight operation + ticker_24() retrieves all tokens + """ + fiat = self.autotrade_db.get_fiat() + ticket_data = self.ticker_24() + + fiat_market_data = sorted((item for item in ticket_data if item["symbol"].endswith(fiat) and float(item["priceChangePercent"]) > 0), key=lambda x: x["priceChangePercent"], reverse=True) + return fiat_market_data[:10] diff --git a/api/charts/routes.py b/api/charts/routes.py index d641305d8..f68f8e386 100644 --- a/api/charts/routes.py +++ b/api/charts/routes.py @@ -1,4 +1,4 @@ -from fastapi import APIRouter +from fastapi import APIRouter, HTTPException from tools.round_numbers import format_ts from charts.models import ( GetMarketDominationResponse, @@ -99,3 +99,22 @@ def md_migration(): return json_response_error( f"Failed to migrate market domination data: {error.args[0]}" ) + + +@charts_blueprint.get("/top-gainers", tags=["assets"]) +def top_gainers(): + try: + response = MarketDominationController().top_gainers() + if response: + return json_response( + { + "data": response, + "message": "Successfully retrieved top gainers data.", + "error": 0, + } + ) + else: + raise HTTPException(404, detail="No data found") + + except Exception as error: + return json_response_error(f"Failed to retrieve top gainers data: {error}") diff --git a/api/database/models/autotrade_table.py b/api/database/models/autotrade_table.py index dedf11cb1..b1fe240bb 100644 --- a/api/database/models/autotrade_table.py +++ b/api/database/models/autotrade_table.py @@ -40,6 +40,7 @@ class AutotradeTable(SQLModel, table=True): stop_loss: float = Field(default=0) take_profit: float = Field(default=2.3) balance_to_use: str = Field(default="USDC") + fiat: str = Field(default="USDC") max_request: int = Field(default=950) telegram_signals: bool = Field(default=True) max_active_autotrade_bots: int = Field(default=1) diff --git a/api/tests/test_autotrade_settings.py b/api/tests/test_autotrade_settings.py index 0dcf77b46..df02c05ec 100644 --- a/api/tests/test_autotrade_settings.py +++ b/api/tests/test_autotrade_settings.py @@ -50,6 +50,8 @@ def test_get_autotrade_settings(client) -> None: "test_autotrade": False, "trailling_deviation": 0.63, "stop_loss": 0.0, + "fiat": "USDC", + # Below to be deprecated "balance_to_use": "USDC", "telegram_signals": True, "close_condition": "dynamic_trailling", diff --git a/binquant b/binquant index 6a4ca1ba2..381181d12 160000 --- a/binquant +++ b/binquant @@ -1 +1 @@ -Subproject commit 6a4ca1ba2b717f8cf83a7b690da1715a7f8830e2 +Subproject commit 381181d122b94e03e39abb86a816871dea033f3e