Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Enumerations.ixx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export module Enumerations;

export
{
enum class OrderType
{
GoodTillCancel,
FillAndKill,
FillOrKill,
GoodForDay,
};

enum class Side
{
Buy,
Sell
};
}
11 changes: 11 additions & 0 deletions Globals.ixx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export module Globals;

import <vector>;

export
{
using Price = std::int32_t;
using Quantity = std::uint32_t;
using OrderId = std::uint64_t;
using OrderIds = std::vector<OrderId>;
}
14 changes: 14 additions & 0 deletions LevelInfo.ixx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export module LevelInfo;

import Globals;

export
{
struct LevelInfo
{
Price price_;
Quantity quantity_;
};

using LevelInfos = std::vector<LevelInfo>;
}
52 changes: 52 additions & 0 deletions Order.ixx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export module Order;

import Globals;
import Enumerations;

import <memory>;
import <format>;
import <exception>;
import <list>;

export
{
class Order
{
public:
Order(OrderType orderType, OrderId orderId, Side side, Price price, Quantity quantity)
: orderType_{ orderType }
, orderId_{ orderId }
, side_{ side }
, price_{ price }
, initialQuantity_{ quantity }
, remainingQuantity_{ quantity }
{ }

OrderId GetOrderId() const { return orderId_; }
Side GetSide() const { return side_; }
Price GetPrice() const { return price_; }
OrderType GetOrderType() const { return orderType_; }
Quantity GetInitialQuantity() const { return initialQuantity_; }
Quantity GetRemainingQuantity() const { return remainingQuantity_; }
Quantity GetFilledQuantity() const { return GetInitialQuantity() - GetRemainingQuantity(); }
bool IsFilled() const { return GetRemainingQuantity() == 0; }
void Fill(Quantity quantity)
{
if (quantity > GetRemainingQuantity())
throw std::logic_error(std::format("Order ({}) cannot be filled for more than its remaining quantity.", GetOrderId()));

remainingQuantity_ -= quantity;
}

private:
OrderType orderType_;
OrderId orderId_;
Side side_;
Price price_;
Quantity initialQuantity_;
Quantity remainingQuantity_;
};

using OrderPointer = std::shared_ptr<Order>;
using OrderPointers = std::list<OrderPointer>;
}
32 changes: 32 additions & 0 deletions OrderModify.ixx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export module OrderModify;

import Globals;
import Enumerations;
import Order;

export class OrderModify
{
public:
OrderModify(OrderId orderId, Side side, Price price, Quantity quantity)
: orderId_{ orderId }
, price_{ price }
, side_{ side }
, quantity_{ quantity }
{ }

OrderId GetOrderId() const { return orderId_; }
Price GetPrice() const { return price_; }
Side GetSide() const { return side_; }
Quantity GetQuantity() const { return quantity_; }

OrderPointer ToOrderPointer(OrderType type) const
{
return std::make_shared<Order>(type, GetOrderId(), GetSide(), GetPrice(), GetQuantity());
}

private:
OrderId orderId_;
Price price_;
Side side_;
Quantity quantity_;
};
191 changes: 23 additions & 168 deletions Orderbook.cpp → Orderbook.ixx
Original file line number Diff line number Diff line change
@@ -1,163 +1,24 @@
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <limits>
#include <string>
#include <vector>
#include <numeric>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <memory>
#include <variant>
#include <optional>
#include <tuple>
#include <format>
#include <chrono>
#include <condition_variable>

enum class OrderType
{
GoodTillCancel,
FillAndKill,
FillOrKill,
GoodForDay,
};

enum class Side
{
Buy,
Sell
};

using Price = std::int32_t;
using Quantity = std::uint32_t;
using OrderId = std::uint64_t;
using OrderIds = std::vector<OrderId>;

struct LevelInfo
{
Price price_;
Quantity quantity_;
};

using LevelInfos = std::vector<LevelInfo>;

class OrderbookLevelInfos
{
public:
OrderbookLevelInfos(const LevelInfos& bids, const LevelInfos& asks)
: bids_{ bids }
, asks_{ asks }
{ }

const LevelInfos& GetBids() const { return bids_; }
const LevelInfos& GetAsks() const { return asks_; }

private:
LevelInfos bids_;
LevelInfos asks_;
};

class Order
{
public:
Order(OrderType orderType, OrderId orderId, Side side, Price price, Quantity quantity)
: orderType_{ orderType }
, orderId_{ orderId }
, side_{ side }
, price_{ price }
, initialQuantity_{ quantity }
, remainingQuantity_{ quantity }
{ }

OrderId GetOrderId() const { return orderId_; }
Side GetSide() const { return side_; }
Price GetPrice() const { return price_; }
OrderType GetOrderType() const { return orderType_; }
Quantity GetInitialQuantity() const { return initialQuantity_; }
Quantity GetRemainingQuantity() const { return remainingQuantity_; }
Quantity GetFilledQuantity() const { return GetInitialQuantity() - GetRemainingQuantity(); }
bool IsFilled() const { return GetRemainingQuantity() == 0; }
void Fill(Quantity quantity)
{
if (quantity > GetRemainingQuantity())
throw std::logic_error(std::format("Order ({}) cannot be filled for more than its remaining quantity.", GetOrderId()));

remainingQuantity_ -= quantity;
}

private:
OrderType orderType_;
OrderId orderId_;
Side side_;
Price price_;
Quantity initialQuantity_;
Quantity remainingQuantity_;
};

using OrderPointer = std::shared_ptr<Order>;
using OrderPointers = std::list<OrderPointer>;

class OrderModify
{
public:
OrderModify(OrderId orderId, Side side, Price price, Quantity quantity)
: orderId_{ orderId }
, price_{ price }
, side_{ side }
, quantity_{ quantity }
{ }

OrderId GetOrderId() const { return orderId_; }
Price GetPrice() const { return price_; }
Side GetSide() const { return side_; }
Quantity GetQuantity() const { return quantity_; }

OrderPointer ToOrderPointer(OrderType type) const
{
return std::make_shared<Order>(type, GetOrderId(), GetSide(), GetPrice(), GetQuantity());
}

private:
OrderId orderId_;
Price price_;
Side side_;
Quantity quantity_;
};

struct TradeInfo
{
OrderId orderId_;
Price price_;
Quantity quantity_;
};

class Trade
{
public:
Trade(const TradeInfo& bidTrade, const TradeInfo& askTrade)
: bidTrade_{ bidTrade }
, askTrade_{ askTrade }
{ }

const TradeInfo& GetBidTrade() const { return bidTrade_; }
const TradeInfo& GetAskTrade() const { return askTrade_; }

private:
TradeInfo bidTrade_;
TradeInfo askTrade_;
};

using Trades = std::vector<Trade>;

class Orderbook
export module Orderbook;

import Globals;
import Enumerations;
import Order;
import OrderModify;
import OrderbookLevelInfos;
import TradeInfo;
import Trade;

import <map>;
import <numeric>;
import <algorithm>;
import <numeric>;
import <unordered_map>;
import <optional>;
import <ctime>;
import <chrono>;
import <condition_variable>;

export class Orderbook
{
private:

Expand Down Expand Up @@ -198,7 +59,7 @@ class Orderbook
const auto now = system_clock::now();
const auto now_c = system_clock::to_time_t(now);
std::tm now_parts;
localtime_s(&now_parts, &now_c);
_localtime64_s(&now_parts, &now_c);

if (now_parts.tm_hour >= end.count())
now_parts.tm_mday += 1;
Expand All @@ -207,7 +68,7 @@ class Orderbook
now_parts.tm_min = 0;
now_parts.tm_sec = 0;

auto next = system_clock::from_time_t(mktime(&now_parts));
auto next = system_clock::from_time_t(_mktime64(&now_parts));
auto till = next - now + milliseconds(100);

{
Expand Down Expand Up @@ -547,10 +408,4 @@ class Orderbook

return OrderbookLevelInfos{ bidInfos, askInfos };
}

};

int main()
{
return 0;
}
11 changes: 10 additions & 1 deletion Orderbook.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,16 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Orderbook.cpp" />
<ClCompile Include="Enumerations.ixx" />
<ClCompile Include="Globals.ixx" />
<ClCompile Include="LevelInfo.ixx" />
<ClCompile Include="Order.ixx" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Orderbook.ixx" />
<ClCompile Include="OrderbookLevelInfos.ixx" />
<ClCompile Include="OrderModify.ixx" />
<ClCompile Include="Trade.ixx" />
<ClCompile Include="TradeInfo.ixx" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
Loading