/
contract.c
52 lines (43 loc) · 1.62 KB
/
contract.c
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
#include "lderiv.h"
#include "assert.h"
#include "mem.h"
#include "table.h"
double Contract_Price(Contract* contract, time_t evalTime, double price, double riskFree, double vol) {
switch(contract->Kind) {
case Contract_Stock:
return price;
case Contract_CallOption:
return black_scholesG('c', price, contract->Strike, diff_in_years(contract->Expiry, evalTime), riskFree, riskFree - contract->Underlying->Yield, vol);
case Contract_PutOption:
return black_scholesG('p', price, contract->Strike, diff_in_years(contract->Expiry, evalTime), riskFree, riskFree - contract->Underlying->Yield, vol);
case Contract_Commission:
return 1.0;
default:
assert(0.0);
return 0.0;
}
}
struct applyStruct {
double total;
time_t evalTime;
double price;
double riskFree;
double vol;
Table_T volTable;
};
static void cApply(void** x, void* cl) {
Position* p = (Position*) *x;
struct applyStruct* e = (struct applyStruct*) cl;
double vol = e->vol;
if(e->volTable)
vol = * (double*) Table_get(e->volTable, p->Contract);
e->total += Contract_Price(p->Contract, e->evalTime, e->price, e->riskFree, vol) * p->Quantity;
}
double Contract_PortPrice(SList_T portfolio, Contract_VolCalculation volCalc, void* volEnv, time_t evalTime, double price, double riskFree, double vol) {
struct applyStruct e = { 0.0, evalTime, price, riskFree, vol, NULL};
if(volCalc == Vol_ConstantByStrike && volEnv) {
e.volTable = (Table_T) volEnv;
}
SList_map(portfolio, cApply, &e);
return e.total;
}