-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst_load_local.py
70 lines (49 loc) · 1.53 KB
/
st_load_local.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
# import libraries
import pandas as pd
from nsetools import Nse
import os
# defining base location
base_location = 'data_sources/stock_data'
# create dir if not exist
try:
os.makedirs(base_location)
except:
pass
# define file name according to indian time zone (NSE stock)
date = pd.Timestamp.now(tz='Asia/Kolkata')
date_str = str(date.date()).replace("-","_")
print("extracting data for date ", date_str)
base_location_date= os.path.join(base_location,date_str)
print(base_location_date)
# defing NSE api object
nse = Nse() #stock API
# create today's date dir
try:
os.makedirs(base_location_date)
except:
pass
# load top 25 companies
companies = pd.read_csv('top_25NSE.csv')
companies_list = list(companies['Company Name'].values)
# function to pull data from nse API
def pull_stock_data(nse,companies_list):
stock =pd.DataFrame()
for i in companies_list:
q= nse.get_quote(i)
if q is None:
continue
else:
stock = stock.append(pd.DataFrame(q,index=[0]),ignore_index=True)
return stock
# csv file location and timestamp
date = pd.Timestamp.now(tz='Asia/Kolkata')
hour = str(date.hour)
min = str(date.minute)
csv_name = 'stock_data_'+hour+'_'+min+'.csv'
csv_location = os.path.join(base_location_date,csv_name)
print("\n saving data location is ",csv_location)
# converting data to csv and savnig to local
stock_data = pull_stock_data(nse,companies_list)
stock_data_csv = stock_data.to_csv()
stock_data.to_csv(csv_location)
print("data saved to disk \n")