-
Notifications
You must be signed in to change notification settings - Fork 2
/
visualizer.py
136 lines (101 loc) · 4.33 KB
/
visualizer.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 12 15:35:11 2019
@author: jacobsolawetz
"""
from matplotlib.backends.backend_pdf import PdfPages
import os
class Visualizer:
def __init__(self,results, backtest_name):
self.results = results
self.backtest_name = backtest_name
def save_figs(self):
pp = PdfPages("output/" + self.backtest_name + '.pdf')
viz_df = self.results
ax = viz_df[['daily_returns_cumulative']].plot()
ax.set_title('daily_returns_cumulative')
ax.set_ylabel('x-return')
fig = ax.get_figure()
pp.savefig(fig)
fig.clear()
ax = viz_df.groupby('roll_period')['roll_cumulative_return_raw'].last().hist(bins=40)
ax.set_title('histogram of roll returns')
fig = ax.get_figure()
pp.savefig(fig)
fig.clear()
viz_df = viz_df.reset_index()
yearly_returns = (viz_df.groupby(viz_df['date'].dt.year)['daily_returns_cumulative'].last() / viz_df.groupby(viz_df['date'].dt.year)['daily_returns_cumulative'].last().shift(1).fillna(1)) - 1
ax = yearly_returns.plot.bar()
ax.set_title('yearly returns')
ax.set_ylabel('pct return')
fig = ax.get_figure()
pp.savefig(fig)
fig.clear()
viz_df = viz_df.set_index('date')
ax = viz_df['margin_pct'].round(2).plot()
ax.set_title('margin_visualization')
ax.set_ylabel('percent_margin_call')
fig = ax.get_figure()
pp.savefig(fig)
fig.clear()
ax = viz_df.groupby('roll_period')['portfolio_delta'].first().plot()
ax.set_title('roll_deltas')
ax.set_ylabel('delta')
fig = ax.get_figure()
pp.savefig(fig)
fig.clear()
ax = viz_df['portfolio_delta'].hist(bins=40)
ax.set_title('portfolio_delta histogram')
ax.set_ylabel('delta')
fig = ax.get_figure()
pp.savefig(fig)
fig.clear()
pp.close()
return None
def print_results(self):
print_df = self.results
text_file = open("output/" + self.backtest_name + ".txt", "w")
max_roll_drawdown = print_df.groupby('roll_period')['roll_cumulative_return_raw'].last().min()
max_intra_roll_drawdown = print_df['roll_cumulative_return_raw'].min()
max_daily_drawdown = print_df['portfolio_returns_raw'].min()
text_file.write("max roll drawdown :" + str(max_roll_drawdown) + "\n\n")
text_file.write("max intra roll drawdown :" + str(max_intra_roll_drawdown) + "\n\n")
text_file.write("max daily drawdown :" + str(max_daily_drawdown) + "\n\n")
text_file.close()
print_df.to_csv("output/" + self.backtest_name + ".csv")
return None
'''
df['roll_period'] = df['roll_date'].shift(1)
#returns for each roll
#we can use a similar method to get roll deltas, ect.
df.groupby('roll_period')['roll_cumulative_return_raw'].last().hist(bins=40)
#max roll drawdown
df.groupby('roll_period')['roll_cumulative_return_raw'].last().min()
#mean roll return
df.groupby('roll_period')['roll_cumulative_return_raw'].last().mean()
#max intra roll drawdown
df['roll_cumulative_return_raw'].min()
#max daily drawdown
df['portfolio_returns_raw'].min()
#mean daily return
df['portfolio_returns_raw'].mean()
df = df.reset_index()
yearly_returns = (df.groupby(df['date'].dt.year)['daily_returns_cumulative'].last() / df.groupby(df['date'].dt.year)['daily_returns_cumulative'].last().shift(1).fillna(1)) - 1
yearly_returns.plot.bar()
#yearly return
yearly_return = math.pow(df['daily_returns_cumulative'].iloc[-1], 1/((df['date'].iloc[-1] - df['date'].iloc[0]).days / 365)) - 1
#yearly std
yearly_std = df.groupby('roll_period')['roll_cumulative_return_raw'].last().std()*math.sqrt(12)
#sharpe ratio
(yearly_return - .031) / yearly_std
df['portfolio_returns_raw'].hist(bins = 300)
df['portfolio_delta'].hist(bins=40)
df.set_index('date')['portfolio_delta'].plot()
df['portfolio_theta'].hist()
#roll deltas
df.groupby('roll_period')['portfolio_delta'].first().hist(bins=40)
#we could imagine where we set a strike based on delta not Z-score
df.groupby('roll_period')['portfolio_delta'].first().plot()
df.groupby('roll_period')['portfolio_theta'].first().hist(bins=40)
'''