當證券(期貨或者個股)RSI高於80或者低於20email自己作交易提醒
環境爲joinquant,語言爲PY
import numpy as np
import talib
import time
import jqdata
import talib
import smtplib
from email.mime.text import MIMEText
from email.header import Header
## 初始化函數,設定基準等等
def initialize(context):
#上海期貨交易所產品
'''AG8888.XSGE 白銀期貨指數 PB8888.XSGE 鉛期貨指數
AU8888.XSGE 黃金期貨指數 RB8888.XSGE 螺紋鋼期貨指數
AL8888.XSGE 鋁期貨指數 RU8888.XSGE 天然橡膠期貨指數
BU8888.XSGE 石油瀝青期貨指數 SN8888.XSGE 錫期貨指數
CU8888.XSGE 銅期貨指數 WR8888.XSGE 線材期貨指數
FU8888.XSGE 燃料油期貨指數 ZN8888.XSGE 鋅期貨指數
HC8888.XSGE 熱軋卷板期貨指數 NI8888.XSGE 鎳期貨指數
SP8888.XSGE 紙漿主力合約
'''
g.stocklist=['AG','RB','PB','AU','AL','RU','BU','SN','CU','WR','FU','ZN','HC','NI','SP']
g.stockkind='AG'
g.stockcode=str('AG9999.XSGE')
#g.stockkind='AU'
#g.stockkind='RB'
# 設定XX期貨作爲基準
set_benchmark('AG9999.XSGE')
# 開啓動態復權模式(真實價格)
set_option('use_real_price', True)
# 過濾掉order系列API產生的比error級別低的log
# log.set_level('order', 'error')
# 輸出內容到日誌 log.info()
log.info('初始函數開始運行且全局只運行一次')
# 開盤前運行
run_daily( before_market_open, time='09:00', reference_security='AG9999.XSGE')
## 開盤前運行函數
def before_market_open(context):
# 輸出運行時間
log.info('函數運行時間(before_market_open):'+str(context.current_dt.time()))
# 給微信發送消息(添加模擬交易,並綁定微信生效)
# send_message('美好的一天~')
stocklist=[]
## 獲取要操作的股票(g.爲全局變量)
mes=[]
for x in g.stocklist:
# 獲取期貨合約
y=get_dominant_future(x, date=context.previous_date)
price=get_price(y, end_date=context.previous_date, frequency='1d', fields=['close'], count=150,skip_paused=False, fq='pre')
#get_price(security, start_date=None, end_date=None, frequency='daily', fields=None, skip_paused=False, fq='pre', count=None, panel=True)
RSI=talib.RSI(price['close'], timeperiod=12)
if RSI[-1]>80:
message='超買'+x
print (message)
mes.append(message)
elif RSI[-1]<20:
message='超賣'+x
print (message)
mes.append(message)
else:
pass
if len(mes)>0:
print (mes)
fasong(str(mes),'*******@163.com')
fasong(str(mes),'*******[email protected]')
else:
print ('no message')
def fasong(message,targetmail):
'''發送郵箱'''
sender = '**********[email protected]' #企業263郵箱
'''接收郵箱'''
receiver = targetmail
'''發送郵件主題'''
subject = 'message stock'
'''發送郵箱服務器'''
smtpserver = 'smtp.163.com'
'''發送郵箱用戶/密碼'''
username = '*******@163.com'
password = '********'
'''中文需參數‘utf-8’ ,單字節字符不需要'''
msg = MIMEText(message,'plain','utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = '*****<**@163.com>'
msg['To'] = targetmail
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
print ("Email has been sent out!")