The market depth of a financial instrument
The market depth of a financial instrument is a chronological list of data on buyers and sellers in the market. The buyers list is a list of prices and their respective quantities at which the buyers are willing to buy the instrument for. Similarly, the sellers list is a list of prices and their respective quantities at which the sellers are willing to sell the instrument for. If you are new to the concept of market depth, the explanation in the How it works… section of this recipe will give you more clarity.
Market depth helps in predicting where the price of an instrument is heading. It also helps to understand whether an order with a large quantity can change the price significantly or not. Market depth is dynamic in nature, meaning it changes constantly during the live trading hours. This recipe helps find out the market depth of a financial instrument in real time.
Getting ready
Make sure the broker_connection and instrument1 objects are available in your Python namespace. Refer to the Technical requirements section of this chapter to set up broker_connection. Refer to the Attributes of a financial instrument recipe of this chapter to set up instrument1.
How to do it…
Fetch and print the buy market depth and sell market depth of instrument1:
>>> buy_market_depth, sell_market_depth = \
broker_connection.get_market_depth(instrument1)
>>> print(f'Buy Market Depth:\n{buy_market_depth}')
>>> print(f'Sell Market Depth:\n{sell_market_depth}')
We get the following output (your output may differ):
Buy Market Depth:
orders price quantity
0 1 350.05 1
1 16 350.00 43294
2 5 349.95 1250
3 8 349.90 3134
4 5 349.85 1078
Sell Market Depth:
orders price quantity
0 1 350.10 25
1 7 350.15 1367
2 13 350.20 4654
3 13 350.25 2977
4 21 350.30 5798
How it works…
The get_market_depth() method of the BrokerConnectionZerodha class fetches the market depth for the given financial instrument. This method takes an object of the Instrument type as a parameter. We use instrument1 as the parameter here. The market depths are shown in separate tables for the buy side and the sell side.
The buy market depth is a table of five entries or bids, in descending order of price. Each entry indicates an available buyer in the market at that point in time, with the price being offered and the quantity available at that price.
The sell market depth is a table of five entries or bids, in ascending order of price. Each entry indicates an existing seller in the market at that point in time, with the price being offered and the quantity available at that price.
When a buyer and seller match, the order is executed at the exchange and the entries are removed from the buy- and sell-side tables.