基于python + tushare 的股票盯盘脚本
目錄
- 基于python + tushare 實現(xiàn)股票盯盤
- tushare簡介
- 設計思路
- 核心代碼實現(xiàn)
- 加點細節(jié)
- 結(jié)束語
基于python + tushare 實現(xiàn)股票盯盤
tushare ID:499871
tushare簡介
Tushare是一個免費、開源的python財經(jīng)數(shù)據(jù)接口包。主要實現(xiàn)對股票等金融數(shù)據(jù)從數(shù)據(jù)采集、清洗加工 到 數(shù)據(jù)存儲的過程,能夠為金融分析人員提供快速、整潔、和多樣的便于分析的數(shù)據(jù),為他們在數(shù)據(jù)獲取方面極大地減輕工作量,使他們更加專注于策略和模型的研究與實現(xiàn)上。考慮到Python pandas包在金融量化分析中體現(xiàn)出的優(yōu)勢,Tushare返回的絕大部分的數(shù)據(jù)格式都是pandas DataFrame類型,非常便于用pandas/NumPy/Matplotlib進行數(shù)據(jù)分析和可視化。當然,如果您習慣了用Excel或者關(guān)系型數(shù)據(jù)庫做分析,您也可以通過Tushare的數(shù)據(jù)存儲功能,將數(shù)據(jù)全部保存到本地后進行分析。應一些用戶的請求,從0.2.5版本開始,Tushare同時兼容Python 2.x和Python 3.x,對部分代碼進行了重構(gòu),并優(yōu)化了一些算法,確保數(shù)據(jù)獲取的高效和穩(wěn)定。
設計思路
核心代碼實現(xiàn)
通過 tushare 庫的 get_realtime_quotes() 接口獲取股票實時數(shù)據(jù),根據(jù)昨日收盤價格和當前價格計算漲跌幅
def startCheck(stock_id):rate = 100while True:stock_info = tushare.get_realtime_quotes(stock_id)current_float_rate = (float(stock_info['price'][0]) - float(stock_info['pre_close'][0])) / float(stock_info['pre_close'][0])current_int_rate = int(current_float_rate*100)if current_int_rate != rate:rate = current_int_ratenotifyWx(stock_id, stock_info)time.sleep(2)else:subject = stock_info['name'][0] + "[" + stock_id + "]"content = "漲幅不明顯" + "\n"content = content + "當前漲跌幅:" + str(round(current_float_rate * 100, 2)) + "%"print(subject, "\n", content)time.sleep(10)加點細節(jié)
使用 chinese_calendar 庫,判斷當前是否處于開盤時間
def isOpenTime():datetime_now = datetime.datetime.now().date()openStatus = 0if is_workday(datetime_now):# 開始時間morning_start_time = datetime.datetime.strptime(str(datetime.datetime.now().date()) + '9:30', '%Y-%m-%d%H:%M')morning_end_time = datetime.datetime.strptime(str(datetime.datetime.now().date()) + '11:30', '%Y-%m-%d%H:%M')afternoon_start_time = datetime.datetime.strptime(str(datetime.datetime.now().date()) + '13:00', '%Y-%m-%d%H:%M')afternoon_end_time = datetime.datetime.strptime(str(datetime.datetime.now().date()) + '15:30', '%Y-%m-%d%H:%M')# 當前時間now_time = datetime.datetime.now()# 判斷當前時間是否在范圍內(nèi)if morning_start_time < now_time < morning_end_time or afternoon_start_time < now_time < afternoon_end_time :openStatus = 1return openStatus配合企業(yè)微信應用,做到實時推送通知,具體細節(jié)可查看企業(yè)微信的文檔
def notifyWx(stock_id, stock_info):subject = stock_info['name'][0] + "[" + stock_id + "]"content = ""content = content + "是否開盤:"if isOpenTime():content = content + "是"else:content = content + "否"content = content + "\n" + "昨日收盤價格:" + str(stock_info['pre_close'][0])content = content + "\n" + "開盤價格:" + str(stock_info['open'][0])content = content + "\n" + "當前價格:" + str(stock_info['price'][0])content = content + "\n"current_float_rate = (float(stock_info['price'][0])-float(stock_info['pre_close'][0]))/float(stock_info['pre_close'][0])content = content + "當前漲跌幅:" + str(round(current_float_rate * 100, 2)) + "%"sendTextData(subject, content)結(jié)束語
Tushare運行三年多以來,數(shù)據(jù)從廣度和深度都得到了提升,Pro版正是在此基礎上做了更大的改進。數(shù)據(jù)內(nèi)容將擴大到包含股票、基金、期貨、債券、外匯、行業(yè)大數(shù)據(jù),同時包括了數(shù)字貨幣行情等區(qū)塊鏈數(shù)據(jù)的全數(shù)據(jù)品類的金融大數(shù)據(jù)平臺,為各類金融投資和研究人員提供適用的數(shù)據(jù)和工具。
大家可以注冊Tushare Pro賬戶,獲得更豐富的接口支持,學生認證可以免費使用~
鏈接:https://tushare.pro/register?reg=499871
總結(jié)
以上是生活随笔為你收集整理的基于python + tushare 的股票盯盘脚本的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mac 配置支持 opengl 的 op
- 下一篇: 常用的相似性度量(距离总结)