예제 #1
0
    def __init__(self):
        self._a_orderbook_ok = False
        self._a_orderbook = None
        self._b_orderbook_ok = False
        self._b_orderbook = None
        self._c_orderbook_ok = False
        self._c_orderbook = None

        # 交易对象A (ETH/USDT)
        params = {
            "strategy": config.strategy,
            "platform": config.A["platform"],
            "symbol": config.A["symbol"],
            "account": config.A["account"],
            "access_key": config.A["access_key"],
            "secret_key": config.A["secret_key"],
            "order_update_callback": self.on_event_order_update_callback,
            "init_callback": self.on_event_init_callback,
            "error_callback": self.on_event_error_callback
        }
        self._a_trader = Trade(**params)

        # 交易对象B (BTC/USDT)
        params = {
            "strategy": config.strategy,
            "platform": config.B["platform"],
            "symbol": config.B["symbol"],
            "account": config.B["account"],
            "access_key": config.B["access_key"],
            "secret_key": config.B["secret_key"],
            "order_update_callback": self.on_event_order_update_callback,
            "init_callback": self.on_event_init_callback,
            "error_callback": self.on_event_error_callback
        }
        self._b_trader = Trade(**params)

        # 交易对象C (ETH/BTC)
        params = {
            "strategy": config.strategy,
            "platform": config.C["platform"],
            "symbol": config.C["symbol"],
            "account": config.C["account"],
            "access_key": config.C["access_key"],
            "secret_key": config.C["secret_key"],
            "order_update_callback": self.on_event_order_update_callback,
            "init_callback": self.on_event_init_callback,
            "error_callback": self.on_event_error_callback
        }
        self._c_trader = Trade(**params)

        # 订阅行情
        MarketSubscribe(const.MARKET_TYPE_ORDERBOOK, config.A["platform"], config.A["platform"], self.on_event_orderbook_update)
        MarketSubscribe(const.MARKET_TYPE_ORDERBOOK, config.B["platform"], config.B["platform"], self.on_event_orderbook_update)
        MarketSubscribe(const.MARKET_TYPE_ORDERBOOK, config.C["platform"], config.C["platform"], self.on_event_orderbook_update)

        # 定时任务
        LoopRunTask.register(self.check_orderbook, 60)  # 定时每隔60秒检查一次订单薄
예제 #2
0
파일: okex.py 프로젝트: lindenwxl/aioquant
    def __init__(self, **kwargs):
        """Initialize."""
        e = None
        if not kwargs.get("account"):
            e = Error("param account miss")
        if not kwargs.get("strategy"):
            e = Error("param strategy miss")
        if not kwargs.get("symbol"):
            e = Error("param symbol miss")
        if not kwargs.get("host"):
            kwargs["host"] = "https://www.okex.com"
        if not kwargs.get("wss"):
            kwargs["wss"] = "wss://real.okex.com:8443"
        if not kwargs.get("access_key"):
            e = Error("param access_key miss")
        if not kwargs.get("secret_key"):
            e = Error("param secret_key miss")
        if not kwargs.get("passphrase"):
            e = Error("param passphrase miss")
        if e:
            logger.error(e, caller=self)
            SingleTask.run(kwargs["error_callback"], e)
            SingleTask.run(kwargs["init_callback"], False)
            return

        self._account = kwargs["account"]
        self._strategy = kwargs["strategy"]
        self._platform = kwargs["platform"]
        self._symbol = kwargs["symbol"]
        self._host = kwargs["host"]
        self._wss = kwargs["wss"]
        self._access_key = kwargs["access_key"]
        self._secret_key = kwargs["secret_key"]
        self._passphrase = kwargs["passphrase"]
        self._order_update_callback = kwargs.get("order_update_callback")
        self._init_callback = kwargs.get("init_callback")
        self._error_callback = kwargs.get("error_callback")

        self._raw_symbol = self._symbol.replace("/", "-")
        self._order_channel = "spot/order:{symbol}".format(
            symbol=self._raw_symbol)

        url = self._wss + "/ws/v3"
        self._ws = Websocket(url,
                             self.connected_callback,
                             process_binary_callback=self.process_binary)

        self._assets = {
        }  # Asset object. e.g. {"BTC": {"free": "1.1", "locked": "2.2", "total": "3.3"}, ... }
        self._orders = {}  # Order objects. e.g. {"order_id": Order, ... }

        # Initializing our REST API client.
        self._rest_api = OKExRestAPI(self._access_key, self._secret_key,
                                     self._passphrase, self._host)

        # Create a loop run task to send ping message to server per 5 seconds.
        LoopRunTask.register(self._send_heartbeat_msg, 5)
예제 #3
0
    def __init__(self, **kwargs):
        """Initialize Trade module."""
        e = None
        if not kwargs.get("account"):
            e = Error("param account miss")
        if not kwargs.get("strategy"):
            e = Error("param strategy miss")
        if not kwargs.get("symbol"):
            e = Error("param symbol miss")
        if not kwargs.get("host"):
            kwargs["host"] = "https://api.binance.com"
        if not kwargs.get("wss"):
            kwargs["wss"] = "wss://stream.binance.com:9443"
        if not kwargs.get("access_key"):
            e = Error("param access_key miss")
        if not kwargs.get("secret_key"):
            e = Error("param secret_key miss")
        if e:
            logger.error(e, caller=self)
            SingleTask.run(kwargs["error_callback"], e)
            SingleTask.run(kwargs["init_callback"], False)

        self._account = kwargs["account"]
        self._strategy = kwargs["strategy"]
        self._platform = kwargs["platform"]
        self._symbol = kwargs["symbol"]
        self._host = kwargs["host"]
        self._wss = kwargs["wss"]
        self._access_key = kwargs["access_key"]
        self._secret_key = kwargs["secret_key"]
        self._order_update_callback = kwargs.get("order_update_callback")
        self._init_callback = kwargs.get("init_callback")
        self._error_callback = kwargs.get("error_callback")

        self._raw_symbol = self._symbol.replace(
            "/", "")  # Row symbol name, same as Binance Exchange.
        self._listen_key = None  # Listen key for Websocket authentication.
        self._assets = {
        }  # Asset data. e.g. {"BTC": {"free": "1.1", "locked": "2.2", "total": "3.3"}, ... }
        self._orders = {}  # Order data. e.g. {order_id: order, ... }

        # Initialize our REST API client.
        self._rest_api = BinanceRestAPI(self._access_key, self._secret_key,
                                        self._host)

        # Create a loop run task to reset listen key every 30 minutes.
        LoopRunTask.register(self._reset_listen_key, 60 * 30)

        # Create a coroutine to initialize Websocket connection.
        SingleTask.run(self._init_websocket)

        LoopRunTask.register(self._send_heartbeat_msg, 10)
예제 #4
0
파일: event.py 프로젝트: zzwpower/aioquant
    def __init__(self):
        self._host = config.rabbitmq.get("host", "localhost")
        self._port = config.rabbitmq.get("port", 5672)
        self._username = config.rabbitmq.get("username", "guest")
        self._password = config.rabbitmq.get("password", "guest")
        self._protocol = None
        self._channel = None  # Connection channel.
        self._connected = False  # If connect success.
        self._subscribers = []  # e.g. `[(event, callback, multi), ...]`
        self._event_handler = {}  # e.g. `{"exchange:routing_key": [callback_function, ...]}`

        # Register a loop run task to check TCP connection's healthy.
        LoopRunTask.register(self._check_connection, 10)

        # Create MQ connection.
        asyncio.get_event_loop().run_until_complete(self.connect())
예제 #5
0
    def __init__(self,
                 url,
                 connected_callback=None,
                 process_callback=None,
                 process_binary_callback=None,
                 check_conn_interval=10):
        """Initialize."""
        self._url = url
        self._connected_callback = connected_callback
        self._process_callback = process_callback
        self._process_binary_callback = process_binary_callback
        self._check_conn_interval = check_conn_interval
        self._ws = None  # Websocket connection object.

        LoopRunTask.register(self._check_connection, self._check_conn_interval)
        SingleTask.run(self._connect)
    def __init__(self) -> None:
        """
        '吃盘口毛刺'的简化策略: 根据实时盘口变化取卖6和卖8的价格,并根据他们的平均价格来挂卖单
        即, average_price = (ask6_price + ask8_price) / 2 取指定位数的数据, EOS是4位;

        在策略启动的时候判断是否有挂单, 如果有挂单, 判断价格是否已经超过 ask6_price 和 ask8_price 的区间,
        如果超过那么撤单后再重新挂单.
        """
        self._symbol = "EOSUSDT"
        self._action = "SELL"
        self._quantity = "2.5"
        self._order_id = ""
        self._price = 0.0

        self._is_ok = False

        params = dict(
            strategy=config.strategy_name,
            platform=config.platform,
            symbol=config.symbol,
            account=config.account,  # 分布式...多个
            access_key=config.access_key,
            secret_key=config.secret_key,
            passphrase=config.passphrase,
            order_update_callback=self.on_order_update_callback,
            init_callback=self.on_init_callback,
            error_callback=self.on_error_callback,
        )

        self._trade = Trade(**params)

        if config.platform == BINANCE:
            LoopRunTask.register(self.dynamic_trade_with_binance, interval=2)
        elif config.platform == HUOBI:
            LoopRunTask.register(self.dynamic_trade_with_huobi, interval=2)
        elif config.platform == OKEX:
            LoopRunTask.register(self.dynamic_trade_with_okex, interval=2)
        else:
            logger.error("platform error:", config.platform, caller=self)
            quant.stop()