脚本的由来
- 写这个脚本主要原因是在平时的工作当中,需要处理工作问题很忙时,没时间看基金情况,通过设置涨幅度值,从而快速确认是否需要卖出或者买入基金。
运行脚本所需环境
- 需要安装dingtalkchatbot、requests 使用pip安装即可
- 需要钉钉群组种创建机器人用于发送信息
- 在写个定时任务周一到周五14:30执行一次
30 14 * * 1,2,3,4,5 python3 /jijin/demo.py
脚本详情
"""
定时推送购买基金涨幅情况,来却认是否赎购基金
基金实时信息:http://fundgz.1234567.com.cn/js/001186.js?rt=1463558676006
新浪基金实时信息: http://hq.sinajs.cn/list=s_sh000905
钉钉告警地址:https://oapi.dingtalk.com/robot/send?access_token=xxxxx
Data:2020年3月20日10:00:56
编写人: 赵路
1、新增大盘指数告警
"""
import requests,json,time
from dingtalkchatbot.chatbot import DingtalkChatbot
def GetTime():
t = time.time()
Timestamp = round(t * 1000)
return Timestamp
def MarketIndex(SharesID):
MarketList = []
Headers = {'content-type':'application/json','User-Agent': 'Apache-HttpClient/4.5.2 (Java/1.8.0_102)'}
sinajs = "http://hq.sinajs.cn/list=s_" + str(SharesID)
Msg = requests.get(sinajs, headers=Headers)
SharesMsg = Msg.text
if SharesID in SharesMsg:
GetSharesName = SharesMsg.split(",")[0].split("=")[1]
Gain = SharesMsg.split(",")[3]
GetMarketMsg = "大盘指数名称: " + GetSharesName + " 涨跌幅: " + str(Gain)
return GetMarketMsg
def GetFundData(fundId,GetTimestamp):
Headers = {'content-type':'application/json','User-Agent': 'Apache-HttpClient/4.5.2 (Java/1.8.0_102)'}
TTurl = "http://fundgz.1234567.com.cn/js/" + str(fundId) + ".js?rt=" + str(GetTimestamp)
r = requests.get(TTurl, headers=Headers)
GetMsg = r.text
if "fundcode" in GetMsg:
FundID = GetMsg.split(",")[0].split(":")[1]
FundName = GetMsg.split(",")[1].split(":")[1]
Gain = GetMsg.split(",")[5].split(":")[1]
currentTime = GetMsg.split(",")[6].split("}")[0].split("\"gztime\":")[1]
GetData = "基金名称:"+ FundName + " "+ "涨跌幅:" + Gain + " "+ "当前时间:" + currentTime
return GetData
def ExeIncreaseDegree(Fundmsg,Differencevalue,Increment):
if Fundmsg:
IncreaseDegree = float(Fundmsg.split(":")[2].split("当前时间")[0].replace("\"",''))
if IncreaseDegree > Increment or IncreaseDegree < Differencevalue:
return Fundmsg
def SendMsg(Title,Msg):
webhook = "https://oapi.dingtalk.com/robot/send?access_token=(自行申请)"
xiaoCCOD = DingtalkChatbot(webhook)
xiaoCCOD.send_text(msg="jijin_send---> " + Title + "\n" + Msg, is_at_all=True)
if __name__ == '__main__':
fundIdList = ["001549","001594"]
TitleList = ["每日基金涨跌详情","当日基金涨跌正负1.5详情","大盘指数详情"]
SharesIDList = ["sh000001","sh000300","sh000905"]
GetSharesMsg = []
GetTimestamp = GetTime()
MsgList = []
DifferencevalueincrementList = []
Differencevalue = -1.5
Increment = 1.5
for SharesID in SharesIDList:
GetMarketIndexMsg = MarketIndex(SharesID)
GetSharesMsg.append(GetMarketIndexMsg)
SendSharesMsg = str(GetSharesMsg).split("[")[1].split("]")[0].replace("None, ", "").replace(",", "\n")
SendMsg(TitleList[2], SendSharesMsg)
for fundId in fundIdList:
GetData = GetFundData(fundId,GetTimestamp)
Fundmsg = GetData
DifferencevalueincrementList.append(ExeIncreaseDegree(Fundmsg,Differencevalue,Increment))
MsgList.append(GetData)
GetNewData = str(DifferencevalueincrementList).split("[")[1].split("]")[0].replace("None, ","").replace(",","\n")
if None is not GetNewData:
SendMsg(TitleList[1], GetNewData)
DifferenceSet = [item for item in MsgList if item not in DifferencevalueincrementList]
GetDifferencevalueincrement = str(DifferenceSet).split("[")[1].split("]")[0].replace("None, ","").replace('\"','').replace(", ","\n")
SendMsg(TitleList[0], GetDifferencevalueincrement)
执行结果
![image.png]()