-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStockTicker_extracting.py
150 lines (71 loc) · 2.47 KB
/
StockTicker_extracting.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
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
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 3 17:17:48 2020
@author: Tejas
"""
#### Downloading Tickers from NASDAQ
import datapackage
import pandas as pd
data_url = 'https://datahub.io/core/nyse-other-listings/datapackage.json'
# to load Data Package into storage
package = datapackage.Package(data_url)
# to load only tabular data
resources = package.resources
for resource in resources:
if resource.tabular:
data = pd.read_csv(resource.descriptor['path'])
print (data)
data.to_csv('Nasdaq_company_ticker.csv', index = False)
########### Getting company name for given symbols for all stocks in NYSE
df = pd.read_csv('NYSE_stocktickers.csv')
tickersList = df['ticker'].to_list()
import yfinance as yf
tickerName = []
companyName = []
for i in range(len(tickersList)):
tick = yf.Ticker(tickersList[i])
try:
companyName.append(tick.info['longName'])
tickerName.append(tickersList[i])
except:
pass
if (i % 200 == 0):
print (i)
NYSE = pd.DataFrame()
NYSE['StockTickers'] = tickerName
NYSE['CompanyName'] = companyName
NYSE.to_csv('NYSE_stock_tickers.csv', index = False)
############### American Stock Exchange ##############
df2 = pd.read_csv('AMEX.csv')
df2.columns
import requests
from fuzzywuzzy import process
def getCompany(text):
r = requests.get('https://api.iextrading.com/1.0/ref-data/symbols')
stockList = r.json()
return process.extractOne(text, stockList)[0]
def get_symbol(symbol):
# url = "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query={}®ion=1&lang=en".format(symbol)
url = "http://finance.yahoo.com/d/quotes.csv?s=amd&f=nb4t8"
result = requests.get(url).json()
for x in result['ResultSet']['Result']:
if x['symbol'] == symbol:
return x['name']
tickersList = df2['<ticker>'].to_list()
tickerName = []
companyName = []
for i in range(len(tickersList)):
try:
companyName.append(getCompany(tickersList[i]))
tickerName.append(tickersList[i])
except:
pass
if (i % 200 == 0):
print (i)
AMEX = pd.DataFrame()
AMEX['StockTickers'] = tickerName
AMEX['CompanyName'] = companyName
pd.read_json(companyName)
AMEX.to_csv('AMEX_stock_tickers.csv', index = False)
getCompany('GOOG')['name']
getCompany('Alphabet')