Skip to content

Issue 3: Missing Error Handling for API Requests #3

@PrinceSajjadHussain

Description

@PrinceSajjadHussain

Title: Add more robust error handling and retry logic to fetch_binance_ohlcv

Body:
The fetch_binance_ohlcv function in backtesting_tool.py includes a basic response.raise_for_status() call, which raises an exception for HTTP error codes (4xx, 5xx). However, this might not be sufficient for handling transient errors or API rate limiting.

Enhancements could include:

  1. Retry Logic: Implement a retry mechanism (using libraries like requests or tenacity) with exponential backoff to handle temporary network issues or API rate limits. This will make the code more resilient.
  2. Specific Exception Handling: Catch specific exceptions from the requests library (e.g., requests.exceptions.ConnectionError, requests.exceptions.Timeout) and handle them appropriately (e.g., log the error and retry, or exit gracefully).
  3. Rate Limit Handling: Check the response headers for rate limit information (e.g., X-RateLimit-Remaining, X-RateLimit-Limit, Retry-After) and pause execution if the rate limit is exceeded.
  4. Logging: Implement robust logging to track errors, warnings, and other relevant information about the API requests.

Here is an example of error handling and retries:

import requests
import pandas as pd
import time
from requests.exceptions import RequestException

def fetch_binance_ohlcv(symbol, interval='1d', limit=365, max_retries=3):
    base_url = "https://api.binance.us/api/v3/klines"
    params = {"symbol": symbol.upper(), "interval": interval, "limit": limit}

    for attempt in range(max_retries):
        try:
            response = requests.get(base_url, params=params)
            response.raise_for_status()  # Raise HTTPError for bad responses (4xx or 5xx)
            data = response.json()
            df = pd.DataFrame(data, columns=[
                "timestamp", "open", "high", "low", "close", "volume",
                "close_time", "quote_asset_volume", "trades",
                "taker_buy_base", "taker_buy_quote", "ignore"
            ])
            df["timestamp"] = pd.to_datetime(df["timestamp"], unit='ms')
            df.set_index("timestamp", inplace=True)
            for col in ["open", "high", "low", "close", "volume"]:
                df[col] = df[col].astype(float)
            return df[["close"]]

        except RequestException as e:
            print(f"Attempt {attempt + 1} failed: {e}")
            if attempt < max_retries - 1:
                wait_time = 2 ** attempt  # Exponential backoff
                print(f"Retrying in {wait_time} seconds...")
                time.sleep(wait_time)
            else:
                print(f"Max retries reached. Unable to fetch data for {symbol}.")
                return None  # Or raise the exception if appropriate

    return None  # Return None if all retries fail

File: strategy-update-backtest.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions