[Mod] auto subscribe market data when add new legs

This commit is contained in:
vn.py 2019-09-18 11:44:11 +08:00
parent 6279ab4ddf
commit c02b37bde2
2 changed files with 21 additions and 7 deletions

View File

@ -8,7 +8,7 @@
<img src ="https://img.shields.io/badge/version-2.0.7-blueviolet.svg"/>
<img src ="https://img.shields.io/badge/platform-windows|linux|macos-yellow.svg"/>
<img src ="https://img.shields.io/badge/python-3.7-blue.svg" />
<img src ="https://img.shields.io/travis/com/vnpy/vnpy/master.svg"/>
<img src ="https://img.shields.io/circleci/build/github/vnpy/vnpy?token=4d11df68295c8cc02a2bede46094991364190bbc"/>
<img src ="https://img.shields.io/github/license/vnpy/vnpy.svg?color=orange"/>
</p>

View File

@ -211,6 +211,24 @@ class SpreadDataEngine:
event = Event(EVENT_SPREAD_POS, spread)
self.event_engine.put(event)
def get_leg(self, vt_symbol: str) -> LegData:
""""""
leg = self.legs.get(vt_symbol, None)
if not leg:
leg = LegData(vt_symbol)
self.legs[vt_symbol] = leg
# Subscribe market data
contract = self.main_engine.get_contract(vt_symbol)
req = SubscribeRequest(
contract.symbol,
contract.exchange
)
self.main_engine.subscribe(req, contract.gateway_name)
return leg
def add_spread(
self,
name: str,
@ -229,11 +247,7 @@ class SpreadDataEngine:
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)
self.legs[vt_symbol] = leg
leg = self.get_leg(vt_symbol)
legs.append(leg)
price_multipliers[vt_symbol] = leg_setting["price_multiplier"]
@ -824,7 +838,7 @@ class SpreadStrategyEngine:
return
self.call_strategy_func(strategy, strategy.on_stop)
strategy.stop_all_algos()
strategy.cancel_all_orders()