![Screenshot 2024-04-17 at 18 44 21](https://private-user-images.githubusercontent.com/73235926/323187520-99695d4a-bc10-4159-87b4-61267913b64a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTg0NzY1NzcsIm5iZiI6MTcxODQ3NjI3NywicGF0aCI6Ii83MzIzNTkyNi8zMjMxODc1MjAtOTk2OTVkNGEtYmMxMC00MTU5LTg3YjQtNjEyNjc5MTNiNjRhLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA2MTUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNjE1VDE4MzExN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTFhMTJlM2VhZjBkYzdjYTA3MWM4NzUyYTQ4ODUyNjRjZmQ5YzIwNzk2MWMxYzUxODE4ZTM1ZjdkMjYzZGRmOTMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.fpagzAn-adWjV_Rb4ctnm-z8pLViD-01_Lm3EmrZLeM)
- Open your command line interface (CLI) or terminal.
- Navigate to the directory where you want to clone the repository.
- Run the following command to clone the repository:
git clone https://github.com/yllvar/buy-and-hold-vs-rsi.git
- Visit the official Python website at python.org.
- Download the latest version of Python for your operating system.
- Follow the installation instructions provided on the website.
- Open your command line interface (CLI) or terminal.
- Install Jupyter Notebook by running the following command:
pip install jupyterlab
- Once installed, start Jupyter Notebook by running the following command:
jupyter notebook
- This will open a new tab in your web browser with the Jupyter Notebook interface.
- Navigate to the directory where you cloned the repository.
- Open a new terminal or command prompt in that directory.
- Run the following command to install the required dependencies:
This command will install all the necessary packages listed in the
pip install -r requirements.txt
requirements.txt
file.
- In the Jupyter Notebook interface, navigate to the directory where you cloned the repository.
- Click on the Python script file (e.g.,
buy_and_hold_vs_rsi.ipynb
) to open it. - Follow the instructions provided within the notebook to execute the code cells.
- To run the code cells, click on each cell and press Shift + Enter or click the "Run" button in the toolbar.
- The code will download the historical stock data, perform analysis, and visualize the results within the notebook.
This repository contains a Python script that compares the performance of a buy-and-hold strategy against a Relative Strength Index (RSI) based trading strategy using historical stock data from Yahoo Finance. The script analyzes the performance of these two strategies on Coca-Cola (KO) stock from 2016 to 2021, providing insights into their returns and risks.
- numpy (as np): For numerical operations.
- pandas (as pd): For data manipulation and analysis.
- yfinance (as yf): For downloading historical stock data from Yahoo Finance.
- matplotlib.pyplot (as plt): For visualization.
- The code downloads historical stock data for the specified stock symbol (
'KO'
for Coca-Cola) from January 1, 2016, to March 21, 2021, using theyf.download()
function from the Yahoo Finance API.
rsi_period
: Specifies the lookback period for calculating the RSI indicator (in this case, 14 days).rsi_oversold
: Specifies the threshold for the RSI indicator to indicate oversold conditions (typically set to 30).rsi_overbought
: Specifies the threshold for the RSI indicator to indicate overbought conditions (typically set to 70).fee
: Specifies the transaction fee for buying and selling stocks (set to 0.05% or 0.0005).
- Calculates the daily returns, Upward movement, Downward movement, Relative Strength (RS), and RSI (Relative Strength Index) based on the provided formulae.
- Computes the returns and risks for both the Buy-and-Hold (BnH) strategy and the RSI-based strategy.
- The BnH strategy simply holds the stock throughout the entire period.
- The RSI-based strategy buys when the RSI falls below the oversold threshold and sells when the RSI exceeds the overbought threshold.
- Prints the return and risk of both strategies.
- Plots the cumulative returns of both strategies over time.
![Screenshot 2024-04-17 at 18 29 32](https://private-user-images.githubusercontent.com/73235926/323183115-a1331b55-52b0-41d0-839e-6ff30990a73e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTg0NzY1NzcsIm5iZiI6MTcxODQ3NjI3NywicGF0aCI6Ii83MzIzNTkyNi8zMjMxODMxMTUtYTEzMzFiNTUtNTJiMC00MWQwLTgzOWUtNmZmMzA5OTBhNzNlLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA2MTUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNjE1VDE4MzExN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTE3NzY5NDMxNjk2YmY5YWQwOWU1Y2I0OGNkZGE5OWFhMGRlMzQ2ZmY1NzE1NjJkZjkxMWE1NzA0NTBlODVlNmImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.pHNGUctiOGVBv8lx1haqctNg-wUqZ9sgfguONQZFJIE)