-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathbt_vix_term_structure_strategy.py
executable file
·174 lines (144 loc) · 5 KB
/
bt_vix_term_structure_strategy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env -S uv run --quiet --script
# /// script
# dependencies = [
# "pandas",
# "yfinance",
# "plotly",
# "persistent-cache@git+https://github.com/namuan/persistent-cache"
# ]
# ///
"""
VIX Term Structure Strategy
Usage:
./bt_vix_term_structure_strategy.py -h
./bt_vix_term_structure_strategy.py -v # To log INFO messages
./bt_vix_term_structure_strategy.py -vv # To log DEBUG messages
"""
import logging
from argparse import ArgumentParser, RawDescriptionHelpFormatter
from datetime import datetime, timedelta
from typing import Dict, List
import pandas as pd
from common.backtest_framework import (
Backtest,
PerformanceAnalyzer,
ResultsVisualizer,
TradingStrategy,
)
from common.logger import setup_logging
from common.market_data import download_ticker_data
class VIXTermStructureStrategy(TradingStrategy):
# Strategy-specific symbols
SYMBOL_SPY = "SPY"
SYMBOL_VIX_9D = "^VIX9D"
SYMBOL_VIX_3M = "^VIX3M"
# Column names in the prepared DataFrame
COL_SPY = "SPY"
COL_VIX_9D = "VIX9D"
COL_VIX_3M = "VIX3M"
COL_IVTS = "IVTS"
def __init__(self, initial_capital: float, window1: int = 5):
super().__init__(initial_capital)
self.window1 = window1
def get_required_tickers(self) -> List[str]:
return [self.SYMBOL_SPY, self.SYMBOL_VIX_9D, self.SYMBOL_VIX_3M]
def prepare_backtest_data(self, data: Dict[str, pd.DataFrame]) -> pd.DataFrame:
# Align all data on the same index
common_idx = (
data[self.SYMBOL_SPY]
.index.intersection(data[self.SYMBOL_VIX_9D].index)
.intersection(data[self.SYMBOL_VIX_3M].index)
)
df = pd.DataFrame(
{
self.COL_SPY: data[self.SYMBOL_SPY].loc[common_idx]["Close"],
self.COL_VIX_9D: data[self.SYMBOL_VIX_9D].loc[common_idx]["Close"],
self.COL_VIX_3M: data[self.SYMBOL_VIX_3M].loc[common_idx]["Close"],
}
)
# Calculate IVTS
df[self.COL_IVTS] = df[self.COL_VIX_9D] / df[self.COL_VIX_3M]
# Calculate median
df[f"IVTS_Med{self.window1}"] = (
df[self.COL_IVTS].rolling(window=self.window1).median()
)
return df
def generate_buy_signals(self, data: pd.DataFrame) -> pd.Series:
# Convert -1 to 0 for sell signal, keep 1 for buy signal
signals = ((data[f"IVTS_Med{self.window1}"] < 1).astype(int) * 2 - 1) == 1
return signals
def generate_sell_signals(self, data: pd.DataFrame) -> pd.Series:
# Convert -1 to 1 for sell signal, 1 to 0 for buy signal
signals = ((data[f"IVTS_Med{self.window1}"] < 1).astype(int) * 2 - 1) == -1
return signals
def get_plot_config(self) -> Dict:
return {
"subplots": [
{"name": "VIX Term Structure", "height": 0.3},
{"name": "Position", "height": 0.3},
],
"traces": [
{
"row": 2,
"traces": [
{"name": "VIX9D", "column": self.COL_VIX_9D},
{"name": "VIX3M", "column": self.COL_VIX_3M},
],
},
],
}
def get_benchmark_column(self) -> str:
return self.COL_SPY
def parse_args():
parser = ArgumentParser(
description=__doc__, formatter_class=RawDescriptionHelpFormatter
)
parser.add_argument(
"-v",
"--verbose",
action="count",
default=0,
dest="verbose",
help="Increase verbosity of logging output",
)
parser.add_argument(
"--start-date",
type=str,
default=(datetime.now() - timedelta(days=365)).strftime("%Y-%m-%d"),
help="Start date for backtest (YYYY-MM-DD)",
)
parser.add_argument(
"--end-date",
type=str,
default=datetime.now().strftime("%Y-%m-%d"),
help="End date for backtest (YYYY-MM-DD)",
)
parser.add_argument(
"--initial-capital",
type=float,
default=100000.0,
help="Initial capital for backtest",
)
return parser.parse_args()
def main(args):
strategy = VIXTermStructureStrategy(args.initial_capital, window1=5)
# Download data for all required tickers
data = {}
for ticker in strategy.get_required_tickers():
data[ticker] = download_ticker_data(ticker, args.start_date, args.end_date)
# Prepare data for backtest
prepared_data = strategy.prepare_backtest_data(data)
# Run backtest
backtest = Backtest(strategy, prepared_data)
results, trades = backtest.run()
# Analyze performance
analyzer = PerformanceAnalyzer(results, args.initial_capital, strategy)
metrics = analyzer.calculate_metrics()
# Visualize results
visualizer = ResultsVisualizer(results, metrics, trades)
visualizer.create_visualization(strategy)
logging.info("Backtest completed successfully")
if __name__ == "__main__":
args = parse_args()
setup_logging(args.verbose)
main(args)