Skip to content

Commit

Permalink
Merge pull request #104 from AsyncAlgoTrading/orderfix
Browse files Browse the repository at this point in the history
fixes #95
  • Loading branch information
timkpaine committed Sep 13, 2020
2 parents 174ab49 + 17a1598 commit e28fb25
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
11 changes: 11 additions & 0 deletions aat/core/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ async def run(self):
# send start event to all callbacks
await self.processEvent(Event(type=EventType.START, target=None))

# **************** #
# Main event loop
# **************** #
async with merge(*(exch.tick() for exch in self.exchanges + [self] if inspect.isasyncgenfunction(exch.tick))).stream() as stream:
# stream through all events
async for event in stream:
Expand All @@ -244,13 +247,21 @@ async def run(self):
await self.processEvent(event)

# process any secondary callback-targeted events (e.g. order fills)
# these need to route to a specific callback,
# rather than all callbacks
while self._queued_targeted_events:
strat, event = self._queued_targeted_events.popleft()

# send to manager
await self.processEvent(event, self.manager)

# send to the generating strategy
await self.processEvent(event, strat)

# process any periodics
await asyncio.gather(*(asyncio.create_task(p.execute(self._latest)) for p in self.manager._periodics))

# Before engine shutdown, send an exit event
await self.processEvent(Event(type=EventType.EXIT, target=None))

async def processEvent(self, event, strategy=None):
Expand Down
Empty file.
33 changes: 33 additions & 0 deletions aat/tests/strategy/test_strategies/test_cancel_all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from aat import Strategy, Event, Order, OrderType, Side


class TestCancelAll(Strategy):
def __init__(self, *args, **kwargs) -> None:
super(TestCancelAll, self).__init__(*args, **kwargs)
self._count = 0

async def onTrade(self, event: Event) -> None:
if self._count < 5:
await self.newOrder(Order(
1,
10000000,
Side.SELL,
self.instruments()[0],
order_type=OrderType.LIMIT,
))
self._count += 1
assert len(self.orders()) == self._count
else:
await self.cancelAll()
assert len(self.orders()) == 0


if __name__ == "__main__":
from aat import TradingEngine, parseConfig
cfg = parseConfig(['--trading_type', 'backtest',
'--exchanges', 'aat.exchange:SyntheticExchange,1,1000',
'--strategies', 'aat.tests.strategy.test_strategies.test_cancel_all::TestCancelAll'
])
print(cfg)
t = TradingEngine(**cfg)
t.start()

0 comments on commit e28fb25

Please sign in to comment.