forked from beinghorizontal/Quantext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
historical_index_backup.py
114 lines (94 loc) · 4.13 KB
/
historical_index_backup.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import datetime
import numpy as np
import pandas as pd
import requests
from io import BytesIO
from zipfile import ZipFile
path = 'd:/demos/storage/hl'
dayback = 0 # To download historical data from current date after market hours put dayback = -1
date_list = []
symbol_list = []
adv_list = []
dec_list = []
open_list = []
high_list = []
low_list = []
close_list = []
qty_list = []
trades_list = []
newhs = []
newls = []
for i in range(0, 90): # You can increase this period if you want, replace 90 with higher number, but I haven't tested
try:
dayback += 1
dt_1 = datetime.date.today() - datetime.timedelta(dayback) # 1 for y'day and 0 for today
day_nse = dt_1.strftime("%d%m%y")
# raw_url = "https://nsearchives.nseindia.com/archives/equities/mkt/MA221223.csv"
# raw_url_hl= "https://nsearchives.nseindia.com/archives/equities/bhavcopy/pr/PR291223.zip"
url = "https://nsearchives.nseindia.com/archives/equities/mkt/MA" + day_nse + ".csv"
urlHL = "https://nsearchives.nseindia.com/archives/equities/bhavcopy/pr/PR" + day_nse + ".zip"
headers = {'Connection': 'keep-alive',
'authority': 'www.nseindia.com',
'path': '/api/marketStatus',
'Origin': 'https://www1.nseindia.com',
'Referer': 'https://www1.nseindia.com/products/content/equities/equities/archieve_eq.htm',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'}
req = requests.get(url, headers=headers)
response = requests.get(urlHL, headers=headers)
if req.status_code == 200 and response.status_code==200:
with ZipFile(BytesIO(response.content)) as zip_file:
# Extract all files in the zip to a temporary directory
zip_file.extractall(path)
csv_path = path + '/HL' + day_nse + '.csv'
dfHL = pd.read_csv(csv_path)
dfHL['numeric'] = np.where(dfHL['NEW_STATUS'] == 'H', 1, 0)
newh = dfHL['numeric'].sum()
newl = len(dfHL) - newh
list_date = req.text.split('\n')
df = pd.DataFrame(list_date)
nifty_val = df.iloc[9, 0]
nifty_list = nifty_val.split(',')
date_obj = pd.to_datetime(dt_1, format="%d%m-%Y")
adv_str = df.iloc[81, 0]
dec_str = df.iloc[82, 0]
adv_r = adv_str.split(",")[2]
dec_r = dec_str.split(",")[2]
adv = float(adv_r)
dec = float(dec_r)
sym = nifty_list[1]
o = float(nifty_list[3])
h = float(nifty_list[4])
l = float(nifty_list[5])
c = float(nifty_list[6])
qty = float(df.iloc[4, 0].split(",")[2])
trades = float(df.iloc[5, 0].split(",")[2])
# append to lists
newhs.append(newh)
newls.append(newl)
date_list.append(date_obj)
symbol_list.append(sym)
open_list.append(o)
high_list.append(h)
low_list.append(l)
close_list.append(c)
qty_list.append(qty)
trades_list.append(trades)
adv_list.append(int(adv))
dec_list.append(int(dec))
else:
print('connection error')
except Exception as e:
print(e)
df1 = pd.DataFrame({'date': date_list, 'symbol_list': symbol_list,
'adv_list': adv_list,
'dec_list': dec_list, 'open_list': open_list,
'high_list': high_list, 'low_list': low_list,
'close_list': close_list, 'qty_list': qty_list,
'trades_list': trades_list,'new_high':newhs, 'new_low':newls})
# df1 = df1.set_index('date')
df2 = df1.sort_index(ascending=False)
df2.to_csv('d:/demos/nifty_index_data.csv', index=False)
# df2 = df2.reset_index()
# df2.close_list.plot()