-
Notifications
You must be signed in to change notification settings - Fork 37
/
bt_spy_pct_change_strategy.py
executable file
·168 lines (143 loc) · 4.88 KB
/
bt_spy_pct_change_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
#!/usr/bin/env -S uv run --quiet --script
# /// script
# dependencies = [
# "backtrader[plotting]",
# ]
# ///
"""
Pct Change Strategy
Usage:
To test over a range and find the best parameters:
$ py bt_spy_pct_change_strategy.py
"""
import argparse
import os
import subprocess
from pathlib import Path
import backtrader as bt
def parse_arguments():
parser = argparse.ArgumentParser(description="Backtest using RSI strategy")
parser.add_argument(
"-s",
"--symbol",
type=str,
default="AAPL",
help="Stock symbol (default: AAPL)",
)
parser.add_argument(
"-t",
"--test",
action="store_true",
help="Run in test mode",
)
parser.add_argument(
"-i",
"--initial_investment",
type=float,
default=10000.0,
help="Initial investment amount (default: 10000.0)",
)
parser.add_argument(
"-l",
"--look-back-period-in-years",
type=str,
default="10",
help="Look back period in years (default: 10)",
)
return parser.parse_args()
class PctChangeStrategy(bt.Strategy):
params = dict(
initial_investment=10000.0,
pct_change_threshold=0.01,
pct_change_period=2,
print_log=False,
)
def __init__(self):
self.order = None
self.data_close = self.datas[0].close
self.pct_change = bt.indicators.PercentChange(
self.data_close, period=self.params.pct_change_period
)
self.days_pct_change_above_threshold = 0
self.days_pct_change_below_threshold = 0
def next(self):
change = self.pct_change[0]
if abs(change) > self.params.pct_change_threshold:
self.log(f"❌ > 1% Change: {change:.2%}", do_print=True)
self.days_pct_change_above_threshold += 1
# self.order = self.buy()
else:
self.log(f"✅ < 1% Change: {change:.2%}")
self.days_pct_change_below_threshold += 1
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
return
if order.status in [order.Completed]:
if order.isbuy():
self.log(
f"BUY Executed, Price: {order.executed.price}, Cost: {order.executed.value}, Comm: {order.executed.comm:.2f}"
)
elif order.issell():
self.log(
f"SELL Executed, Price: {order.executed.price}, Cost: {order.executed.value}, Comm: {order.executed.comm:.2f}"
)
elif order.status in [order.Canceled, order.Margin, order.Rejected]:
self.log(
f"⚠️ Order Canceled/Margin/Rejected - {order.status}", do_print=True
)
def notify_trade(self, trade):
if not trade.isclosed:
return
self.log(f"OPERATION PROFIT, GROSS: {trade.pnl:.2f}, NET: {trade.pnlcomm:.2f}")
def stop(self):
approx_gain = (
self.days_pct_change_below_threshold * 100
- self.days_pct_change_above_threshold * 100
)
self.log(
f" ⚫ Ending Value {self.broker.getvalue():.2f}"
f" ⚫ Days Above Threshold {self.days_pct_change_above_threshold}"
f" ⚫ Days Below Threshold {self.days_pct_change_below_threshold}"
f" ⚫ Approximate Gain {approx_gain}",
do_print=True,
)
def log(self, txt, dt=None, do_print=False):
if self.params.print_log or do_print:
dt = dt or self.datas[0].datetime.date(0)
print(f"{dt.isoformat()}, {txt}", flush=True)
def main(args):
cerebro = bt.Cerebro()
initial_investment = args.initial_investment
cerebro.addstrategy(
PctChangeStrategy,
initial_investment=initial_investment,
pct_change_threshold=0.03,
pct_change_period=7,
)
data = load_data(args.symbol, args.look_back_period_in_years)
cerebro.adddata(data)
cerebro.broker.setcash(initial_investment)
cerebro.broker.setcommission(commission=0.001)
print("Starting Portfolio Value: %.2f" % cerebro.broker.getvalue())
cerebro.run()
print("Final Portfolio Value: %.2f" % cerebro.broker.getvalue())
def load_data(symbol: str, look_back_period_in_years: str = "10"):
data_path = Path.cwd().joinpath("output").joinpath(f"{symbol}.csv")
if not os.path.isfile(data_path):
command = [
"python3",
"download_stocks_ohlcv.py",
"-t",
symbol,
"--back-period-in-years",
look_back_period_in_years,
]
print(f"Running command: {0}".format("".join(command)))
subprocess.run(command)
else:
print(f"Found existing data file: {data_path}")
data = bt.feeds.YahooFinanceCSVData(dataname=data_path)
return data
if __name__ == "__main__":
args = parse_arguments()
main(args)