Indian Stock Market Live Price Chart Update in Python

Save as Filename.py in a Folder:


import requests
import pandas as pd
import datetime
import numpy as np
import time
api_key=open('alpha.txt','r').read()
def main():
    while (True):
        data=requests.get('https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&interval=1min&symbol=YESBANK.NS&apikey={}'.format(api_key))
        data=data.json()
        data=data['Time Series (1min)']
        df=pd.DataFrame(columns=['date','open','high','low','close','volume'])
        for d,p in data.items():
            date=datetime.datetime.strptime(d,'%Y-%m-%d %H:%M:%S')
            data_row=[date,float(p['1. open']),float(p['2. high']),float(p['3. low']),float(p['4. close']),int(p['5. volume'])]
            df.loc[-1,:]=data_row
            df.index=df.index+1
        data=df.sort_values('date')
        data.to_csv('data1.csv')
        print(data['close'][-1:])
        time.sleep(59)
main()


Save as Filename2.py in a Seperate Folder and do Follow Video to Run them and then go for the Code:


import matplotlib.pyplot as plt
import matplotlib.animation as graph
import time
import csv
plt.style.use('fivethirtyeight')

fig=plt.figure()
ax1=fig.add_subplot(1,1,1)

def chart(i):
    x_array=[]
    y_array=[]
    with open('folder/data1.csv','r') as csvfile:
        csvFileReader=csv.reader(csvfile)
        next(csvFileReader)
        for row in csvFileReader:
            if (len(row)>1):
                x_array.append(int(row[0]))
                y_array.append(float(row[4]))
    ax1.clear()
    ax1.plot(x_array,y_array)
livegraph=graph.FuncAnimation(fig,chart,interval=1)
plt.show()

Upstox WebSocket data in Python

Type in CMD and Hit Enter:pip install websocket-client==0.40.0


from upstox_api.api import *
api_key=open('api_key.txt','r').read()
access_token=open('access_token.txt','r').read()
u=Upstox(api_key,access_token)
u.get_master_contract('NSE_FO')

def event_handler_quote_update(message):
    print(message)
u.set_on_quote_update( event_handler_quote_update)
u.subscribe(u.get_instrument_by_symbol('NSE_FO','RELIANCE19JUNFUT'),LiveFeedType.Full)

u.start_websocket(True)