掘金社区

海龟策略里的shift(-1)是不是有问题?Pinned highlighted

quantboy 发表在策略研究 2021-11-26 17:39:24

策略研究
173
1
0

def algo(context):
# 计算通道的数据:当日最低、最高、上一交易日收盘
# 注:由于talib库计算ATR的结果与公式求得的结果不符,所以这里利用公式计算ATR
# 如果是回测模式,当天的数据直接用history取到
if context.mode == 2:
data = history_n(symbol=context.symbol, frequency='1d',
count=context.n+1, end_time=context.now,
fields='close,high,low,bob', df=True) # 计算ATR
tr_list = []
for i in range(0, len(data)-1):
tr = max((data['high'].iloc[i] - data['low'].iloc[i]), data['close'].shift(-1).iloc[i] - data['high'].iloc[i],
data['close'].shift(-1).iloc[i] - data['low'].iloc[i])
tr_list.append(tr)
context.atr = int(np.floor(np.mean(tr_list)))
context.atr_half = int(np.floor(0.5 * context.atr))
# 计算唐奇安通道
context.don_open = np.max(data['high'].values[-context.n:])
context.don_close = np.min(data['low'].values[-context.n:])

评论: 1

Looks like your connection to 掘金量化社区 - 量化交易者的交流社区 was lost, please wait while we try to reconnect.