Example #1
0
 async def cancel_order(self, client: aiohttp.ClientSession, order_id: int) -> (bool, float):
     """ Cancel a regular order, return success and executed quantity """
     params = {
         'symbol': self.pair['symbol'],
         'orderId': order_id
     }
     try:
         _, resp = await self.req(client, 'DELETE', 'order', params, {})
         CColors.iprint(f'Canceled limit order #{order_id} (status: {resp["status"]})')
         return True, float(resp['executedQty'])
     except BinanceAPI.ApiError as exc:
         CColors.eprint(f'Order cancel failed with {exc.data}')
         return False, 0
Example #2
0
 async def cancel_oco_order(self, client: aiohttp.ClientSession,
                            order_list_id: int) -> (bool, float):
     """ Cancel an OCO order, return success and executed quantity """
     params = {
         'symbol': self.pair['symbol'],
         'orderListId': order_list_id
     }
     try:
         _, resp = await self.req(client, 'DELETE', 'orderList', params, {})
         CColors.iprint(f'Canceled OCO #{order_list_id} (status: {resp["listOrderStatus"]})')
         qty = sum((float(rep['executedQty']) for rep in resp['orderReports']))
         return True, qty
     except BinanceAPI.ApiError as exc:
         CColors.eprint(f'OCO order cancel failed with {exc.data}')
         return False, 0
Example #3
0
    async def sell_coin_market(self, client: aiohttp.ClientSession,
                               bqty: float) -> (bool, float, float):
        """ sell <bqty> of <bcoin> for <qcoin> at market price
            return success, amount of <qcoin> purchased and average trade price """
        bcoin = self.pair['baseAsset']
        CColors.cprint(f'[MARKET SELL] Selling {self.bfmt_mkt(bqty)} {bcoin}',
                       CColors.WARNING)

        params = {
            'symbol': self.pair['symbol'],
            'side': 'SELL',
            'type': 'MARKET',
            'quantity': self.bfmt_mkt(bqty), # sell <bqty> of <bcoin>
        }
        try:
            _, resp = await self.req(client, 'POST', 'order', params, {})
            return (True, *self.market_order_status(resp))
        except BinanceAPI.ApiError as exc:
            CColors.eprint(f'Market sell failed with {exc.data}')
            return False, 0, 0
Example #4
0
    async def buy_coin_market(self, client: aiohttp.ClientSession,
                              qqty: float) -> (bool, float, float):
        """ buy <bcoin> with <qqty> of <qcoin> at market price
            return amount of <bcoin> purchased and average trade price """
        bcoin, qcoin = self.pair['baseAsset'], self.pair['quoteAsset']
        msg = f'[MARKET BUY] Buying {bcoin} with {self.qfmt(qqty)} {qcoin}'
        CColors.cprint(msg, CColors.WARNING)

        params = {
            'symbol': self.pair['symbol'],
            'side': 'BUY',
            'type': 'MARKET',
            'quoteOrderQty': self.qfmt(qqty), # buy with <qqty> of <qcoin>
        }
        try:
            _, resp = await self.req(client, 'POST', 'order', params, {})
            return (True, *self.market_order_status(resp))
        except BinanceAPI.ApiError as exc:
            CColors.eprint(f'Market buy failed with {exc.data}')
            return False, 0, 0
Example #5
0
    async def sell_coin_limit(self, client: aiohttp.ClientSession,
                              bqty: float, price: float) -> int:
        """ sell <bqty> of <bcoin>, return order ID (0 = fail) """
        bcoin = self.pair['baseAsset']
        CColors.cprint(f'[LIMIT SELL] Selling {self.bfmt(bqty)} {bcoin} at {self.qfmt(price)}',
                       CColors.WARNING)

        params = {
            'symbol': self.pair['symbol'],
            'side': 'SELL',
            'type': 'LIMIT',
            'timeInForce': 'GTC', # good till cancelled
            'quantity': self.bfmt(bqty),
            'price': self.qfmt(price),
        }
        try:
            _, resp = await self.req(client, 'POST', 'order', params, {})
            CColors.iprint(f'Executed limit sell order (status: {resp["status"]})')
        except BinanceAPI.ApiError as exc:
            CColors.eprint(f'Limit sell failed with {exc.data}')
            return 0
        return resp['orderId']
Example #6
0
    async def sell_coin_oco(self, client: aiohttp.ClientSession,
                            bqty: float, price: float, sprice: float) -> int:
        """ sell <bqty> of <bcoin> as OCO, return order ID (0 = fail) """
        bcoin = self.pair['baseAsset']
        msg = f'[OCO SELL] Selling {self.bfmt(bqty)} {bcoin} at {self.qfmt(price)}' + \
              f', stop: {self.qfmt(sprice)}'
        CColors.cprint(msg, CColors.WARNING)

        params = {
            'symbol': self.pair['symbol'],
            'side': 'SELL',
            'quantity': self.bfmt(bqty),
            'price': self.qfmt(price),
            'stopPrice': self.qfmt(sprice),
            'stopLimitPrice': self.qfmt(sprice * 0.98),
            'stopLimitTimeInForce': 'GTC' # good till cancelled
        }
        try:
            _, resp = await self.req(client, 'POST', 'order/oco', params, {})
            CColors.iprint(f'Executed OCO order (status: {resp["listOrderStatus"]})')
        except BinanceAPI.ApiError as exc:
            CColors.eprint(f'OCO sell failed with {exc.data}')
            return 0
        return resp['orderListId']