This repository has been archived by the owner on May 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate.py
134 lines (116 loc) · 4.31 KB
/
calculate.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
# -*- coding: utf-8 -*-
"""
Calculate options' prices.
Example command for calculating europian option:
python calculate.py -t europian
Example command for calculating asian option:
python calculate.py -t asian
"""
import os
import time
import ConfigParser
from market import (
MarketData,
EuropianOption,
AsianOption
)
from fdms.core import Nodes
from fdms.explicit_fdms import (
EuropianOptionExplicitFDM,
AsianOptionExplicitFDM
)
from fdms.implicit_fdms import EuropianOptionImplicitFDM
from argument_parser import OptionsSolverArgumentParser
if __name__ == "__main__":
config = ConfigParser.RawConfigParser()
config.read('config.cfg')
results_path = config.get('other', 'results_path')
parser = OptionsSolverArgumentParser.get_parser()
args = parser.parse_args()
market_data = MarketData(
interest=config.getfloat(args.type, 'interest_rate'),
volatility=config.getfloat(args.type, 'volatility')
)
if args.type == 'europian':
method_type = config.get(args.type, 'method_type')
strike = config.getfloat(args.type, 'strike_price')
maturity = config.getfloat(args.type, 'maturity')
time_steps_number = config.getint(args.type, 'time_steps_number')
asset_price_steps_number = config.getint(
args.type, 'asset_price_steps_number'
)
asset_price_min = config.getfloat(args.type, 'asset_price_min')
asset_price_max = config.getfloat(args.type, 'asset_price_max')
if method_type == 'explicit':
fdm_class = EuropianOptionExplicitFDM
elif method_type == 'implicit':
fdm_class = EuropianOptionImplicitFDM
start_time = time.time()
option = EuropianOption(
strike=strike, maturity=maturity
)
nodes = Nodes([
([0.0, maturity], time_steps_number, 'time'),
([asset_price_min, asset_price_max],
asset_price_steps_number, 'asset_price')
])
fdm = fdm_class(
option, market_data, nodes
)
prices = fdm.calculate_prices()
end_time = time.time()
fdm.export_to_file(
os.path.join(
results_path,
"europian %d %d.data.xlsx" %
(time_steps_number, asset_price_steps_number),
),
points_number=100
)
fdm.compare_with_analytical()
print("Executing time %f" % (end_time - start_time))
elif args.type == 'asian':
method_type = config.get(args.type, 'method_type')
strike = config.getfloat(args.type, 'strike_price')
maturity = config.getfloat(args.type, 'maturity')
time_steps_number = config.getint(args.type, 'time_steps_number')
asset_price_steps_number = config.getint(
args.type, 'asset_price_steps_number'
)
average_price_steps_number = config.getint(
args.type, 'average_price_steps_number'
)
asset_price_min = config.getfloat(args.type, 'asset_price_min')
asset_price_max = config.getfloat(args.type, 'asset_price_max')
average_price_min = config.getfloat(args.type, 'average_price_min')
average_price_max = config.getfloat(args.type, 'average_price_max')
start_time = time.time()
option = AsianOption(
strike=strike, maturity=maturity
)
nodes = Nodes([
([0.0, maturity], time_steps_number, 'time'),
([asset_price_min, asset_price_max],
asset_price_steps_number, 'asset_price'),
([average_price_min, average_price_max],
average_price_steps_number, 'average_price')
])
fdm = AsianOptionExplicitFDM(
option, market_data, nodes
)
prices = fdm.calculate_prices()
end_time = time.time()
fdm.plot_option_prices(asset_price_sparse=35, average_price_sparse=20)
fdm.export_to_file(
os.path.join(
results_path,
"asian_data %d %d %d.xlsx" %
(time_steps_number, asset_price_steps_number,
average_price_steps_number)
)
)
print("Executing time %f" % (end_time - start_time))
else:
raise ValueError(
"Only europian and asian options are supported at this moment"
)