在2分钟内:Python分析股票
如果您打算投资股票,那么快速查看个别历史股票价格绝对是一个好主意。第一步是下载历史价格 - 但交易所网站不允许您有效地提取所有股票价格的数据。以下列出的是为NSE(印度国家证券交易所)上市的所有股票提供日常股票价格数据的主要步骤:
导入模块 - 熊猫,numpy,日期时间,csv,pandas_datareader
# Import modules
from datetime import datetime, timedelta
import pandas as pd
pd.core.common.is_list_like = pd.api.types.is_list_like #For solving import pandas_datareader issue
import numpy as np
import datetime
import csv
import requests
import pandas_datareader.data as web
import pandas_datareader as pdr
from pandas_datareader import data, wb
输入我们想要股票价格的开始和结束日期
# Input Start and End Date
start = datetime.datetime(2018,4,20)
end = datetime.datetime(2018,5,20)
提取NSE上列出的所有股票代码,例如20MICRONS,RELIANCE,BOSCHLTD
Today = datetime.datetime.now().strftime ("%Y-%m-%d")
# Import list of stock names from NSE website
with requests.Session() as s:
download = s.get('https://www.nseindia.com/products/content/sec_bhavdata_full.csv')
decoded_content = download.content.decode('utf-8')
cr = csv.reader(decoded_content.splitlines(), delimiter=',')
my_list = pd.DataFrame(list(cr))
#View the top rows
my_list.head()
# Clean the downloaded data
# Rename the headers
new_header = my_list.iloc[0] #grab the first row for the header
my_list = my_list[1:] #take the data less the header row
my_list = my_list.rename(columns = new_header)
# Get only the list of stock names - remove everything else
my_list['stock_name'] = "NSE/"+ my_list['SYMBOL']
stock_list = my_list['stock_name'].tolist()
stock_list = list(set(stock_list))
#View the top few stock names
stock_list[1:10]
输出:
['NSE/HARRMALAYA',
'NSE/ANSALHSG',
'NSE/DPL',
'NSE/STAN',
'NSE/VOLTAMP',
'NSE/SUMEETINDS',
'NSE/JKPAPER',
'NSE/KPRMILL',
'NSE/INEOSSTYRO']
运行for循环以下载每个股票代码的股票价格数据。
在for循环中,我们将使用web.Datareader()函数从quandl(https://www.quandl.com/)下载数据
# Create Empty Dataframe
stock_final = pd.DataFrame()
# Scrape the stock prices from quandl
# for i in range(len(stock_list))#Use this to scrape all the stocks
for i in range(10):
print(i)
try:
stock=[]
stock = web.DataReader(stock_list[i],"quandl",start,end)
stock['Name']=stock_list[i]
stock_final = pd.DataFrame.append(stock_final,stock)
except Exception: # Replace Exception with something more specific.
i = i+1
清理并附加数据,我们应该有以下分析数据集:
#View the top 10 rows of the downloaded data
stock_final.head()
我们可以使用这些数据来执行我们所需的所有分析和可视化
#Plot trend for a particular stock
#Subset for a particular stock
stock_final = stock_final[stock_final['Name']=='NSE/SUTLEJTEX']
#Generate a line plot
stock_final.plot(y='High')
plt.show()