forked from learnatyourdesk/stockAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSEStock_plotLiveStock.py
75 lines (53 loc) · 1.77 KB
/
NSEStock_plotLiveStock.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import sys
import time
import random
import requests
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
import matplotlib.animation as animation
sp = []
sp_tmp = []
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
x_axis = []
c = 0
max_steps = 100
def fetch_NSE_stock_price(stock_code):
print ("Fetching details..")
stock_url = 'https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol='+str(stock_code)
headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36'}
response = requests.get(stock_url, headers=headers)
print(response)
soup = BeautifulSoup(response.text, 'html.parser')
data_array = soup.find(id='responseDiv').getText().strip().split(":")
for item in data_array:
if 'lastPrice' in item:
index = data_array.index(item)+1
latestPrice=data_array[index].split('"')[1]
return float(latestPrice.replace(',',''))
def plotGraph(i):
global sp_tmp
global c
#rnd = random.randint(5,10)
nse_sp = fetch_NSE_stock_price(stock_code)
sp_tmp.append(nse_sp)
c = c +1
if c <= max_steps:
x_axis.append(c)
if len(sp_tmp) > max_steps:
sp = sp_tmp[1:max_steps+1]
else:
sp = sp_tmp
sp_tmp = sp
print ("length = "+str(len(sp)))
print ("length = "+str(len(x_axis)))
print(sp)
print(x_axis)
ax1.clear()
plt.grid(which='major', linestyle='-', linewidth='0.5', color='red')
plt.grid(which='minor', linestyle=':', linewidth='0.5', color='black')
ax1.plot(x_axis,sp)
### MAIN ####
stock_code = sys.argv[1]
ani = animation.FuncAnimation(fig, plotGraph, interval=1000)
plt.show()