我正在尝试在另一条线上绘制 ZigZag 线。我的基准线(价格)有最高点和最低点。我正在尝试用一条线连接顶部和底部点。这是一个例子:
这是我的数据集:
这就是我能走多远:
任何帮助表示赞赏。
编辑1:
这是我不工作的代码:
trend_index = target_out.columns.get_loc("trend_shifted")
close_index = target_out.columns.get_loc("price_close")
for i in range(1, len(target_out)):
if target_out.iloc[i, trend_index] == target_out.iloc[i-1, trend_index] and target_out.iloc[i-1, trend_index] is not None:
target_out.iloc[i, trend_index] = np.nan
target_out.iloc[i-1, trend_index] = target_out.iloc[i-1, close_index]
# multiple line plot
plt.plot( 'ind', 'price_close', data=target_out, marker='o', markerfacecolor='blue', markersize=12, color='skyblue', linewidth=4)
plt.plot( 'ind', 'trend_shifted', data=target_out, marker='', color='olive', linewidth=2)
plt.legend()
trend_shifted
列有ones
和zeros
。连续 1 和 0 的第一个元素实际上是之字形的顶部和底部点。其余的点并不重要。确定了最高点和最低点后,我需要画一条线,但由于价格和趋势的值相对不同,图表不平衡(我的意思是,价格就像 0.00001 但趋势是 0 和 1)
编辑2:
@rgk 的代码有效。这是输出: