-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplotter.py
120 lines (83 loc) · 3.48 KB
/
plotter.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
import sp
import wi
import subprocess
import sys
import os
def png_setup(dict_arg, symbol, month, chart_var):
r_price = "price <- c("
r_calls = "calls <- c("
r_puts = "puts <- c("
sorted_keys = []
for price in dict_arg.keys():
sorted_keys.append(price)
sorted_keys.sort()
for key in sorted_keys:
r_price += "{0},".format(round(key,
sp.PRODUCT_SYMBOLS[symbol]["sig_figs"]))
r_calls += "{0},".format(dict_arg[key]["CALL"])
r_puts += "{0},".format(dict_arg[key]["PUT"])
r_script = ""
for string in [r_price, r_calls, r_puts]:
r_script += string[:-1]
r_script += ")\n"
img_name = "{0}_{1}_{2}.png".format(symbol, month, chart_var)
return (r_script, img_name)
def write_png(r_script, img_name):
script_dir = os.path.dirname(os.path.abspath(__file__))
with open("{0}".format(os.path.join(script_dir, "temp.R")), "w") as f:
f.write(r_script)
subprocess.call("R -f {0}".format(os.path.join(script_dir, "temp.R")),
shell=True)
dest_dir = os.path.join(script_dir, "img")
if not os.path.exists(dest_dir):
os.mkdir(dest_dir)
subprocess.call("mv {0} ./img".format(img_name), shell=True)
def option_greek_png(skewed_months, symbol, month, greek):
total_greek_dict = {}
for key in skewed_months:
total_greek = wi.calc_total_greek(skewed_months[key], greek)
total_greek_dict[key] = total_greek
(args, img_name) = png_setup(total_greek_dict, symbol, month, greek)
with open("greek_template.R", "r") as f:
template = f.read()
r_script = template.format(symbol=symbol,
args=args,
month=month,
greek=greek,
commodity=sp.PRODUCT_SYMBOLS[symbol]["name"])
write_png(r_script, img_name)
return img_name
def stacked_options_png(settlements, symbol, month, name):
if name == "stack":
itm_options = wi.get_itm_ladder(settlements, symbol, month)
desc = "in the Money Options"
elif name == "pain":
itm_options = wi.get_pain_ladder(settlements, symbol, month)
desc = "Pain Value"
(args, img_name) = png_setup(itm_options, symbol, month, name)
with open("stack_template.R", "r") as f:
template = f.read()
r_script = template.format(symbol=symbol,
graph=name,
desc=desc,
args=args,
month=month,
commodity=sp.PRODUCT_SYMBOLS[symbol]["name"])
write_png(r_script, img_name)
return img_name
def make_all(settlements, symbol, month):
imgs = []
imgs.append(stacked_options_png(settlements, symbol, month, "stack"))
imgs.append(stacked_options_png(settlements, symbol, month, "pain"))
skewed_months = wi.make_skewed_months(settlements, symbol, month)
imgs.append(option_greek_png(skewed_months, symbol, month, "delta"))
imgs.append(option_greek_png(skewed_months, symbol, month, "gamma"))
return imgs
def main(symbol, month):
settlements = sp.get_all_settlements(symbol)
skewed_months = wi.make_skewed_months(settlements, symbol, month)
make_all(settlements, symbol, month)
if __name__ == "__main__":
symbol = sys.argv[1]
month = sys.argv[2]
main(symbol, month)