Giter Site home page Giter Site logo

Comments (1)

canstralian avatar canstralian commented on July 30, 2024

import finnhub # Import the Finnhub API client
import pandas as pd
import datetime
import time

def get_valid_crypto_symbols(file_path):
"""Reads cryptocurrency symbols from a file and returns a list of valid symbols."""
valid_symbols = []
with open(file_path, 'r') as file:
for line in file:
symbol = line.strip() # Remove leading/trailing whitespaces
if symbol:
valid_symbols.append(symbol)
return valid_symbols

def fetch_crypto_data(client, symbol, start_timestamp, end_timestamp):
"""
Fetches cryptocurrency data for the given symbol within the specified time range.
Returns a pandas DataFrame containing the fetched data.
"""
try:
# Call Finnhub API to get cryptocurrency candles data
res = client.crypto_candles(symbol, 'D', start_timestamp, end_timestamp)
if 's' in res:
return pd.DataFrame(res['s']) # Convert the response to a DataFrame
else:
print(f"No data found for symbol {symbol}")
return None
except Exception as e:
print(f"Error fetching data for symbol {symbol}: {e}")
return None

def main():
# Define time range
start_time = datetime.datetime.now() - datetime.timedelta(days=200)
end_time = datetime.datetime.now()
start_timestamp = int(start_time.timestamp())
end_timestamp = int(end_time.timestamp())

# Initialize Finnhub client
api_key = "YOUR_API_KEY"  # Replace "YOUR_API_KEY" with your actual API key
client = finnhub.Client(api_key=api_key)

# Get valid cryptocurrency symbols from the file
crypto_symbols = get_valid_crypto_symbols("CryptoList.txt")

# Fetch data for each cryptocurrency symbol
for symbol in crypto_symbols:
    # Fetch data for the symbol
    crypto_data = fetch_crypto_data(client, symbol, start_timestamp, end_timestamp)
    if crypto_data is not None:
        # Process the fetched data or call other functions here
        print(f"Fetched data for {symbol}:")
        print(crypto_data.head())  # Just printing the first few rows for demonstration
        print()

    time.sleep(1)  # Add a delay to avoid hitting API rate limits

if name == "main":
main()

from finnhub-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.