Minor project
Minor project
ON
Stock AI Trader
1
TABLE OF CONTENT
1 Abstract 3
2 Introduction 4-5
3 Hardware Requirement 6
5 Problem Statement 9
6 Literature Survey
8 Snapshot 17 - 20
9 References 21
2
ABSTRACT
3
INTRODUCTION
4
Hardware Requirement
3. Storage:
At least 500GB of SSD storage to store application data, user inputs, and
system logs, with additional storage if managing larger product databases.
5. Operating System:
Windows 10 /11 would be compatible for this
5
Software Requirement
1. Operating System
Windows 10 or later, macOS, or Linux distributions (Ubuntu recommended)
2. Programming Language
Python 3.8 or later: The core programming language used for developing the
backtesting engine and implementing the strategies.
3. Python Libraries
The project relies on various Python libraries for data handling, analysis, and
visualization. These include:
Streamlit: For building the interactive web application interface.
Pandas: For data manipulation and analysis of stock data.
NumPy: For numerical operations and calculations.
Matplotlib: For visualizing stock data and performance metrics.
Plotly: For interactive and dynamic data visualizations within the Streamlit
app.
yfinance: For fetching historical stock data from Yahoo Finance.
TA-Lib (Technical Analysis Library): For implementing technical indicators
like RSI, MACD, and Bollinger Bands.
4. Development Environment
Jupyter Notebook (optional): For prototyping and experimenting with data
analysis and strategy implementation.
Visual Studio Code (VS Code) or PyCharm: For coding and debugging with
features like IntelliSense, linting, and git integration.
5. Data Source and API
Yahoo Finance API via yfinance library: For real-time and historical stock
data retrieval.
CSV File Upload: The application also supports custom data uploads in
CSV format for backtesting.
6. Web Framework and User Interface
Streamlit: A Python-based framework for creating interactive web
applications with minimal effort, used here for building the front end of the
backtesting engine.
7. Version Control System
6
Git: For version control, collaboration, and tracking changes in the project
codebase.
GitHub/GitLab/Bitbucket: For code hosting and repository management.
8. Virtual Environment (Recommended)
venv or conda: For creating isolated Python environments to manage
dependencies without conflicts.
9. Visualization Tools
Plotly Dash : For enhanced interactive data visualization if additional
features are required in future versions.
7
Problem Statement
8
Literature Survey
The retail and e-commerce industries are transforming with the help of
Artificial Intelligence (AI) and Machine Learning (ML), allowing
businesses to better understand customer behavior and adapt to shifting
trends. These technologies make it possible to predict demand, provide
personalized product recommendations, and optimize inventory
management in real-time. AI and ML dive deep into consumer data,
uncovering patterns that traditional methods miss, which helps enhance
the shopping experience and boost sales. However, businesses still face
hurdles such as data quality and integrating these systems effectively. As
these technologies continue to evolve, they are reshaping how companies
forecast trends, manage stock, and engage with customers.
9
Chapter 1: Algorithmic Trading
WHAT IS ALGORITHMIC TRADING?
Introduction
Algorithmic trading is the common term for automated or algo-trading. This is the
process of using a computer programme that executes trades in consonance with
sets of instructions or algorithms. Such an instruction could be built around
multiple parameters like time, price, volume, or even more complex mathematical
models. The basic objective, however, is to generate trades at a speed and
frequency that is well beyond human capability.
This article, discusses the benefits and drawbacks of algorithmic trading, along
with the technical aspects of trading in detail, along with real-world examples that
explain how algorithmic trading can be applied in a variety of ways.
10
For example, a simple algorithm can be set to instruct a system to buy a stock if
the stock's 50-day moving average crosses above its 200-day moving average and
sell when the reverse happens.
Since computers can process market information faster than humans, algorithmic
trading considerably accelerates the speedy and effective execution of trades. This
is especially observed in high-frequency trading, where algorithms carry out a
high volume of trades in less than a second.
Key Advantages
Speed: Algorithms execute trades in real time and are able to grab
opportunities quicker than a human trader.
Efficiency: Automation prohibits the emotional bias and human mistakes
that are inherent in traditional manual trading.
Backtesting: Algorithms are tested by means of historical data; thus, the
trader is able to test how they would have performed under given previous
market conditions.
Most benefits of algorithmic trading bring with them risks that may or may not be
detrimental to the execution of any trade at hand, such as technical failures or
other market disruptions.
Advantages
Optimal Execution: Algorithms trade at the best possible prices from which
maximum profit can be achieved.
11
Instant and Precise Trade Placement: As trade is placed promptly, it does
have less impact on changing prices.
Low Transaction Costs: The error in human trade placement decreases and it
becomes more effective to trade through algorithms on various markets.
Disadvantages
A Simple Moving Average (SMA) is probably one of the most popular technical
indicators used in trading because it smoothes the price data by calculating an
average price over a certain number of periods. It helps the trader identify trends
and smooth out short-term fluctuations in price.
Where:
A1, A2 ,…, An are the prices at different intervals and n is the total number of
12
intervals over which the average is calculated.
For example, the 50-day SMA calculates the average price of an asset over the last
50 days. The moving average "smoothes" the fluctuations of the price and allows
the traders to determine whether the asset is in the uptrend or in the downtrend.
Buy: It would be the time when the current price is less than the SMA, that is, that
it is selling below its value, which the algorithm would imply to buy through a
buy order.
Sell: It would give a selling signal at any point in time when the price exceeds the
SMA.
15
RSIStrategy).
o Contains methods for generating buy/sell signals based on price
indicators.
o Modular structure allows adding new strategies without modifying the
core engine.
3. Execution Module
Purpose: Simulate trades based on the signals generated by the strategy
module.
Design Features:
o Tracks portfolio state, including cash, holdings, and transaction
history.
o Simulates order execution at historical prices (e.g., close price).
o Handles transaction costs, such as commissions or slippage.
4. Performance Analysis Module
Purpose: Evaluate the results of the backtest.
Design Features:
o Calculates key metrics like final portfolio value, profit/loss, return
on investment (ROI), and sharpe ratio.
o Compares strategy performance with a benchmark (e.g., buy-and-
hold).
o Generates visualizations, including equity curves and buy/sell signal
overlays.
5. User Interface Module
Purpose: Facilitate user interaction for strategy selection and results
interpretation.
Design Features:
16
o Implemented using Streamlit for a web-based interface.
o Allows users to upload data, configure strategies, and view results
interactively.
Working of the Backtesting Engine
The backtesting engine operates in a sequence of steps, as described below:
Step 1: Load Data
Load historical stock data using the Data Handling Module.
Ensure data is aligned with the specified date range.
Provide users with options to upload custom data or select predefined stock
symbols.
Step 2: Configure Parameters
User specifies initial capital, date range, and investment style (e.g.,
aggressive, moderate, passive).
Select a trading strategy from the available options.
Step 3: Generate Signals
The selected strategy processes the loaded data to generate buy/sell signals.
Signals are based on the logic defined in the corresponding strategy class.
Step 4: Simulate Trades
The engine simulates trades by executing orders whenever a signal is
generated.
Updates portfolio value by accounting for:
o The price at which trades occur.
o Transaction costs.
o Changes in holdings and available cash.
Step 5: Analyze Performance
Evaluate the performance of the strategy by calculating:
17
o Final Portfolio Value: Value of cash and holdings at the end of the
test.
o Holdings Value: Comparison with the buy-and-hold strategy.
o Trade History: List of trades with date, price, action, and profit/loss
per trade.
Generate visualizations to help users interpret results.
Step 6: Visualize Results
Display equity curves, cumulative profit/loss, and buy/sell signals on price
charts.
Present trade history and metrics in a tabular format
18
Major Modules
1. Data Collection
This module is responsible for fetching stock market data for analysis. It utilizes:
yfinance: A Python library used to extract stock market data for indices like
Dow Jones, Nifty50, and more.
Custom Module: src/data/data_loader.py handles the loading and
preprocessing of the stock data.
2. Backtesting Engine
The core of the project, it runs backtests to evaluate strategies:
Custom Module: src/backtest/backtester.py implements the backtesting
logic for strategies.
It supports comparing strategies like Bollinger Bands, MACD, and others
against HODL (buy-and-hold) metrics.
3. Strategies
The project includes implementations of various trading strategies, located in
src/strategies:
bollinger_band.py: Bollinger Bands strategy.
macd.py: Moving Average Convergence Divergence strategy.
moving_average.py: General moving average strategy.
simple_moving_average.py: Basic SMA implementation.
rsi.py: Relative Strength Index strategy.
vwap.py: Volume Weighted Average Price strategy.
base_strategy.py: A base class/interface for standardizing strategy
implementations.
4. Visualization
Visualization modules handle creating charts and performance metrics:
Visualization File: src/visualizations.py generates graphical insights into
trading strategies.
Visualization Libraries: Likely includes matplotlib or plotly for plotting
data.
5. Streamlit Web Interface
The Streamlit module builds the project's interactive user interface:
Primary Files: Located in streamlit_app/, including:
o main.py: Main entry point for the Streamlit app.
o layout.py and sidebar.py: Manage the app's layout and user inputs.
o display.py: Displays the backtest results and insights.
6. Utility Modules
19
Utility functions support metrics calculation and visualization:
src/utils/metrics.py: Calculates performance metrics like Sharpe Ratio and
Max Drawdown.
src/utils/visualizations.py: Contains helper functions for data plotting.
7. Testing
The project includes a robust testing framework using:
Test Files: Located in tests/, such as test_backtester.py for backtesting,
test_data_loader.py for data loading, and test_strategies.py for trading
strategies.
Testing Libraries: Likely includes unittest or pytest.
8. Project Setup and Dependencies
The project structure supports easy setup and maintenance:
setup.py: Used for packaging the project.
requirements.txt: Lists external dependencies (e.g., streamlit, yfinance,
pandas).
20
Snapshots
MAIN PAGE:
VISUALIZATION
21
BACKTEST RESULTS:
22
REFERENCES
Research Papers
1. Cartea, Á., Jaimungal, S., & Penalva, J. (2015). Algorithmic and High-
Frequency Trading. Cambridge University Press.
o Discusses algorithmic trading and includes sections on strategy
backtesting.
2. Bailey, D. H., Borwein, J., Lopez de Prado, M., & Zhu, Q. J. (2014). The
Probability of Backtest Overfitting.
o Addresses statistical issues in backtesting and how to avoid
overfitting.
Link: SSRN paper
3. López de Prado, M. (2018). Advances in Financial Machine Learning.
Wiley.
o Explores machine learning applications in finance, including
backtesting frameworks.
Web Resources
1. Investopedia: Backtesting
o A beginner-friendly explanation of backtesting, its importance,
and limitations.
2. QuantInsti Blog: A Guide to Backtesting Trading Strategies
o Practical insights into implementing backtesting frameworks.
using Python.
23