Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
pnl_calculator.hpp
Go to the documentation of this file.
1#pragma once
2
6#include <slick/logger.hpp>
7#include <unordered_map>
8#include <string>
9
10namespace slick::trader {
11
12// PnL snapshot for a strategy
14 std::string strategy_id;
15 price_t realized_pnl{0}; // Net realized PnL (gross realized minus fees on closed trades)
16 price_t unrealized_pnl{0}; // Net unrealized PnL (mark-to-market minus open entry fees)
17 price_t total_fees{0}; // Total fees paid (realized_fees + open_fees, for reference)
18 price_t total_pnl{0}; // realized_pnl + unrealized_pnl
19 uint64_t timestamp_ns{0}; // Snapshot timestamp
20
21 double get_realized() const { return from_price_t(realized_pnl); }
22 double get_unrealized() const { return from_price_t(unrealized_pnl); }
23 double get_fees() const { return from_price_t(total_fees); }
24 double get_total() const { return from_price_t(total_pnl); }
25};
26
27// Single-writer PnL calculator
28// Tracks mark prices and calculates PnL using PositionTracker data
29// NOTE: Single-writer only (called from StrategyManager thread)
31public:
32 explicit PnLCalculator(PositionTracker& position_tracker)
33 : position_tracker_(position_tracker) {}
34
35 ~PnLCalculator() = default;
36
37 // Update mark price from a single-price source (fills, trades).
38 // Stores the same price as both bid and ask.
39 void update_mark_price(const std::string& instrument, price_t price, uint64_t timestamp_ns);
40
41 // Update bid and ask from a top-of-book event.
42 // Used by calculate_pnl for directional pricing (long→bid, short→ask).
43 void update_top_of_book(const std::string& instrument, price_t bid, price_t ask, uint64_t timestamp_ns);
44
45 // Calculate PnL for a specific strategy
46 PnLSnapshot calculate_pnl(const std::string& strategy_id, uint64_t timestamp_ns) const;
47
48 // Calculate PnL for all strategies
49 std::unordered_map<std::string, PnLSnapshot> calculate_all_pnl(uint64_t timestamp_ns) const;
50
51 // Get current mark price (mid of bid/ask if available, else last single price)
52 price_t get_mark_price(const std::string& instrument) const;
53
54 // Check if mark price exists
55 bool has_mark_price(const std::string& instrument) const;
56
57private:
58 struct MarkPrice {
59 price_t price{0};
60 uint64_t timestamp_ns{0};
61 };
62
63 PositionTracker& position_tracker_;
64 std::unordered_map<std::string, MarkPrice> bid_prices_;
65 std::unordered_map<std::string, MarkPrice> ask_prices_;
66};
67
68} // 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
PnLCalculator(PositionTracker &position_tracker)
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
double from_price_t(price_t value)
Definition types.hpp:36
int64_t price_t
Definition types.hpp:16