Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
pnl_calculator.cpp
Go to the documentation of this file.
2#include <slick/logger.hpp>
3#include <chrono>
4
5namespace slick::trader {
6
7namespace {
8
9// A usable mark price. Zero and negative prices are real - calendar spreads trade through
10// zero - so only the null price and exchange null sentinels are rejected. NULL_PRICE/100
11// (~9.22e16 in integer units, ~$9.22e8 per unit) is far beyond any real price in either
12// direction, and sentinels such as Coinbase's INT64_MAX/1e8 exceed it.
13constexpr bool is_valid_mark(price_t price) noexcept {
14 constexpr price_t SENTINEL_THRESHOLD = NULL_PRICE / 100;
15 return price != NULL_PRICE && price < SENTINEL_THRESHOLD && price > -SENTINEL_THRESHOLD;
16}
17
18} // namespace
19
20void PnLCalculator::update_mark_price(const std::string& instrument, price_t price, uint64_t timestamp_ns) {
21 if (!is_valid_mark(price)) {
22 LOG_DEBUG("PnLCalculator::update_mark_price: rejected invalid mark price {} for {}", price, instrument);
23 return;
24 }
25 bid_prices_[instrument] = {price, timestamp_ns};
26 ask_prices_[instrument] = {price, timestamp_ns};
27}
28
29void PnLCalculator::update_top_of_book(const std::string& instrument, price_t bid, price_t ask, uint64_t timestamp_ns) {
30 if (!is_valid_mark(bid) || !is_valid_mark(ask)) {
31 LOG_DEBUG("PnLCalculator::update_top_of_book: rejected invalid tob for {} bid={} ask={}", instrument, bid, ask);
32 return;
33 }
34 if (bid != bid_prices_[instrument].price || ask != ask_prices_[instrument].price) {
35 bid_prices_[instrument] = {bid, timestamp_ns};
36 ask_prices_[instrument] = {ask, timestamp_ns};
37 }
38}
39
40price_t PnLCalculator::get_mark_price(const std::string& instrument) const {
41 auto bid_it = bid_prices_.find(instrument);
42 auto ask_it = ask_prices_.find(instrument);
43 if (bid_it != bid_prices_.end() && ask_it != ask_prices_.end()) {
44 return (bid_it->second.price / 2) + (ask_it->second.price / 2);
45 }
46 if (bid_it != bid_prices_.end()) return bid_it->second.price;
47 if (ask_it != ask_prices_.end()) return ask_it->second.price;
48 return 0;
49}
50
51bool PnLCalculator::has_mark_price(const std::string& instrument) const {
52 return bid_prices_.find(instrument) != bid_prices_.end()
53 || ask_prices_.find(instrument) != ask_prices_.end();
54}
55
56PnLSnapshot PnLCalculator::calculate_pnl(const std::string& strategy_id, uint64_t timestamp_ns) const {
57 PnLSnapshot snapshot;
58 snapshot.strategy_id = strategy_id;
59 snapshot.timestamp_ns = timestamp_ns;
60
61 auto positions = position_tracker_.get_all_positions(strategy_id);
62
63 price_t total_realized = 0;
64 price_t total_unrealized = 0;
65 price_t total_realized_fees = 0;
66 price_t total_open_fees = 0;
67
68 for (const auto& [instrument, position] : positions) {
69 // Net realized = gross realized minus fees on closed trades
70 total_realized += position.realized_pnl - position.realized_fees;
71 total_realized_fees += position.realized_fees;
72 total_open_fees += position.open_fees;
73
74 // Net unrealized = mark-to-market minus entry fees paid for currently held lots
75 if (!position.is_flat()) {
76 const bool is_long = position.quantity > 0;
77 const auto& price_map = is_long ? bid_prices_ : ask_prices_;
78 auto mark_it = price_map.find(instrument);
79 if (mark_it != price_map.end()) {
80 const price_t mark_price = mark_it->second.price;
81 if (!is_valid_mark(mark_price)) {
82 LOG_WARN("PnLCalculator::calculate_pnl: skipping unrealized PnL for {} — invalid mark price {}", instrument, mark_price);
83 } else {
84 total_unrealized += to_price_t(position.get_unrealized_pnl(mark_price))
85 - position.open_fees;
86 }
87 }
88 }
89 }
90
91 snapshot.realized_pnl = total_realized;
92 snapshot.unrealized_pnl = total_unrealized;
93 snapshot.total_fees = total_realized_fees + total_open_fees; // informational
94 snapshot.total_pnl = total_realized + total_unrealized;
95
96 return snapshot;
97}
98
99std::unordered_map<std::string, PnLSnapshot>
100PnLCalculator::calculate_all_pnl(uint64_t timestamp_ns) const {
101 std::unordered_map<std::string, PnLSnapshot> result;
102
103 auto all_positions = position_tracker_.get_all_positions();
104
105 for (const auto& [strategy_id, positions] : all_positions) {
106 result.emplace(strategy_id, calculate_pnl(strategy_id, timestamp_ns));
107 }
108
109 return result;
110}
111
112} // namespace slick::trader
std::unordered_map< std::string, PnLSnapshot > calculate_all_pnl(uint64_t timestamp_ns) const
bool has_mark_price(const std::string &instrument) const
void update_top_of_book(const std::string &instrument, price_t bid, price_t ask, uint64_t timestamp_ns)
void update_mark_price(const std::string &instrument, price_t price, uint64_t timestamp_ns)
price_t get_mark_price(const std::string &instrument) const
PnLSnapshot calculate_pnl(const std::string &strategy_id, uint64_t timestamp_ns) const
price_t to_price_t(double value)
Definition types.hpp:24
constexpr price_t NULL_PRICE
Definition types.hpp:19
int64_t price_t
Definition types.hpp:16