1. Homepage
  2. Programming
  3. [2022] SMU - QF633 C++ for Financial Engineering - Assignment 4 - L3 Book Buidling

[2022] SMU - QF633 C++ for Financial Engineering - Assignment 4 - L3 Book Buidling

Engage in a Conversation
Financial EngineeringC++QF633C++ for Financial EngineeringSMU

QF633 Assignment 4 CourseNana.COM

June 11, 2022 CourseNana.COM

  CourseNana.COM

In this assignment, we will continue exploring the L3 Book buidling. The following files can be found in the attached zip file: CourseNana.COM

  CourseNana.COM

|-- data CourseNana.COM

| |-- l3_market_data . csv # market data in csv format CourseNana.COM

| |-- l3_market_data . drop . csv # market data in csv format , but with messages drops CourseNana.COM

|-- src CourseNana.COM

|-- book CourseNana.COM

| |-- CMakeLists . txt CourseNana.COM

| |-- l3_book . cpp # L3Book cpp file CourseNana.COM

| |-- l3_book .h # L3Book header file CourseNana.COM

|-- CMakeLists .txt CourseNana.COM

|-- csv_l3_md CourseNana.COM

| |-- CMakeLists . txt CourseNana.COM

| |-- main . cpp # exeutable to read csv market data , feed into L3Book and print the book CourseNana.COM

|-- util CourseNana.COM

|-- CMakeLists .txt CourseNana.COM

|-- incremental_csv_reader .cpp # util class IncrementalCSVReader CourseNana.COM

|-- incremental_csv_reader .h CourseNana.COM

|-- price .h # util class Price CourseNana.COM

  CourseNana.COM

The project produces a exeutable csv_l3_md, which takes two arguments: CourseNana.COM

·       ./csv_l3_md path/to/your/market_data.csv l2: print the L2 view of the book CourseNana.COM

·       ./csv_l3_md path/to/your/market_data.csv l3: print the L3 view of the book CourseNana.COM

The path/to/your/market_data.csv should be path to either CourseNana.COM

·       l3_market_data.csv: no message drops to validate the book iteration logic and the order execution update handlings. CourseNana.COM

·       l3_market_data.drop.csv: some message drops to validate the book uncrossing logic. CourseNana.COM

  CourseNana.COM

You are expected to give implementations in src/book/l3_book.h and src/book/l3_book.cpp, and create a zip file Assignment4_YourName.zip, which contains the completed source codes. CourseNana.COM

·       L3Book::ForEachLevel and L3Book::ForEachOrder to iterate the book eitherby level or by order. CourseNana.COM

·       L3Book::ProcessExec to handle another type of book update message CourseNana.COM

·       L3Book::UncrossBookSide to uncross the book in case of message drops CourseNana.COM

  CourseNana.COM

We have been using the following convention to print the book in the lecture: CourseNana.COM

# the number not inside the parentheses nor the square brackets is the price for the price level CourseNana.COM

# the number inside the parentheses is the aggregated qty for that price level CourseNana.COM

# the numbers inside the square brackets are the qties for each order in that price level , following the CourseNana.COM

FIFO ordering CourseNana.COM

Bid | Ask CourseNana.COM

[2, 8] (10) 100.00 | 101.00 (11) [3, 4, 4] CourseNana.COM

[1, 1, 7] (9) 99.00 | 102.00 (11) [7, 4] CourseNana.COM

[] (0) 0.00 | 103.00 (12) [1, 1, 10] CourseNana.COM

# 1. levels are printed following inner levels to outer levels CourseNana.COM

# 2. for the top buy level : CourseNana.COM

# a. the price is 100 CourseNana.COM

# b. the aggregated qty is 10 CourseNana.COM

# c. there are two orders in that price level , and the qties for the order are 2 and 8, while 2 is CourseNana.COM

queued before 8. CourseNana.COM

# 3. there ’re two buy levels and three sell levels , and thus we zero filled the aggregated qty and price for CourseNana.COM

the empty 3rd buy level CourseNana.COM

  CourseNana.COM

In this assignment, we will use two slightly different representations of the book as illustrated below CourseNana.COM

L2 View of the Book CourseNana.COM

  CourseNana.COM

# In the L2 view of the book , only the aggregated information for each price level is presented : CourseNana.COM

# a) price , b) aggregated qty , c) order counts CourseNana.COM

bid_count bid_qty bid_price ask_price ask_qty ask_count CourseNana.COM

2 10 100.00 101.00 11 3 CourseNana.COM

3 9 99.00 102.00 11 2 CourseNana.COM

0 0 0.00 103.00 12 3 CourseNana.COM

# for the top buy level : CourseNana.COM

# a. the price is 100 CourseNana.COM

# b. the aggregated qty is 10 CourseNana.COM

# c. the number of orders is 3 CourseNana.COM

  CourseNana.COM

L3 view of the Book CourseNana.COM

  CourseNana.COM

# There are four columns : CourseNana.COM

# a. side : S for Sell side , B for Buy side CourseNana.COM

# b. price : price for the level CourseNana.COM

# c. qty : aggregated qty for the level CourseNana.COM

# d. orders : list of order_qty ( order_id ), "1(20) " means a order with qty =1 and order_id =20 CourseNana.COM

side price qty orders CourseNana.COM

S 103.00 12 1(20) , 1(17) , 10(3) CourseNana.COM

S 102.00 11 7(1) , 4(7) CourseNana.COM

S 101.00 11 3(7) , 4(3) , 4(5) CourseNana.COM

B 100.00 10 2(12) , 8(5) CourseNana.COM

B 99.00 9 1(41) , 1(40) , 7(28) CourseNana.COM

# 1. levels are printed from the highest price to lowest price , i.e. outer levels to inner levels for sell CourseNana.COM

side and inner levels to outer levels for buy side CourseNana.COM

# 2. for the top buy level CourseNana.COM

# a) the price is 100.00 CourseNana.COM

# b) the aggregated qty is 10 CourseNana.COM

# c) there are two orders in that level , order with qty =2, order_id =12 and order with qty =8, order_id =5 CourseNana.COM

  CourseNana.COM

1 L3 Book Iteration CourseNana.COM

There are two functions inside the src/csv_l3_md/main.cpp: CourseNana.COM

·       void PrintLevel2Book(...): The function prints the "L2 View of the Book". It relies on the L3Book::ForEachLevel to iterate the levels on both sides simultaneously, from inner levels to outer levels. CourseNana.COM

class L3Book { CourseNana.COM

public : CourseNana.COM

template <typename F> CourseNana.COM

void ForEachLevel (F&& f) const { CourseNana.COM

// f could be either a lambda function or a functor with the following function signature CourseNana.COM

// bool process_level ( double bid_price , int64_t bid_qty , int64_t bid_count , CourseNana.COM

// double ask_price , int64_t ask_qty , int64_t ask_count ); CourseNana.COM

// 1. For the above book snapshot , below is the sequence of events that should be delivered to f CourseNana.COM

f (100.0 , 10, 2, 101.0 , 11, 3); CourseNana.COM

f (99.0 , 9, 3, 102.0 , 11, 2); CourseNana.COM

f( 0.0 , 0, 0, 103.0 , 12, 3); CourseNana.COM

} CourseNana.COM

// 2. NOTE : the return type of "f" is bool , and this function should consider f’s return value : CourseNana.COM

// -> true : continue the iteration if there ’s any more level CourseNana.COM

// -> false : break the loop and stop the iteration CourseNana.COM

// with the above event sequence , if f(99.0 , 9, 3, 102.0 , 11, 2) returns false , there should CourseNana.COM

// be no call with f( 0.0 , 0, 0, 103.0 , 12, 3) CourseNana.COM

}; CourseNana.COM

·       void PrintLevel3Book(...): The function prints the "L3 View of the Book". It relies on the L3Book::ForEachOrder to firstly iterate the sell side from outer levels to inner levels and then buy side from inner levels to outer levels. For each price level, it iterates the price level following the priority of the order, i.e. the FIFO ordering. CourseNana.COM

class L3Book { CourseNana.COM

public : CourseNana.COM

template <typename F> CourseNana.COM

void ForEachOrder ( bool is_buy , bool inner_to_outer , F&& f) const { CourseNana.COM

// - is_buy : the side of the book to iterate . CourseNana.COM

// true - iterate buy side book ; CourseNana.COM

// false - iterate sell side book CourseNana.COM

// - inner_to_outer : whether to iterate from inner levels to outer levels or from outer levels CourseNana.COM

// to inner levels . In "L3 view of the Book " printing , we first iterate the sell side with CourseNana.COM

// inner_to_outer = false (i.e. higher price to lower price ), and then buy side with CourseNana.COM

// inner_to_outer = true (i.e. higher price to lower price ) CourseNana.COM

// - f: could be either a lambda function or a functor with the following function signature CourseNana.COM

// bool process_order ( bool is_buy , double level_price , int64_t level_qty , CourseNana.COM

// int64_t qty , int64_t order_id ); CourseNana.COM

// For the above book snapshot , below is the sequence of events that should be delivered to f CourseNana.COM

f(false , 103.00 , 12, 1, 20) ; CourseNana.COM

f(false , 103.00 , 12, 1, 17) ; CourseNana.COM

f(false , 103.00 , 12, 10, 3); CourseNana.COM

// calls to orders with price = 102 skipped here CourseNana.COM

f(false , 101 , 11, 3, 7); CourseNana.COM

f(false , 101 , 11, 4, 3); CourseNana.COM

f(false , 101 , 11, 4, 5); CourseNana.COM

f(true , 100 , 10, 2, 12) ; CourseNana.COM

f(true , 100 , 10, 8, 5); CourseNana.COM

// calls to orders with price = 99.0 skipped here CourseNana.COM

} CourseNana.COM

// 2. NOTE : the return type of "f" is bool to control the iteration CourseNana.COM

// true : continue the iteration if there ’s any more order CourseNana.COM

// false : break the loop and stop the iteration CourseNana.COM

// 3. HINT : there ’s a reversed iterator , i.e. rbegin () , rend () , with which you can iterate CourseNana.COM

// the container in reversed order . CourseNana.COM

// a) In the L3Book , the Order pointers are saved in the Level in reversed order , i.e. Order CourseNana.COM

// added later is infront of orders added earlier . You could use the reversed iterator to CourseNana.COM

// iterate the container . CourseNana.COM

// b) In the L3Book , both Buy side and Sell side books are stored from inner levels to outer CourseNana.COM

// levels . You could also use reversed iterator to iterate the book from outer levels to CourseNana.COM

inner levels . CourseNana.COM

}; CourseNana.COM

  CourseNana.COM

2 Execution Messages Handlings CourseNana.COM

  CourseNana.COM

During the lecture, we have discussed the implementation of Add, Delete and Replace operations. In addition to the above three, there’s another common type of order update message, i.e. the Order Execution Update. It is used to inform a order with given order_id is executed by exec_qty: CourseNana.COM

·         partial execution: the exec_qty is less than the open_qty. It’s similar tothe inplace Replace Operation. CourseNana.COM

·         full execution: the exec_qty is equal to the open_qty. It’s similar to the Delete operation. CourseNana.COM

  CourseNana.COM

  CourseNana.COM

# L3 book view before the update CourseNana.COM

side price qty orders CourseNana.COM

S 99.30 18 4(15) , 5(17) , 7(25) , 2(11) , CourseNana.COM

S 99.20 16 3(1) , 6(8) , 7(5) , CourseNana.COM

B 99.10 8 1(3) , 2(6) , 5(7) , CourseNana.COM

B 99.00 5 3(2) , 2(9) , CourseNana.COM

B 98.90 5 4(10) , 1(12) , CourseNana.COM

# L3 book view after processing the execution msg : side =Sell , order_id =1, price =99.2 , exec_qty =1. CourseNana.COM

# i.e. the order with order_id =1 is partially filled by a qty =1, so the Order ’s open_qty and the Level ’s CourseNana.COM

aggregated qty are both reduced by 1 CourseNana.COM

side price qty orders CourseNana.COM

S 99.30 18 4(15) , 5(17) , 7(25) , 2(11) , CourseNana.COM

S 99.20 15 2(1) , 6(8) , 7(5) , # <- the 1st order is partially executed CourseNana.COM

B 99.10 8 1(3) , 2(6) , 5(7) , CourseNana.COM

B 99.00 5 3(2) , 2(9) , CourseNana.COM

B 98.90 5 4(10) , 1(12) , CourseNana.COM

# L3 book view after processing the execution msg : side =Sell , order_id =1, price =99.2 , exec_qty =2 CourseNana.COM

# i.e. the order with order_id =1 is now fully filled , so the order should be deleted and Levels ’s aggregated CourseNana.COM

qty is reduced by 2 CourseNana.COM

side price qty orders CourseNana.COM

S 99.30 18 4(15) , 5(17) , 7(25) , 2(11) , CourseNana.COM

S 99.20 13 6(8) , 7(5) , # <- the 1st order is fully executed CourseNana.COM

B 99.10 8 1(3) , 2(6) , 5(7) , CourseNana.COM

B 99.00 5 3(2) , 2(9) , CourseNana.COM

B 98.90 5 4(10) , 1(12) , CourseNana.COM

  CourseNana.COM

3 Book Uncrossing CourseNana.COM

In real life, there could be packet drops during the delivering of the order book update messages. And one possible consequence is crossed book, as explained in the following example. CourseNana.COM

  CourseNana.COM

Without any packet drops CourseNana.COM

  CourseNana.COM

# book snapshot before the update CourseNana.COM

side price qty orders CourseNana.COM

S 99.30 18 4(15) , 5(17) , 7(25) , 2(11) , CourseNana.COM

S 99.20 7 7(5) , CourseNana.COM

B 99.10 8 1(3) , 2(6) , 5(7) , CourseNana.COM

B 99.00 5 3(2) , 2(9) , CourseNana.COM

B 98.90 5 4(10) , 1(12) , CourseNana.COM

# process msg action exec , side : S, order_id : 5, price : 99.2 , qty: 7 CourseNana.COM

side price qty orders CourseNana.COM

S 99.30 18 4(15) , 5(17) , 7(25) , 2(11) , CourseNana.COM

B 99.10 8 1(3) , 2(6) , 5(7) , CourseNana.COM

B 99.00 5 3(2) , 2(9) , CourseNana.COM

B 98.90 5 4(10) , 1(12) , CourseNana.COM

# process msg action add , side : B, order_id : 30, price : 99.2 , qty: 5 CourseNana.COM

side price qty orders CourseNana.COM

S 99.30 18 4(15) , 5(17) , 7(25) , 2(11) , CourseNana.COM

B 99.20 5 5(30) , CourseNana.COM

B 99.10 8 1(3) , 2(6) , 5(7) , CourseNana.COM

B 99.00 5 3(2) , 2(9) , CourseNana.COM

B 98.90 5 4(10) , 1(12) , CourseNana.COM

  CourseNana.COM

Without packet drops CourseNana.COM

  CourseNana.COM

# the second exec msg is lost , and thus the order with order_id =5 is retained in the book , i.e. stale order CourseNana.COM

# book snapshot before the update CourseNana.COM

side price qty orders CourseNana.COM

S 99.30 18 4(15) , 5(17) , 7(25) , 2(11) , CourseNana.COM

S 99.20 7 7(5) , CourseNana.COM

B 99.10 8 1(3) , 2(6) , 5(7) , CourseNana.COM

B 99.00 5 3(2) , 2(9) , CourseNana.COM

B 98.90 5 4(10) , 1(12) , CourseNana.COM

# the exec msg is lost , and the order with order_id =5 stays in the book CourseNana.COM

# process msg action add , side : B, order_id : 30, price : 99.2 , qty: 5 CourseNana.COM

side price qty orders CourseNana.COM

S 99.30 18 4(15) , 5(17) , 7(25) , 2(11) , CourseNana.COM

S 99.20 7 7(5) , # <- crossed book , bid side and ask side overlapped CourseNana.COM

B 99.20 5 5(30) , # <- crossed book CourseNana.COM

B 99.10 8 1(3) , 2(6) , 5(7) , CourseNana.COM

B 99.00 5 3(2) , 2(9) , CourseNana.COM

B 98.90 5 4(10) , 1(12) , CourseNana.COM

  CourseNana.COM

The crossed book could be troublesome, and we can use some simple logic to uncross the book. When a order is added to the buy side with price P, any sell orders with prices less or equal to P are stale orders and should be purged. Similarly when a order is added to the sell side with price P, any buy orders with prices greater or equal to P are considered to be stale orders. The rationale behind the assumption is that newer operations (i.e. Add operation) should better represent the state of the book at that moment. CourseNana.COM

  CourseNana.COM

Without packet drops and book uncrossing CourseNana.COM

# the second exec msg is lost , and thus the order with order_id =5 is retained in the book , i.e. stale order CourseNana.COM

# book snapshot before the update CourseNana.COM

side price qty orders CourseNana.COM

S 99.30 18 4(15) , 5(17) , 7(25) , 2(11) , CourseNana.COM

S 99.20 7 7(5) , CourseNana.COM

B 99.10 8 1(3) , 2(6) , 5(7) , CourseNana.COM

B 99.00 5 3(2) , 2(9) , CourseNana.COM

B 98.90 5 4(10) , 1(12) , CourseNana.COM

# the exec msg is lost , and the order with order_id =5 stays in the book CourseNana.COM

  CourseNana.COM

# process msg action add , side : B, order_id : 30, price : 99.2 , qty: 5. CourseNana.COM

# temporary crossed book after the Add operation CourseNana.COM

side price qty orders CourseNana.COM

S 99.30 18 4(15) , 5(17) , 7(25) , 2(11) , CourseNana.COM

S 99.20 7 7(5) , # <- crossed book , bid side and ask side overlapped CourseNana.COM

B 99.20 5 5(30) , # <- crossed book CourseNana.COM

B 99.10 8 1(3) , 2(6) , 5(7) , CourseNana.COM

B 99.00 5 3(2) , 2(9) , CourseNana.COM

B 98.90 5 4(10) , 1(12) , CourseNana.COM

# uncrossed book : the sell order with order_id =5 is purged as the price is equal to the price of the CourseNana.COM

# newly inserted order , i.e. price of order with order_id =5 CourseNana.COM

side price qty orders CourseNana.COM

S 99.30 18 4(15) , 5(17) , 7(25) , 2(11) , CourseNana.COM

B 99.20 5 5(30) , CourseNana.COM

B 99.10 8 1(3) , 2(6) , 5(7) , CourseNana.COM

B 99.00 5 3(2) , 2(9) , CourseNana.COM

B 98.90 5 4(10) , 1(12) , CourseNana.COM

  CourseNana.COM

3.1 Hint: Stale order discovery CourseNana.COM

L3Book::ForEachOrder can be used to iterate orders from inner levels to outer levels with early stop condition. And it can be used to find the stale orders. CourseNana.COM

  CourseNana.COM

3.2 Hint: Modify the container during iteration CourseNana.COM

For almost all containers, it’s not safe to modify the container during iteration. For example in the following code snippet, itr is invalidated immediately after the erase operation, and thus ++itr in the following for loop is a invalid operation. CourseNana.COM

std :: vector <int > vec; CourseNana.COM

// ... CourseNana.COM

for ( auto itr = vec . begin (); itr != vec . end (); ++ itr ) { CourseNana.COM

if ( some_condition ) { CourseNana.COM

vec . erase ( itr ); CourseNana.COM

} CourseNana.COM

} CourseNana.COM

Considering there’re multiple containers used in the L3Book, it would be very difficult to deal with the iterator invalidation rules by modifying the containers while iterating them. CourseNana.COM

CourseNana.COM

Get in Touch with Our Experts

WeChat WeChat
Whatsapp WhatsApp
Financial Engineering代写,C++代写,QF633代写,C++ for Financial Engineering代写,SMU代写,Financial Engineering代编,C++代编,QF633代编,C++ for Financial Engineering代编,SMU代编,Financial Engineering代考,C++代考,QF633代考,C++ for Financial Engineering代考,SMU代考,Financial Engineeringhelp,C++help,QF633help,C++ for Financial Engineeringhelp,SMUhelp,Financial Engineering作业代写,C++作业代写,QF633作业代写,C++ for Financial Engineering作业代写,SMU作业代写,Financial Engineering编程代写,C++编程代写,QF633编程代写,C++ for Financial Engineering编程代写,SMU编程代写,Financial Engineeringprogramming help,C++programming help,QF633programming help,C++ for Financial Engineeringprogramming help,SMUprogramming help,Financial Engineeringassignment help,C++assignment help,QF633assignment help,C++ for Financial Engineeringassignment help,SMUassignment help,Financial Engineeringsolution,C++solution,QF633solution,C++ for Financial Engineeringsolution,SMUsolution,