The script begins by importing the necessary libraries:
requestsfor making HTTP requests to the CoinGecko API,pandasfor data manipulation and analysis,datetimefor handling dates and times, althoughdatetimeis not used in the script.
The function accepts four parameters:
crypto_id(default'ethereum'): Specifies the cryptocurrency ID for which data is fetched.vs_currency(default'usd'): Sets the currency against which the cryptocurrency's price is compared.days(default'max'): Determines the time span of historical data to fetch (e.g.,'max'for all available historical data).interval(default'daily'): Sets the granularity of the price data (e.g.,'daily'for daily prices).
- Constructs a request URL using these parameters and sends an HTTP GET request to the CoinGecko API.
- The response, assumed to be in JSON format containing price data, is processed to extract prices. These prices are then converted into a pandas DataFrame with columns for timestamp (converted to a human-readable date) and price.
- The DataFrame is returned, now containing only the date and price columns, with the original timestamp column removed.
- By calling
fetch_crypto_data()without any arguments, the function uses its default parameters to fetch historical price data for Ethereum in USD over its entire available history, at daily intervals.
- The retrieved DataFrame is then saved to a CSV file named
ethereum_historical_prices.csv, with no index column.