import pandas as pd
def rsi(n=10,close=close):
sma1=[]
sma2=[]
ut=[max(close[i]-close[i-1],0) for i in arange(1,len(close))]
dt=[max(close[i-1]-close[i],0) for i in arange(1,len(close))]
for i in arange(n,len(close)):
sma1.append(mean(ut[i-n:i]))
sma2.append(mean(dt[i-n:i]))
rs=[sma1[j]/sma2[j] for j in arange(len(sma1))]
rsi=[100*(rs[jj])/(1+rs[jj]) for jj in arange(len(rs))]
return rsi
hs300=get_price('IF8888.CCFX', start_date='2012-01-01', end_date='2017-10-31', frequency='1d', fields='close', skip_paused=False, fq='pre')
close1=hs300['close']
#IC8888.CCFX中证500 IH8888.CCFX上证50 IF8888.CCFX沪深300
n=7
rsitest=rsi(n=7,close=close1)
rate=(close1.diff(1)[1:]/list(close1[:-1]))[n:]
tempt=0
tempt11=[]
for i in arange(len(rsitest)):
if rsitest[i]>80:
tempt=-1
elif rsitest[i]<20:
tempt=1
tempt11.append(tempt)
tempt11=tempt11[1:]
tempt13=1.
cumrate=[]
for i in arange(len(rate)-1):
tempt13=tempt13*list(tempt11[:-1]*rate[1:]+1)[i]
cumrate.append(tempt13)
plt.plot(cumrate)
plt.title('传统RSI反转策略——按天')
<matplotlib.text.Text at 0x7fa8fa598690>
hs300=get_price('IF8888.CCFX', start_date='2012-01-01', end_date='2012-10-31', frequency='5m', fields='close', skip_paused=False, fq='pre')
close2=hs300['close']
#IC8888.CCFX中证500 IH8888.CCFX上证50 IF8888.CCFX沪深300
n=14
rsitest=rsi(n=14,close=close2)
rate=(close2.diff(1)[1:]/list(close2[:-1]))[n:]
tempt=0
tempt11=[]
for i in arange(len(rsitest)):
if rsitest[i]>80:
tempt=-1
elif rsitest[i]<20:
tempt=1
tempt11.append(tempt)
tempt11=tempt11[1:]
tempt13=1.
cumrate=[]
for i in arange(len(rate)-1):
tempt13=tempt13*list(tempt11[:-1]*rate[1:]+1)[i]
cumrate.append(tempt13)
plt.plot(cumrate)
plt.title('传统RSI反转策略——按五分钟')
<matplotlib.text.Text at 0x7fa8e4355f10>
###趋势策略,包含金叉死叉
hs300=get_price('IF8888.CCFX', start_date='2012-01-01', end_date='2017-10-31', frequency='1d', fields='close', skip_paused=False, fq='pre')
close1=hs300['close']
#IC8888.CCFX中证500 IH8888.CCFX上证50 IF8888.CCFX沪深300
n1=7
n2=14
rsitest1=rsi(n=7,close=close1)[n2-n1:]
rsitest2=rsi(n=14,close=close1)
rate=(close1.diff(1)[1:]/list(close1[:-1]))[n2:]
tempt=0
tempt11=[]
for i in arange(len(rsitest1)-1):
if rsitest1[i]<rsitest2[i] and rsitest1[i+1]>rsitest2[i+1]:#快线穿过慢线金叉
tempt=1
elif rsitest1[i]>rsitest2[i] and rsitest1[i+1]<rsitest2[i+1]:#慢线穿过快线死叉
tempt=-1
tempt11.append(tempt)
tempt13=1.
cumrate=[]
for i in arange(1,len(rate)-1):
if tempt11[i]*tempt11[i-1]<>1:##发生交易行为,算手续费
tempt13=tempt13*list(tempt11[:-1]*rate[1:]+1)[i]*(1-0.000023-0.0023)
else:
tempt13=tempt13*list(tempt11[:-1]*rate[1:]+1)[i]
cumrate.append(tempt13)
plt.plot(cumrate)
plt.title('传统RSI趋势策略——按天')
<matplotlib.text.Text at 0x7fa8e443db90>
###趋势策略,包含金叉死叉
hs300=get_price('IF8888.CCFX', start_date='2016-01-01', end_date='2016-10-31', frequency='5m', fields='close', skip_paused=False, fq='pre')
close1=hs300['close']
#IC8888.CCFX中证500 IH8888.CCFX上证50 IF8888.CCFX沪深300
n1=7
n2=14
rsitest3=rsi(n=7,close=close1)[n2-n1:]
rsitest4=rsi(n=14,close=close1)
rate=(close1.diff(1)[1:]/list(close1[:-1]))[n2:]
tempt=0
tempt11=[]
for i in arange(len(rsitest3)-1):
if rsitest3[i]<rsitest4[i] and rsitest3[i+1]>rsitest4[i+1]:#快线穿过慢线金叉
tempt=1
elif rsitest3[i]>rsitest4[i] and rsitest3[i+1]<rsitest4[i+1]:#慢线穿过快线死叉
tempt=-1
tempt11.append(tempt)
tempt13=1.
cumrate=[]
for i in arange(1,len(rate)-1):
if tempt11[i]*tempt11[i-1]<>1:##发生交易行为,算手续费,万二加上印花税
tempt13=tempt13*list(tempt11[:-1]*rate[1:]+1)[i]*(1-0.000023-0.0023)
else:
tempt13=tempt13*list(tempt11[:-1]*rate[1:]+1)[i]
cumrate.append(tempt13)
plt.plot(cumrate)
plt.title('传统RSI趋势策略——按五分钟')
<matplotlib.text.Text at 0x7fa8e46d64d0>
###新的RSI策略
##设置K线周期:长K线周期为15分钟,短K线周期为5分钟
hs300_long=get_price('IF8888.CCFX', start_date='2016-01-01', end_date='2016-10-31', frequency='15m', fields='close', skip_paused=False, fq='pre')
hs300_short=get_price('IF8888.CCFX', start_date='2016-01-01', end_date='2016-10-31', frequency='5m', fields='close', skip_paused=False, fq='pre')
close_long=hs300_long['close']
close_short=hs300_short['close']
###改进的rsi函数,显示index
def rsi2(n,close):
sma1=[]
sma2=[]
rsistore=pd.DataFrame()
ut=[max(close[i]-close[i-1],0) for i in arange(1,len(close))]
dt=[max(close[i-1]-close[i],0) for i in arange(1,len(close))]
for i in arange(n,len(close)):
sma1.append(mean(ut[i-n:i]))
sma2.append(mean(dt[i-n:i]))
rs=[sma1[j]/sma2[j] for j in arange(len(sma1))]
rsistore['rsi']=[100*(rs[jj])/(1+rs[jj]) for jj in arange(len(rs))]
rsistore.index=close.index[n:]
return rsistore
###新的RSI策略
##设置K线周期:长K线周期为15分钟,短K线周期为5分钟
hs300_long=get_price('IF8888.CCFX', start_date='2016-01-01', end_date='2016-10-31', frequency='15m', fields='close', skip_paused=False, fq='pre')
hs300_short=get_price('IF8888.CCFX', start_date='2016-01-01', end_date='2016-10-31', frequency='5m', fields='close', skip_paused=False, fq='pre')
close_long=hs300_long['close']
close_short=hs300_short['close']
n=11
rsi_long=rsi2(n,close=close_long)
rsi_short=rsi2(n,close=close_short)
##归到同一个时间点开始
rsi_short=rsi_short[24:]
S=80
L=50
rate=(close_short.diff(1)[1:]/list(close_short[:-1]))[n+24-1:]
close_short=close_short[(len(close_short)-len(rate)-1):]
tempt=0
tempt11=[]
uu=0
for i in arange(1,len(rsi_short)):
if tempt==0 and rsi_short['rsi'][i]>100-S and rsi_long['rsi'][i/3]>100-L:#短期rsi>100-S,长期rsi>100-L做空
tempt=-1
uu=i##建仓点
elif tempt==0 and rsi_short['rsi'][i]>S and rsi_long['rsi'][i/3]>L:#短期rsi>S,长期rsi>L做多
tempt=1
uu=i##建仓点
tempt11.append(tempt)
if tempt11[-1]<>0 and abs((close_short[i]-close_short[uu])/close_short[uu])>0.02:##止盈止损判断,建仓后
tempt=0
tempt13=1.
cumrate=[]
for i in arange(1,len(rate)-1):
if tempt11[i-1]==0 and tempt11[i]<>0 :##开仓
tempt13=tempt13*list(tempt11*rate[:-1]+1)[i]*(1-0.000023)
elif tempt11[i-1]<>0 and tempt11[i]==0:##平仓
tempt13=tempt13*list(tempt11*rate[:-1]+1)[i]*(1-0.0023)
elif tempt11[i-1]*tempt11[i]==-1:
tempt13=tempt13*list(tempt11*rate[:-1]+1)[i]*(1-0.0023-0.000023)
else:
tempt13=tempt13*list(tempt11*rate[1:]+1)[i]
cumrate.append(tempt13)
plt.plot(cumrate)
plt.title('新型RSI交易策略')
<matplotlib.text.Text at 0x7fa8e418b790>
本社区仅针对特定人员开放
查看需注册登录并通过风险意识测评
5秒后跳转登录页面...
移动端课程