-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
168 lines (155 loc) · 5.89 KB
/
main.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
# imports
import sys
from PyQt5 import uic, QtWidgets
from PyQt5.QtWidgets import QWidget, QMessageBox
from comp7405.basic import *
from comp7405.asian import *
from comp7405.basket import *
from comp7405.binomial import *
# load ui file
baseUIClass, baseUIWidget = uic.loadUiType('Qt/minipricer.ui')
# use loaded ui file in the logic class
class Logic(baseUIWidget, baseUIClass):
def __init__(self, parent=None):
super(Logic, self).__init__(parent)
self.setupUi(self)
self.btn_cal_price_1.clicked.connect(self.cal_basic_price)
self.btn_cal_iv_1.clicked.connect(self.cal_basic_imp_vol)
self.btn_cal_geom_2.clicked.connect(self.cal_asian_geom)
self.btn_cal_arith_2.clicked.connect(self.cal_asian_arith)
self.btn_cal_geom_3.clicked.connect(self.cal_basket_geom)
self.btn_cal_arith_3.clicked.connect(self.cal_basket_arith)
self.btn_cal_price_4.clicked.connect(self.cal_binomial_price)
def exception_hook(self, exctype, value, traceback):
# Prompt the error
QMessageBox.warning(self, 'Warning', str(value))
def cal_basic_price(self):
S0 = self.dsb_S0_1.value()
K = self.dsb_K_1.value()
T = self.dsb_T_1.value()
r = self.dsb_r_1.value() / 100.0
q = self.dsb_q_1.value() / 100.0
sigma = self.dsb_sigma_1.value() / 100.0
C = black_scholes(S0, K, T, sigma, r, q, option_type='C')
P = black_scholes(S0, K, T, sigma, r, q, option_type='P')
self.lb_C_1.setText('%.3f' % C)
self.lb_P_1.setText('%.3f' % P)
def cal_basic_imp_vol(self):
S0 = self.dsb_S0_1.value()
K = self.dsb_K_1.value()
T = self.dsb_T_1.value()
r = self.dsb_r_1.value() / 100.0
q = self.dsb_q_1.value() / 100.0
if self.rb_call_1.isChecked():
option_type = 'C'
else:
option_type = 'P'
V = self.dsb_V_1.value()
sigma = imp_vol(V, S0, K, T, r, q, option_type)
self.lb_iv_1.setText('%.3f' % (sigma * 100.0))
def cal_asian_geom(self):
S0 = self.dsb_S0_2.value()
n = self.sb_n_2.value()
K = self.dsb_K_2.value()
T = self.dsb_T_2.value()
r = self.dsb_r_2.value() / 100.0
sigma = self.dsb_sigma_2.value() / 100.0
if self.rb_call_2.isChecked():
option_type = 'C'
else:
option_type = 'P'
m = self.sb_m_2.value()
if self.rb_closed_2.isChecked():
V = geom_asian_exact(S0, K, T, sigma, r, n, option_type)
std = 0
else:
V, low, up = monte_carlo_asian(S0, K, T, sigma, r, n, option_type, sim_type='G', m=m)
std = up - low
self.lb_Vgeom_2.setText('%.3f' % V)
self.lb_Vgeom_std_2.setText('%.3f' % std)
def cal_asian_arith(self):
S0 = self.dsb_S0_2.value()
n = self.sb_n_2.value()
K = self.dsb_K_2.value()
T = self.dsb_T_2.value()
r = self.dsb_r_2.value() / 100.0
sigma = self.dsb_sigma_2.value() / 100.0
if self.rb_call_2.isChecked():
option_type = 'C'
else:
option_type = 'P'
m = self.sb_m_2.value()
if self.rb_yes_2.isChecked():
sim_type = 'C'
else:
sim_type = 'A'
V, low, up = monte_carlo_asian(S0, K, T, sigma, r, n, option_type, sim_type, m)
std = up - low
self.lb_Varith_2.setText('%.3f' % V)
self.lb_Varith_std_2.setText('%.3f' % std)
def cal_basket_geom(self):
S1 = self.dsb_S1_3.value()
S2 = self.dsb_S2_3.value()
K = self.dsb_K_3.value()
T = self.dsb_T_3.value()
r = self.dsb_r_3.value() / 100.0
sigma1 = self.dsb_sigma1_3.value() / 100.0
sigma2 = self.dsb_sigma2_3.value() / 100.0
rho = self.dsb_rho_3.value()
if self.rb_call_3.isChecked():
option_type = 'C'
else:
option_type = 'P'
m = self.sb_m_3.value()
if self.rb_closed_3.isChecked():
V = geom_basket_exact(S1, S2, K, T, sigma1, sigma2, r, rho, option_type)
std = 0
else:
V, low, up = monte_carlo_basket(S1, S2, K, T, sigma1, sigma2, r, rho, option_type,
sim_type='G', m=m)
std = up - low
self.lb_Vgeom_3.setText('%.3f' % V)
self.lb_Vgeom_std_3.setText('%.3f' % std)
def cal_basket_arith(self):
S1 = self.dsb_S1_3.value()
S2 = self.dsb_S2_3.value()
K = self.dsb_K_3.value()
T = self.dsb_T_3.value()
r = self.dsb_r_3.value() / 100.0
sigma1 = self.dsb_sigma1_3.value() / 100.0
sigma2 = self.dsb_sigma2_3.value() / 100.0
rho = self.dsb_rho_3.value()
if self.rb_call_3.isChecked():
option_type = 'C'
else:
option_type = 'P'
m = self.sb_m_3.value()
if self.rb_yes_3.isChecked():
sim_type = 'C'
else:
sim_type = 'A'
V, low, up = monte_carlo_basket(S1, S2, K, T, sigma1, sigma2, r, rho, option_type,
sim_type, m)
std = up - low
self.lb_Varith_3.setText('%.3f' % V)
self.lb_Varith_std_3.setText('%.3f' % std)
def cal_binomial_price(self):
S0 = self.dsb_S0_4.value()
K = self.dsb_K_4.value()
T = self.dsb_T_4.value()
r = self.dsb_r_4.value() / 100.0
sigma = self.dsb_sigma_4.value() / 100.0
if self.rb_call_4.isChecked():
option_type = 'C'
else:
option_type = 'P'
N = self.sb_N_4.value()
V = binomial_tree(S0, K, T, sigma, r, option_type, N)
self.lb_V_4.setText('%.3f' % V)
app = QtWidgets.QApplication(sys.argv)
#app.setStyle(QtWidgets.QStyleFactory.create('Fusion'))
ui = Logic(None)
ui.show()
# Set the exception hook to our wrapping function
sys.excepthook = ui.exception_hook
sys.exit(app.exec_())