Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Plotting a Swing chart over the generic OHLC chart

未关闭
#541 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
python

调研方向

从示例中的 mpf.plot 和 mpf.make_addplot 调用开始,然后使用所述的 daily 和 swing DataFrames 重现不匹配问题。跟踪 addplot 数据如何与 OHLC x 轴对齐;完成的标准是:稀疏的 swing 序列可以叠加到普通 OHLC 图表上,不出现 x/y 维度错误,并且符合所要求的 swing 可视化效果。

由索引模型根据 Issue 内容生成。

描述

question

I'm want to visualize a swings/price movement chart over a normal OHLC chart, e.g., like the attachment below.

image

I have written the following code:

def get_swing_dataframe(daily: DataFrame):
    last_high = -999999999
    last_low = abs(last_high)
    raw_dftb = []
    dip = True

    for row in daily.iterrows():
        close = row[1]["Close"]
        mark = False

        if dip:
            if close > last_high:
                dip = False
                mark = True
                last_high = close
        else:
            if close < last_low:
                dip = True
                mark = True
                last_low = close

        if mark:
            raw_dftb.append(row[1])

    df = pd.DataFrame(raw_dftb)
    df.index.name = "Date"
    return df


def main():
    daily = pd.read_csv("ES___CCB.csv", index_col=0, parse_dates=True)
    daily.index.name = "Date"
    swing = get_swing_dataframe(daily)
    mpf.plot(
        daily,
        style="binance",
        type="ohlc",
        addplot=[mpf.make_addplot(swing, type="line", color="g", panel=1)],
    )


if __name__ == "__main__":
    main()

The issue here is that the "Swing" dataframe does not have a data entry for every single date, it only marks where and when the price changes, spitting out the following error:

  File "D:\Andaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 1605, in plot
    lines = [*self._get_lines(*args, data=data, **kwargs)]
  File "D:\Andaconda3\lib\site-packages\matplotlib\axes\_base.py", line 315, in __call__
    yield from self._plot_args(this, kwargs)
  File "D:\Andaconda3\lib\site-packages\matplotlib\axes\_base.py", line 501, in _plot_args
    raise ValueError(f"x and y must have same first dimension, but "
ValueError: x and y must have same first dimension, but have shapes (6232,) and (23,)

How can I achieve this?
Recap: I want to visualize a swings/price movement chart over a normal OHLC chart, e.g., like the attachment below.

主要语言
Python
星标
4.4k
派生
677
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

matplotlib/mplfinance 的其他 Issue

查看 matplotlib/mplfinance 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。