Please rectify this code to respect the upper limit of 30 calls per minute to the API…
- User
Please rectify this code to respect the upper limit of 30 calls per minute to the API
import pandas as pd from pycoingecko import CoinGeckoAPI from datetime import datetime, timedelta import concurrent.futures import json import logging
logging.basicConfig(level=logging.INFO) logger = logging.getLogger(name)
cg = CoinGeckoAPI()
def get_top_n_coins(n): # Get the top n coins by market cap coins = cg.get_coins_markets(vs_currency='usd', order='market_cap_desc', per_page=n, page=1)
code# Log the JSON data for the first coin if coins: logger.info(f"Sample coin data: {json.dumps(coins[0], indent=2)}") return [coin['id'] for coin in coins if not is_stablecoin(coin)]def is_stablecoin(coin): # Simple check: if price change percentage is very low, it's likely a stablecoin return abs(coin.get('price_change_percentage_24h', 0)) < 0.1
def get_historical_data(coin_id, days): # Get market chart data for the last 'days' days data = cg.get_coin_market_chart_by_id(id=coin_id, vs_currency='usd', days=days) prices = data['prices'] # Each entry: [timestamp, price] df = pd.DataFrame(prices, columns=['timestamp', 'price']) df['date'] = pd.to_datetime(df['timestamp'], unit='ms').dt.date df = df.groupby('date').last().reset_index() return df
def calculate_ma100(df): df['MA100'] = df['price'].rolling(window=100).mean() return df
def process_coin(coin_id): try: df = get_historical_data(coin_id, 120) if len(df) < 100: logger.info(f"Not enough data for {coin_id}. Skipping.") return None
codedf = calculate_ma100(df) latest_close = df.iloc[-1]['price'] latest_ma100 = df.iloc[-1]['MA100'] if pd.isna(latest_ma100): logger.info(f"MA100 not available for {coin_id}. Skipping.") return None if latest_close > latest_ma100: logger.info(f"{coin_id} is above MA100.") return coin_id except Exception as e: logger.error(f"Error processing {coin_id}: {e}") return Nonedef main(): top_coins = get_top_n_coins(50)
code - Assistant