from fastapi import FastAPI, Query import requests app = FastAPI() def fetch_ticker_data(): """ Fetch all trading pair data from MEXC Exchange. Returns: List of trading pair data as JSON. """ url = "https://api.mexc.com/api/v3/ticker/bookTicker" try: response = requests.get(url, timeout=10) response.raise_for_status() return response.json() except requests.exceptions.RequestException as e: print(f"Error fetching ticker data: {e}") return [] def fetch_order_book(symbol, limit=10): """ Fetch order book data for a specific trading pair from MEXC Exchange. """ url = f"https://api.mexc.com/api/v3/depth?symbol={symbol}&limit={limit}" try: response = requests.get(url, timeout=10) response.raise_for_status() return response.json() except requests.exceptions.RequestException as e: print(f"Error fetching order book for {symbol}: {e}") return {} def find_large_seller_spread(tickers, result_limit=10): """ Find tokens where the 5th seller bid is 100% greater than the 1st seller bid. """ tokens = [] for ticker in tickers: symbol = ticker.get("symbol") if not symbol: continue order_book = fetch_order_book(symbol, limit=5) asks = order_book.get("asks", []) if len(asks) >= 5: try: first_ask = float(asks[0][0]) fifth_ask = float(asks[4][0]) if fifth_ask >= 2 * first_ask: tokens.append({ "symbol": symbol, "1st_seller_bid": first_ask, "5th_seller_bid": fifth_ask, "difference_percentage": round(((fifth_ask - first_ask) / first_ask) * 100, 2) }) except (ValueError, IndexError): continue if len(tokens) >= result_limit: break return tokens[:result_limit] @app.get("/") def read_root(): return {"message": "Welcome to the MEXC Order Book API!"} @app.get("/get_tokens/") def get_tokens(limit: int = Query(10, title="Limit", description="Number of tokens to return")): """ Fetch and return tokens where the 5th seller bid is 100% greater than the 1st seller bid. """ tickers = fetch_ticker_data() if not tickers: return {"error": "No data available"} tokens = find_large_seller_spread(tickers, result_limit=limit) return {"count": len(tokens), "tokens": tokens}