[Mod] add offset of SpreadAlgoTemplate to determine which price to use for inverse contract volume calculation

This commit is contained in:
vn.py 2019-11-01 15:19:41 +08:00
parent d8d2506e7a
commit 08de86dede
4 changed files with 43 additions and 12 deletions

View File

@ -1,6 +1,6 @@
from typing import Any from typing import Any
from vnpy.trader.constant import Direction from vnpy.trader.constant import Direction, Offset
from vnpy.trader.object import (TickData, OrderData, TradeData) from vnpy.trader.object import (TickData, OrderData, TradeData)
from .template import SpreadAlgoTemplate from .template import SpreadAlgoTemplate
@ -17,6 +17,7 @@ class SpreadTakerAlgo(SpreadAlgoTemplate):
algoid: str, algoid: str,
spread: SpreadData, spread: SpreadData,
direction: Direction, direction: Direction,
offset: Offset,
price: float, price: float,
volume: float, volume: float,
payup: int, payup: int,
@ -25,8 +26,9 @@ class SpreadTakerAlgo(SpreadAlgoTemplate):
): ):
"""""" """"""
super().__init__( super().__init__(
algo_engine, algoid, spread, direction, algo_engine, algoid, spread,
price, volume, payup, interval, lock direction, offset, price, volume,
payup, interval, lock
) )
self.cancel_interval: int = 2 self.cancel_interval: int = 2

View File

@ -425,6 +425,7 @@ class SpreadAlgoEngine:
self, self,
spread_name: str, spread_name: str,
direction: Direction, direction: Direction,
offset: Offset,
price: float, price: float,
volume: float, volume: float,
payup: int, payup: int,
@ -448,6 +449,7 @@ class SpreadAlgoEngine:
algoid, algoid,
spread, spread,
direction, direction,
offset,
price, price,
volume, volume,
payup, payup,
@ -905,6 +907,7 @@ class SpreadStrategyEngine:
strategy: SpreadStrategyTemplate, strategy: SpreadStrategyTemplate,
spread_name: str, spread_name: str,
direction: Direction, direction: Direction,
offset: Offset,
price: float, price: float,
volume: float, volume: float,
payup: int, payup: int,
@ -915,6 +918,7 @@ class SpreadStrategyEngine:
algoid = self.spread_engine.start_algo( algoid = self.spread_engine.start_algo(
spread_name, spread_name,
direction, direction,
offset,
price, price,
volume, volume,
payup, payup,

View File

@ -23,11 +23,12 @@ class SpreadAlgoTemplate:
algoid: str, algoid: str,
spread: SpreadData, spread: SpreadData,
direction: Direction, direction: Direction,
offset: Offset,
price: float, price: float,
volume: float, volume: float,
payup: int, payup: int,
interval: int, interval: int,
lock: bool lock: bool,
): ):
"""""" """"""
self.algo_engine = algo_engine self.algo_engine = algo_engine
@ -36,6 +37,7 @@ class SpreadAlgoTemplate:
self.spread: SpreadData = spread self.spread: SpreadData = spread
self.spread_name: str = spread.name self.spread_name: str = spread.name
self.offset: Offset = offset
self.direction: Direction = direction self.direction: Direction = direction
self.price: float = price self.price: float = price
self.volume: float = volume self.volume: float = volume
@ -195,7 +197,12 @@ class SpreadAlgoTemplate:
# calculate contract trading volume from coin trading volume # calculate contract trading volume from coin trading volume
if self.spread.is_inverse(vt_symbol): if self.spread.is_inverse(vt_symbol):
size = self.spread.get_leg_size(vt_symbol) size = self.spread.get_leg_size(vt_symbol)
volume = volume * price / size
if self.offset == Offset.CLOSE:
leg = self.spread.legs[vt_symbol]
volume = volume * leg.net_pos_price / size
else:
volume = volume * price / size
vt_orderids = self.algo_engine.send_order( vt_orderids = self.algo_engine.send_order(
self, self,
@ -454,7 +461,8 @@ class SpreadStrategyTemplate:
volume: float, volume: float,
payup: int, payup: int,
interval: int, interval: int,
lock: bool lock: bool,
offset: Offset
) -> str: ) -> str:
"""""" """"""
if not self.trading: if not self.trading:
@ -464,6 +472,7 @@ class SpreadStrategyTemplate:
self, self,
self.spread_name, self.spread_name,
direction, direction,
offset,
price, price,
volume, volume,
payup, payup,
@ -481,10 +490,14 @@ class SpreadStrategyTemplate:
volume: float, volume: float,
payup: int, payup: int,
interval: int, interval: int,
lock: bool = False lock: bool = False,
offset: Offset = Offset.NONE
) -> str: ) -> str:
"""""" """"""
return self.start_algo(Direction.LONG, price, volume, payup, interval, lock) return self.start_algo(
Direction.LONG, price, volume,
payup, interval, lock, offset
)
def start_short_algo( def start_short_algo(
self, self,
@ -492,10 +505,14 @@ class SpreadStrategyTemplate:
volume: float, volume: float,
payup: int, payup: int,
interval: int, interval: int,
lock: bool = False lock: bool = False,
offset: Offset = Offset.NONE
) -> str: ) -> str:
"""""" """"""
return self.start_algo(Direction.SHORT, price, volume, payup, interval, lock) return self.start_algo(
Direction.SHORT, price, volume,
payup, interval, lock, offset
)
def stop_algo(self, algoid: str): def stop_algo(self, algoid: str):
"""""" """"""

View File

@ -4,7 +4,7 @@ Widget for spread trading.
from vnpy.event import EventEngine, Event from vnpy.event import EventEngine, Event
from vnpy.trader.engine import MainEngine from vnpy.trader.engine import MainEngine
from vnpy.trader.constant import Direction from vnpy.trader.constant import Direction, Offset
from vnpy.trader.ui import QtWidgets, QtCore, QtGui from vnpy.trader.ui import QtWidgets, QtCore, QtGui
from vnpy.trader.ui.widget import ( from vnpy.trader.ui.widget import (
BaseMonitor, BaseCell, BaseMonitor, BaseCell,
@ -169,6 +169,7 @@ class SpreadAlgoMonitor(BaseMonitor):
"algoid": {"display": "算法", "cell": BaseCell, "update": False}, "algoid": {"display": "算法", "cell": BaseCell, "update": False},
"spread_name": {"display": "价差", "cell": BaseCell, "update": False}, "spread_name": {"display": "价差", "cell": BaseCell, "update": False},
"direction": {"display": "方向", "cell": DirectionCell, "update": False}, "direction": {"display": "方向", "cell": DirectionCell, "update": False},
"offset": {"display": "开平", "cell": EnumCell, "update": False},
"price": {"display": "价格", "cell": BaseCell, "update": False}, "price": {"display": "价格", "cell": BaseCell, "update": False},
"payup": {"display": "超价", "cell": BaseCell, "update": False}, "payup": {"display": "超价", "cell": BaseCell, "update": False},
"volume": {"display": "数量", "cell": BaseCell, "update": False}, "volume": {"display": "数量", "cell": BaseCell, "update": False},
@ -226,6 +227,11 @@ class SpreadAlgoWidget(QtWidgets.QFrame):
[Direction.LONG.value, Direction.SHORT.value] [Direction.LONG.value, Direction.SHORT.value]
) )
self.offset_combo = QtWidgets.QComboBox()
self.offset_combo.addItem(
[Offset.NONE.value, Offset.OPEN.value, Offset.CLOSE.value]
)
float_validator = QtGui.QDoubleValidator() float_validator = QtGui.QDoubleValidator()
self.price_line = QtWidgets.QLineEdit() self.price_line = QtWidgets.QLineEdit()
@ -273,6 +279,7 @@ class SpreadAlgoWidget(QtWidgets.QFrame):
form = QtWidgets.QFormLayout() form = QtWidgets.QFormLayout()
form.addRow("价差", self.name_line) form.addRow("价差", self.name_line)
form.addRow("方向", self.direction_combo) form.addRow("方向", self.direction_combo)
form.addRow("开平", self.offset_combo)
form.addRow("价格", self.price_line) form.addRow("价格", self.price_line)
form.addRow("数量", self.volume_line) form.addRow("数量", self.volume_line)
form.addRow("超价", self.payup_line) form.addRow("超价", self.payup_line)
@ -298,6 +305,7 @@ class SpreadAlgoWidget(QtWidgets.QFrame):
"""""" """"""
name = self.name_line.text() name = self.name_line.text()
direction = Direction(self.direction_combo.currentText()) direction = Direction(self.direction_combo.currentText())
offset = Offset(self.offset_combo.currentText())
price = float(self.price_line.text()) price = float(self.price_line.text())
volume = float(self.volume_line.text()) volume = float(self.volume_line.text())
payup = int(self.payup_line.text()) payup = int(self.payup_line.text())
@ -310,7 +318,7 @@ class SpreadAlgoWidget(QtWidgets.QFrame):
lock = False lock = False
self.spread_engine.start_algo( self.spread_engine.start_algo(
name, direction, price, volume, payup, interval, lock name, direction, offset, price, volume, payup, interval, lock
) )
def add_spread(self): def add_spread(self):