Comments (1)
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)
- Internal Server Error when testing technical indicator endpoint
- Missing 1 month of data on candles endpoint
- Why not wrapping stock_candles results into a pandas.DataFrame?
- Earning Calendar
- Could I use this API to get daily stock price everyday?
- symbol lookup is not returning useful results HOT 3
- QQQ 2021-06-15 13:30 data missing HOT 1
- Feasability of using endpoint: stock/symbol?exchange=US to create/update a masterfile
- META (Facebook) 2022-01-31 ~ 2022-06-08 1 minute bars missing
- any plans to add futures data?
- raise Key Error
- Just curious... HOT 1
- marketcap
- #stock_candle not clear HOT 1
- [Question] About the type `PUBLIC` in stock_symbols
- Multiple symbols request.
- financials_reported not getting last report of 2023 on aapl stock
- [Bug] Stocks without peers HOT 2
- Not access to Finnhub resource
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from finnhub-python.