don't trigger events and state callbacks in strategy evaluation

This commit is contained in:
Giulio De Pasquale 2020-12-17 10:50:08 +00:00
parent 95b1921f37
commit a0c1a58d39

View File

@ -138,13 +138,24 @@ class SymbolStatus:
# Applies strategy and adds position to list
async def add_position(self, position: Position):
events = []
# if a strategy is defined then the strategy takes care of creating a PW for us
if not self.strategy:
pw = PositionWrapper(position)
else:
pw = await self.__apply_strategy_to_position__(position)
pw, events = await self.__apply_strategy_to_position__(position)
self.positions = __add_to_dict_list__(self.positions, self.current_tick, pw)
# triggering state callbacks
await self.__trigger_position_state_callbacks__(pw)
# triggering events callbacks
for e in events:
if not isinstance(e, Event):
raise ValueError
await self.add_event(e)
def all_prices(self) -> List[float]:
return list(map(lambda x: self.prices[x], range(1, self.current_tick + 1)))
@ -173,10 +184,7 @@ class SymbolStatus:
def set_tick_price(self, tick, price):
self.prices[tick] = price
async def __apply_strategy_to_position__(self, position: Position):
if not self.strategy:
return
async def __apply_strategy_to_position__(self, position: Position) -> Tuple[PositionWrapper, List[Event]]:
pw, events = self.strategy.position_on_new_tick(position, self)
if not isinstance(pw, PositionWrapper):
@ -185,16 +193,7 @@ class SymbolStatus:
if not isinstance(events, list):
raise ValueError
# triggering state callbacks
await self.__trigger_position_state_callbacks__(pw)
# triggering events callbacks
for e in events:
if not isinstance(e, Event):
raise ValueError
await self.add_event(e)
return pw
return pw, events
async def __trigger_position_state_callbacks__(self, pw: PositionWrapper):
await self.eh.call_position_state(self, pw)