DEV Community

Tikam Singh Alma
Tikam Singh Alma

Posted on

Using ArcticDB for Financial Analysis with Python and yFinance

Using ArcticDB for Financial Analysis with Python and yFinance

Efficient financial data storage and analysis are essential for traders, analysts, and data scientists. ArcticDB, a high-performance database optimized for time-series data, combined with yFinance, makes it easy to fetch, store, and analyze financial data at scale.


Why Use ArcticDB for Financial Data?

  • Fast read/write operations for large datasets
  • Supports DataFrame operations like deduplication
  • Versioning for historical data comparisons
  • Optimized for time-series data

Step 1: Install Required Libraries

pip install arcticdb yfinance pandas
Enter fullscreen mode Exit fullscreen mode

Step 2: Fetch Stock Data with yFinance

import yfinance as yf
import pandas as pd

# Fetch historical data for Apple (AAPL)
ticker = yf.Ticker("AAPL")
hist_data = ticker.history(period="5y", auto_adjust=False)
print(hist_data.head())
Enter fullscreen mode Exit fullscreen mode

Step 3: Store Data in ArcticDB

from arcticdb import Arctic

DB_PATH = 'lmdb://./stock_db'
LIBRARY_NAME = 'financial_data'

# Connect to ArcticDB and create a library
ac = Arctic(DB_PATH)
if LIBRARY_NAME not in ac.list_libraries():
    ac.create_library(LIBRARY_NAME)

lib = ac[LIBRARY_NAME]

# Write data to ArcticDB
lib.write("AAPL", hist_data)
print("Data written to ArcticDB.")
Enter fullscreen mode Exit fullscreen mode

Step 4: Read and Analyze Data from ArcticDB

# Read data from ArcticDB
result = lib.read("AAPL")
df = result.data
print(df.tail())

# Simple moving average (SMA) example
df['SMA_50'] = df['Close'].rolling(window=50).mean()

# Plot the stock price with SMA
df[['Close', 'SMA_50']].plot(title="AAPL Stock Price with 50-Day SMA")
Enter fullscreen mode Exit fullscreen mode

Step 5: Update and Deduplicate Data

# New data update
new_data = ticker.history(period="1mo", auto_adjust=False)
combined = pd.concat([result.data, new_data])
filtered = combined[~combined.index.duplicated(keep='last')]

lib.write("AAPL", filtered.sort_index())
print("Data updated.")
Enter fullscreen mode Exit fullscreen mode

Conclusion

By integrating ArcticDB and yFinance, you can efficiently fetch, store, and analyze financial data for better decision-making. ArcticDB’s versioning and high-performance capabilities make it a game-changer for large-scale financial analysis.

Want to take your financial analysis even further? Explore more with technical indicators and machine learning techniques!

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more