基于参考点查找指数下一个高低点的代码问题排查
问题排查:标记价格突破后索引无法回退的K线高低点查找逻辑
需求说明
编写两个方法,根据指定交易对的标记价格,基于参考点查找下一组4根K线中的最高高点或最低低点。
示例说明
基础参考数组
Min Array - [2, 3.5, 5, 8.5, 9] Max Array - [20, 17, 9, 7, 5]
输入输出示例
输入:标记价格 3.6
预期输出 最低低点 - 3.5 最高高点 - 5
输入:标记价格 5.1
预期输出 最低低点 - 5 最高高点 - 7
输入:标记价格 3.4
预期输出 最低低点 - 2 最高高点 - 5
当前问题
方法仅能单向移动索引,例如标记价格跌破$2.50时,索引会指向后续值,但当价格再次突破$2.50时,索引无法回退到之前的位置。
说明:ohlcv用于获取交易对的开/高/低/收数据,
candle[2]为当前K线最高价,candle[3]为最低价。标记价格突破指数高点时,索引递减(引用前一组4根K线找下一个高点,数组按从高到低排序);突破指数低点时则相反。
原代码
def compare_hl(ohlcv, mark, symbol): minarray = [] maxarray = [] low_index = 0 high_index = 0 # Extract the lows of all candles lows = [candle[3] for candle in ohlcv] highs = [candle[2] for candle in ohlcv] # Initialize variables to track the starting index of the previous four bars while low_index < len(lows): # Extract the lows of the previous four bars previous_lows = lows[low_index:low_index + 4] # Check if the mark price falls below the lows of the previous four bars if mark < min(previous_lows): # Update the starting index for the next set of previous four bars low_index += 4 elif mark > min(previous_lows) and low_index < -4: print("\nMark Price above Minimum of Previous Lows. Reverting to Previous Set.") low_index -= 4 break else: # If mark price is not below the lows, move to the next set of four bars low_index += 4 minarray.append(min(previous_lows)) while high_index < len(highs): # Extract the lows of the previous four bars previous_highs = highs[high_index:high_index + 4] # Check if the mark price falls below the lows of the previous four bars if mark > max(previous_highs): # Update the starting index for the next set of previous four bars high_index += 4 elif mark < max(previous_highs) and high_index < -4: high_index -= 4 break else: # If mark price is not below the lows, move to the next set of four bars high_index += 4 maxarray.append(max(previous_highs)) return minarray, maxarray def find_reference(mark_price, minarray, maxarray, lo_index, hi_index, lowref, hiref, crossbelow, crossabove): if mark_price > maxarray[hi_index]: crossabove = True elif mark_price < minarray[lo_index]: crossbelow = True if mark_price > maxarray[hi_index]: print(colors.CYAN + "Mark Max: ", str(mark_price), str(maxarray[hi_index]) + colors.END) print(colors.CYAN + "Mark has crossed above previous 4-bar Max.\nMark: ", str(mark_price),"\nMin: ", str(minarray[lo_index]) + colors.END) hi_index -= 1 highreference = maxarray[hi_index] crossabove = True crossbelow = False print(colors.CYAN + "New High Reference Set: ", str(maxarray[hi_index]) + colors.END) elif hi_index <= -2 and mark_price < maxarray[hi_index + 1]: if not crossabove: print(colors.CYAN + "Mark has crossed below last high. Adjusting reference." + colors.END) hi_index += 1 highreference = maxarray[hi_index] print(colors.CYAN + "New High Reference Set: ", str(maxarray[hi_index]) + colors.END) crossabove = False crossbelow = True if lo_index >= -len(minarray) and mark_price < minarray[lo_index]: print(colors.CYAN + "Mark has crossed below Min: ", str(mark_price), str(minarray[lo_index]) + colors.END) lo_index -= 1 lowreference = minarray[lo_index] crossabove = False crossbelow = True print(colors.CYAN + "New Low Reference Set: ", str(minarray[lo_index]) + colors.END) elif lo_index <= -2 and mark_price > minarray[lo_index + 1]: if not crossbelow: print(colors.CYAN + "Mark has crossed above last min. Adjusting reference." + colors.END) lo_index += 1 lowreference = minarray[lo_index] print(colors.CYAN + "New Low Reference Set: ", str(minarray[lo_index]) + colors.END) crossabove = True crossbelow = False lowreference = minarray[lo_index] highreference = maxarray[hi_index] return lowreference, highreference, lo_index, hi_index
问题根源
compare_hl方法逻辑错误:- 生成参考数组时,
elif条件low_index < -4和high_index < -4永远无法触发(初始索引为0,循环中仅累加4,索引始终非负),导致无法回退。 - 循环逻辑混乱,生成的数组并非按4根K线分组的有效极值序列,而是无意义的遍历结果。
- 生成参考数组时,
find_reference方法缺陷:- 索引回退依赖
crossabove/crossbelow状态,但状态初始化和更新逻辑不严谨,反向突破时无法触发回退。 - 索引边界判断限制过严(如
hi_index <= -2),导致部分场景下无法正常调整索引。 - 强制覆盖
lowreference/highreference的逻辑会忽略正确赋值,导致结果错误。
- 索引回退依赖
修复方案
1. 修正compare_hl方法:生成正确的分组极值数组
按4根K线为一组,依次计算每组极值,反转数组以匹配"从高到低"的索引需求:
def compare_hl(ohlcv): minarray = [] maxarray = [] lows = [candle[3] for candle in ohlcv] highs = [candle[2] for candle in ohlcv] # 按4根K线分组计算极值 for i in range(0, len(lows), 4): group_lows = lows[i:i+4] group_highs = highs[i:i+4] if group_lows: minarray.append(min(group_lows)) maxarray.append(max(group_highs)) # 反转数组,匹配索引从高到低的需求 minarray.reverse() maxarray.reverse() return minarray, maxarray
2. 修正find_reference方法:实现双向索引调整
基于价格突破方向,支持索引双向移动,简化状态判断逻辑:
def find_reference(mark_price, minarray, maxarray, lo_index, hi_index): # 调整高点索引 if mark_price > maxarray[hi_index]: if hi_index > 0: hi_index -= 1 print(f"标记价格突破当前高点,更新高点参考为: {maxarray[hi_index]}") elif mark_price < maxarray[hi_index + 1] and hi_index < len(maxarray) - 1: hi_index += 1 print(f"标记价格回落至上一高点区间,更新高点参考为: {maxarray[hi_index]}") # 调整低点索引 if mark_price < minarray[lo_index]: if lo_index > 0: lo_index -= 1 print(f"标记价格突破当前低点,更新低点参考为: {minarray[lo_index]}") elif mark_price > minarray[lo_index + 1] and lo_index < len(minarray) - 1: lo_index += 1 print(f"标记价格回升至上一低点区间,更新低点参考为: {minarray[lo_index]}") return minarray[lo_index], maxarray[hi_index], lo_index, hi_index
3. 使用示例
# 模拟ohlcv数据(对应示例中的Min/Max Array) ohlcv = [ [0,0,20,2], [0,0,17,3.5], [0,0,9,5], [0,0,7,8.5], [0,0,5,9] ] minarray, maxarray = compare_hl(ohlcv) lo_idx, hi_idx = 0, 0 # 测试标记价格3.6 low_ref, high_ref, lo_idx, hi_idx = find_reference(3.6, minarray, maxarray, lo_idx, hi_idx) print(f"最低低点 - {low_ref}") # 输出3.5 print(f"最高高点 - {high_ref}") # 输出5 # 测试标记价格5.1 low_ref, high_ref, lo_idx, hi_idx = find_reference(5.1, minarray, maxarray, lo_idx, hi_idx) print(f"最低低点 - {low_ref}") # 输出5 print(f"最高高点 - {high_ref}") # 输出7 # 测试标记价格3.4 low_ref, high_ref, lo_idx, hi_idx = find_reference(3.4, minarray, maxarray, lo_idx, hi_idx) print(f"最低低点 - {low_ref}") # 输出2 print(f"最高高点 - {high_ref}") # 输出5
内容的提问来源于stack exchange,提问作者Zach S
相关产品推荐
相关产品推荐

