forked from alpha-unito/pico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseq_stock_pricing.cpp
92 lines (77 loc) · 2.67 KB
/
seq_stock_pricing.cpp
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
/*
* Copyright (c) 2019 alpha group, CS department, University of Torino.
*
* This file is part of pico
* (see https://github.com/alpha-unito/pico).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// This code implements a pipeline for batch processing of stocks.
// It first computes a price for each option from a text file,
// then it extracts the maximum price for each stock name.
#include <algorithm>
#include <cassert>
#include <chrono>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
#include "black_scholes.hpp"
#include "defs.h"
int main(int argc, char** argv) {
/* parse command line */
if (argc < 3) {
std::cerr << "Usage: " << argv[0];
std::cerr << " <input-file> <output-file>\n";
return -1;
}
std::string in_fname = argv[1], out_fname = argv[2];
/* read options from the input file */
std::ifstream in_file(in_fname);
assert(in_file.is_open());
std::chrono::seconds wt(0);
std::unordered_map<std::string, StockAndPrice> red;
while (in_file.good()) {
std::string opt_line;
std::getline(in_file, opt_line, '\n');
/*
* map + reduce
*/
auto t0 = std::chrono::high_resolution_clock::now();
std::string name;
OptionData opt;
char otype;
std::stringstream ins(opt_line);
/* read stock name */
ins >> name;
/* read stock option data */
ins >> opt.s >> opt.strike >> opt.r >> opt.divq;
ins >> opt.v >> opt.t >> otype >> opt.divs >> opt.DGrefval;
opt.OptionType = (otype == 'P');
StockPrice res = black_scholes(opt);
if (red.find(name) != red.end())
red[name] = std::max(StockAndPrice(name, res), red[name]);
else
red[name] = StockAndPrice(name, res);
auto t1 = std::chrono::high_resolution_clock::now();
wt += std::chrono::duration_cast<std::chrono::seconds>(t0 - t1);
}
/* print results */
std::ofstream out_file(out_fname);
for (auto item : red) out_file << item.second.to_string() << std::endl;
std::cerr << "map = " << wt.count() << " s" << std::endl;
return 0;
}