From bbd62e02b72e8a0cd92b3f0a4404ebfae2084725 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 16 Sep 2019 23:06:40 +0800 Subject: [PATCH] [Mod] add EVENT_SPREAD_POS event type --- vnpy/app/spread_trading/base.py | 41 ++++++++++++++++++++++++++-- vnpy/app/spread_trading/ui/widget.py | 8 ++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/vnpy/app/spread_trading/base.py b/vnpy/app/spread_trading/base.py index d9ab9529..5eff4b48 100644 --- a/vnpy/app/spread_trading/base.py +++ b/vnpy/app/spread_trading/base.py @@ -2,11 +2,12 @@ from typing import Dict, List from math import floor, ceil from datetime import datetime -from vnpy.trader.object import TickData, PositionData -from vnpy.trader.constant import Direction +from vnpy.trader.object import TickData, PositionData, TradeData +from vnpy.trader.constant import Direction, Offset, Exchange EVENT_SPREAD_DATA = "eSpreadData" +EVENT_SPREAD_POS = "eSpreadPos" EVENT_SPREAD_LOG = "eSpreadLog" EVENT_SPREAD_ALGO = "eSpreadAlgo" EVENT_SPREAD_STRATEGY = "eSpreadStrategy" @@ -29,6 +30,9 @@ class LegData: self.short_pos: float = 0 self.net_pos: float = 0 + # Tick data buf + self.tick: TickData = None + def update_tick(self, tick: TickData): """""" self.bid_price = tick.bid_price_1 @@ -36,6 +40,8 @@ class LegData: self.bid_volume = tick.bid_volume_1 self.ask_volume = tick.ask_volume_1 + self.tick = tick + def update_position(self, position: PositionData): """""" if position.direction == Direction.NET: @@ -47,6 +53,21 @@ class LegData: self.short_pos = position.volume self.net_pos = self.long_pos - self.short_pos + def update_trade(self, trade: TradeData): + """""" + if trade.direction == Direction.LONG: + if trade.offset == Offset.OPEN: + self.long_pos += trade.volume + else: + self.short_pos -= trade.volume + else: + if trade.offset == Offset.OPEN: + self.short_pos += trade.volume + else: + self.long_pos -= trade.volume + + self.net_pos = self.long_pos - self.net_pos + class SpreadData: """""" @@ -196,3 +217,19 @@ class SpreadData: spread_volume = ceil(spread_volume) return spread_volume + + def to_tick(self): + """""" + tick = TickData( + symbol=self.name, + exchange=Exchange.LOCAL, + datetime=self.datetime, + name=self.name, + last_price=(self.bid_price + self.ask_price) / 2, + bid_price_1=self.bid_price, + ask_price_1=self.ask_price, + bid_volume_1=self.bid_volume, + ask_volume_1=self.ask_volume, + gateway_name="SPREAD" + ) + return tick diff --git a/vnpy/app/spread_trading/ui/widget.py b/vnpy/app/spread_trading/ui/widget.py index 33ab52c9..289b38fa 100644 --- a/vnpy/app/spread_trading/ui/widget.py +++ b/vnpy/app/spread_trading/ui/widget.py @@ -17,6 +17,7 @@ from ..engine import ( SpreadEngine, APP_NAME, EVENT_SPREAD_DATA, + EVENT_SPREAD_POS, EVENT_SPREAD_LOG, EVENT_SPREAD_ALGO ) @@ -105,6 +106,13 @@ class SpreadDataMonitor(BaseMonitor): "datetime": {"display": "时间", "cell": TimeCell, "update": True}, } + def register_event(self): + """ + Register event handler into event engine. + """ + super().register_event() + self.event_engine.register(EVENT_SPREAD_POS, self.signal.emit) + class SpreadLogMonitor(QtWidgets.QTextEdit): """