-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
163 lines (129 loc) · 3.67 KB
/
Copy pathmain.py
File metadata and controls
163 lines (129 loc) · 3.67 KB
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
import quantstats as qs
import bt
import time
qs.extend_pandas()
#################################################################### Portfolio Allocations #####################################################################
stock_choice_1 = "spy"
stock_choice_2 = "agg"
stock_choice_3 = "qqq"
stock_choice_4 = "msft"
alloc1 = 50
alloc2 = 40
alloc3 = 7
alloc4 = 3
stock_dic = {
stock_choice_1: float(alloc1) / 100,
stock_choice_2: float(alloc2) / 100,
stock_choice_3: float(alloc3) / 100,
stock_choice_4: float(alloc4) / 100,
}
################################################################## BT STRATEGY ##################################################################
bt_start = time.time()
data = bt.get([stock_choice_1, stock_choice_2, stock_choice_3, stock_choice_4])
data = data.dropna()
# print(data)
strategy_ = bt.Strategy(
"Custom Strategy",
[
bt.algos.RunQuarterly(),
bt.algos.SelectAll(),
bt.algos.WeighSpecified(**stock_dic),
bt.algos.Rebalance(),
],
)
test = bt.Backtest(strategy_, data)
#This is backtesting object, full of multiple dataframes and dictionaries
results = bt.run(test)
#This is the series data
portfolio_bt = results._get_series(None).rebase()
bt_end = time.time()
print("BT Took: ", bt_end - bt_start)
# print(portfolio_bt)
################################################################## QUANTSTATS STRATEGY ##################################################################
"""
Here is quantstats doing the same thing
"""
qs_start = time.time()
#Quantstats grabs more data
portfolio_qs = qs.utils.make_index(stock_dic, rebalance="1Q")
qs_end = time.time()
print("QuantStats Took: ", qs_end-qs_start)
############################################################## USING QUANTSTATS TO SHOW STATS FOR BT #####################################################
#Quantstats likes the series in a percent change way
portfolio_bt["Custom Strategy"] = portfolio_bt["Custom Strategy"].pct_change()
From = '2018-01-01'
To = '2021-01-01'
portfolio_bt_splice = portfolio_bt.loc[From:To,:]
print(portfolio_bt_splice)
#Quantstats extends pandas (at the top) so qs can get all the stats straight from the series dataframe
print("Max Drawdown: ", portfolio_bt.max_drawdown())
print("CAGR: ", portfolio_bt.cagr())
print("Volatility: ", portfolio_bt.volatility())
print("Sharpe: ", portfolio_bt.sharpe())
print("Sortino: ", portfolio_bt.sortino())
print("Comp: ", portfolio_bt.comp())
print(portfolio_bt_splice.monthly_returns())
############################################################## ALL THE QUANTSTATS FUNCTIONS ##################################################################
# all the stats we can generate from quantstats. These work on any pandas dataframe
"""
['avg_loss',
'avg_return',
'avg_win',
'best',
'cagr',
'calmar',
'common_sense_ratio',
'comp',
'compare',
'compsum',
'conditional_value_at_risk',
'consecutive_losses',
'consecutive_wins',
'cpc_index',
'cvar',
'drawdown_details',
'expected_return',
'expected_shortfall',
'exposure',
'gain_to_pain_ratio',
'geometric_mean',
'ghpr',
'greeks',
'implied_volatility',
'information_ratio',
'kelly_criterion',
'kurtosis',
'max_drawdown',
'monthly_returns',
'outlier_loss_ratio',
'outlier_win_ratio',
'outliers',
'payoff_ratio',
'profit_factor',
'profit_ratio',
'r2',
'r_squared',
'rar',
'recovery_factor',
'remove_outliers',
'risk_of_ruin',
'risk_return_ratio',
'rolling_greeks',
'ror',
'sharpe',
'skew',
'sortino',
'adjusted_sortino',
'tail_ratio',
'to_drawdown_series',
'ulcer_index',
'ulcer_performance_index',
'upi',
'utils',
'value_at_risk',
'var',
'volatility',
'win_loss_ratio',
'win_rate',
'worst']
"""