forked from alpha-unito/pico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstock_pricing.cpp
76 lines (66 loc) · 2.51 KB
/
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
/*
* 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 <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include "pico/pico.hpp"
#include "black_scholes.hpp"
#include "common.hpp"
#include "defs.h"
int main(int argc, char** argv) {
// parse command line
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <input file> <output file> \n";
return -1;
}
std::string in_fname(argv[1]), out_fname(argv[2]);
/*
* define a batch pipeline that:
* 1. read options from file
* 2. computes prices by means of the blackScholes pipeline
* 3. extracts the maximum price for each stock name
* 4. write prices to file
*/
pico::Map<std::string, StockAndPrice> blackScholes([](const std::string& in) {
OptionData opt;
char otype, name[128];
parse_opt(opt, otype, name, in);
opt.OptionType = (otype == 'P');
return StockAndPrice(std::string(name), black_scholes(opt));
});
auto stockPricing = pico::Pipe() //
.add(pico::ReadFromFile(in_fname)) //
.add(blackScholes) //
.add(SPReducer()) //
.add(pico::WriteToDisk<StockAndPrice>(out_fname));
/* print the semantic graph and generate dot file */
stockPricing.print_semantics();
stockPricing.to_dotfile("stock_pricing.dot");
/* execute the pipeline */
stockPricing.run();
/* print execution time */
std::cout << "done in " << stockPricing.pipe_time() << " ms\n";
return 0;
}