用vnpy写一个通道分析方法的策略 你刚刚写到 if self.fast_k > self.slow
时间: 2023-03-02 20:54:22 浏览: 185
如果您想编写一个使用 VnPy 库的通道分析策略,您可以这样做:
```
from vnpy.trader.object import (TickData, BarData, TradeData, OrderData,
ContractData, PositionData)
from vnpy.trader.constant import Direction, Offset, OrderType
from vnpy.trader.engine import BaseStrategy
class ChannelAnalysisStrategy(BaseStrategy):
""""""
def __init__(self, engine, setting):
""""""
super().__init__(engine, setting)
self.fast_window = 10
self.slow_window = 20
self.fast_k = 0
self.slow_k = 0
self.long_entry = 0
self.short_entry = 0
self.long_exit = 0
self.short_exit = 0
def on_init(self):
"""
Callback when strategy is inited.
"""
pass
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.
"""
pass
def on_bar(self, bar: BarData):
"""
Callback of new bar data update.
"""
self.fast_k = self.fast_k * (self.fast_window - 1) / self.fast_window + bar.close * 2 / self.fast_window
self.slow_k = self.slow_k * (self.slow_window - 1) / self.slow_window + bar.close * 2 / self.slow_window
if self.fast_k > self.slow_k:
if self.pos == 0:
self.buy(bar.close, 1)
elif self.fast_k < self.slow_k:
if self.pos == 1:
self.sell(bar.close, 1)
def on_trade(self, trade: TradeData):
"""
Callback of new trade data update.
"""
pass
def on_order(self, order: OrderData):
"""
Callback of new order data update.
"""
pass
```
这是一个简单的通道分析策略的框架,您可以在其中添加更多的策略逻辑和判断条件。
阅读全文