-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_data.py
More file actions
168 lines (102 loc) · 4.1 KB
/
Copy pathget_data.py
File metadata and controls
168 lines (102 loc) · 4.1 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
from datetime import datetime as dt
from nsepy import get_history
import requests
import bs4 as bs
import pickle
import time
import os
style.use("ggplot")
# Web Scraping
# Fetching all 50 company's listed in NIFTY50 index tickers from wikipedia page
def get_tickers():
#getting the webpage of Nifty50 from wikipedia
data = requests.get("https://en.wikipedia.org/wiki/NIFTY_50")
soup = bs.BeautifulSoup(data.text, 'lxml')
#fetching the table structure from html which contains the company names and tickers data
table = soup.find("table",{"id":"constituents"})
tickers = []
for row in table.findAll('tr')[1:]:
ticker = row.findAll("td")[1].text
ticker = ticker.split(".")[0]
tickers.append(ticker)
#saving the fetched and cleaned tickers data to a pickle file for later use
with open("Nifty50_tickers.pickle",'wb') as f:
pickle.dump(tickers,f)
return tickers
# print(get_tickers())
# Fetch stock price data for each ticker listed in Nifty 50 index
start_date = dt(2000,1,1)
end_date = dt.now()
def get_stock_data():
#check if scarped ticker dat is already present
if os.path.exists('./Nifty50_tickers.pickle'):
tickers = pickle.load(open("Nifty50_tickers.pickle",'rb'))
else:
tickers = get_tickers()
# for each ticker in list tickers pull stock data from start date to end date
for i,ticker in enumerate(tickers) :
if os.path.exists("./data/"+ticker+".csv"):
print(ticker + " Already present")
else:
print("Getting stock data of "+ ticker)
time.sleep(30)
df = get_history(ticker,start_date,end_date)
df.to_csv("./data/"+ticker+".csv")
print(f"Remaining tickers {50-i+1}")
#optimize dataframe for less memory consumption and faster computation
def type_conversion(pds_obj):
optimized_obj = pds_obj.copy()
#selecting all columns with integer datatype
int_col = pds_obj.select_dtypes(include=['int64'])
if len(int_col.columns)==0:
pass
else:
new_int = int_col.apply(pd.to_numeric,downcast='unsigned')
optimized_obj[int_col.columns] = new_int
#selecting all columns with float datatype
float_col = pds_obj.select_dtypes('float')
if len(float_col.columns)==0:
pass
else:
new_float = float_col.apply(pd.to_numeric,downcast='float')
optimized_obj[float_col.columns] = new_float
#selecting all columns with object datatype
objects = pds_obj.select_dtypes('object').copy()
if len(objects.columns)==0:
pass
else:
obj =objects.astype('category')
optimized_obj[objects.columns]=obj
return optimized_obj
# function to combine closing price for all 50 tickers in one file
def combine_data():
tickers = pickle.load(open("./Nifty50_tickers.pickle",'rb'))
#initialize an empty dataframe which will hold all the combined data
all_data = pd.DataFrame()
# for each ticker read it's csv file, get the closing price and add it to the all_data dataframe
for i,ticker in enumerate(tickers):
df = pd.read_csv("./data/"+ticker+".csv")
df.rename(columns={'Close':ticker},inplace=True)
df['Date'] = pd.to_datetime(df['Date'])
df.set_index(['Date'],inplace=True)
df = df.drop(['Symbol','Series','Prev Close','Open','High','Low','Last','VWAP','Volume','Turnover','Trades','Deliverable Volume',"%Deliverble"],axis=1)
print(df)
if all_data.empty:
all_data=df
else:
all_data = all_data.join(df,how='outer')
all_data = type_conversion(all_data)
all_data.drop_duplicates(inplace=True)
if i % 5 ==0:
print(i)
print(all_data.head())
print(all_data.shape)
print(all_data.info(memory_usage='deep'))
print(len(set(all_data.index)))
all_data.to_csv("Nifty50_combined.csv")
get_stock_data()
combine_data()