The tick data that you receive from Zerodha looks like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
[{'tradable': True, 'mode': 'full', 'instrument_token': 14242050, 'last_price': 117.0, 'last_traded_quantity': 75, 'average_traded_price': 149.61, 'volume_traded': 72978225, 'total_buy_quantity': 90450, 'total_sell_quantity': 449850, 'ohlc': {'open': 198.05, 'high': 212.9, 'low': 114.0, 'close': 214.15}, 'change': -45.36539808545412, 'last_trade_time': datetime.datetime(2025, 2, 21, 15, 29, 59), 'oi': 2414550, 'oi_day_high': 3735150, 'oi_day_low': 567450, 'exchange_timestamp': datetime.datetime(2025, 2, 21, 17, 26, 10), 'depth': {'buy': [{'quantity': 75, 'price': 117.25, 'orders': 1}, {'quantity': 300, 'price': 116.7, 'orders': 1}, {'quantity': 150, 'price': 116.6, 'orders': 1}, {'quantity': 600, 'price': 116.4, 'orders': 1}, {'quantity': 1050, 'price': 116.3, 'orders': 2}], 'sell': [{'quantity': 150, 'price': 117.85, 'orders': 1}, {'quantity': 150, 'price': 117.9, 'orders': 1}, {'quantity': 150, 'price': 117.95, 'orders': 1}, {'quantity': 1650, 'price': 118.0, 'orders': 2}, {'quantity': 300, 'price': 118.2, 'orders': 1}]}}, {'tradable': True, 'mode': 'full', 'instrument_token': 14243586, 'last_price': 61.05, 'last_traded_quantity': 300, 'average_traded_price': 85.93, 'volume_traded': 126344025, 'total_buy_quantity': 285825, 'total_sell_quantity': 1585650, 'ohlc': {'open': 118.0, 'high': 132.25, 'low': 59.65, 'close': 133.25}, 'change': -54.18386491557223, 'last_trade_time': datetime.datetime(2025, 2, 21, 15, 29, 59), 'oi': 10949400, 'oi_day_high': 13643325, 'oi_day_low': 7405125, 'exchange_timestamp': datetime.datetime(2025, 2, 21, 17, 26, 10), 'depth': {'buy': [{'quantity': 600, 'price': 59.85, 'orders': 1}, {'quantity': 525, 'price': 59.8, 'orders': 2}, {'quantity': 225, 'price': 59.75, 'orders': 2}, {'quantity': 1125, 'price': 59.6, 'orders': 2}, {'quantity': 75, 'price': 59.55, 'orders': 1}], 'sell': [{'quantity': 675, 'price': 61.05, 'orders': 1}, {'quantity': 75, 'price': 61.1, 'orders': 1}, {'quantity': 150, 'price': 61.2, 'orders': 1}, {'quantity': 1125, 'price': 61.4, 'orders': 3}, {'quantity': 600, 'price': 61.5, 'orders': 3}]}}] |
For the sake of convenience, I have included just two instruments (scrips), you can include upto 3,000 instruments.
You’ll notice that this tick data is basically a list of dictionaries. So, how would you go about retiriving the required data from this list?
I have provided here a solution.
The first step is to import datetime. If you don’t do this, it will throw an error.
The second step is to give a name to this list. I name it ticks.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import datetime ticks=[{'tradable': True, 'mode': 'full', 'instrument_token': 14242050, 'last_price': 117.0, 'last_traded_quantity': 75, 'average_traded_price': 149.61, 'volume_traded': 72978225, 'total_buy_quantity': 90450, 'total_sell_quantity': 449850, 'ohlc': {'open': 198.05, 'high': 212.9, 'low': 114.0, 'close': 214.15}, 'change': -45.36539808545412, 'last_trade_time': datetime.datetime(2025, 2, 21, 15, 29, 59), 'oi': 2414550, 'oi_day_high': 3735150, 'oi_day_low': 567450, 'exchange_timestamp': datetime.datetime(2025, 2, 21, 17, 26, 10), 'depth': {'buy': [{'quantity': 75, 'price': 117.25, 'orders': 1}, {'quantity': 300, 'price': 116.7, 'orders': 1}, {'quantity': 150, 'price': 116.6, 'orders': 1}, {'quantity': 600, 'price': 116.4, 'orders': 1}, {'quantity': 1050, 'price': 116.3, 'orders': 2}], 'sell': [{'quantity': 150, 'price': 117.85, 'orders': 1}, {'quantity': 150, 'price': 117.9, 'orders': 1}, {'quantity': 150, 'price': 117.95, 'orders': 1}, {'quantity': 1650, 'price': 118.0, 'orders': 2}, {'quantity': 300, 'price': 118.2, 'orders': 1}]}}, {'tradable': True, 'mode': 'full', 'instrument_token': 14243586, 'last_price': 61.05, 'last_traded_quantity': 300, 'average_traded_price': 85.93, 'volume_traded': 126344025, 'total_buy_quantity': 285825, 'total_sell_quantity': 1585650, 'ohlc': {'open': 118.0, 'high': 132.25, 'low': 59.65, 'close': 133.25}, 'change': -54.18386491557223, 'last_trade_time': datetime.datetime(2025, 2, 21, 15, 29, 59), 'oi': 10949400, 'oi_day_high': 13643325, 'oi_day_low': 7405125, 'exchange_timestamp': datetime.datetime(2025, 2, 21, 17, 26, 10), 'depth': {'buy': [{'quantity': 600, 'price': 59.85, 'orders': 1}, {'quantity': 525, 'price': 59.8, 'orders': 2}, {'quantity': 225, 'price': 59.75, 'orders': 2}, {'quantity': 1125, 'price': 59.6, 'orders': 2}, {'quantity': 75, 'price': 59.55, 'orders': 1}], 'sell': [{'quantity': 675, 'price': 61.05, 'orders': 1}, {'quantity': 75, 'price': 61.1, 'orders': 1}, {'quantity': 150, 'price': 61.2, 'orders': 1}, {'quantity': 1125, 'price': 61.4, 'orders': 3}, {'quantity': 600, 'price': 61.5, 'orders': 3}]}}] |
Now, we will iterate through this list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
for x in ticks: for y,z in x.items(): if z==14243586: for y,z in x.items(): if y=='last_price': print(z , ": This is the last price") if y=='total_sell_quantity': print(z) if y=='ohlc': for u,v in z.items(): if u=='low': print(v) if u=='close': print(v) if y=='depth': for u,v in z.items(): if u=='buy': print(v[0]['price'], ": This is the highest buy(bid) price") #best buy offer print(v[1]['price']) #2nd best buy offer print(v[2]['price']) #3rd best buy offer print(v[3]['price']) #4th best buy offer print(v[4]['price'], ": This is the lowest buy(bid) price") #4th best buy offer if u=='sell': print(v[0]['price'], ": This is the lowest sell(ask) price") #best sell offer print(v[1]['price']) #2nd best sell offer print(v[2]['price']) #3rd best sell offer print(v[3]['price']) #4th best sell offer print(v[4]['price'], ": This is the highest sell(ask) price") #4th best sell offer |
You see, it’s not very difficult. Just go through the code and you’ll understand it.