版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_44580977/article/details/102475891
通常交易策略中会融入多个因子协同触发信号,在N日突破择时策略的基础上引入风险管理因子。该因子采用止盈止损机制来管理可能出现的风险,ATR指标则作为止盈止损的基准值。
ATR指标的实现
ATR指标的计算分为以下两步:
第一步为计算真实波幅TR。TR=MAX[(当日最高价-当日最低价)、abs(当日最高价-昨日收盘价)、abs(昨日收盘价-当日最低价)],指的是今日振幅、今日最高价与昨日收盘价之间的波幅、昨日收盘价与今日最低价之间的波幅,取这三者之中的最大值。 第二步对真实波幅TR进行N日移动平均计算。ATR=MA(TR,N),常用参数N为14日或21日。
代码语言:javascript复制df_stockload = web.DataReader("600410.SS", "yahoo", datetime.datetime(2018,10,1), datetime.datetime(2019,4,1))
df_stockload['art14'] = talib.ATR(df_stockload.High.values,df_stockload.Low.values,df_stockload.Close.values,timeperiod=14)#计算ATR14
df_stockload['art21'] = talib.ATR(df_stockload.High.values,df_stockload.Low.values,df_stockload.Close.values,timeperiod=21)#计算ATR21
得到的ATR指标序列化成图像如下:
止盈止损的实现
此处将ATR值作为止盈止损的基准值,止盈值设置为n_win倍的ATR值,止损值设置为n_loss倍的ATR值,n_win和n_loss分别为最大止盈系数和最大止损系数,此处设置最大止盈系数为2,最大止损系数为0.8,倾向于盈利值要大于亏损值。触发止盈止损条件为:
当n_winATR值 > (今日收盘价格 - 买入价格),触发止盈信号,卖出股票 当n_lossATR值 > (买入价格 - 今日收盘价格),触发止损信号,卖出股票
用根据风险因子,控制买入卖出
代码语言:javascript复制import pandas_datareader as web
# 融入风险管理
#股票数据获取及处理接口
import talib
def GetStockDatApi(stockName=None,stockTimeS=None,stockTimeE=None, N1=15, N2=5, n_loss=0.8, n_win=2):
stockdata = web.DataReader(stockName, "yahoo", stockTimeS, stockTimeE)
stockdata['N1_High'] = stockdata.High.rolling(window=N1).max()#计算最近N1个交易日最高价
stockdata['N1_High'] = stockdata.N1_High.shift(1)
expan_max = stockdata.Close.expanding().max()
stockdata['N1_High'].fillna(value=expan_max,inplace=True)#目前出现过的最大值填充前N1个nan
stockdata['N2_Low'] = stockdata.Low.rolling(window=N2).min()#计算最近N2个交易日最低价
stockdata['N2_Low'] = stockdata.N2_Low.shift(1)
expan_min = stockdata.Close.expanding().min()
stockdata['N2_Low'].fillna(value=expan_min,inplace=True)#目前出现过的最小值填充前N2个nan
stockdata['atr14'] = talib.ATR(stockdata.High.values, stockdata.Low.values, stockdata.Close.values, timeperiod=14) # 计算ATR14
buy_price = 0
for kl_index, today in stockdata.iterrows():
if today.Close > today.N1_High:
print('N_day_buy', kl_index, today.Close)
buy_price = today.Close
stockdata.loc[kl_index, 'signal'] = 1
#到达收盘价少于买入价后触发卖出
elif (buy_price != 0) and (buy_price > today.Close) and ((buy_price - today.Close) > n_loss * today.atr14):
print('stop_loss_n', kl_index, today.Close, buy_price)
stockdata.loc[kl_index, 'signal'] = 0
buy_price = 0
#到达收盘价多于买入价后触发卖出
elif (buy_price != 0) and (buy_price < today.Close) and ((today.Close - buy_price) > n_win * today.atr14):
print('stop_win_n', kl_index, today.Close, buy_price)
stockdata.loc[kl_index, 'signal'] = 0
buy_price = 0
elif today.Close < today.N2_Low:
print('N_day_sell', kl_index, today.Close, buy_price)
stockdata.loc[kl_index, 'signal'] = 0
buy_price = 0
else:
pass
stockdata['signal'].fillna(method = 'ffill',inplace = True)
stockdata['signal'] = stockdata.signal.shift(1)
stockdata['signal'].fillna(method = 'bfill',inplace = True)
return stockdata
总结
将ATR止盈止损策略作为风险管理因子与N日突破择时策略相融合,将多个策略作为因子作用在一起判断走势,可以从不同的维度保证交易的可靠性,从而避免策略的不确定性所带来的交易上的风险。