# encoding: utf-8 import requests import tushare, time import datetime # 消息内容,url地址 from pandas._libs import json # 机器人回调地址 webhook = 'https://oapi.dingtalk.com/robot/send?access_token=23e2b46e8b55a0573a0e92a26b427281f9aa85f387593c5e9f1b3c889c141148' # 开市时间、闭市时间 # 09:20 11:30 13:00 15:00 amStart = 920 amEnd = 1130 pmStart = 1300 pmEnd = 1500 # 默认当前状态为闭市 nowStatus = 'off' def dingtalk(msg): print ( '【钉钉】:' , msg) headers = { 'Content-Type' : 'application/json; charset=utf-8' } data = { 'msgtype' : 'text' , 'text' : { 'content' : msg}, 'at' : { 'atMobiles' : [], 'isAtAll' : False }} post_data = json.dumps(data) response = requests.post(webhook, headers = headers, data = post_data) return response.text def getrealtimedata(share): data = tushare.get_realtime_quotes(share.code) share.name = data.loc[ 0 ][ 0 ] share. open = float (data.loc[ 0 ][ 1 ]) share.price = float (data.loc[ 0 ][ 3 ]) share.high = float (data.loc[ 0 ][ 4 ]) share.low = float (data.loc[ 0 ][ 5 ]) share.describe = '股票【{}{}】,当前【{}】,今日最高【{}】,今日最低【{}】' . format (share.code, share.name, share.price, share.high, share.low) return share class Share(): def __init__( self , code, buy, sale): self .name = '' self . open = '' self .price = '' self .high = '' self .low = '' self .describe = '' self .code = code self .buy = buy self .sale = sale self .num = 0 def main(sharelist): for share in sharelist: stock = getrealtimedata(share) print (stock.describe) if stock.price = = 0 : continue if stock.price < = stock.buy: # 如果连续提示10次,就不再提示 if share.num > 5 : continue dingtalk( '【价格低于[{}]赶紧买入】{}' . format (share.buy, stock.describe)) print (share.num) share.num + = 1 print (share.num) elif stock.price > = stock.sale: dingtalk( '【价格高于[{}]赶紧卖出】{}' . format (share.sale, stock.describe)) else : print ( '静观其变……' ) # 重置计数器num def reset(sharelist): for share in sharelist: share.num = 0 # 股票编号 买价提示,,卖价提示 # 002273水晶光电 share1 = Share( "002273" , 13.4 , 15 ) # 600100同方股份 share2 = Share( "600100" , 5.92 , 6.03 ) # 000810创维数字 share3 = Share( "000810" , 7.66 , 7.77 ) sharelist = [share1, share2, share3] while True : now = datetime.datetime.now() dayOfWeek = now.isoweekday() print (dayOfWeek) # 工作日 if dayOfWeek < 6 : print ( "workday" ) nowTimeInt = int (now.strftime( "%H%M" )) print ( "当前时间:" , nowTimeInt) # 判断时间 if amStart < nowTimeInt < amEnd or pmStart < nowTimeInt < pmEnd: if nowStatus = = 'off' : reset(sharelist) nowStatus = 'on' # dingtalk('股票开市啦!!!!!') main(sharelist) else : if nowStatus = = 'on' : nowStatus = 'off' # dingtalk('股票闭市啦!!!!!') else : print ( "weekend" )<br data - filtered = "filtered" > # 间隔5s执行一次 time.sleep( 5 ) |