diff --git a/vnpy/app/cta_strategy/backtesting.py b/vnpy/app/cta_strategy/backtesting.py index aebc78ff..989d41c6 100644 --- a/vnpy/app/cta_strategy/backtesting.py +++ b/vnpy/app/cta_strategy/backtesting.py @@ -9,14 +9,14 @@ import matplotlib.pyplot as plt import seaborn as sns from pandas import DataFrame -from vnpy.trader.constant import Direction, Exchange, Interval, Status +from vnpy.trader.constant import (Direction, Offset, Exchange, + Interval, Status) from vnpy.trader.database import DbBarData, DbTickData from vnpy.trader.object import OrderData, TradeData from vnpy.trader.utility import round_to_pricetick from .base import ( BacktestingMode, - CtaOrderType, EngineType, ORDER_CTA2VT, STOPORDER_PREFIX, @@ -718,7 +718,8 @@ class BacktestingEngine: def send_order( self, strategy: CtaTemplate, - order_type: CtaOrderType, + direction: Direction, + offset: Offset, price: float, volume: float, stop: bool = False, @@ -726,17 +727,24 @@ class BacktestingEngine: """""" price = round_to_pricetick(price, self.pricetick) if stop: - return self.send_stop_order(order_type, price, volume) + return self.send_stop_order(direction, offset, price, volume) else: - return self.send_limit_order(order_type, price, volume) + return self.send_limit_order(direction, offset, price, volume) - def send_stop_order(self, order_type: CtaOrderType, price: float, volume: float): + def send_stop_order( + self, + direction: Direction, + offset: Offset, + price: float, + volume: float + ): """""" self.stop_order_count += 1 stop_order = StopOrder( vt_symbol=self.vt_symbol, - order_type=order_type, + direction=direction, + offset=offset, price=price, volume=volume, stop_orderid=f"{STOPORDER_PREFIX}.{self.stop_order_count}", @@ -748,11 +756,16 @@ class BacktestingEngine: return stop_order.stop_orderid - def send_limit_order(self, order_type: CtaOrderType, price: float, volume: float): + def send_limit_order( + self, + direction: Direction, + offset: Offset, + price: float, + volume: float + ): """""" self.limit_order_count += 1 - direction, offset = ORDER_CTA2VT[order_type] - + order = OrderData( symbol=self.symbol, exchange=self.exchange, diff --git a/vnpy/app/cta_strategy/base.py b/vnpy/app/cta_strategy/base.py index 4ee50b70..82fc03ce 100644 --- a/vnpy/app/cta_strategy/base.py +++ b/vnpy/app/cta_strategy/base.py @@ -11,13 +11,6 @@ APP_NAME = "CtaStrategy" STOPORDER_PREFIX = "STOP" -class CtaOrderType(Enum): - BUY = "买开" - SELL = "卖平" - SHORT = "卖开" - COVER = "买平" - - class StopOrderStatus(Enum): WAITING = "等待中" CANCELLED = "已撤销" @@ -37,7 +30,8 @@ class BacktestingMode(Enum): @dataclass class StopOrder: vt_symbol: str - order_type: CtaOrderType + direction: Direction + offset: Offset price: float volume: float stop_orderid: str @@ -45,18 +39,8 @@ class StopOrder: status: StopOrderStatus = StopOrderStatus.WAITING vt_orderid: str = "" - def __post_init__(self): - """""" - self.direction, self.offset = ORDER_CTA2VT[self.order_type] - EVENT_CTA_LOG = "eCtaLog" EVENT_CTA_STRATEGY = "eCtaStrategy" EVENT_CTA_STOPORDER = "eCtaStopOrder" -ORDER_CTA2VT = { - CtaOrderType.BUY: (Direction.LONG, Offset.OPEN), - CtaOrderType.SELL: (Direction.SHORT, Offset.CLOSE), - CtaOrderType.SHORT: (Direction.SHORT, Offset.OPEN), - CtaOrderType.COVER: (Direction.LONG, Offset.CLOSE), -} diff --git a/vnpy/app/cta_strategy/engine.py b/vnpy/app/cta_strategy/engine.py index 9ee850b9..d1ab5b5f 100644 --- a/vnpy/app/cta_strategy/engine.py +++ b/vnpy/app/cta_strategy/engine.py @@ -21,7 +21,7 @@ from vnpy.trader.object import ( BarData ) from vnpy.trader.event import EVENT_TICK, EVENT_ORDER, EVENT_TRADE -from vnpy.trader.constant import Direction, OrderType, Interval, Exchange +from vnpy.trader.constant import Direction, OrderType, Interval, Exchange, Offset from vnpy.trader.utility import load_json, save_json from vnpy.trader.database import DbTickData, DbBarData from vnpy.trader.setting import SETTINGS @@ -30,7 +30,6 @@ from .base import ( EVENT_CTA_LOG, EVENT_CTA_STRATEGY, EVENT_CTA_STOPORDER, - CtaOrderType, EngineType, StopOrder, StopOrderStatus, @@ -233,7 +232,11 @@ class CtaEngine(BaseEngine): price = tick.bid_price_5 vt_orderid = self.send_limit_order( - strategy, stop_order.order_type, price, stop_order.volume + strategy, + stop_order.direction, + stop_order.offset, + price, + stop_order.volume ) # Update stop order status if placed successfully @@ -256,7 +259,8 @@ class CtaEngine(BaseEngine): def send_limit_order( self, strategy: CtaTemplate, - order_type: CtaOrderType, + direction: Direction, + offset: Offset, price: float, volume: float, ): @@ -268,8 +272,6 @@ class CtaEngine(BaseEngine): self.write_log(f"委托失败,找不到合约:{strategy.vt_symbol}", strategy) return "" - direction, offset = ORDER_CTA2VT[order_type] - # Create request and send order. req = OrderRequest( symbol=contract.symbol, @@ -294,7 +296,8 @@ class CtaEngine(BaseEngine): def send_stop_order( self, strategy: CtaTemplate, - order_type: CtaOrderType, + direction: Direction, + offset: Offset, price: float, volume: float, ): @@ -306,7 +309,8 @@ class CtaEngine(BaseEngine): stop_order = StopOrder( vt_symbol=strategy.vt_symbol, - order_type=order_type, + direction=direction, + offset=offset, price=price, volume=volume, stop_orderid=stop_orderid, @@ -358,7 +362,8 @@ class CtaEngine(BaseEngine): def send_order( self, strategy: CtaTemplate, - order_type: CtaOrderType, + direction: Direction, + offset: Offset, price: float, volume: float, stop: bool, @@ -366,9 +371,9 @@ class CtaEngine(BaseEngine): """ """ if stop: - return self.send_stop_order(strategy, order_type, price, volume) + return self.send_stop_order(strategy, direction, offset, price, volume) else: - return self.send_limit_order(strategy, order_type, price, volume) + return self.send_limit_order(strategy, direction, offset, price, volume) def cancel_order(self, strategy: CtaTemplate, vt_orderid: str): """ diff --git a/vnpy/app/cta_strategy/template.py b/vnpy/app/cta_strategy/template.py index c3804286..eaa821b2 100644 --- a/vnpy/app/cta_strategy/template.py +++ b/vnpy/app/cta_strategy/template.py @@ -2,10 +2,10 @@ from abc import ABC from typing import Any, Callable -from vnpy.trader.constant import Interval, Status +from vnpy.trader.constant import Interval, Status, Direction, Offset from vnpy.trader.object import BarData, TickData, OrderData, TradeData -from .base import CtaOrderType, StopOrder, EngineType +from .base import StopOrder, EngineType class CtaTemplate(ABC): @@ -139,29 +139,30 @@ class CtaTemplate(ABC): """ Send buy order to open a long position. """ - return self.send_order(CtaOrderType.BUY, price, volume, stop) + return self.send_order(Direction.LONG, Offset.OPEN, price, volume, stop) def sell(self, price: float, volume: float, stop: bool = False): """ Send sell order to close a long position. """ - return self.send_order(CtaOrderType.SELL, price, volume, stop) + return self.send_order(Direction.SHORT, Offset.CLOSE, price, volume, stop) def short(self, price: float, volume: float, stop: bool = False): """ Send short order to open as short position. """ - return self.send_order(CtaOrderType.SHORT, price, volume, stop) + return self.send_order(Direction.SHORT, Offset.OPEN, price, volume, stop) def cover(self, price: float, volume: float, stop: bool = False): """ Send cover order to close a short position. """ - return self.send_order(CtaOrderType.COVER, price, volume, stop) + return self.send_order(Direction.LONG, Offset.CLOSE, price, volume, stop) def send_order( self, - order_type: CtaOrderType, + direction: Direction, + offset: Offset, price: float, volume: float, stop: bool = False, @@ -171,7 +172,7 @@ class CtaTemplate(ABC): """ if self.trading: vt_orderid = self.cta_engine.send_order( - self, order_type, price, volume, stop + self, direction, offset, price, volume, stop ) else: vt_orderid = ""