VWAP-会话/基于天

问题描述 投票:0回答:1

我有这个代码:

https://pastebin.com/n9QHrHN7

他应该做什么?: 这段代码应该只为我绘制 VWAP 指标,它确实做到了!

问题是什么?: VWAP 指标已经存在,但我想每天使用它。这意味着 VWAP 应始终在开盘时重新生成。 到目前为止,它看起来像这样: https://prnt.sc/7mK_6cfvumSQ (粉色 = VWAP)

因此,在这个示例中,您可以清楚地看到,VWAP 不是基于会话的,而是将所有内容视为会话,这是它不应该这样做的

有人可以帮助我吗

我尝试使用代码构建整个开关原理,例如: `def next(自身): 如果 self.data.datetime.date(0) != self.data.datetime.date(-1): self._vwap_period = 1

self.l.typprice[0] = ((self.data.close + self.data.high + self.data.low)/3) * self.data.volume`

粘贴到“vwap”类中,但不幸的是,这并没有达到我想要的结果。 我也尝试过用静态时间来完成整个事情,但这也不起作用

python stock trading indicator backtrader
1个回答
0
投票

你可以尝试这个实现

class VwapIntradayIndicator(bt.Indicator):
    """
    Volume Weighted Average Price (VWAP) indicator for intraday trading.
    """

    lines = ("vwap_intraday",)
    params = {"timezone": "America/New_York"}
    plotinfo = {"subplot": False}
    plotlines = {"vwap_intraday": {"color": "blue"}}

    def __init__(self) -> None:
        self.hlc = (self.data.high + self.data.low + self.data.close) / 3.0
        self.current_date: Optional[datetime.date] = None
        self.previous_date_index: int = -1

    def next(self) -> None:
        # Localize datetime to specified timezone and get current date
        current_date = (
            pytz.utc.localize(self.data.datetime.datetime()).astimezone(pytz.timezone(self.p.timezone)).date()
        )
        len_self: int = len(self)

        # Reset VWAP calculation if new day
        if self.current_date != current_date:
            self.current_date = current_date
            self.previous_date_index = len_self - 1

        # Retrieve volumes and typical prices since the beginning of the day
        volumes = self.data.volume.get(size=len_self - self.previous_date_index)
        hlc = self.hlc.get(size=len_self - self.previous_date_index)

        # Calculate intraday VWAP
        numerator = sum(hlc[i] * volumes[i] for i in range(len(volumes)))
        self.lines.vwap_intraday[0] = None if sum(volumes) == 0 else numerator / sum(volumes)

请随时查看此处的 GitHub 存储库:VWAP Backtrader

© www.soinside.com 2019 - 2024. All rights reserved.