用vnpy写一个通道分析方法的策略
时间: 2023-03-02 08:54:22 浏览: 333
vnpyEngineFunction:如何使用vnpy制定策略
VnPy是一个用于开发自动化交易系统的开源平台,它提供了一个丰富的API,可以用于实现各种交易策略。
下面是一个简单的通道分析策略的代码示例:
```python
from vnpy.trader.object import BarData
from vnpy.trader.constant import Interval
from vnpy.app.cta_strategy import (
CtaTemplate,
StopOrder,
TickData,
BarData,
TradeData,
OrderData,
BarGenerator,
ArrayManager,
)
class ChannelStrategy(CtaTemplate):
""""""
author = "Your Name"
fast_window = 20
slow_window = 60
fast_k, fast_b = 0, 0
slow_k, slow_b = 0, 0
parameters = ["fast_window", "slow_window"]
variables = ["fast_k", "fast_b", "slow_k", "slow_b"]
def __init__(self, cta_engine, strategy_name, vt_symbol, setting):
""""""
super().__init__(cta_engine, strategy_name, vt_symbol, setting)
self.bg = BarGenerator(self.on_bar)
self.am = ArrayManager()
def on_init(self):
"""
Callback when strategy is inited.
"""
self.write_log("策略初始化")
self.load_bar(30)
def on_start(self):
"""
Callback when strategy is started.
"""
self.write_log("策略启动")
def on_stop(self):
"""
Callback when strategy is stopped.
"""
self.write_log("策略停止")
def on_tick(self, tick: TickData):
"""
Callback of new tick data update.
"""
self.bg.update_tick(tick)
def on_bar(self, bar: BarData):
"""
Callback of new bar data update.
"""
self.cancel_all()
self.am.update_bar(bar)
if not self.am.inited:
return
fast_array = self.am.sma(self.fast_window, array=True)
slow_array = self.am.sma(self.slow_window, array=True)
self.fast_k, self.fast_b = self.fit_line(fast_array)
self.slow_k, self.slow_b = self.fit_line(slow_array)
if self.fast_k > self.slow
阅读全文