[Mod]remove CtaOrderType enum
This commit is contained in:
parent
ab929f196a
commit
ec9a170ef3
@ -9,14 +9,14 @@ import matplotlib.pyplot as plt
|
|||||||
import seaborn as sns
|
import seaborn as sns
|
||||||
from pandas import DataFrame
|
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.database import DbBarData, DbTickData
|
||||||
from vnpy.trader.object import OrderData, TradeData
|
from vnpy.trader.object import OrderData, TradeData
|
||||||
from vnpy.trader.utility import round_to_pricetick
|
from vnpy.trader.utility import round_to_pricetick
|
||||||
|
|
||||||
from .base import (
|
from .base import (
|
||||||
BacktestingMode,
|
BacktestingMode,
|
||||||
CtaOrderType,
|
|
||||||
EngineType,
|
EngineType,
|
||||||
ORDER_CTA2VT,
|
ORDER_CTA2VT,
|
||||||
STOPORDER_PREFIX,
|
STOPORDER_PREFIX,
|
||||||
@ -718,7 +718,8 @@ class BacktestingEngine:
|
|||||||
def send_order(
|
def send_order(
|
||||||
self,
|
self,
|
||||||
strategy: CtaTemplate,
|
strategy: CtaTemplate,
|
||||||
order_type: CtaOrderType,
|
direction: Direction,
|
||||||
|
offset: Offset,
|
||||||
price: float,
|
price: float,
|
||||||
volume: float,
|
volume: float,
|
||||||
stop: bool = False,
|
stop: bool = False,
|
||||||
@ -726,17 +727,24 @@ class BacktestingEngine:
|
|||||||
""""""
|
""""""
|
||||||
price = round_to_pricetick(price, self.pricetick)
|
price = round_to_pricetick(price, self.pricetick)
|
||||||
if stop:
|
if stop:
|
||||||
return self.send_stop_order(order_type, price, volume)
|
return self.send_stop_order(direction, offset, price, volume)
|
||||||
else:
|
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
|
self.stop_order_count += 1
|
||||||
|
|
||||||
stop_order = StopOrder(
|
stop_order = StopOrder(
|
||||||
vt_symbol=self.vt_symbol,
|
vt_symbol=self.vt_symbol,
|
||||||
order_type=order_type,
|
direction=direction,
|
||||||
|
offset=offset,
|
||||||
price=price,
|
price=price,
|
||||||
volume=volume,
|
volume=volume,
|
||||||
stop_orderid=f"{STOPORDER_PREFIX}.{self.stop_order_count}",
|
stop_orderid=f"{STOPORDER_PREFIX}.{self.stop_order_count}",
|
||||||
@ -748,11 +756,16 @@ class BacktestingEngine:
|
|||||||
|
|
||||||
return stop_order.stop_orderid
|
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
|
self.limit_order_count += 1
|
||||||
direction, offset = ORDER_CTA2VT[order_type]
|
|
||||||
|
|
||||||
order = OrderData(
|
order = OrderData(
|
||||||
symbol=self.symbol,
|
symbol=self.symbol,
|
||||||
exchange=self.exchange,
|
exchange=self.exchange,
|
||||||
|
@ -11,13 +11,6 @@ APP_NAME = "CtaStrategy"
|
|||||||
STOPORDER_PREFIX = "STOP"
|
STOPORDER_PREFIX = "STOP"
|
||||||
|
|
||||||
|
|
||||||
class CtaOrderType(Enum):
|
|
||||||
BUY = "买开"
|
|
||||||
SELL = "卖平"
|
|
||||||
SHORT = "卖开"
|
|
||||||
COVER = "买平"
|
|
||||||
|
|
||||||
|
|
||||||
class StopOrderStatus(Enum):
|
class StopOrderStatus(Enum):
|
||||||
WAITING = "等待中"
|
WAITING = "等待中"
|
||||||
CANCELLED = "已撤销"
|
CANCELLED = "已撤销"
|
||||||
@ -37,7 +30,8 @@ class BacktestingMode(Enum):
|
|||||||
@dataclass
|
@dataclass
|
||||||
class StopOrder:
|
class StopOrder:
|
||||||
vt_symbol: str
|
vt_symbol: str
|
||||||
order_type: CtaOrderType
|
direction: Direction
|
||||||
|
offset: Offset
|
||||||
price: float
|
price: float
|
||||||
volume: float
|
volume: float
|
||||||
stop_orderid: str
|
stop_orderid: str
|
||||||
@ -45,18 +39,8 @@ class StopOrder:
|
|||||||
status: StopOrderStatus = StopOrderStatus.WAITING
|
status: StopOrderStatus = StopOrderStatus.WAITING
|
||||||
vt_orderid: str = ""
|
vt_orderid: str = ""
|
||||||
|
|
||||||
def __post_init__(self):
|
|
||||||
""""""
|
|
||||||
self.direction, self.offset = ORDER_CTA2VT[self.order_type]
|
|
||||||
|
|
||||||
|
|
||||||
EVENT_CTA_LOG = "eCtaLog"
|
EVENT_CTA_LOG = "eCtaLog"
|
||||||
EVENT_CTA_STRATEGY = "eCtaStrategy"
|
EVENT_CTA_STRATEGY = "eCtaStrategy"
|
||||||
EVENT_CTA_STOPORDER = "eCtaStopOrder"
|
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),
|
|
||||||
}
|
|
||||||
|
@ -21,7 +21,7 @@ from vnpy.trader.object import (
|
|||||||
BarData
|
BarData
|
||||||
)
|
)
|
||||||
from vnpy.trader.event import EVENT_TICK, EVENT_ORDER, EVENT_TRADE
|
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.utility import load_json, save_json
|
||||||
from vnpy.trader.database import DbTickData, DbBarData
|
from vnpy.trader.database import DbTickData, DbBarData
|
||||||
from vnpy.trader.setting import SETTINGS
|
from vnpy.trader.setting import SETTINGS
|
||||||
@ -30,7 +30,6 @@ from .base import (
|
|||||||
EVENT_CTA_LOG,
|
EVENT_CTA_LOG,
|
||||||
EVENT_CTA_STRATEGY,
|
EVENT_CTA_STRATEGY,
|
||||||
EVENT_CTA_STOPORDER,
|
EVENT_CTA_STOPORDER,
|
||||||
CtaOrderType,
|
|
||||||
EngineType,
|
EngineType,
|
||||||
StopOrder,
|
StopOrder,
|
||||||
StopOrderStatus,
|
StopOrderStatus,
|
||||||
@ -233,7 +232,11 @@ class CtaEngine(BaseEngine):
|
|||||||
price = tick.bid_price_5
|
price = tick.bid_price_5
|
||||||
|
|
||||||
vt_orderid = self.send_limit_order(
|
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
|
# Update stop order status if placed successfully
|
||||||
@ -256,7 +259,8 @@ class CtaEngine(BaseEngine):
|
|||||||
def send_limit_order(
|
def send_limit_order(
|
||||||
self,
|
self,
|
||||||
strategy: CtaTemplate,
|
strategy: CtaTemplate,
|
||||||
order_type: CtaOrderType,
|
direction: Direction,
|
||||||
|
offset: Offset,
|
||||||
price: float,
|
price: float,
|
||||||
volume: float,
|
volume: float,
|
||||||
):
|
):
|
||||||
@ -268,8 +272,6 @@ class CtaEngine(BaseEngine):
|
|||||||
self.write_log(f"委托失败,找不到合约:{strategy.vt_symbol}", strategy)
|
self.write_log(f"委托失败,找不到合约:{strategy.vt_symbol}", strategy)
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
direction, offset = ORDER_CTA2VT[order_type]
|
|
||||||
|
|
||||||
# Create request and send order.
|
# Create request and send order.
|
||||||
req = OrderRequest(
|
req = OrderRequest(
|
||||||
symbol=contract.symbol,
|
symbol=contract.symbol,
|
||||||
@ -294,7 +296,8 @@ class CtaEngine(BaseEngine):
|
|||||||
def send_stop_order(
|
def send_stop_order(
|
||||||
self,
|
self,
|
||||||
strategy: CtaTemplate,
|
strategy: CtaTemplate,
|
||||||
order_type: CtaOrderType,
|
direction: Direction,
|
||||||
|
offset: Offset,
|
||||||
price: float,
|
price: float,
|
||||||
volume: float,
|
volume: float,
|
||||||
):
|
):
|
||||||
@ -306,7 +309,8 @@ class CtaEngine(BaseEngine):
|
|||||||
|
|
||||||
stop_order = StopOrder(
|
stop_order = StopOrder(
|
||||||
vt_symbol=strategy.vt_symbol,
|
vt_symbol=strategy.vt_symbol,
|
||||||
order_type=order_type,
|
direction=direction,
|
||||||
|
offset=offset,
|
||||||
price=price,
|
price=price,
|
||||||
volume=volume,
|
volume=volume,
|
||||||
stop_orderid=stop_orderid,
|
stop_orderid=stop_orderid,
|
||||||
@ -358,7 +362,8 @@ class CtaEngine(BaseEngine):
|
|||||||
def send_order(
|
def send_order(
|
||||||
self,
|
self,
|
||||||
strategy: CtaTemplate,
|
strategy: CtaTemplate,
|
||||||
order_type: CtaOrderType,
|
direction: Direction,
|
||||||
|
offset: Offset,
|
||||||
price: float,
|
price: float,
|
||||||
volume: float,
|
volume: float,
|
||||||
stop: bool,
|
stop: bool,
|
||||||
@ -366,9 +371,9 @@ class CtaEngine(BaseEngine):
|
|||||||
"""
|
"""
|
||||||
"""
|
"""
|
||||||
if stop:
|
if stop:
|
||||||
return self.send_stop_order(strategy, order_type, price, volume)
|
return self.send_stop_order(strategy, direction, offset, price, volume)
|
||||||
else:
|
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):
|
def cancel_order(self, strategy: CtaTemplate, vt_orderid: str):
|
||||||
"""
|
"""
|
||||||
|
@ -2,10 +2,10 @@
|
|||||||
from abc import ABC
|
from abc import ABC
|
||||||
from typing import Any, Callable
|
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 vnpy.trader.object import BarData, TickData, OrderData, TradeData
|
||||||
|
|
||||||
from .base import CtaOrderType, StopOrder, EngineType
|
from .base import StopOrder, EngineType
|
||||||
|
|
||||||
|
|
||||||
class CtaTemplate(ABC):
|
class CtaTemplate(ABC):
|
||||||
@ -139,29 +139,30 @@ class CtaTemplate(ABC):
|
|||||||
"""
|
"""
|
||||||
Send buy order to open a long position.
|
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):
|
def sell(self, price: float, volume: float, stop: bool = False):
|
||||||
"""
|
"""
|
||||||
Send sell order to close a long position.
|
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):
|
def short(self, price: float, volume: float, stop: bool = False):
|
||||||
"""
|
"""
|
||||||
Send short order to open as short position.
|
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):
|
def cover(self, price: float, volume: float, stop: bool = False):
|
||||||
"""
|
"""
|
||||||
Send cover order to close a short position.
|
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(
|
def send_order(
|
||||||
self,
|
self,
|
||||||
order_type: CtaOrderType,
|
direction: Direction,
|
||||||
|
offset: Offset,
|
||||||
price: float,
|
price: float,
|
||||||
volume: float,
|
volume: float,
|
||||||
stop: bool = False,
|
stop: bool = False,
|
||||||
@ -171,7 +172,7 @@ class CtaTemplate(ABC):
|
|||||||
"""
|
"""
|
||||||
if self.trading:
|
if self.trading:
|
||||||
vt_orderid = self.cta_engine.send_order(
|
vt_orderid = self.cta_engine.send_order(
|
||||||
self, order_type, price, volume, stop
|
self, direction, offset, price, volume, stop
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
vt_orderid = ""
|
vt_orderid = ""
|
||||||
|
Loading…
Reference in New Issue
Block a user