loader2
Partner With Us NRI

Open Free Demat Account Online with ICICIDIRECT

How to download historical data using Breeze API Python SDK

27 Apr 2022 0 COMMENT

Of late, a plethora of trading APIs that facilitate automated trading have popped up in Indian market. One of the main benefits of using a Trading API to formulate your trading strategies is that it provides a way to test your strategy on historical data. Historical data helps traders do a scenario analysis for their strategies and backtest them, which is one of the most important steps in creating an automated trading system. Backtesting gives the traders a chance to optimise their portfolio and also indicate how their strategy might perform in the future, assuming market conditions do not change a lot.

Unfortunately, there is a dearth of free, high quality historical data from public sources. For many traders, it is not possible to subscribe to expensive services for historical data and this is a problem that ICICIdirect is trying to solve through its trading API - Breeze.

If you have a Python IDE already set up to run your code on and have created an App on Breeze API, then you are ready to go. Make sure your App is active (check status of your App in ‘View Apps’ section on Breeze API homepage) and generate a session key for it. You should have the following details ready before you start coding your strategy in a Python IDE:

  • API Key
  • Secret Key and,
  • Session Key

#Note: In our earlier blogs, we have shown how to create an App and get it’s API Key & Secret Key and how to generate a Session key for your App.

Refer to code given below to install / upgrade the latest set of libraries that essentially establish the connection to your demat portfolio and allow you to place orders via lines of code.

As an example to showcase the power of Trading APIs, consider the following lines of code:

# connecting with Breeze API

 

from breeze_connect import BreezeConnect

 

isec = BreezeConnect(api_key="your api key comes here")

isec.generate_session(api_secret="your secret key comes         

    here", session_token="your session key comes here")

 

# initializing input variables like expiry date and strike price

start_date = "2022-04-19T07:00:00.000Z"

end_date = "2022-04-19T18:00:00.000Z"

 

expiry = "2022-04-21T07:00:00.000Z"

time_interval = "1minute"

 

strike = 17000

 

# downloading historical data for put option contract

data1 = isec.get_historical_data(interval = time_interval,

                            from_date = start_date,

                            to_date = end_date,

                            stock_code = "NIFTY",

                            exchange_code = "NFO",

                            product_type = "options",

                            expiry_date = expiry,

                            right = "put",

                            strike_price = strike)

put_data = pd.DataFrame(data1["Success"])

 

# downloading historical data for Nifty

data2 = isec.get_historical_data(interval = time_interval,

                            from_date = start_date,

                            to_date = end_date,

                            stock_code = "NIFTY",

                            exchange_code = "NSE",

                            product_type = "cash")

stock_data = pd.DataFrame(data2["Success"])

 

# Transforming downloaded data into excel format

put_data.to_csv(index=False)

put_data.to_csv('Nifty put data.csv')

 

stock_data.to_csv(index=False)

stock_data.to_csv('Nifty index data.csv')

 

At this point you will have 2 Excel files - ‘Nifty put data’ and ‘Nifty index data’ saved in your system. Note that the excel dataset also contains Open Interest data as one of the columns - a feature unique to ICICI Trading API - Breeze.

 

Analysis ->

 

 

Nifty chart on 19-04-2022

 

From the above chart it can be seen that the Nifty started falling around 2:45 pm IST from 17,200 levels to 16,900 levels in just 30 minutes.

With a simple analysis using ‘sort function’ in excel, you will notice that the open interest (OI) spiked in the interval 2:00 pm - 2:30 pm. This can be read as an early indicator of increased activity by derivative traders in the market which inevitably reflects in the price of underlying i.e. Nifty50 index.

 

The OI started falling around 3:00 pm onwards around the time when Nifty started to crash. Also note that this is the time when volume started spiking meaning that the traders started executing their orders leading to the crash. Numerous put options were exercised to book profits by traders.

 

"Volume" refers to the number of contracts traded in a given period, and "open interest" denotes the number of contracts that are active, or not settled.

Conclusion:

You can do a lot more using the rich data (both historical and live) that Breeze API gives access to. Please visit the Breeze community forum and the Breeze SDK package to know more on Breeze API usage. The links for community page and the SDK package are as follows:

Disclaimer: ICICI Securities Ltd.( I-Sec). Registered office of I-Sec is at ICICI Securities Ltd. - ICICI Venture House, Appasaheb Marathe Marg, Prabhadevi, Mumbai - 400025, India, Tel No:- 022 - 2288 2460, 022 - 2288 2470. I-Sec is a Member of National Stock Exchange of India Ltd (Member Code:-07730) and BSE Ltd (Member Code :103) and having SEBI registration no. INZ000183631. Name of the Compliance officer (broking): Mr. Anoop Goyal, Contact number: 022-40701000, E-mail address: complianceofficer@icicisecurities.com. Investment in securities market are subject to market risks, read all the related documents carefully before investing. The contents herein above shall not be considered as an invitation or persuasion to trade or invest.  I-Sec and affiliates accept no liabilities for any loss or damage of any kind arising out of any actions taken in reliance thereon.