Skip to content

Commit 94c0195

Browse files
committed
Refactor delist recommender API integration and enhance scoring logic
- Updated Drift API configuration by renaming base URL and headers for clarity and consistency. - Introduced a new scoring mechanism that applies a score boost for specific symbols, improving recommendation accuracy. - Refactored scoring cutoffs to use full dollar amounts for better precision in calculations. - Enhanced documentation within the code to clarify the purpose of ignored symbols and scoring boundaries. - Streamlined the data fetching process by ensuring consistent use of the updated API configuration across all relevant functions.
1 parent 7c44e9d commit 94c0195

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

backend/api/delist_recommender.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
# This script is used to provide backend functionality for /src/page/delist_recommender.py
2-
# It should make use of the backend state and middleware to fetch the data borrowing similar logic from /backend/api/asset_liability.py and /backend/api/health.py and /backend/api/price_shock.py
32

4-
# It should return a JSON object with the following fields:
5-
# - status: "success" or "error"
6-
# - message: a message to be displayed to the user
7-
# - data: a JSON object containing the data to be displayed to the user
8-
9-
# This script provides backend functionality for delist recommender analysis
10-
# It makes use of the backend state and middleware to fetch data efficiently
113
import os
124
import math
135
import sys
@@ -43,15 +35,23 @@
4335
DAYS_TO_CONSIDER = 30
4436

4537
# Drift API configuration
46-
DRIFT_API_BASE_URL = "https://y7n4m4tnpb.execute-api.eu-west-1.amazonaws.com"
47-
DRIFT_API_HEADERS = {"X-Origin-Verify": "AolCE35uXby9TJHHgoz6"}
38+
DRIFT_DATA_API_BASE_URL = "https://y7n4m4tnpb.execute-api.eu-west-1.amazonaws.com"
39+
DRIFT_DATA_API_HEADERS = {"X-Origin-Verify": "AolCE35uXby9TJHHgoz6"}
4840
API_RATE_LIMIT_INTERVAL = 0.1 # seconds between requests
4941

42+
# Drift Score Boost - These are symbols that get a score boost in the delist recommender
43+
DRIFT_SCORE_BOOST_SYMBOLS = {
44+
"DRIFT-PERP",
45+
}
46+
47+
# Drift Score Boost Amount - The amount of score boost to apply to the symbols in DRIFT_SCORE_BOOST_SYMBOLS
48+
DRIFT_SCORE_BOOST_AMOUNT = 10
49+
5050
# Global rate limiter variables for API calls
5151
rate_limit_lock = asyncio.Lock()
5252
last_request_time = 0.0
5353

54-
# Symbols to ignore completely during analysis
54+
# Prediction Market Symbols to ignore completely during analysis
5555
IGNORED_SYMBOLS = {
5656
"TRUMP-WIN-2024-BET",
5757
"KAMALA-POPULAR-VOTE-2024-BET",
@@ -74,12 +74,12 @@
7474
# Note: Inputs to scoring ('MC', 'Spot Volume', etc.) are expected in full dollar amounts, not millions.
7575
DRIFT_SCORE_CUTOFFS = {
7676
'Market Cap Score': {
77-
'MC': {'kind': 'exp', 'start': 1000000, 'end': 5000000000, 'steps': 20}, # $1M to $5B
77+
'MC': {'kind': 'exp', 'start': 1_000_000, 'end': 5_000_000_000, 'steps': 20}, # $1M to $5B
7878
},
7979
'Spot Vol Score': {
8080
# Expects 'Spot Volume' (sum of avg daily vol) and 'Spot Vol Geomean' (geomean of top 3 avg daily vol) in full dollars
81-
'Spot Volume': {'kind': 'exp', 'start': 10000, 'end': 1000000000, 'steps': 10}, # $10k to $1B
82-
'Spot Vol Geomean': {'kind': 'exp', 'start': 10000, 'end': 1000000000, 'steps': 10}, # $10k to $1B
81+
'Spot Volume': {'kind': 'exp', 'start': 10_000, 'end': 1_000_000_000, 'steps': 10}, # $10k to $1B
82+
'Spot Vol Geomean': {'kind': 'exp', 'start': 10_000, 'end': 1_000_000_000, 'steps': 10}, # $10k to $1B
8383
},
8484
'Futures Vol Score': {
8585
# Expects 'Fut Volume' (sum of avg daily vol) and 'Fut Vol Geomean' (geomean of top 3 avg daily vol) in full dollars
@@ -88,13 +88,14 @@
8888
},
8989
'Drift Activity Score': {
9090
# Expects 'Volume on Drift' (estimated 30d vol) and 'OI on Drift' in full dollars
91-
'Volume on Drift': {'kind': 'exp', 'start': 1000, 'end': 500000000, 'steps': 10}, # $1k to $500M
92-
'OI on Drift': {'kind': 'exp', 'start': 1000, 'end': 500000000, 'steps': 10}, # $1k to $500M
91+
'Volume on Drift': {'kind': 'exp', 'start': 1_000, 'end': 500_000_000, 'steps': 10}, # $1k to $500M
92+
'OI on Drift': {'kind': 'exp', 'start': 1_000, 'end': 500_000_000, 'steps': 10}, # $1k to $500M
9393
},
9494
}
9595

9696
# Score boundaries for delist recommendations
97-
SCORE_LB = {0: 0, 5: 37, 10: 48, 20: 60} # Lower bounds
97+
SCORE_UB = {0: 62, 3: 75, 5: 85, 10: 101} # Upper Bound: If score >= this, consider increasing leverage
98+
SCORE_LB = {0: 0, 5: 37, 10: 48, 20: 60} # Lower Bound: If score < this, consider decreasing leverage/delisting
9899

99100
# Reference exchanges for market data
100101
REFERENCE_SPOT_EXCH = {
@@ -1327,6 +1328,15 @@ def build_scores(df):
13271328
score_components = list(DRIFT_SCORE_CUTOFFS.keys())
13281329
output_df['Score'] = output_df[score_components].sum(axis=1)
13291330

1331+
# Apply score boost for specific symbols
1332+
output_df['Score'] = output_df['Score'].add(
1333+
pd.Series(
1334+
[DRIFT_SCORE_BOOST_AMOUNT if symbol in DRIFT_SCORE_BOOST_SYMBOLS else 0
1335+
for symbol in output_df.index],
1336+
index=output_df.index
1337+
)
1338+
)
1339+
13301340
return output_df
13311341

13321342
def generate_recommendation(row):
@@ -2062,7 +2072,7 @@ async def fetch_api_page(session, url: str, retries: int = 5):
20622072
last_request_time = time.time()
20632073

20642074
try:
2065-
async with session.get(url, headers=DRIFT_API_HEADERS, timeout=10) as response:
2075+
async with session.get(url, headers=DRIFT_DATA_API_HEADERS, timeout=10) as response:
20662076
if response.status != 200:
20672077
logger.warning(f"API request failed: {url}, status: {response.status}")
20682078
if attempt < retries - 1:
@@ -2104,7 +2114,7 @@ async def fetch_market_trades(session, symbol: str, start_date: datetime, end_da
21042114

21052115
while current_date <= end_date:
21062116
year, month, day = current_date.year, current_date.month, current_date.day
2107-
url = f"{DRIFT_API_BASE_URL}/market/{symbol}/trades/{year}/{month}/{day}?format=json"
2117+
url = f"{DRIFT_DATA_API_BASE_URL}/market/{symbol}/trades/{year}/{month}/{day}?format=json"
21082118

21092119
logger.debug(f"Fetching trades for {symbol} on {year}/{month}/{day}")
21102120

0 commit comments

Comments
 (0)