请教大佬,30日收盘价的平均值在vnpy怎么写?谢谢.
请教大佬,30日收盘价的平均值在vnpy怎么写?谢谢.
from typing import Callable
from vnpy.trader.object import BarData
from vnpy.trader.constant import Interval
from vnpy.trader.utility import BarGenerator
class BarGenerator_Daliy(BarGenerator):
"""日线合成器"""
def __init__(
self,
on_bar:Callable,
window:int = 0,
on_window_bar:Callable = None,
interval:Interval = Interval.DAILY
):
super().__init__(on_bar,window,on_window_bar,interval)
self.daliy_bar =None
def update_bar(self, bar:BarData) -> None:
if self.interval == Interval.MINUTE:
self.update_bar_minute_window(bar)
elif self.interval == Interval.HOUR:
self.update_bar_hour_window(bar)
elif self.interval == Interval.DAILY:
self.update_bar_daliy_window(bar)
def update_bar_daliy_window(self,bar:BarData)->None:
"""自定义的日线合成方法"""
if not self.daliy_bar:
dt = bar.datetime.replace(second=0, microsecond=0)
self.daliy_bar = BarData(
symbol=bar.symbol,
exchange=bar.exchange,
datetime=dt,
gateway_name=bar.gateway_name,
open_price=bar.open_price,
high_price=bar.high_price,
low_price=bar.low_price
)
# Otherwise, update high/low price into window bar
else:
self.daliy_bar.high_price = max(
self.daliy_bar.high_price,
bar.high_price
)
self.daliy_bar.low_price = min(
self.daliy_bar.low_price,
bar.low_price
)
# Update close price/volume into window bar
self.daliy_bar.close_price = bar.close_price
self.daliy_bar.volume += int(bar.volume)
self.daliy_bar.open_interest = bar.open_interest
if bar.datetime.hour == 14 and bar.datetime.minute == 59:
self.on_window_bar(self.daliy_bar)
self.daliy_bar = None
self.last_bar = bar
这是合成日k线代码,继承BarGenerator类,然后在on_window_bar函数下写self.am.ma(30)
搞定
老板,收盘价30平均值也是用self.am.ma(30),敢接触 不是很明白?
要是30日 最高价的平均值呢?
ArrayManager这个类里面有计算均线的指标,改为bar.high就是30日最高价了
老天 是不是我没有表述好,10天最高价的平均值 然后还有10天内最高价的标准差
你只要合成日线bar,这些东西节解决了
感谢🙏,搞定了.