Skip to content

Commit

Permalink
[Feature] - Merge Closed Order method
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Mar 10, 2024
1 parent 8092696 commit 87e6e4b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/api-objects/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
aux_source_directory(src API_OBJECTS_SRC)

add_library(coincenter_api-objects STATIC ${API_OBJECTS_SRC})
target_link_libraries(coincenter_api-objects PRIVATE coincenter_tech)
target_link_libraries(coincenter_api-objects PRIVATE coincenter_objects)
target_link_libraries(coincenter_api-objects PUBLIC coincenter_tech)
target_link_libraries(coincenter_api-objects PUBLIC coincenter_objects)

target_include_directories(coincenter_api-objects PUBLIC include)

Expand All @@ -11,6 +11,13 @@ add_unit_test(
test/baseconstraints_test.cpp
)

add_unit_test(
closed-order_test
test/closed-order_test.cpp
LIBRARIES
coincenter_api-objects
)

add_unit_test(
deposit_test
test/deposit_test.cpp
Expand All @@ -20,10 +27,9 @@ add_unit_test(

add_unit_test(
withdrawsordepositsconstraints_test
src/withdrawsordepositsconstraints.cpp
test/withdrawsordepositsconstraints_test.cpp
LIBRARIES
coincenter_objects
coincenter_api-objects
DEFINITIONS
CCT_DISABLE_SPDLOG
)
Expand Down
4 changes: 4 additions & 0 deletions src/api-objects/include/closed-order.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class ClosedOrder : public Order {

string matchedTimeStr() const;

/// Compute the resulting merged closed order from *this and given one.
/// Given closed order should be of same ID, TradeSide and Market.
[[nodiscard]] ClosedOrder mergeWith(const ClosedOrder &closedOrder) const;

private:
TimePoint _matchedTime;
};
Expand Down
19 changes: 19 additions & 0 deletions src/api-objects/src/closed-order.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,23 @@ ClosedOrder::ClosedOrder(OrderId id, MonetaryAmount matchedVolume, MonetaryAmoun
: Order(std::move(id), matchedVolume, price, placedTime, side), _matchedTime(matchedTime) {}

string ClosedOrder::matchedTimeStr() const { return ToString(_matchedTime); }

ClosedOrder ClosedOrder::mergeWith(const ClosedOrder &closedOrder) const {
const MonetaryAmount totalMatchedVolume = closedOrder.matchedVolume() + matchedVolume();
const auto previousMatchedTs = TimestampToMs(matchedTime());
const auto currentMatchedTs = TimestampToMs(closedOrder.matchedTime());
const auto avgMatchedTs = (((previousMatchedTs * matchedVolume().toNeutral()) +
(currentMatchedTs * closedOrder.matchedVolume().toNeutral())) /
totalMatchedVolume.toNeutral())
.integerPart();
const TimePoint avgMatchedTime{TimeInMs{avgMatchedTs}};

MonetaryAmount avgPrice = price();
if (closedOrder.price() != price()) {
avgPrice =
((matchedVolume().toNeutral() * price()) + (closedOrder.matchedVolume().toNeutral() * closedOrder.price())) /
totalMatchedVolume.toNeutral();
}
return ClosedOrder(id(), totalMatchedVolume, avgPrice, placedTime(), avgMatchedTime, side());
}
} // namespace cct
44 changes: 44 additions & 0 deletions src/api-objects/test/closed-order_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "closed-order.hpp"

#include <gtest/gtest.h>

#include <limits>

#include "monetaryamount.hpp"
#include "timedef.hpp"
#include "tradeside.hpp"

namespace cct {

class ClosedOrderTest : public ::testing::Test {
protected:
TimePoint tp1{TimeInMs{std::numeric_limits<int64_t>::max() / 10000000}};
TimePoint tp2{TimeInMs{std::numeric_limits<int64_t>::max() / 9900000}};
TimePoint tp3{TimeInMs{std::numeric_limits<int64_t>::max() / 9800000}};

ClosedOrder closedOrder1{"1", MonetaryAmount(15, "BTC", 1), MonetaryAmount(35000, "USDT"), tp1, tp1, TradeSide::kBuy};
ClosedOrder closedOrder2{"2", MonetaryAmount(25, "BTC", 1), MonetaryAmount(45000, "USDT"), tp2, tp3, TradeSide::kBuy};
};

TEST_F(ClosedOrderTest, SelfMerge) {
ClosedOrder mergedClosedOrder = closedOrder1.mergeWith(closedOrder1);

EXPECT_EQ(mergedClosedOrder.id(), closedOrder1.id());
EXPECT_EQ(mergedClosedOrder.placedTime(), closedOrder1.placedTime());
EXPECT_EQ(mergedClosedOrder.matchedVolume(), closedOrder1.matchedVolume() * 2);
EXPECT_EQ(mergedClosedOrder.price(), closedOrder1.price());
EXPECT_EQ(mergedClosedOrder.market(), closedOrder1.market());
EXPECT_EQ(mergedClosedOrder.matchedTime(), closedOrder1.matchedTime());
}

TEST_F(ClosedOrderTest, Merge) {
ClosedOrder mergedClosedOrder = closedOrder1.mergeWith(closedOrder2);

EXPECT_EQ(mergedClosedOrder.id(), closedOrder1.id());
EXPECT_EQ(mergedClosedOrder.placedTime(), closedOrder1.placedTime());
EXPECT_EQ(mergedClosedOrder.matchedVolume(), closedOrder1.matchedVolume() + closedOrder2.matchedVolume());
EXPECT_EQ(mergedClosedOrder.price(), MonetaryAmount(41250, closedOrder1.price().currencyCode()));
EXPECT_EQ(mergedClosedOrder.market(), closedOrder1.market());
EXPECT_EQ(mergedClosedOrder.matchedTime(), TimePoint{TimeInMs{934101708833}});
}
} // namespace cct

0 comments on commit 87e6e4b

Please sign in to comment.