Skip to content

Commit

Permalink
fixes #23
Browse files Browse the repository at this point in the history
  • Loading branch information
timkpaine committed Jul 2, 2019
1 parent a33b55d commit 835495e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
29 changes: 18 additions & 11 deletions aat/trading.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import threading
import tornado
import operator
import uvloop
from functools import reduce
from .backtest import Backtest
from .callback import Print
Expand Down Expand Up @@ -153,17 +153,14 @@ def portfolio_value(self) -> list:

def run(self):
if self._live or self._simulation or self._sandbox:
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

loop = tornado.platform.asyncio.AsyncIOMainLoop().install()

port = 8081
self.application = ServerApplication(self,
extra_handlers=self._ui_handlers,
custom_settings=self._ui_settings)
log.critical('')
log.critical('Server listening on port: %s', port)
log.critical('')
self.application.listen(port)
self._t = threading.Thread(target=tornado.ioloop.IOLoop.current().start)
self._t.daemon = True # So it terminates on exit
self._t.start()

# trigger starts
for strat in self.query().strategies():
Expand All @@ -172,9 +169,19 @@ def run(self):
# run on exchange
async def _run():
await asyncio.wait([ex.run(self) for ex in self._exchanges.values()])
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(_run())

# get event loop
loop = asyncio.get_event_loop()

# hook in tornado to asyncio
log.critical('')
log.critical('Server listening on port: %s', port)
log.critical('')
self.application.listen(port)

# run asyncio loop
loop.create_task(_run())
loop.run_forever()

elif self._backtest:
# trigger starts
Expand Down
14 changes: 14 additions & 0 deletions aat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

@lru_cache(100)
def parse_date(indate: str) -> datetime:
'''parse date
Args:
indate (string, int, or datetime): input to convert to datetime
Returns:
datetime
'''
if isinstance(indate, datetime):
return indate
try:
Expand All @@ -23,6 +30,13 @@ def parse_date(indate: str) -> datetime:

@lru_cache(None)
def ex_type_to_ex(ex: ExchangeType):
'''Convert Exchange type to Exchange class
Args:
ex (ExchangeType): exchange type
Returns:
Exchange
'''
if ex == ExchangeType.COINBASE:
from .exchanges.coinbase import CoinbaseExchange
return CoinbaseExchange
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ requests>=2.13.0
tornado>=5.1
traitlets>=4.3.2
ujson>=1.35
uvloop>=0.12.2
websocket-client>=0.40.0

0 comments on commit 835495e

Please sign in to comment.