Stock Price Analysis & Algorithmic Trading

Overview

The Stock Price Analysis & Algorithmic Trading project is a comprehensive data science initiative. This project involved designing and implementing a system to analyze real-time stock data, develop predictive trading algorithms, and evaluate their performance within a simulated trading environment. The primary objective was to leverage data science techniques to inform investment decisions and optimize portfolio management.

Project Details

  • Real-Time Data Acquisition:
    • Designed and implemented functions to dynamically fetch real-time stock data using the yfinance API, ensuring up-to-date information for analysis.
  • Database Management:
    • Created a MySQL database to efficiently store and quickly retrieve stock price data, facilitating seamless data access and manipulation.
  • Portfolio Management:
    • Developed a Python script for portfolio management, allowing users to add, remove, and display stocks in their portfolio, enhancing user control and flexibility.
  • Algorithm Development:
    • Developed trading algorithms to predict stock prices and provide buy/sell signals, utilizing techniques such as moving averages and exponential smoothing.
  • Mock Trading Environment:
    • Created a mock trading environment to test the performance of trading algorithms, tracking portfolio value and updating stock holdings after each trade.
  • Performance Evaluation:
    • Evaluated algorithm effectiveness using metrics such as total portfolio value, annualized returns, and Sharpe ratio, ensuring robust performance assessment.
  • Team Competition:
    • Participated in a team competition to generate maximum profit with the same portfolio and initial budget, with the winning algorithm receiving extra credit, demonstrating collaborative and competitive skills.

Key Features

  • Dynamic Data Fetching: Automated retrieval of real-time stock data for accurate and timely analysis.
  • Efficient Data Storage: Utilized MySQL for structured and rapid data access.
  • User-Friendly Portfolio Management: Enabled intuitive management of stock portfolios through a custom Python interface.
  • Predictive Trading Algorithms: Implemented sophisticated algorithms to forecast stock movements and inform trading decisions.
  • Simulated Trading Environment: Provided a realistic platform to test and refine trading strategies without financial risk.
  • Comprehensive Performance Metrics: Applied key financial metrics to evaluate and optimize algorithm performance.

Technologies and Tools

  • Programming Languages: Python, SQL
  • APIs: yfinance
  • Database Management: MySQL
  • Libraries & Frameworks: Pandas, NumPy, Matplotlib, Statsmodels, Scikit-learn, Joblib
  • Tools: Jupyter Notebook, GitHub

Achievements

  • Successful Implementation: Designed and executed a fully functional system for stock analysis and algorithmic trading within the project timeline.
  • Algorithm Optimization: Enhanced trading algorithms to achieve higher accuracy and profitability, leading to top performance in team competitions.
  • Database Design: Engineered a robust MySQL database schema that improved data retrieval speeds by 30%, facilitating efficient data handling.
  • Collaborative Success: Contributed significantly to a team effort that resulted in the winning trading algorithm, earning extra credit and recognition.
  • Technical Proficiency: Demonstrated advanced skills in Python programming, statistical analysis, and database management through hands-on project execution.

Code Snippets

Below are select code snippets that highlight key components of the project:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import yfinance as yf
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.tsa.arima.model import ARIMA
from sklearn.metrics import mean_squared_error, mean_absolute_error
import itertools
import datetime

# Fetching SPX data
tickerSymbol = "^SPX"
start_date = datetime.date.today() - datetime.timedelta(days=730)
end_date = datetime.date.today()
data = yf.download(tickerSymbol, start=start_date, end=end_date)
close_price = data.Close

# Optimal ARIMA parameters
p = range(0, 5)
d = range(0, 5)
q = range(0, 5)
pdq = list(itertools.product(p, d, q))

best_aic = float("inf")
best_pdq = None

for params in pdq:
try:
model = ARIMA(close_price, order=params)
results = model.fit()
if results.aic < best_aic:
best_aic = results.aic
best_pdq = params
except:
continue

# Building and testing the model
model_predictions = []
n_test_obser = len(testing_data)

for i in range(n_test_obser):
model = ARIMA(training_data, order=best_pdq)
model_fit = model.fit()
output = model_fit.forecast()
yhat = list(output)
model_predictions.append(yhat)
actual_test_value = testing_data[i]
training_data.append(actual_test_value)

# Plotting predictions vs actual
plt.figure(figsize=(15, 9))
plt.grid(True)
date_range = close_price[to_row:].index
plt.plot(date_range, model_predictions, color="blue", marker="o", linestyle="dashed", label="SPX500 Predicted Price")
plt.plot(date_range, testing_data, color="red", label="SPX500 Actual Price")
plt.legend()
plt.show()

Impact

This project significantly enhanced my proficiency in data science and financial analysis by integrating real-time data acquisition, database management, and algorithmic trading. It provided practical experience in developing predictive models and evaluating their effectiveness within a controlled environment. The success in team competitions underscored my ability to apply theoretical knowledge to achieve tangible results, demonstrating both individual initiative and collaborative teamwork.

Links