-
Notifications
You must be signed in to change notification settings - Fork 0
/
pricer.h
125 lines (111 loc) · 2.34 KB
/
pricer.h
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
/*
module to implement different options as pricing functors
*/
#ifndef PRICER_H
#define PRICER_H
#include<functional>
#include<vector>
#include<algorithm>
template<typename T>
//accepts arbitrary pay offs to create novel options on the fly
class Pricer
{
using function_type = std::function<T(std::vector<T>)>;
private:
function_type priceStrat_;
public:
Pricer(const function_type& strat): priceStrat_{strat} {};
T operator () (std::vector<T> path){
return priceStrat_(path);
};
void setStrat(const function_type& strat){priceStrat_ = strat;}
};
template<typename T>
class PlainCallPricer
{
private:
T S_;
public:
PlainCallPricer(const T& s): S_{s} {};
T operator () (std::vector<T> path){
return std::max(path.back() - S_, 0.0);
}
};
//can be used by itself or as part of Pricer
template<typename T>
class AsianCallPricer
{
private:
T S_;
public:
AsianCallPricer(const T& s): S_{s} {};
T operator () (std::vector<T> path){
T sum;
//apparently there's no straightforward STL function for a std::vector sum.. weird
for(const auto& value: path){
sum += value;
}
return std::max((sum / path.size()) - S_, 0.0);
}
};
template<typename T>
class LookbackCallPricer
{
private:
T S_;
public:
LookbackCallPricer(const T& s): S_{s} {};
T operator () (std::vector<T> path){
T biggestPrice = 0.0;
for(const T& value : path){
if(value > biggestPrice){
biggestPrice = value;
}
};
return std::max(biggestPrice - S_, 0.0);
}
};
template<typename T>
class PlainPutPricer
{
private:
T S_;
public:
PlainPutPricer(const T& s): S_{s} {};
T operator () (std::vector<T> path){
return std::max(S_ - path.back(), 0.0);
}
};
template<typename T>
class AsianPutPricer
{
private:
T S_;
public:
AsianPutPricer(const T& s): S_{s} {};
T operator () (std::vector<T> path){
T sum;
for(const auto& value: path){
sum += value;
}
return std::max(S_ - (sum / path.size()), 0.0);
}
};
template<typename T>
class LookbackPutPricer
{
private:
T S_;
public:
LookbackPutPricer(const T& s): S_{s} {};
T operator () (std::vector<T> path){
T biggestPrice = 0.0;
for(const T& value : path){
if(value > biggestPrice){
biggestPrice = value;
}
};
return std::max(S_ - biggestPrice, 0.0);
}
};
#endif