loader2
NRI

Open Free Trading Account Online with ICICIDIRECT

Incur '0' Brokerage upto ₹500

How to create charts using Breeze API data?

You might have seen a few articles from our side highlighting the many use-cases of Breeze API. In our relentless pursuit of making you aware of the multi-fold ways of using the data, let us focus on charts as well. If you’re a trader or even a beginner in the field of markets, you must have seen technical charts of markets. These charts can range from simple line graphs to complex candle sticks with multiple technical indicators fitted in.

So in this article, we will be looking at the data visualisation aspect of data. We will first download data for an equity stock using Breeze API and then use the powerful open source libraries like pandas & matplotlib to create charts for the downloaded data. 

Task

We will download daily OHLC data for ITC stock from the start of 2022 and then create a chart for it.

#We have provided each block of code with a simple explanation for your understanding:

1st step is to establish connection with Breeze API using your API keys & Session Token. (Click on the words to know where to find these keys).

from breeze_connect import BreezeConnect
isec = BreezeConnect(api_key=" your api key ")
isec.generate_session(api_secret=" your api secret key",

session_token=" your session key ")

Next step is to download certain libraries that would add to the capability of your code to do multiple things like - do spreadsheet analysis or create charts

#install pandas & matplot lib library
pip install pandas

pip install matplotlib


#import pandas library in your code
import pandas as pd
#import charting library – matplot in your code
import matplotlib.pyplot as plt

Now we will download the daily data for ‘ITC’ share as listed on NSE from start of the year i.e. 01st Jan 2022 to today i.e. 20th May 2022

#downloading daily historical data using Breeze API

data = isec.get_historical_data(interval="1day",
                            from_date= "2022-01-01T07:00:00.000Z",
                            to_date= "2022-05-20T18:00:00.000Z",
                            stock_code="ITC",
                            exchange_code="NSE",
                            product_type="cash")

#storing the above data in a dataframe to operate on it
df = pd.DataFrame(data["Success"])

We will now adjust the data we just received from Breeze API into a workable format. We do this because the data received is in text (String) format and we have to extract numbers from the data.

# convert the 'Close' column to number format
df['new_close'] = df['close'].astype(float)
# convert the 'datetime' column to Date format
df['Date']= pd.to_datetime(df['datetime'])

The last step is to create charts for the 2 columns - date (in X axis) and daily closing price (in Y axis)

#charting the coloumns - 'datetime' and 'closing price' of dataframe
plt.figure(figsize=(13,3))   #size of the chart
plt.title('Closing Price of ITC') #Title of the chart
plt.xlabel('datetime') #label of x axis
plt.ylabel('closing price') #label of y axis

plt.plot(df['Date'], df['new_close'])  #command to plot the dataframe coloumns in a chart

The output should look something like this:

 

Happy learning & Trading!!

Please check out our essential links containing more such snippets:

Home Page: https://www.icicidirect.com/futures-and-options/api/breeze

Breeze Community: https://community.icicidirect.com/api

Python SDK Package: https://pypi.org/project/breeze-connect/

 

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.