-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRiskEngine.cpp
More file actions
31 lines (25 loc) · 1015 Bytes
/
RiskEngine.cpp
File metadata and controls
31 lines (25 loc) · 1015 Bytes
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
#include "RiskEngine.h"
void RiskEngine::updatePosition(const std::string& symbol, int quantity, double price, bool isBuy) {
int& pos = positions[symbol];
double& avgPrice = averagePrice[symbol];
if (isBuy) {
double totalCost = avgPrice * pos + price * quantity;
pos += quantity;
avgPrice = pos ? totalCost / pos : 0.0;
} else {
pos -= quantity;
}
}
double RiskEngine::getPnL(const std::string& symbol) const {
auto posIt = positions.find(symbol);
auto priceIt = averagePrice.find(symbol);
if (posIt == positions.end() || priceIt == averagePrice.end()) return 0.0;
int netPos = posIt->second;
double avgPrice = priceIt->second;
// assume current market price is same as avgPrice for simplicity?
return netPos * (avgPrice - avgPrice); // replace with market price later
}
int RiskEngine::getNetPosition(const std::string& symbol) const {
auto it = positions.find(symbol);
return it != positions.end() ? it->second : 0;
}