Hello, I have a global variable of type orderBook. The class is detailed below.
1 2 3 4 5
|
class orderBook {
public:
std::vector<priceLevel> levels;
... // Other functions removed for readability.
};
|
I declare this global at the beginning of my CPP file.
This class stores a vector of price levels. Each priceLevel holds a queue of orderElements, see below.
1 2 3 4 5 6
|
class priceLevel {
public:
int price;
std::queue<orderElement> orderQueue;
... // Other functions removed for readability.
};
|
Each orderElement is an order.
Instead of passing my orderBook (declared as OB) into the function, I take a priceLevel from the std::vector<priceLevel> levels in the orderBook by reference as such:
|
priceLevel& pricePoint = OB.levels[priceEntry];
|
I believe this is the root cause for much of my woes, as trades that are matched are not persisting.
For instance, the bestBid starts at 8975, the bestAsk starts at 8976. A user places a sell for a large amount of contracts at a price of 8974, eating through the 8975 and 8974 level. The remainder of his unmatched contracts are queued into 8974. The bestBid and bestAsk are updated to 8973 and 8974 respectively. Given that he has eaten through the 8974 level, his remaining contracts on the 8974 level should be the first to be matched from an incoming buy order.
I provide an example of the output below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
[EXECUTION] 0: Order (MTAXY-DZXWE-RNWEH-DAPIO-PCGEO) fully filled at 8975
// Execution 0 is against a sell order for 1200 contracts @ 8974 (order A).
// Order A eats through 8975
[EXECUTION] 1: Order (KIIAO-KZLUE-UDDCF-YZRJM-XLEBZ) fully filled at 8974
// Execution 1 is against order A as well.
// Order A eats through 8974
[ORDER SUBMITTED] (QIXQG-GSLBQ-IIUKC-RSJGV-LWUUV) for 1044 contracts.
// Order A was issued for 8974 and can't go deeper.
// Remaining unmatched contracts lie dormant on 8974.
// These contracts should be the first to match with an incoming buy order.
[EXECUTION] 2: Order (KIIAO-KZLUE-UDDCF-YZRJM-XLEBZ) fully filled at 8974
// Incoming buy order (order B) fills against a sell order which shouldn't theoretically be there
// This was already eaten through on 8974.
// Should have matched against QIXQG-GSLBQ-IIUKC-RSJGV-LWUUV
|
My questions are:
1. Should I be passing the entire orderbook by reference, even though it's a global?
2. If I want to index into std::vector<priceLevels> levels to performing my matching operation, do I need to assign it to a new variable by reference for any operations on it to persist? Or is passing the orderbook by reference all that matters? For instance, is this needed (see below)?
1 2 3
|
void matchingFunction(int quantity, int price, orderBook& x) {
priceLevel& levelofInterest = x.levels[2]
}
|
Thanks for reading through my long-winded question. I've been hacking at this for a while so I apologize if I'm getting things confused.