Skip to content

Commit 0061d95

Browse files
committed
Outline added for framework
1 parent 0210cf0 commit 0061d95

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

notebooks/OptionsBacktesting.ipynb

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Options Backtesting Framework\n",
8+
"\n",
9+
"This is a simple backtesting framework for options. Given end of day data for both the capital and derivatives markets, we would be backtesting for different strategies.\n",
10+
"\n",
11+
"> All options are opened at the start of the period and closed by the end of the period or by a stop loss if provided\n",
12+
"\n",
13+
"### Implementation\n",
14+
"\n",
15+
"1. Load and transform necessary data\n",
16+
"2. Create a list of option contracts to be entered into each day\n",
17+
"3. Create a list of signals on, whether to enter into the contract, for each day\n",
18+
"4. Extract the necessary data for the options contracts that would be executed\n",
19+
"5. Analyze Profit and loss and other metrices\n"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": 9,
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [
28+
"# Load necessary libraries\n",
29+
"\n",
30+
"import pandas as pd\n",
31+
"import numpy as np\n",
32+
"from fastbt.rapid import backtest"
33+
]
34+
},
35+
{
36+
"cell_type": "markdown",
37+
"metadata": {},
38+
"source": [
39+
"## Utility Functions"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 24,
45+
"metadata": {},
46+
"outputs": [
47+
{
48+
"data": {
49+
"text/plain": [
50+
"10225"
51+
]
52+
},
53+
"execution_count": 24,
54+
"metadata": {},
55+
"output_type": "execute_result"
56+
}
57+
],
58+
"source": [
59+
"def itm(price, opt='C', step=100):\n",
60+
" \"\"\"\n",
61+
" Get the strike price of the in the money option\n",
62+
" price\n",
63+
" spot price\n",
64+
" opt\n",
65+
" option type - C for Call and P for Put\n",
66+
" step\n",
67+
" multiple of option strike price\n",
68+
" \"\"\"\n",
69+
" if opt == 'C':\n",
70+
" return int(price/step)*step\n",
71+
" elif opt == 'P':\n",
72+
" return (int(price/step)+1)*step"
73+
]
74+
},
75+
{
76+
"cell_type": "markdown",
77+
"metadata": {},
78+
"source": [
79+
"Data Preparation\n",
80+
"-----------------\n",
81+
"Load the raw data and do some transformations. All raw options data is loaded from the bhav copy of the NSE website. Index data is from [niftyindices.com](https://www.niftyindices.com/)."
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 10,
87+
"metadata": {},
88+
"outputs": [],
89+
"source": [
90+
"nifty = pd.read_csv('/home/machine/Downloads/NIFTY 50_Data.csv',\n",
91+
" parse_dates=['Date'])"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": null,
97+
"metadata": {},
98+
"outputs": [],
99+
"source": []
100+
}
101+
],
102+
"metadata": {
103+
"kernelspec": {
104+
"display_name": "Python 3",
105+
"language": "python",
106+
"name": "python3"
107+
},
108+
"language_info": {
109+
"codemirror_mode": {
110+
"name": "ipython",
111+
"version": 3
112+
},
113+
"file_extension": ".py",
114+
"mimetype": "text/x-python",
115+
"name": "python",
116+
"nbconvert_exporter": "python",
117+
"pygments_lexer": "ipython3",
118+
"version": "3.6.8"
119+
}
120+
},
121+
"nbformat": 4,
122+
"nbformat_minor": 2
123+
}

0 commit comments

Comments
 (0)