0% found this document useful (0 votes)
7 views2 pages

Trading Project

Uploaded by

Arpan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Trading Project

Uploaded by

Arpan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Exchange Functional Requirements:

SignUp → KYC Aadhaar → Balance Rs 0 ← Transfer Rs 10000(miniumum required to trade) to


the account created

Now you can trade:


I want to buy 10 share of TATA which is currently trading at Rs 100/share

Then our account will be debited by Rs 10 * 100 = 1000 :- 10000 – 1000 = Rs 9000 in balance
and we will be credit 10 stocks of TATA.

After a year the TATA stock goes to Rs 110/share, so I sell all 10 stocks of TATA:
My account gets credited with 10 * 110 = Rs 1110 :- 9000 + 1110 = Rs 10110 in balance in the
trading account. We can transfer 110 back to the bank account and keeping Rs10000 in the trading
account.

Finance Terms:

Orderbook: It shows

*) [BUYER] The price list of the buyers in descending order of the price. The top most of the list
willsignify the buyer which is willing to pay Rs X amount to buy that stock

*) [SELLER] The price list of sellers in descending order of the price. The below most of the list
will signify the seller which is willing to give away the stock for Rs X amount.

The price in the middle represents the price of the stock {It is the price at which the exchange has
happened between the highest price buyer and the lowest price seller few seconds ago.}. When the
highest price buyer and the lowest price seller becomes some Rs X.YZ amount then the exchange
will happen and the price of the stock after that exchange becomes Rs X.YZ

Therefore the price of the stock is the price at which last trade took place.

We have two types of ORDERS:- (*) limit order and (*) market order

What is a limit order ?

This is used by the REAL TRADER, not you. [You poor person]

*) puts a tuple containing { price at which he/she wants to buy the stocks for, the no.of quantity } .
This will be put in the orderbook. And whenever a match happens the credit of stocks and debit of
money happens.

*) Some trading fee associated with every order aka exchange that matches

e.g I want to buy AXIS bank stock and I want to buy limit order. So I provided the tuple{X,Y} to
zerodha and if the tuple can be immediately filled means someone is willing to sell at Rs X, then my
tuple will not be listed in the orderbook. But it is not the case then my tuple will be listed in the
order book of zerodha for that stock.

What is market order?

This is what we do as users{retailers} of Zerodha.

*) It is preceded by a getQuote request, where the the best price of the seller from the orderbook is
presented to the retailer. And the retailer buys it as per the GIVEN QUANTITY at that QUOTE
PRICE ONLY.

This is how market order eats out the [SELLER] orders in the orderbook.

*) Usually higher fee than limit order.

------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------

(*) More liquidity == Better exchange. MARKET MAKERS provide liquidity.

More Liquidity means decreasing the spread between the lowest price seller and highest price
buyer, so that AN EXCHANGE CAN HAPPEN.

It is so because anyone can make a limit order of {Rs 0.000000000000000000000000001, 100}


tuple in the order book or a limit order of {Rs 10000000000000000000000000000, 10} in the order
book which results in very high spread, which means MARKET WILL BE FUCKED UP>

Thats why MARKET MAKERS like Citadel provide liquidity by making a REALISTIC limit order
for that stock{ Citadel will make a limit order in the seller side and as well as the buyer side}. More
liquidity means more customers {RETAILER / REAL TRADER} will willing to create limit order
or market order.

------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------

Maker [SELLER of the stock] VS Taker [BUYER of the stock]

(*) The person whose orders exists on the orderbook is called a maker ( hence market maker )
(*) The preson who gets their order filled is called taker.
(*) Taker fee is usually higher than maker fee because they take away liquidity {SPREADNESS}
from the orderbook of the stock.

------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------

Where does the latency matter ?

EXCHANGE like NSE, BSE:

(*) Order placement time: How quickly the order is placed in the order book
(*) Time to fetch the order book : Realtime updates on the orderbook

TRADER:

(*) Order ACK time: The acknowledgement time (whether the order is fully placed in the orderbook
or it is failed or it is partially filled {means only few QUANTITY of the order tuple is placed in the
orderbook })
(*) Order cancel time: Cancellation time should be fast because the trader might want to cancel at
specific price.

------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------

How latency made better for the EXCHANGE:

(*) Faster serialization and deserialization of the data {orders}


(*) IN MEMORY orderbooks: For faster updates of the orderbook, the order books are stored in a
variable instead of a DB

How is latency made better for a TRADER:

(*) Being closer to the trading server.

------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------

Endpoints for the Application:

/order → POST: { side: {buy | sell}, price : number, qty: number, userId: string }.
/balance→ GET : gets the balance of the trading account for that given {userId}
/depth → GET : orderbook

/order will support LIMIT ORDER for now.

------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
Matching Algorithm

(*) Every stock will have its own OrderBook

OrderType { BID (BUY), ASK (SELL) }

Order {
order_type: OrderType
price: double
quantity: double
}

ExchangeInfo {

price: double,
quantity: quantity
}

OrderBook {

/*
Sorted in descending order. It means the 0th index will be the highest
and best bid [BEST BID PRICE]
*/
bids_vec: deque<Order>,

/*
Sorted in ascending order. It means the 0th index will be the lowest
and best ask [BEST ASK PRICE]
*/
asks_vec: deque<Order>,

add_bids_order(order: Order);
remove_bids_order(order: Order);

add_asks_order(order: Order);
remove_asks_order(order: Order);
}

exchanges_happened_vec: vector<ExchangeInfo>;
order_book_for_a_stock: OrderBook;

do_matching(given_order: Order)
{
/*
If the given_order is matched then save it in “exchanges_happened_vec”
and it it did not matched then save it in the “order_book_for_a_stock” either in
its “bids_vec” or “ask_vec”
*/

if given_order.order_type == OrderType::BID
&& given_order.price >= order_book_for_a_stock.asks_vec[0]:

{
no_of_quantities_filled = 0;
for (ask&: order_book_for_a_stock.asks_vec):

# BUG: given_order.quantity is getting ZEROED


if no_of_quantities_filled == given_order.quantity;
break;

if given_order.quantity <= ask.quantity + no_of_quantities_filled:

# Bidder (given_order) wants to buy 5 qty


# and Seller wants to sell 6 qty
# It means full quantity of given_order will be
# exchanged with the Seller
# OR we can say Buyer (given_order) will be fulfilled

left_qty_in_seller = ask.quantity – given_order.quantity;


no_of_quantities_filled += given_order.quantity;

exchanges_happened_vec
.push_back(ExchangeInfo{ask.price, given_order.quantity});

/* Information of element of asks_vec is changed*/


ask.quantity -= given_order.quantity;

given_order.quantity = 0;

elif given_order.quantity > ask.quantity + no_of_quantities_filled:

# Bidder(given_order) wants to buy 5 qty


# and Seller wants to sell 4 qty
# It means partial quantity of given_order will be
# exchanged with the Seller OR we can say Seller will be fulfilled

no_of_quantities_filled += ask.quantity;
given_order.quantity -= ask.quantity;

exchanges_happened_vec
.push_back(ExchangeInfo{ask.price, ask.quantity});

# Remove the seller from the ask section of orderbook


order_book_for_a_stock.remove_asks_order(ask);
forend;

elif given_order.order_type == OrderType::SELL


&& given_order.price <= order_book_for_a_stock.bids_vec[0]:

{
no_of_quantities_filled = 0;
for (bid&: order_book_for_a_stock.bids_vec):

if no_of_quantities_filled == given_order.quantity;
break;

if given_order.quantity <= bid.quantity + no_of_quantities_filled:

# Seller (given_order) wants to sell 5 qty


# and Buyer wants to buy 6 qty
# It means full quantity of given_order will be
# exchanged with the Buyer
# OR we can say Seller(given_order) will be fulfilled

left_qty_in_buyer = bid.quantity – given_order.quantity;


no_of_quantities_filled += given_order.quantity;

exchanges_happened_vec
.push_back(ExchangeInfo{bid.price, given_order.quantity});

/* Information of element of bids_vec is changed*/


bid.quantity -= given_order.quantity;

given_order.quantity = 0;

elif given_order.quantity > ask.quantity + no_of_quantities_filled:

# Seller(given_order) wants to sell 5 qty


# and Buyer wants to buy 4 qty
# It means partial quantity of given_order will be
# exchanged with the Buyer OR we can say Buyer will be fulfilled

no_of_quantities_filled += bid.quantity;
given_order.quantity -= bid.quantity;

exchanges_happened_vec
.push_back(ExchangeInfo{bid.price, bid.quantity});

# Remove the buyer from the buy section of orderbook


order_book_for_a_stock.remove_bids_order(bid);
forend;
}

else:
# If given_order is buyer then add it to the bids section of orderbook
# If given_order is seller then add it to the asks section of orderbook
if (given_order.order_type == OrderType::BID):
order_book_for_a_stock.add_bids_order(given_order);
elif (given_order.order_type == OrderType::ASK):
order_book_for_a_stock.add_asks_order(given_order);
}

------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------

You might also like