-
Notifications
You must be signed in to change notification settings - Fork 2
/
input_run_bt.py
117 lines (94 loc) · 2.91 KB
/
input_run_bt.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 12 21:52:32 2019
@author: jacobsolawetz
"""
import warnings
#toggle pandas future warnings off
warnings.filterwarnings('ignore')
from backtester import Backtester
from visualizer import Visualizer
'''DATA STRUCTURES'''
valid_strings = {
'option_type': ['P', 'C'],
'right': ['SELL', 'BUY'],
'your decision': ['Y', 'N'],
'your backtest type': ['normal', 'constant_margin']
}
inputs = []
'''FUNCTIONS'''
def get_str_input(parameter, name):
while parameter == None:
parameter = input('What is the ' + name + '?\n')
if parameter not in valid_strings[name]:
print('That is invalid. Your options are ', valid_strings[name])
parameter = None
inputs.append(parameter)
return parameter
def get_int_input(parameter, name):
while parameter == None:
parameter = input('What is the ' + name + '?\n')
try:
parameter = float(parameter)
except:
print('That was not a number. Please enter a number.')
parameter = None
inputs.append(parameter)
return parameter
def print_portfolio(p):
print('\nBacktesting a portfolio of {} option(s)'.format(len(p)))
option_num = 1
for o in p:
print('Option', option_num)
print('\tOption: {}\n\tZ-Scores Out: {}\n\tLong or Short: {}'.format(*o))
option_num += 1
print()
#backtest_name = '2_scores_constantmargin_8x' DELETE THIS LATER
done = False
'''PORTFOLIO VARIABLES'''
strategy = []
option_type = None
zscore = None
right = None
decision = None
leverage = None
backtest_type = None
print('Input the first option for your portfolio.\n')
while not done:
option_type = get_str_input(option_type, 'option_type')
zscore = get_int_input(zscore, 'zscore')
right = get_str_input(right, 'right')
strategy.append([option_type, zscore, right])
print('\nYou can still add more options.\n')
if get_str_input(decision, 'your decision') == 'Y':
option_type = None
zscore = None
right = None
else:
done = True
backtest_type = get_str_input(backtest_type, 'your backtest type')
inputs.append(backtest_type)
leverage = get_int_input(leverage, 'leverage')
inputs.append(leverage)
print_portfolio(strategy)
#strategy = [['P', 2.0, 'SELL']]
#backtest_type = 'normal'
#backtest_type = 'constant_margin'
roll_day = 10
#leverage = 8
backtest_name = '{}_scores_{}_{}'.format(zscore, backtest_type, leverage)
tester = Backtester(roll_day, strategy, leverage, backtest_type)
tester.load_data()
tester.set_up_calendar()
tester.get_roll_days()
tester.backtest()
viz = Visualizer(tester.results,backtest_name)
viz.save_figs()
viz.print_results()
f = open('repeat', 'w')
for _input in inputs:
f.write(str(_input))
f.write('\n')
f.close()
print('`python input_run_bt.py < repeat` will repeat this backtest')