金融数据之获取

1. 从不同数据来源获取——本地

1.1 常用:本地文件读取

with open('data/000001.csv', 'r') as f: #推荐这种方法;

    for i in range(5):

        print(f.readline())

1.2 Python CSV模块读取

import csv

csv_reader = csv.reader(open('data/000001.csv', 'r'))

1.3 常用:Pandas读取CSV

import pandas as pd

import numpy as np

data = pd.read_csv('data/000001.csv')

data = pd.read_csv('data/000001.csv', index_col=1,

                  parse_dates=True)                #解析日期

2. 从网络Open Source读取

2.1 Yahoo

from pandas_datareader import data as web

import fix_yahoo_finance as yf

yf.pdr_override()

#不翻墙或者网速较慢可能无法从Yahoo读取,属于正常现象;

data = web.get_data_yahoo('GS', start = '2010-01-01', end = '2012-01-01')

data= web.get_data_yahoo('MSFT', start = '2016-01-01', end = '2017-06-30')

data= web.get_data_yahoo('600030.ss', start = '2016-01-01', end = '2017-07-01')

2.2 Quandl

import quandl

data = quandl.get('EOD/KO',start_date='2016-1-1',end_date='2017-06-30')

2.3 常用:Tushare

获取结构化行情数据

import pandas as pd

import tushare as ts

hs300 = ts.get_k_data('hs300',start ='2015-01-01', end = '2017-06-30') #get_k_data()

hs300.set_index('date', inplace = True) #pd.set_index(),将df中的某一列设置成为索引;

hs300.head()

hs300['close'].plot(figsize=(10, 6))

hs300.close.plot(figsize=(10, 6)) #等价;

data = ts.get_k_data('600030') #默认前复权价格;

data2 = ts.get_k_data('600030', autype='hfq') #不复权

data3 = ts.get_k_data('600030', ktype = '5') #两个日期之间的前复权数据

data = ts.get_k_data(['600030','000001']) #tushare API接口不支持多股票数据;

df = ts.get_tick_data('600030',date='2017-07-28') #get_tick_data()

df.sort_indexs(inplace = True, ascending = False)

Tushare获得当前主流指数列表

df = ts.get_index()

Tushare获得股票的基本面信息

df = ts.get_stock_basics() #基本面数据

date = df.ix['600848']['timeToMarket']

date = df.loc['600030']['timeToMarket'] #ix即将要被取消;

获得所有股票基本面数据

data = ts.get_stock_basics() #get_stocl_basics()

data.ix['600030'][['pe','esp']] #pandas数据选择的复习;

data = ts.get_profit_data(2017,1) #获得公司盈利数据;

ts.get_latest_news(top=5,show_content=True) #显示最新5条新闻,并打印出新闻内容

top_list = ts.top_list('2017-08-11')

2.4 常用:优矿

可以通过优矿下载数据,并保存成CSV文件下载再导入;

# 获得某一只当天的tick数据;

data=DataAPI.MktTickRTIntraDayGet(securityID=u"000001.XSHE",startTime=u"09:30",endTime=u"15:00",field=u"",pandas="1")

data.to_csv('tick_data.csv')  #下载并保存数据以供分析;

# 获得某一些股票具体某一天的因子数据;

DataAPI.MktStockFactorsOneDayGet(tradeDate=u"20170630",secID=u"",ticker=u"000001,600030",field=u"ticker,ROE,PE,PB",pandas="1")

#某一只股票一段时间之内的因子数据;

DataAPI.MktStockFactorsDateRangeGet(secID=u"",ticker=u"000001",beginDate=u"20100101",endDate=u"20170616",field=u"tradeDate,ROE,PE,PB",pandas="1")

# 获取交易日历

start_date = '2014-01-01'

end_date = '2017-07-01'

trading_date = DataAPI.TradeCalGet(exchangeCD=u"XSHG",beginDate=u"",endDate=u"",field=u"",pandas="1")

# trading_date.to_csv('trading_date.csv')

# 筛选2013年到2016年每月最后一个交易日的日期

print(trading_date)

month_end = trading_date[(trading_date['isMonthEnd']==1) & (trading_date['calendarDate']>start_date) & (trading_date['calendarDate']

print month_end

# 获取某个日期以前上市的,正常交易或暂停交易的股票代码,格式为xxxxxx.XSHE或xxxxxx.XSHG

date = '2017-10-01'

stock_basics = DataAPI.EquGet(equTypeCD=u"A",secID=u"",ticker=u"",listStatusCD=u"",field=u"",pandas="1")

# stock_basics.to_csv('data/stock_basics.csv', encoding='GB18030')

valid_stocks = stock_basics.loc[(stock_basics['listDate']

# valid_stocks.to_csv('data/valid_stocks.csv', encoding='GB18030')

print valid_stocks

# 获取对应股票在对应日期的多个因子值

import pandas as pd

mkt_value = [DataAPI.MktStockFactorsOneDayGet(tradeDate=date,secID=valid_stocks,ticker=u"",field=["secID", 'LCAP','PE', 'REVS20', 'tradeDate'],pandas="1").set_index(['tradeDate', 'secID']) for date in month_end]

lcap = pd.concat(mkt_value, axis=0)

# lcap.to_csv('data/raw_factors.csv')

print lcap.head(5)

# 每个月最后一个交易日计算市值最小的20只股票

import pandas as pd

min_cap_pool = {date: lcap['LCAP'][date].sort_values(ascending=True).index[:20] for date in month_end}

min_cap_pool = pd.DataFrame(min_cap_pool)

print min_cap_pool

你可能感兴趣的:(金融数据之获取)