[Mod] add EVENT_SPREAD_POS event type

This commit is contained in:
vn.py 2019-09-16 23:06:40 +08:00
parent f46f796475
commit bbd62e02b7
2 changed files with 47 additions and 2 deletions

View File

@ -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

View File

@ -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):
"""