diff --git a/vnpy/app/algo_trading/template.py b/vnpy/app/algo_trading/template.py index 609641af..3b6620b7 100644 --- a/vnpy/app/algo_trading/template.py +++ b/vnpy/app/algo_trading/template.py @@ -1,6 +1,7 @@ from vnpy.trader.engine import BaseEngine from vnpy.trader.object import TickData, OrderData, TradeData from vnpy.trader.constant import OrderType, Offset, Direction +from vnpy.trader.utility import virtual class AlgoTemplate: @@ -63,22 +64,27 @@ class AlgoTemplate: """""" pass + @virtual def on_stop(self): """""" pass + @virtual def on_tick(self, tick: TickData): """""" pass + @virtual def on_order(self, order: OrderData): """""" pass + @virtual def on_trade(self, trade: TradeData): """""" pass + @virtual def on_timer(self): """""" pass diff --git a/vnpy/app/cta_strategy/template.py b/vnpy/app/cta_strategy/template.py index 20cf88d2..0fde32c0 100644 --- a/vnpy/app/cta_strategy/template.py +++ b/vnpy/app/cta_strategy/template.py @@ -4,6 +4,7 @@ from typing import Any, Callable from vnpy.trader.constant import Interval, Direction, Offset from vnpy.trader.object import BarData, TickData, OrderData, TradeData +from vnpy.trader.utility import virtual from .base import StopOrder, EngineType @@ -87,48 +88,56 @@ class CtaTemplate(ABC): } return strategy_data + @virtual def on_init(self): """ Callback when strategy is inited. """ pass + @virtual def on_start(self): """ Callback when strategy is started. """ pass + @virtual def on_stop(self): """ Callback when strategy is stopped. """ pass + @virtual def on_tick(self, tick: TickData): """ Callback of new tick data update. """ pass + @virtual def on_bar(self, bar: BarData): """ Callback of new bar data update. """ pass + @virtual def on_trade(self, trade: TradeData): """ Callback of new trade data update. """ pass + @virtual def on_order(self, order: OrderData): """ Callback of new order data update. """ pass + @virtual def on_stop_order(self, stop_order: StopOrder): """ Callback of stop order update. @@ -255,12 +264,14 @@ class CtaSignal(ABC): """""" self.signal_pos = 0 + @virtual def on_tick(self, tick: TickData): """ Callback of new tick data update. """ pass + @virtual def on_bar(self, bar: BarData): """ Callback of new bar data update. @@ -292,6 +303,7 @@ class TargetPosTemplate(CtaTemplate): ) self.variables.append("target_pos") + @virtual def on_tick(self, tick: TickData): """ Callback of new tick data update. @@ -301,12 +313,14 @@ class TargetPosTemplate(CtaTemplate): if self.trading: self.trade() + @virtual def on_bar(self, bar: BarData): """ Callback of new bar data update. """ self.last_bar = bar + @virtual def on_order(self, order: OrderData): """ Callback of new order data update. diff --git a/vnpy/trader/utility.py b/vnpy/trader/utility.py index ca788227..9951e1f1 100644 --- a/vnpy/trader/utility.py +++ b/vnpy/trader/utility.py @@ -385,3 +385,14 @@ class ArrayManager(object): if array: return up, down return up[-1], down[-1] + + +def virtual(func: "callable"): + """ + mark a function as "virtual", which means that this function can be override. + any base class should use this or @abstractmethod to decorate all functions + that can be (re)implemented by subclasses. + """ + return func + +