class類(lèi)在python中獲取金融數(shù)據(jù)的實(shí)例方法
我們搜集金融數(shù)據(jù),通常想要的是利用爬蟲(chóng)的方法。其實(shí)我們最近所學(xué)的class不僅可以進(jìn)行類(lèi)調(diào)用,在獲取數(shù)據(jù)方面同樣是可行的,很多小伙伴都比較關(guān)注理財(cái)方面的情況,對(duì)金融數(shù)據(jù)的需要也是比較多的。下面就class類(lèi)在python中獲取金融數(shù)據(jù)的方法為大家?guī)?lái)講解。
使用tushare獲取所有A股每日交易數(shù)據(jù),保存到本地?cái)?shù)據(jù)庫(kù),同時(shí)每日更新數(shù)據(jù)庫(kù);根據(jù)行情數(shù)據(jù)進(jìn)行可視化和簡(jiǎn)單的策略分析與回測(cè)。由于篇幅有限,本文著重介紹股票數(shù)據(jù)管理(下載、數(shù)據(jù)更新)的面向?qū)ο缶幊虘?yīng)用實(shí)例。
#導(dǎo)入需要用到的模塊
import numpy as np
import pandas as pd
from dateutil.parser import parse
from datetime import datetime,timedelta
#操作數(shù)據(jù)庫(kù)的第三方包,使用前先安裝pip install sqlalchemy
from sqlalchemy import create_engine
#tushare包設(shè)置
import tushare as ts
token='輸入你在tushare上獲得的token'
pro=ts.pro_api(token)
#使用python3自帶的sqlite數(shù)據(jù)庫(kù)
#本人創(chuàng)建的數(shù)據(jù)庫(kù)地址為c:\zjy\db_stock\
file='sqlite:///c:\\zjy\\db_stock\\'
#數(shù)據(jù)庫(kù)名稱(chēng)
db_name='stock_data.db'
engine = create_engine(file+db_name)
class Data(object):
def __init__(self,
start='20050101',
end='20191115',
table_name='daily_data'):
self.start=start
self.end=end
self.table_name=table_name
self.codes=self.get_code()
self.cals=self.get_cals()
#獲取股票代碼列表
def get_code(self):
codes = pro.stock_basic(list_status='L').ts_code.values
return codes
#獲取股票交易日歷
def get_cals(self):
#獲取交易日歷
cals=pro.trade_cal(exchange='')
cals=cals[cals.is_open==1].cal_date.values
return cals
#每日行情數(shù)據(jù)
def daily_data(self,code):
try:
df0=pro.daily(ts_code=code,start_date=self.start,
end_date=self.end)
df1=pro.adj_factor(ts_code=code,trade_date='')
#復(fù)權(quán)因子
df=pd.merge(df0,df1) #合并數(shù)據(jù)
except Exception as e:
print(code)
print(e)
return df
#保存數(shù)據(jù)到數(shù)據(jù)庫(kù)
def save_sql(self):
for code in self.codes:
data=self.daily_data(code)
data.to_sql(self.table_name,engine,
index=False,if_exists='append')
#獲取最新交易日期
def get_trade_date(self):
#獲取當(dāng)天日期時(shí)間
pass
#更新數(shù)據(jù)庫(kù)數(shù)據(jù)
def update_sql(self):
pass #代碼省略
#查詢(xún)數(shù)據(jù)庫(kù)信息
def info_sql(self):
代碼運(yùn)行
#假設(shè)你將上述代碼封裝成class Data #保存在'C:\zjy\db_stock'目錄下的down_data.py中 import sys #添加到當(dāng)前工作路徑 sys.path.append(r'C:\zjy\db_stock') #導(dǎo)入py文件中的Data類(lèi) from download_data import Data #實(shí)例類(lèi) data=Data() #data.save_sql() #只需運(yùn)行一次即可 data.update_sql() data.info_sql()
實(shí)例擴(kuò)展:
Python下,pandas_datareader模塊可以用于獲取研究數(shù)據(jù)。例子如下:
>>> from pandas_datareader.data import DataReader
>>>
>>> datas = DataReader(name='AAPL', data_source='yahoo', start='2018-01-01')
>>>
>>> type(datas)
<class 'pandas.core.frame.DataFrame'>
>>> datas
Open High Low Close Adj Close \
Date
2018-01-02 170.160004 172.300003 169.259995 172.259995 172.259995
2018-01-03 172.529999 174.550003 171.960007 172.229996 172.229996
2018-01-04 172.539993 173.470001 172.080002 173.029999 173.029999
2018-01-05 173.440002 175.369995 173.050003 175.000000 175.000000
2018-01-08 174.350006 175.610001 173.929993 174.350006 174.350006
2018-01-09 174.550003 175.059998 173.410004 174.330002 174.330002
2018-01-10 173.160004 174.300003 173.000000 174.289993 174.289993
2018-01-11 174.589996 175.490005 174.490005 175.279999 175.279999
2018-01-12 176.179993 177.360001 175.649994 177.089996 177.089996
Volume
Date
2018-01-02 25555900
2018-01-03 29517900
2018-01-04 22434600
2018-01-05 23660000
2018-01-08 20567800
2018-01-09 21584000
2018-01-10 23959900
2018-01-11 18667700
2018-01-12 25226000
>>>
>>> print(datas.to_csv())
Date,Open,High,Low,Close,Adj Close,Volume
2018-01-02,170.160004,172.300003,169.259995,172.259995,172.259995,25555900
2018-01-03,172.529999,174.550003,171.960007,172.229996,172.229996,29517900
2018-01-04,172.539993,173.470001,172.080002,173.029999,173.029999,22434600
2018-01-05,173.440002,175.369995,173.050003,175.0,175.0,23660000
2018-01-08,174.350006,175.610001,173.929993,174.350006,174.350006,20567800
2018-01-09,174.550003,175.059998,173.410004,174.330002,174.330002,21584000
2018-01-10,173.160004,174.300003,173.0,174.289993,174.289993,23959900
2018-01-11,174.589996,175.490005,174.490005,175.279999,175.279999,18667700
2018-01-12,176.179993,177.360001,175.649994,177.089996,177.089996,25226000
>>>
到此這篇關(guān)于class類(lèi)在python中獲取金融數(shù)據(jù)的實(shí)例方法的文章就介紹到這了,更多相關(guān)class類(lèi)怎樣在python中獲取金融數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python通過(guò)命令開(kāi)啟http.server服務(wù)器的方法
這篇文章主要給大家介紹了關(guān)于Python通過(guò)命令開(kāi)啟http.server服務(wù)器的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
Python實(shí)現(xiàn)個(gè)人微信號(hào)自動(dòng)監(jiān)控告警的示例
python、Matlab求定積分的實(shí)現(xiàn)
python利用urllib實(shí)現(xiàn)爬取京東網(wǎng)站商品圖片的爬蟲(chóng)實(shí)例
python使用BeautifulSoup分頁(yè)網(wǎng)頁(yè)中超鏈接的方法
python實(shí)現(xiàn)學(xué)生成績(jī)測(cè)評(píng)系統(tǒng)
教你利用Python玩轉(zhuǎn)histogram直方圖的五種方法
使用Python爬取最好大學(xué)網(wǎng)大學(xué)排名

