|
6 | 6 |
|
7 | 7 | #include "tradeManager.hpp" |
8 | 8 | #include <atomic> |
| 9 | +#include <chrono> |
| 10 | +#include <ctime> |
| 11 | +#include <iomanip> |
| 12 | +#include <iostream> |
| 13 | +#include <sstream> |
9 | 14 |
|
10 | 15 | namespace { |
11 | 16 | std::string nextTradeId() { |
@@ -35,19 +40,35 @@ size_t TradeManager::reviewAccount() const { |
35 | 40 | return activeTrades.size(); |
36 | 41 | } |
37 | 42 |
|
38 | | -bool TradeManager::closeTrade(const std::string& tradeId, boost::decimal::decimal64_t closePrice) { |
| 43 | +bool TradeManager::closeTrade(const std::string& tradeId, |
| 44 | + boost::decimal::decimal64_t closePrice, |
| 45 | + const PriceData& tick) { |
39 | 46 | auto it = activeTrades.find(tradeId); |
40 | 47 | if (it != activeTrades.end()) { |
41 | 48 | Trade closed = it->second; |
42 | 49 | closed.closePrice = closePrice; |
43 | | - closed.closeTime = std::chrono::system_clock::now(); |
| 50 | + closed.closeTime = tick.timestamp; |
44 | 51 | auto diff = closePrice - closed.entryPrice; |
45 | 52 | if (closed.direction == Direction::SHORT) diff = -diff; |
46 | 53 | // scalingFactor stays an int — boost::decimal has overloads for builtin |
47 | 54 | // integer types, so no conversion is needed there. |
48 | 55 | closed.pnl = diff * closed.scalingFactor * closed.size; |
49 | 56 | closedTrades.push_back(closed); |
50 | 57 | activeTrades.erase(it); |
| 58 | + |
| 59 | + auto t = std::chrono::system_clock::to_time_t(tick.timestamp); |
| 60 | + std::tm utc{}; |
| 61 | + gmtime_r(&t, &utc); |
| 62 | + std::ostringstream ts; |
| 63 | + ts << std::put_time(&utc, "%Y-%m-%dT%H:%M:%SZ"); |
| 64 | + |
| 65 | + const char* side = (closed.direction == Direction::LONG) ? "BUY" : "SELL"; |
| 66 | + std::cout << ts.str() |
| 67 | + << ", Trade Closed, " << closed.symbol |
| 68 | + << ", " << side |
| 69 | + << ", " << std::showpos << std::fixed << std::setprecision(2) << closed.pnl |
| 70 | + << std::noshowpos |
| 71 | + << std::endl; |
51 | 72 | return true; |
52 | 73 | } |
53 | 74 | return false; |
|
0 commit comments