關注股市,發家致富
問題:一天天盯著股市多累,尤其上班,還不能暴露,股票軟體,紅紅綠綠,這麼明顯的列表頁面,一看就知道在摸魚。被領導發現飯碗就沒了
解決:搞個指令碼監聽一下自己關注的股票,一到價格就發個釘釘訊息推送,上班摸魚兩不誤。
一、配置centos的python版本
- centos7自帶了python2,但是安裝模組的時候各種報錯,基本上都是版本的原因,pip install 預設都下載了最新版本的模組包,但是最新版本的模組包都不支援python2,需要python3,不閒累的話,可以指定版本號進行模組的安裝。
- 兩個等號用於指定版本 pip install pandas==0.19.0 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com (帶上代理站點,安裝起來嗖嗖的)
- 還是安裝個python3比較靠譜,yum install python3
- 不同版本的模組安裝:
-
python2 -m pip install 模組名稱 python3 -m pip install 模組名稱
二、新增釘釘機器人
-
新增完機器人後,獲取webhook地址
三、編寫python指令碼
# 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")
# 間隔5s執行一次 time.sleep(5)
四、逐步更新優化