Beispiel #1
0
def parseConfig(argv: list = None) -> dict:
    from aat import TradingType

    parser = argparse.ArgumentParser()

    parser.add_argument("--config", help="Config file", default="")

    parser.add_argument("--verbose",
                        action="store_true",
                        help="Run in verbose mode",
                        default=False)

    parser.add_argument("--api",
                        action="store_true",
                        help="Enable HTTP server",
                        default=False)

    parser.add_argument(
        "--load_accounts",
        action="store_true",
        help="Load accounts from exchanges",
        default=False,
    )

    parser.add_argument(
        "--trading_type",
        help='Trading Type in ("live", "sandbox", "simulation", "backtest")',
        choices=[_.lower() for _ in TradingType.members()],
        default="simulation",
    )

    parser.add_argument(
        "--strategies",
        action="append",
        nargs="+",
        help="Strategies to run in form <path.to.module:Class,args,for,strat>",
        default=[],
    )

    parser.add_argument(
        "--exchanges",
        action="append",
        nargs="+",
        help="Exchanges to run on",
        default=[],
    )

    args = parser.parse_args(argv)

    # Every engine run requires a static config object
    if args.config:
        return _config_to_dict(args.config)

    return _args_to_dict(args)
Beispiel #2
0
    def __init__(self, **config):
        # get port for API access
        self.port = config.get('general', {}).get('port', self.port)

        # run in verbose mode (print all events)
        self.verbose = bool(
            int(config.get('general', {}).get('verbose', self.verbose)))

        # enable API access?
        self.api = bool(int(config.get('general', {}).get('api', self.api)))

        # Trading type
        self.trading_type = TradingType(
            config.get('general', {}).get('trading_type',
                                          'simulation').upper())

        # Load account information from exchanges
        self._load_accounts = config.get('general',
                                         {}).get('load_accounts', False)

        # Load exchange instances
        self.exchanges = getExchanges(config.get('exchange',
                                                 {}).get('exchanges', []),
                                      trading_type=self.trading_type,
                                      verbose=self.verbose)

        # instantiate the Strategy Manager
        self.manager = StrategyManager(self, self.trading_type, self.exchanges,
                                       self._load_accounts)

        # set event loop to use uvloop
        if uvloop:
            asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

        # install event loop
        self.event_loop = asyncio.get_event_loop()

        # setup subscriptions
        self._handler_subscriptions = {
            m: []
            for m in EventType.__members__.values()
        }

        # setup `now` handler for backtest
        self._latest = datetime.fromtimestamp(
            0) if self._offline() else datetime.now()

        # register internal management event handler before all strategy handlers
        self.registerHandler(self.manager)

        # install event handlers
        self.strategies = getStrategies(
            config.get('strategy', {}).get('strategies', []))
        for strategy in self.strategies:
            self.log.critical("Installing strategy: {}".format(strategy))
            self.registerHandler(strategy)

        # warn if no event handlers installed
        if not self.event_handlers:
            self.log.critical('Warning! No event handlers set')

        # install print handler if verbose
        if self.verbose:
            self.log.critical('Installing print handler')
            self.registerHandler(PrintHandler())

        # install webserver
        if self.api:
            self.log.critical('Installing API handlers')

            if PerspectiveManager is not None:
                table_handler = TableHandler()
                table_handler.installTables(self.table_manager)
                self.registerHandler(table_handler)

            self.api_handlers.append((r"/", RedirectHandler, {
                "url": "index.html"
            }))
            self.api_handlers.append(
                (r"/api/v1/ws", PerspectiveTornadoHandler, {
                    "manager": self.table_manager,
                    "check_origin": True
                }))
            self.api_handlers.append((r"/static/js/(.*)", StaticFileHandler, {
                "path":
                os.path.join(os.path.dirname(__file__), '..', 'ui', 'assets',
                             'static', 'js')
            }))
            self.api_handlers.append((r"/static/css/(.*)", StaticFileHandler, {
                "path":
                os.path.join(os.path.dirname(__file__), '..', 'ui', 'assets',
                             'static', 'css')
            }))
            self.api_handlers.append(
                (r"/static/fonts/(.*)", StaticFileHandler, {
                    "path":
                    os.path.join(os.path.dirname(__file__), '..', 'ui',
                                 'assets', 'static', 'fonts')
                }))
            self.api_handlers.append((r"/(.*)", StaticFileHandler, {
                "path":
                os.path.join(os.path.dirname(__file__), '..', 'ui', 'assets',
                             'static', 'html')
            }))

            self.api_application = ServerApplication(
                handlers=self.api_handlers)

            self.log.critical('.......')
            self.log.critical(f'listening on 0.0.0.0:{self.port}')
            self.log.critical('.......')

            self.api_application.listen(self.port)
Beispiel #3
0
    def __init__(self, **config: dict) -> None:
        # get port for API access
        self.port = config.get("general", {}).get("port", self.port)

        # run in verbose mode (print all events)
        self.verbose = bool(
            int(config.get("general", {}).get("verbose", self.verbose)))

        # enable API access?
        self.api = bool(int(config.get("general", {}).get("api", self.api)))

        # Trading type
        self.trading_type = TradingType(
            config.get("general", {}).get("trading_type",
                                          "simulation").upper())

        # Load account information from exchanges
        self._load_accounts = config.get("general",
                                         {}).get("load_accounts", False)

        # Load exchange instances
        self.exchanges = getExchanges(
            config.get("exchange", {}).get("exchanges", []),
            trading_type=self.trading_type,
            verbose=self.verbose,
        )

        # instantiate the Strategy Manager
        self.manager = StrategyManager(self, self.trading_type, self.exchanges,
                                       self._load_accounts)

        # set event loop to use uvloop
        if uvloop:
            asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

        # install event loop
        self.event_loop = asyncio.get_event_loop()

        # setup subscriptions
        self._handler_subscriptions: Dict[EventType, List] = {
            m: []
            for m in EventType.__members__.values()
        }

        # setup `now` handler for backtest
        self._latest = datetime.fromtimestamp(
            0) if self._offline() else datetime.now()

        # register internal management event handler before all strategy handlers
        self.registerHandler(self.manager)

        # install event handlers
        self.strategies = getStrategies(
            config.get("strategy", {}).get("strategies", []))

        for strategy in self.strategies:
            self.log.critical("Installing strategy: {}".format(strategy))
            self.registerHandler(strategy)

        # warn if no event handlers installed
        if not self.event_handlers:
            self.log.critical("Warning! No event handlers set")

        # install print handler if verbose
        if self.verbose:
            self.log.critical("Installing print handler")
            self.registerHandler(PrintHandler())

        # install webserver
        if self.api:
            self.log.critical("Installing API handlers")

            if PerspectiveManager is not None:
                table_handler = TableHandler()
                table_handler.installTables(self.table_manager)
                self.registerHandler(table_handler)

            self.api_handlers.append((r"/", RedirectHandler, {
                "url": "index.html"
            }))
            self.api_handlers.append((
                r"/api/v1/ws",
                PerspectiveTornadoHandler,
                {
                    "manager": self.table_manager,
                    "check_origin": True
                },
            ))
            self.api_handlers.append((
                r"/static/js/(.*)",
                StaticFileHandler,
                {
                    "path":
                    os.path.join(
                        os.path.dirname(__file__),
                        "..",
                        "ui",
                        "assets",
                        "static",
                        "js",
                    )
                },
            ))
            self.api_handlers.append((
                r"/static/css/(.*)",
                StaticFileHandler,
                {
                    "path":
                    os.path.join(
                        os.path.dirname(__file__),
                        "..",
                        "ui",
                        "assets",
                        "static",
                        "css",
                    )
                },
            ))
            self.api_handlers.append((
                r"/static/fonts/(.*)",
                StaticFileHandler,
                {
                    "path":
                    os.path.join(
                        os.path.dirname(__file__),
                        "..",
                        "ui",
                        "assets",
                        "static",
                        "fonts",
                    )
                },
            ))
            self.api_handlers.append((
                r"/(.*)",
                StaticFileHandler,
                {
                    "path":
                    os.path.join(
                        os.path.dirname(__file__),
                        "..",
                        "ui",
                        "assets",
                        "static",
                        "html",
                    )
                },
            ))

            self.api_application = ServerApplication(
                handlers=self.api_handlers)  # type: ignore

            self.log.critical(".......")
            self.log.critical(f"listening on 0.0.0.0:{self.port}")
            self.log.critical(".......")

            self.api_application.listen(self.port)