-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
139 additions
and
54 deletions.
There are no files selected for viewing
42 changes: 35 additions & 7 deletions
42
src/trading/common/include/market-trader-engine-context.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,51 @@ | ||
#pragma once | ||
|
||
#include <span> | ||
|
||
#include "exchangeprivateapitypes.hpp" | ||
#include "monetaryamount.hpp" | ||
#include "opened-order.hpp" | ||
#include "stringhelpers.hpp" | ||
#include "trader-command.hpp" | ||
|
||
namespace cct { | ||
struct MarketTraderEngineContext { | ||
class MarketTraderEngineContext { | ||
public: | ||
MarketTraderEngineContext(MonetaryAmount startAmountBase, MonetaryAmount startAmountQuote); | ||
|
||
void placeNewOrder(TimePoint placedTime, MonetaryAmount volume, MonetaryAmount price, TradeSide side, | ||
MonetaryAmount computeBuyFrom(TraderCommand traderCommand) const; | ||
|
||
MonetaryAmount computeSellVolume(TraderCommand traderCommand) const; | ||
|
||
void placeBuyOrder(TimePoint placedTime, MonetaryAmount remainingVolume, MonetaryAmount price, | ||
MonetaryAmount matchedVolume = MonetaryAmount{}); | ||
|
||
auto totalNbOrders() const { return openedOrders.size() + closedOrders.size(); } | ||
void placeBuyTakerOrder(TimePoint placedTime, MonetaryAmount avgPrice, MonetaryAmount matchedVolume); | ||
|
||
void placeSellOrder(TimePoint placedTime, MonetaryAmount remainingVolume, MonetaryAmount price, | ||
MonetaryAmount matchedVolume = MonetaryAmount{}); | ||
|
||
void placeSellTakerOrder(TimePoint placedTime, MonetaryAmount avgPrice, MonetaryAmount matchedVolume); | ||
|
||
auto totalNbOrders() const { return _openedOrders.size() + _closedOrders.size(); } | ||
|
||
auto nextOrderId() const { return ToString(totalNbOrders()); } | ||
|
||
MonetaryAmount availableBaseAmount; | ||
MonetaryAmount availableQuoteAmount; | ||
OpenedOrderVector openedOrders; | ||
ClosedOrderVector closedOrders; | ||
MonetaryAmount availableBaseAmount() const { return _availableBaseAmount; } | ||
MonetaryAmount availableQuoteAmount() const { return _availableQuoteAmount; } | ||
|
||
std::span<const OpenedOrder> openedOrders() const { return _openedOrders; } | ||
std::span<const ClosedOrder> closedOrders() const { return _closedOrders; } | ||
|
||
bool matchOrder(const OpenedOrder &matchedOrder, MonetaryAmount matchedAmount, MonetaryAmount price, | ||
TimePoint matchedTime); | ||
|
||
void eraseNewlyClosedOrders(std::span<const OpenedOrder> newlyClosedOrders); | ||
|
||
private: | ||
MonetaryAmount _availableBaseAmount; | ||
MonetaryAmount _availableQuoteAmount; | ||
OpenedOrderVector _openedOrders; | ||
ClosedOrderVector _closedOrders; | ||
}; | ||
} // namespace cct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,79 @@ | ||
#include "market-trader-engine-context.hpp" | ||
|
||
#include "monetaryamount.hpp" | ||
#include "timedef.hpp" | ||
#include "trader-command.hpp" | ||
#include "tradeside.hpp" | ||
|
||
namespace cct { | ||
MarketTraderEngineContext::MarketTraderEngineContext(MonetaryAmount startAmountBase, MonetaryAmount startAmountQuote) | ||
: availableBaseAmount(startAmountBase), availableQuoteAmount(startAmountQuote) {} | ||
: _availableBaseAmount(startAmountBase), _availableQuoteAmount(startAmountQuote) {} | ||
|
||
void MarketTraderEngineContext::placeNewOrder(TimePoint placedTime, MonetaryAmount volume, MonetaryAmount price, | ||
TradeSide side, MonetaryAmount matchedVolume) { | ||
openedOrders.emplace_back(nextOrderId(), matchedVolume, volume, price, placedTime, side); | ||
MonetaryAmount MarketTraderEngineContext::computeBuyFrom(TraderCommand traderCommand) const { | ||
return (_availableQuoteAmount * traderCommand.amountIntensityPercentage()) / 100; | ||
} | ||
|
||
MonetaryAmount MarketTraderEngineContext::computeSellVolume(TraderCommand traderCommand) const { | ||
return (_availableBaseAmount * traderCommand.amountIntensityPercentage()) / 100; | ||
} | ||
|
||
void MarketTraderEngineContext::placeBuyOrder(TimePoint placedTime, MonetaryAmount remainingVolume, | ||
MonetaryAmount price, MonetaryAmount matchedVolume) { | ||
_availableQuoteAmount -= matchedVolume.toNeutral() * price; | ||
_openedOrders.emplace_back(nextOrderId(), matchedVolume, remainingVolume, price, placedTime, TradeSide::kBuy); | ||
if (remainingVolume == 0) { | ||
_openedOrders.emplace_back(nextOrderId(), matchedVolume, remainingVolume, price, placedTime, TradeSide::kBuy); | ||
} else { | ||
_closedOrders.emplace_back(nextOrderId(), matchedVolume, price, placedTime, placedTime, TradeSide::kBuy); | ||
} | ||
} | ||
|
||
void MarketTraderEngineContext::placeBuyTakerOrder(TimePoint placedTime, MonetaryAmount avgPrice, | ||
MonetaryAmount matchedVolume) { | ||
_availableQuoteAmount -= matchedVolume.toNeutral() * avgPrice; | ||
_closedOrders.emplace_back(nextOrderId(), matchedVolume, avgPrice, placedTime, placedTime, TradeSide::kBuy); | ||
} | ||
|
||
void MarketTraderEngineContext::placeSellOrder(TimePoint placedTime, MonetaryAmount remainingVolume, | ||
MonetaryAmount price, MonetaryAmount matchedVolume) { | ||
_availableBaseAmount -= matchedVolume; | ||
_openedOrders.emplace_back(nextOrderId(), matchedVolume, remainingVolume, price, placedTime, TradeSide::kSell); | ||
} | ||
|
||
void MarketTraderEngineContext::placeSellTakerOrder(TimePoint placedTime, MonetaryAmount avgPrice, | ||
MonetaryAmount matchedVolume) { | ||
_availableBaseAmount -= matchedVolume; | ||
_closedOrders.emplace_back(nextOrderId(), matchedVolume, avgPrice, placedTime, placedTime, TradeSide::kSell); | ||
} | ||
|
||
bool MarketTraderEngineContext::matchOrder(const OpenedOrder &matchedOrder, MonetaryAmount matchedAmount, | ||
MonetaryAmount price, TimePoint matchedTime) { | ||
_closedOrders.emplace_back(matchedOrder.id(), matchedAmount, price, matchedOrder.placedTime(), matchedTime, | ||
matchedOrder.side()); | ||
|
||
if (matchedOrder.remainingVolume() == matchedAmount) { | ||
// no more opened order, we can mark it for remove from the opened orders list | ||
return true; | ||
} | ||
|
||
// Adjust remaining amount of the opened order | ||
auto openedOrderIt = std::ranges::find_if( | ||
_openedOrders, [&matchedOrder](const auto &openedOrder) { return matchedOrder.id() == openedOrder.id(); }); | ||
|
||
*openedOrderIt = OpenedOrder(matchedOrder.id(), matchedOrder.matchedVolume() + matchedAmount, | ||
matchedOrder.remainingVolume() - matchedAmount, matchedOrder.price(), | ||
matchedOrder.placedTime(), matchedOrder.side()); | ||
|
||
return false; | ||
} | ||
|
||
void MarketTraderEngineContext::eraseNewlyClosedOrders(std::span<const OpenedOrder> newlyClosedOrders) { | ||
const auto [first, last] = std::ranges::remove_if(_openedOrders, [newlyClosedOrders](const OpenedOrder &openedOrder) { | ||
return std::ranges::any_of(newlyClosedOrders, [&openedOrder](const auto &newlyClosedOrder) { | ||
return openedOrder.id() == newlyClosedOrder.id(); | ||
}); | ||
}); | ||
_openedOrders.erase(first, last); | ||
} | ||
|
||
} // namespace cct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters