[Mod] move leg multiplier to SpreadData
This commit is contained in:
parent
80c9dfe378
commit
f46f796475
@ -15,21 +15,10 @@ EVENT_SPREAD_STRATEGY = "eSpreadStrategy"
|
|||||||
class LegData:
|
class LegData:
|
||||||
""""""
|
""""""
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, vt_symbol: str):
|
||||||
self,
|
|
||||||
vt_symbol: str,
|
|
||||||
price_multiplier: float,
|
|
||||||
trading_multiplier: float
|
|
||||||
):
|
|
||||||
""""""
|
""""""
|
||||||
self.vt_symbol: str = vt_symbol
|
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
|
# Price and position data
|
||||||
self.bid_price: float = 0
|
self.bid_price: float = 0
|
||||||
self.ask_price: float = 0
|
self.ask_price: float = 0
|
||||||
@ -66,6 +55,8 @@ class SpreadData:
|
|||||||
self,
|
self,
|
||||||
name: str,
|
name: str,
|
||||||
legs: List[LegData],
|
legs: List[LegData],
|
||||||
|
price_multipliers: Dict[str, int],
|
||||||
|
trading_multipliers: Dict[str, int],
|
||||||
active_symbol: str
|
active_symbol: str
|
||||||
):
|
):
|
||||||
""""""
|
""""""
|
||||||
@ -75,6 +66,12 @@ class SpreadData:
|
|||||||
self.active_leg: LegData = None
|
self.active_leg: LegData = None
|
||||||
self.passive_legs: List[LegData] = []
|
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.price_formula: str = ""
|
||||||
self.trading_formula: str = ""
|
self.trading_formula: str = ""
|
||||||
|
|
||||||
@ -85,15 +82,17 @@ class SpreadData:
|
|||||||
else:
|
else:
|
||||||
self.passive_legs.append(leg)
|
self.passive_legs.append(leg)
|
||||||
|
|
||||||
if leg.price_multiplier > 0:
|
price_multiplier = self.price_multipliers[leg.vt_symbol]
|
||||||
self.price_formula += f"+{leg.trading_multiplier}*{leg.vt_symbol}"
|
if price_multiplier > 0:
|
||||||
|
self.price_formula += f"+{price_multiplier}*{leg.vt_symbol}"
|
||||||
else:
|
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:
|
trading_multiplier = self.trading_multipliers[leg.vt_symbol]
|
||||||
self.trading_formula += f"+{leg.trading_multiplier}*{leg.vt_symbol}"
|
if trading_multiplier > 0:
|
||||||
|
self.trading_formula += f"+{trading_multiplier}*{leg.vt_symbol}"
|
||||||
else:
|
else:
|
||||||
self.trading_formula += f"{leg.trading_multiplier}*{leg.vt_symbol}"
|
self.trading_formula += f"{trading_multiplier}*{leg.vt_symbol}"
|
||||||
|
|
||||||
# Spread data
|
# Spread data
|
||||||
self.bid_price: float = 0
|
self.bid_price: float = 0
|
||||||
@ -116,24 +115,27 @@ class SpreadData:
|
|||||||
return
|
return
|
||||||
|
|
||||||
# Calculate price
|
# Calculate price
|
||||||
if leg.price_multiplier > 0:
|
price_multiplier = self.price_multipliers[leg.vt_symbol]
|
||||||
self.bid_price += leg.bid_price * leg.price_multiplier
|
if price_multiplier > 0:
|
||||||
self.ask_price += leg.ask_price * leg.price_multiplier
|
self.bid_price += leg.bid_price * price_multiplier
|
||||||
|
self.ask_price += leg.ask_price * price_multiplier
|
||||||
else:
|
else:
|
||||||
self.bid_price += leg.ask_price * leg.price_multiplier
|
self.bid_price += leg.ask_price * price_multiplier
|
||||||
self.ask_price += leg.bid_price * leg.price_multiplier
|
self.ask_price += leg.bid_price * price_multiplier
|
||||||
|
|
||||||
# Calculate volume
|
# Calculate volume
|
||||||
if leg.trading_multiplier > 0:
|
trading_multiplier = self.trading_multipliers[leg.vt_symbol]
|
||||||
|
|
||||||
|
if trading_multiplier > 0:
|
||||||
adjusted_bid_volume = floor(
|
adjusted_bid_volume = floor(
|
||||||
leg.bid_volume / leg.trading_multiplier)
|
leg.bid_volume / trading_multiplier)
|
||||||
adjusted_ask_volume = floor(
|
adjusted_ask_volume = floor(
|
||||||
leg.ask_volume / leg.trading_multiplier)
|
leg.ask_volume / trading_multiplier)
|
||||||
else:
|
else:
|
||||||
adjusted_bid_volume = floor(
|
adjusted_bid_volume = floor(
|
||||||
leg.ask_volume / abs(leg.trading_multiplier))
|
leg.ask_volume / abs(trading_multiplier))
|
||||||
adjusted_ask_volume = floor(
|
adjusted_ask_volume = floor(
|
||||||
leg.bid_volume / abs(leg.trading_multiplier))
|
leg.bid_volume / abs(trading_multiplier))
|
||||||
|
|
||||||
# For the first leg, just initialize
|
# For the first leg, just initialize
|
||||||
if not n:
|
if not n:
|
||||||
@ -152,7 +154,8 @@ class SpreadData:
|
|||||||
self.net_pos = 0
|
self.net_pos = 0
|
||||||
|
|
||||||
for n, leg in enumerate(self.legs.values()):
|
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:
|
if adjusted_net_pos > 0:
|
||||||
adjusted_net_pos = floor(adjusted_net_pos)
|
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:
|
def calculate_leg_volume(self, vt_symbol: str, spread_volume: float) -> float:
|
||||||
""""""
|
""""""
|
||||||
leg = self.legs[vt_symbol]
|
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
|
return leg_volume
|
||||||
|
|
||||||
def calculate_spread_volume(self, vt_symbol: str, leg_volume: float) -> float:
|
def calculate_spread_volume(self, vt_symbol: str, leg_volume: float) -> float:
|
||||||
""""""
|
""""""
|
||||||
leg = self.legs[vt_symbol]
|
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:
|
if spread_volume > 0:
|
||||||
spread_volume = floor(spread_volume)
|
spread_volume = floor(spread_volume)
|
||||||
|
@ -107,10 +107,13 @@ class SpreadDataEngine:
|
|||||||
for spread in self.spreads.values():
|
for spread in self.spreads.values():
|
||||||
leg_settings = []
|
leg_settings = []
|
||||||
for leg in spread.legs.values():
|
for leg in spread.legs.values():
|
||||||
|
price_multiplier = spread.price_multipliers[leg.vt_symbol]
|
||||||
|
trading_multiplier = spread.trading_multipliers[leg.vt_symbol]
|
||||||
|
|
||||||
leg_setting = {
|
leg_setting = {
|
||||||
"vt_symbol": leg.vt_symbol,
|
"vt_symbol": leg.vt_symbol,
|
||||||
"price_multiplier": leg.price_multiplier,
|
"price_multiplier": price_multiplier,
|
||||||
"trading_multiplier": leg.trading_multiplier
|
"trading_multiplier": trading_multiplier
|
||||||
}
|
}
|
||||||
leg_settings.append(leg_setting)
|
leg_settings.append(leg_setting)
|
||||||
|
|
||||||
@ -185,21 +188,28 @@ class SpreadDataEngine:
|
|||||||
return
|
return
|
||||||
|
|
||||||
legs: List[LegData] = []
|
legs: List[LegData] = []
|
||||||
|
price_multipliers: Dict[str, int] = {}
|
||||||
|
trading_multipliers: Dict[str, int] = {}
|
||||||
|
|
||||||
for leg_setting in leg_settings:
|
for leg_setting in leg_settings:
|
||||||
vt_symbol = leg_setting["vt_symbol"]
|
vt_symbol = leg_setting["vt_symbol"]
|
||||||
|
|
||||||
leg = self.legs.get(vt_symbol, None)
|
leg = self.legs.get(vt_symbol, None)
|
||||||
if not leg:
|
if not leg:
|
||||||
leg = LegData(
|
leg = LegData(vt_symbol)
|
||||||
vt_symbol,
|
|
||||||
leg_setting["price_multiplier"],
|
|
||||||
leg_setting["trading_multiplier"]
|
|
||||||
)
|
|
||||||
self.legs[vt_symbol] = leg
|
self.legs[vt_symbol] = leg
|
||||||
|
|
||||||
legs.append(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
|
self.spreads[name] = spread
|
||||||
|
|
||||||
for leg in spread.legs.values():
|
for leg in spread.legs.values():
|
||||||
@ -221,6 +231,7 @@ class SpreadDataEngine:
|
|||||||
for leg in spread.legs:
|
for leg in spread.legs:
|
||||||
self.symbol_spread_map[leg.vt_symbol].remove(spread)
|
self.symbol_spread_map[leg.vt_symbol].remove(spread)
|
||||||
|
|
||||||
|
self.save_setting()
|
||||||
self.write_log("价差删除成功:{}".format(name))
|
self.write_log("价差删除成功:{}".format(name))
|
||||||
|
|
||||||
|
|
||||||
|
@ -211,7 +211,9 @@ class SpreadAlgoTemplate:
|
|||||||
|
|
||||||
for n, leg in enumerate(self.spread.legs.values()):
|
for n, leg in enumerate(self.spread.legs.values()):
|
||||||
leg_traded = self.leg_traded[leg.vt_symbol]
|
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:
|
if adjusted_leg_traded > 0:
|
||||||
adjusted_leg_traded = floor(adjusted_leg_traded)
|
adjusted_leg_traded = floor(adjusted_leg_traded)
|
||||||
|
Loading…
Reference in New Issue
Block a user