예제 #1
0
        async def call_api():
            async with websockets.connect(url) as websocket:
                # authenticate
                await websocket.send(self.getAuthenticationJson())
                while websocket.open:
                    response = await websocket.recv()
                    j = json.loads(response)
                    if self.isErrorResponse("authentication", j):
                        return

                    # look up the tick size based on symbol
                    await websocket.send(self.getInstrumentsJson(self.currency)
                                         )
                    response = await websocket.recv()
                    j = json.loads(response)
                    if self.isErrorResponse("instrument request", j):
                        return
                    precision = next(
                        (item for item in j.get("result")
                         if item["instrument_name"] == alert.symbol),
                        None).get("tick_size")

                    # get the account information
                    await websocket.send(self.getAccountInfoJson(self.currency)
                                         )
                    response = await websocket.recv()
                    j = json.loads(response)
                    if self.isErrorResponse("account info request", j):
                        return
                    accountInfo = j.get("result")

                    for block in alert.blocks:
                        if block and block.type:
                            if block.wait:
                                self.logger.info("Waiting %s seconds",
                                                 block.wait)
                                time.sleep(int(block.wait))

                            self.logger.info("Executing Block: %s",
                                             block.type.value)

                            if block.type == BlockType.CANCEL_ORDER:
                                await self.cancelOrder(websocket, alert, block)
                            elif block.type == BlockType.CLOSE_POSITION:
                                await self.closePosition(
                                    websocket, precision, alert, block)
                            elif block.type == BlockType.STANDARD_ORDER:
                                await self.trade(websocket, precision,
                                                 accountInfo, alert, block)
                            elif block.type == BlockType.PLUGIN:
                                continueProcessingBlocks = PluginLoader.processPluginBlock(
                                    alert, block)
                                if not continueProcessingBlocks:
                                    self.logger.warning(
                                        "Plugin %s returned False, skipping remaining blocks for alert",
                                        block.plugin)
                                    return

                    # close the websocket
                    await websocket.close()
예제 #2
0
    def processAlert(self, alert):
        if not alert.blocks:
            self.logger.error("No blocks found for alert")
            return

        for block in alert.blocks:
            if block and block.type:
                if block.wait:
                    self.logger.info("Waiting %s seconds", block.wait)
                    time.sleep(int(block.wait))

                self.logger.info("Executing Block: %s", block.type.value)

                if block.type == BlockType.CANCEL_ORDER:
                    self.cancelOrders(alert, block)
                elif block.type == BlockType.CLOSE_POSITION:
                    self.closePosition(alert, block)
                elif block.type == BlockType.STANDARD_ORDER:
                    self.trade(alert, block)
                elif block.type == BlockType.ADJUST_POSITION:
                    self.adjustPosition(alert, block)
                elif block.type == BlockType.PLUGIN:
                    continueProcessingBlocks = PluginLoader.processPluginBlock(
                        alert, block)
                    if not continueProcessingBlocks:
                        self.logger.warning(
                            "Plugin %s returned False, skipping remaining blocks for alert",
                            block.plugin)
                        return