{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"# **Backtesting Momentum Trading Strategies using OpenBB**\n",
"\n",
"This notebook demonstrates how to perform backtesting of a momentum trading strategy using historical stock price data from OpenBB. A momentum trading strategy involves buying or selling assets based on recent price movements. In this notebook, we will:\n",
"- Fetch Historical Stock Data using OpenBB.\n",
"- Apply a Momentum Strategy based on moving averages.\n",
"- Simulate Trades to backtest the strategy.\n",
"- Analyze Performance by comparing the strategy’s returns to a buy-and-hold strategy.\n",
"\n",
"The goal of the analysis is to test the effectiveness of a momentum-based trading strategy over time and to see how it performs in comparison to a simple buy-and-hold approach.\n",
"\n",
"Author:
\n",
"[Sanchit Mahajan](https://github.com/SanchitMahajan236)\n",
"\n",
"[](https://colab.research.google.com/drive/1FCMR2oFACCP-YciCcvN5qRnh1r1GypzV?usp=sharing)"
],
"metadata": {
"id": "K_fd_9baXaH9"
}
},
{
"cell_type": "code",
"source": [
"!pip install openbb -q"
],
"metadata": {
"id": "9SiXPtRwW_lo"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from openbb import obb"
],
"metadata": {
"id": "J7B1R7s10Bsa"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"source": [
"symbols = ['AAPL', 'GOOG', 'MSFT', 'NVDA']\n",
"start_date = '2015-01-01'\n",
"initial_capital = 10000\n",
"short_window = 40\n",
"long_window = 100\n",
"dataframes = []\n",
"\n",
"for symbol in symbols:\n",
" try:\n",
" data = obb.equity.price.historical(\n",
" symbol=symbol,\n",
" start_date=start_date,\n",
" provider=\"yfinance\"\n",
" ).to_df()\n",
" data['Symbol'] = symbol\n",
" dataframes.append(data)\n",
" except Exception as e:\n",
" print(f\"Failed to fetch data for {symbol}: {str(e)}\")\n",
"\n",
"combined_data = pd.concat(dataframes)\n",
"combined_data = combined_data.reset_index()\n",
"\n",
"combined_data.head()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 206
},
"id": "MrRw8lT_zD11",
"outputId": "c5a1b53a-d41c-4c1e-de3b-8acf9aa5c8a1"
},
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" date open high low close volume \\\n",
"0 2015-01-02 27.847500 27.860001 26.837500 27.332500 212818400 \n",
"1 2015-01-05 27.072500 27.162500 26.352501 26.562500 257142000 \n",
"2 2015-01-06 26.635000 26.857500 26.157499 26.565001 263188400 \n",
"3 2015-01-07 26.799999 27.049999 26.674999 26.937500 160423600 \n",
"4 2015-01-08 27.307501 28.037500 27.174999 27.972500 237458000 \n",
"\n",
" split_ratio dividend Symbol \n",
"0 0.0 0.0 AAPL \n",
"1 0.0 0.0 AAPL \n",
"2 0.0 0.0 AAPL \n",
"3 0.0 0.0 AAPL \n",
"4 0.0 0.0 AAPL "
],
"text/html": [
"\n",
"
| \n", " | date | \n", "open | \n", "high | \n", "low | \n", "close | \n", "volume | \n", "split_ratio | \n", "dividend | \n", "Symbol | \n", "
|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "2015-01-02 | \n", "27.847500 | \n", "27.860001 | \n", "26.837500 | \n", "27.332500 | \n", "212818400 | \n", "0.0 | \n", "0.0 | \n", "AAPL | \n", "
| 1 | \n", "2015-01-05 | \n", "27.072500 | \n", "27.162500 | \n", "26.352501 | \n", "26.562500 | \n", "257142000 | \n", "0.0 | \n", "0.0 | \n", "AAPL | \n", "
| 2 | \n", "2015-01-06 | \n", "26.635000 | \n", "26.857500 | \n", "26.157499 | \n", "26.565001 | \n", "263188400 | \n", "0.0 | \n", "0.0 | \n", "AAPL | \n", "
| 3 | \n", "2015-01-07 | \n", "26.799999 | \n", "27.049999 | \n", "26.674999 | \n", "26.937500 | \n", "160423600 | \n", "0.0 | \n", "0.0 | \n", "AAPL | \n", "
| 4 | \n", "2015-01-08 | \n", "27.307501 | \n", "28.037500 | \n", "27.174999 | \n", "27.972500 | \n", "237458000 | \n", "0.0 | \n", "0.0 | \n", "AAPL | \n", "