Advertisement
actorcat

crypto python

Sep 2nd, 2024 (edited)
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | None | 0 0
  1. import pandas as pd
  2. import requests
  3.  
  4. # Load the CSV file into a DataFrame
  5. tickers = pd.read_csv('CRYPTO.csv')
  6.  
  7. # Get unique currencies and tickers
  8. currencies = tickers['Currency'].dropna().unique()  # Remove NaN values from currencies
  9. tickers_list = tickers['Ticker'].dropna().unique()  # Remove NaN values from tickers
  10.  
  11. # Initialize an empty DataFrame with tickers as rows and currencies as columns
  12. pd_frame = pd.DataFrame(index=tickers_list, columns=currencies)
  13.  
  14. # Loop through each ticker
  15. for ticker in tickers_list:
  16.     for currency in currencies:
  17.         try:
  18.             url = f"https://min-api.cryptocompare.com/data/histoday?fsym={ticker}&tsym={currency}&limit=60&aggregate=3&e=CCCAGG"
  19.             response = requests.get(url)
  20.             response.raise_for_status()  # Raise HTTPError for bad responses
  21.             data = response.json()
  22.            
  23.             # Check if the data contains enough entries
  24.             if len(data['Data']) > 0:
  25.                 # Get the most recent close price (last entry)
  26.                 close_price = data['Data'][60]['close']
  27.             else:
  28.                 close_price = None
  29.                 print(f"Not enough data for {ticker} in {currency}")
  30.            
  31.             # Update the DataFrame cell with the close price
  32.             pd_frame.at[ticker, currency] = close_price
  33.        
  34.         except requests.RequestException as e:
  35.             print(f"Request failed for {ticker} in {currency}: {e}")
  36.  
  37. # Print the final DataFrame
  38. print(pd_frame)
  39.  
  40. # Save the DataFrame to a CSV file
  41. output_csv_path = 'crypto_prices.csv'
  42. pd_frame.to_csv(output_csv_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement