Skip to content

Commit 7c97a80

Browse files
authored
Added verbose logging on trade action (#30)
* Added verbose logging on trade action * updating contributing.md
1 parent 8828128 commit 7c97a80

5 files changed

Lines changed: 33 additions & 16 deletions

File tree

CONTRIBUTING.md

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
# Contributing
22

3-
Thanks for taking a look! This is an active solo experiment, and I'm grateful for the interest.
3+
Thanks for taking a look! This is an active experiment, and I'm grateful for the interest.
44

55
## I'm not accepting pull requests right now
66

7-
This repository isn't currently accepting external contributions. I am actively experimenting with different approaches and want to avoid merge conflicts, so any PR opened from a non-owner account will be commented on and closed automatically by a GitHub bot.
7+
This repository isn't currently accepting external contributions.
88

9-
Please don't take it personally — the policy is the same for everyone, and it isn't a judgement on the change.
9+
I am actively experimenting with different approaches and want to avoid merge conflicts, so any PR opened from a non-owner account will be commented on and closed automatically by a GitHub bot.
1010

1111
## Please fork freely
1212

13-
The project is [MIT-licensed](LICENSE.MD), so you're very welcome to fork it and take the code in your own direction. If your fork sparks a discussion or you want to share what you've built on top of it, feel free to open an issue (see below).
13+
The project is [MIT-licensed](LICENSE.MD), so you're very welcome to fork it and take the code in your own direction.
1414

1515
## Use GitHub Issues for bugs, questions, and ideas
1616

17-
Bugs, questions, and ideas are all welcome on the [Issues tab](https://github.com/mccaffers/backtesting-engine-cpp/issues). That's the supported way to flag something — much more useful to me than a PR I'd have to close.
18-
19-
When filing an issue, a short description of what you saw, what you expected, and (for bugs) how to reproduce it goes a long way.
20-
21-
## A note on `@claude` mentions
22-
23-
This repo uses the [Claude Code GitHub Action](https://github.com/anthropics/claude-code-action) on `@claude` mentions, but it's restricted to the repository owner — `@claude` comments from other accounts won't trigger a run. This keeps paid API usage under control.
17+
Bugs, questions, and ideas are all welcome on the [Issues tab](https://github.com/mccaffers/backtesting-engine-cpp/issues).

include/trading/tradeManager.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ class TradeManager {
2525
boost::decimal::decimal64_t stopDistancePips = boost::decimal::decimal64_t{0},
2626
boost::decimal::decimal64_t limitDistancePips = boost::decimal::decimal64_t{0});
2727
size_t reviewAccount() const;
28-
bool closeTrade(const std::string& tradeId, boost::decimal::decimal64_t closePrice);
28+
bool closeTrade(const std::string& tradeId,
29+
boost::decimal::decimal64_t closePrice,
30+
const PriceData& tick);
2931
const std::unordered_map<std::string, Trade>& getActiveTrades() const;
3032
const std::vector<Trade>& getClosedTrades() const;
3133
boost::decimal::decimal64_t calculatePnl() const;

source/operations.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void reviewStopAndLimit(TradeManager& tradeManager, const PriceData& tick) {
3939
}
4040
}
4141
for (const auto& [id, exitPrice] : toClose) {
42-
tradeManager.closeTrade(id, exitPrice);
42+
tradeManager.closeTrade(id, exitPrice, tick);
4343
}
4444
}
4545

source/trading/tradeManager.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
#include "tradeManager.hpp"
88
#include <atomic>
9+
#include <chrono>
10+
#include <ctime>
11+
#include <iomanip>
12+
#include <iostream>
13+
#include <sstream>
914

1015
namespace {
1116
std::string nextTradeId() {
@@ -35,19 +40,35 @@ size_t TradeManager::reviewAccount() const {
3540
return activeTrades.size();
3641
}
3742

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) {
3946
auto it = activeTrades.find(tradeId);
4047
if (it != activeTrades.end()) {
4148
Trade closed = it->second;
4249
closed.closePrice = closePrice;
43-
closed.closeTime = std::chrono::system_clock::now();
50+
closed.closeTime = tick.timestamp;
4451
auto diff = closePrice - closed.entryPrice;
4552
if (closed.direction == Direction::SHORT) diff = -diff;
4653
// scalingFactor stays an int — boost::decimal has overloads for builtin
4754
// integer types, so no conversion is needed there.
4855
closed.pnl = diff * closed.scalingFactor * closed.size;
4956
closedTrades.push_back(closed);
5057
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;
5172
return true;
5273
}
5374
return false;

tests/tradeManager.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ - (void)testOpenTrade {
3939
- (void)testCloseTrade {
4040
PriceData tick("100.0"_dd, "99.0"_dd, std::chrono::system_clock::now(), "EURUSD");
4141
std::string tradeId = self.manager->openTrade(tick, "1.0"_dd, Direction::LONG);
42-
bool closed = self.manager->closeTrade(tradeId, "110.0"_dd);
42+
bool closed = self.manager->closeTrade(tradeId, "110.0"_dd, tick);
4343
XCTAssertTrue(closed, "Trade should be closed successfully");
4444
XCTAssertEqual(self.manager->reviewAccount(), 0, "Should have 0 active trades");
4545
}

0 commit comments

Comments
 (0)