-
Notifications
You must be signed in to change notification settings - Fork 0
/
Output.cpp
103 lines (88 loc) · 3.76 KB
/
Output.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
93
94
95
96
97
98
99
100
101
102
103
/**********************************************************************************************************************
* Black-Scholes Option pricing application - Output class
*
* Created by Michael Lewis on 8/9/20.
*********************************************************************************************************************/
#include <chrono>
#include <fstream>
#include <ios>
#include <iostream>
#include <iomanip>
#include <sstream>
#include "Output.hpp"
/**
* Initialize a new Output object
* @throws OutOfMemoryError Indicates insufficient memory for this new Output
*/
Output::Output() {}
/**
* Initialize a new Output object
* @param source An output object whose members will be used to initialize this new Output
* @throws OutOfMemoryError Indicates insufficent memory for this new Output
*/
Output::Output(const Output &source) {}
/**
* Destroy this Output
*/
Output::~Output() {}
/**
* Deeply copy the source
* @param source An Output object whose members will be deeply copied into this Output object
* @return This Output object whose members are now a deep copy of the source members
*/
Output & Output::operator=(const Output &source) {
// Avoid self assign
if (this == &source) {return *this;}
return *this;
}
/**
* Send option data to American_Option_Data.csv
* @param meshPoints A vector of mesh points where each point is a monotonically increased option parameter
* @param matrix A prices of Call and Put prices
*/
void Output::csv(const std::vector<double>& meshPoints, const std::vector<std::vector<double> > &prices) {
std::ofstream outFile; // Object for writing to a file
// Current time used to create unique file names
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
std::stringstream ss;
ss << std::put_time(&tm, "%m-%d-%Y %H-%M-%S");
outFile.open("American_Option_Data " + ss.str() + ".csv");
if (outFile.is_open()) {
outFile << "Mesh Points" << "," << "Call Price" << "," << "Put Price" << std::endl;
for (int i = 0; i < prices.size(); ++i) {
outFile << meshPoints[i] << "," << prices[i][0] << "," << prices[i][1] << std::endl;
}
outFile.close();
} else {
std::cout << "Unable to open the file. Check filepath permissions\n";
}
}
/**
* Send option data to European_Option_Data.csv
* @param meshPoints A vector of mesh points where each point is a monotonically increased option parameter
* @param prices A matrix of Call and Put prices
* @param deltas A matrix of Call and Put Deltas
* @param gammas A matrix of gammas. Note that there is no distinction between a Call and Put gamma
*/
void Output::csv(const std::vector<double>& meshPoints, const std::vector<std::vector<double> > &prices,
const std::vector<std::vector<double> > &deltas, const std::vector<double>& gammas) {
std::ofstream outFile; // Object for writing to a file
// Current time used to create unique file names
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
std::stringstream ss;
ss << std::put_time(&tm, "%m-%d-%Y %H-%M-%S");
outFile.open("European_Option_Data " + ss.str() + ".csv");
if (outFile.is_open()) {
outFile << "Mesh Points" << "," << "Call Price" << "," << "Put Price" << "," << "Call Delta" << ","
<< "Put Delta"<< "," << "Gamma" << std::endl;
for (int i = 0; i < prices.size(); ++i) {
outFile << meshPoints[i] << "," << prices[i][0] << "," << prices[i][1] << "," << deltas[i][0] << ","
<< deltas[i][1] << "," << gammas[i] << std::endl;
}
outFile.close();
} else {
std::cout << "Unable to open the file. Check filepath permissions\n";
}
}