From f46f796475e4283464cb0d3a3f80c8ec244abec3 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 16 Sep 2019 17:22:47 +0800 Subject: [PATCH] [Mod] move leg multiplier to SpreadData --- vnpy/app/spread_trading/base.py | 67 ++++++++++++++++------------- vnpy/app/spread_trading/engine.py | 27 ++++++++---- vnpy/app/spread_trading/template.py | 4 +- 3 files changed, 58 insertions(+), 40 deletions(-) diff --git a/vnpy/app/spread_trading/base.py b/vnpy/app/spread_trading/base.py index aee999c4..d9ab9529 100644 --- a/vnpy/app/spread_trading/base.py +++ b/vnpy/app/spread_trading/base.py @@ -15,21 +15,10 @@ EVENT_SPREAD_STRATEGY = "eSpreadStrategy" class LegData: """""" - def __init__( - self, - vt_symbol: str, - price_multiplier: float, - trading_multiplier: float - ): + def __init__(self, vt_symbol: str): """""" self.vt_symbol: str = vt_symbol - # For calculating spread price - self.price_multiplier: float = price_multiplier - - # For calculating spread pos and sending orders - self.trading_multiplier: float = trading_multiplier - # Price and position data self.bid_price: float = 0 self.ask_price: float = 0 @@ -66,6 +55,8 @@ class SpreadData: self, name: str, legs: List[LegData], + price_multipliers: Dict[str, int], + trading_multipliers: Dict[str, int], active_symbol: str ): """""" @@ -75,6 +66,12 @@ class SpreadData: self.active_leg: LegData = None self.passive_legs: List[LegData] = [] + # For calculating spread price + self.price_multipliers: Dict[str: int] = price_multipliers + + # For calculating spread pos and sending orders + self.trading_multipliers: Dict[str: int] = trading_multipliers + self.price_formula: str = "" self.trading_formula: str = "" @@ -85,15 +82,17 @@ class SpreadData: else: self.passive_legs.append(leg) - if leg.price_multiplier > 0: - self.price_formula += f"+{leg.trading_multiplier}*{leg.vt_symbol}" + price_multiplier = self.price_multipliers[leg.vt_symbol] + if price_multiplier > 0: + self.price_formula += f"+{price_multiplier}*{leg.vt_symbol}" else: - self.price_formula += f"{leg.trading_multiplier}*{leg.vt_symbol}" + self.price_formula += f"{price_multiplier}*{leg.vt_symbol}" - if leg.trading_multiplier > 0: - self.trading_formula += f"+{leg.trading_multiplier}*{leg.vt_symbol}" + trading_multiplier = self.trading_multipliers[leg.vt_symbol] + if trading_multiplier > 0: + self.trading_formula += f"+{trading_multiplier}*{leg.vt_symbol}" else: - self.trading_formula += f"{leg.trading_multiplier}*{leg.vt_symbol}" + self.trading_formula += f"{trading_multiplier}*{leg.vt_symbol}" # Spread data self.bid_price: float = 0 @@ -116,24 +115,27 @@ class SpreadData: return # Calculate price - if leg.price_multiplier > 0: - self.bid_price += leg.bid_price * leg.price_multiplier - self.ask_price += leg.ask_price * leg.price_multiplier + price_multiplier = self.price_multipliers[leg.vt_symbol] + if price_multiplier > 0: + self.bid_price += leg.bid_price * price_multiplier + self.ask_price += leg.ask_price * price_multiplier else: - self.bid_price += leg.ask_price * leg.price_multiplier - self.ask_price += leg.bid_price * leg.price_multiplier + self.bid_price += leg.ask_price * price_multiplier + self.ask_price += leg.bid_price * price_multiplier # Calculate volume - if leg.trading_multiplier > 0: + trading_multiplier = self.trading_multipliers[leg.vt_symbol] + + if trading_multiplier > 0: adjusted_bid_volume = floor( - leg.bid_volume / leg.trading_multiplier) + leg.bid_volume / trading_multiplier) adjusted_ask_volume = floor( - leg.ask_volume / leg.trading_multiplier) + leg.ask_volume / trading_multiplier) else: adjusted_bid_volume = floor( - leg.ask_volume / abs(leg.trading_multiplier)) + leg.ask_volume / abs(trading_multiplier)) adjusted_ask_volume = floor( - leg.bid_volume / abs(leg.trading_multiplier)) + leg.bid_volume / abs(trading_multiplier)) # For the first leg, just initialize if not n: @@ -152,7 +154,8 @@ class SpreadData: self.net_pos = 0 for n, leg in enumerate(self.legs.values()): - adjusted_net_pos = leg.net_pos / leg.trading_multiplier + trading_multiplier = self.trading_multipliers[leg.vt_symbol] + adjusted_net_pos = leg.net_pos / trading_multiplier if adjusted_net_pos > 0: adjusted_net_pos = floor(adjusted_net_pos) @@ -177,13 +180,15 @@ class SpreadData: def calculate_leg_volume(self, vt_symbol: str, spread_volume: float) -> float: """""" leg = self.legs[vt_symbol] - leg_volume = spread_volume * leg.trading_multiplier + trading_multiplier = self.trading_multipliers[leg.vt_symbol] + leg_volume = spread_volume * trading_multiplier return leg_volume def calculate_spread_volume(self, vt_symbol: str, leg_volume: float) -> float: """""" leg = self.legs[vt_symbol] - spread_volume = leg_volume / leg.trading_multiplier + trading_multiplier = self.trading_multipliers[leg.vt_symbol] + spread_volume = leg_volume / trading_multiplier if spread_volume > 0: spread_volume = floor(spread_volume) diff --git a/vnpy/app/spread_trading/engine.py b/vnpy/app/spread_trading/engine.py index a9f1d66e..71be653b 100644 --- a/vnpy/app/spread_trading/engine.py +++ b/vnpy/app/spread_trading/engine.py @@ -107,10 +107,13 @@ class SpreadDataEngine: for spread in self.spreads.values(): leg_settings = [] for leg in spread.legs.values(): + price_multiplier = spread.price_multipliers[leg.vt_symbol] + trading_multiplier = spread.trading_multipliers[leg.vt_symbol] + leg_setting = { "vt_symbol": leg.vt_symbol, - "price_multiplier": leg.price_multiplier, - "trading_multiplier": leg.trading_multiplier + "price_multiplier": price_multiplier, + "trading_multiplier": trading_multiplier } leg_settings.append(leg_setting) @@ -185,21 +188,28 @@ class SpreadDataEngine: return legs: List[LegData] = [] + price_multipliers: Dict[str, int] = {} + trading_multipliers: Dict[str, int] = {} + for leg_setting in leg_settings: vt_symbol = leg_setting["vt_symbol"] leg = self.legs.get(vt_symbol, None) if not leg: - leg = LegData( - vt_symbol, - leg_setting["price_multiplier"], - leg_setting["trading_multiplier"] - ) + leg = LegData(vt_symbol) self.legs[vt_symbol] = leg legs.append(leg) + price_multipliers[vt_symbol] = leg_setting["price_multiplier"] + trading_multipliers[vt_symbol] = leg_setting["trading_multiplier"] - spread = SpreadData(name, legs, active_symbol) + spread = SpreadData( + name, + legs, + price_multipliers, + trading_multipliers, + active_symbol + ) self.spreads[name] = spread for leg in spread.legs.values(): @@ -221,6 +231,7 @@ class SpreadDataEngine: for leg in spread.legs: self.symbol_spread_map[leg.vt_symbol].remove(spread) + self.save_setting() self.write_log("价差删除成功:{}".format(name)) diff --git a/vnpy/app/spread_trading/template.py b/vnpy/app/spread_trading/template.py index cef6781e..c723010c 100644 --- a/vnpy/app/spread_trading/template.py +++ b/vnpy/app/spread_trading/template.py @@ -211,7 +211,9 @@ class SpreadAlgoTemplate: for n, leg in enumerate(self.spread.legs.values()): leg_traded = self.leg_traded[leg.vt_symbol] - adjusted_leg_traded = leg_traded / leg.trading_multiplier + trading_multiplier = self.spread.trading_multipliers[ + leg.vt_symbol] + adjusted_leg_traded = leg_traded / trading_multiplier if adjusted_leg_traded > 0: adjusted_leg_traded = floor(adjusted_leg_traded)