UpAndInOption.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
#include "UpAndInOption.h"
#include "BarrierOption.h"
 
using namespace std;
 
 
double UpAndInOption::payoff(
    const std::vector<double>& prices) const {
    int n = prices.size();
    double in = false;
    for (int i = 0; i<n; i++) {
        if (prices[i]>getBarrier()) {
            in = true;
        }
    }
    if (!in) {
        return 0.0;
    }
    double stockAtMaturity = prices.back();
    if (stockAtMaturity>getStrike()) {
        return stockAtMaturity - getStrike();
    }
    else {
        return 0.0;
    }
}
 
/////////////////////////////////////
//
//   TESTS
//
/////////////////////////////////////
 
 
void testUpAndInOption() {
    UpAndInOption o;
    o.setBarrier(100);
    o.setStrike(70);
    vector<double> prices;
    prices.push_back(120);
    prices.push_back(80);
    ASSERT_APPROX_EQUAL(o.payoff(prices), 10.0, 0.001);
    prices[0] = 90;
    ASSERT_APPROX_EQUAL(o.payoff(prices), 0.0, 0.001);
    prices[1] = 60;
    ASSERT_APPROX_EQUAL(o.payoff(prices), 0.0, 0.001);
}