コード例 #1
0
ファイル: BitMEX.py プロジェクト: mikeyQuantR/TraderBot
    async def amend_order(self, orderID=None, origClOrdID=None, clOrdID=None, orderQty=None, leavesQty=None, price=None, stopPx=None, pegOffsetValue=None, text=None):
        """
        Amend the quantity or price of an open order.
        Send an orderID or origClOrdID to identify the order you wish to amend.
        Both order quantity and price can be amended. Only one qty field can be used to amend.
        Use the leavesQty field to specify how much of the order you wish to remain open. 
        This can be useful if you want to adjust your position's delta by a certain amount, regardless of how much of the order has already filled.
        A leavesQty can be used to make a "Filled" order live again, if it is received within 60 seconds of the fill.
        """
        if orderID == None and origClOrdID == None:
            raise InvalidArgError([orderID, origClOrdID], "Must submit either orderID or origClOrdID to amend order(s).")
        
        path = "order"
        postdict = {}

        if orderID:         postdict['orderID']         = orderID
        if origClOrdID:     postdict['origClOrdID']     = origClOrdID
        if clOrdID:         postdict['clOrdID']         = clOrdID
        if orderQty:        postdict['orderQty']        = orderQty
        if leavesQty:       postdict['leavesQty']       = leavesQty
        if price:           postdict['price']           = price
        if stopPx:          postdict['stopPx']          = stopPx
        if pegOffsetValue:  postdict['pegOffsetValue']  = pegOffsetValue
        if text:            postdict['text']            = text

        return await self._http_request(path=path, postdict=postdict, verb='PUT')
コード例 #2
0
 def is_tickSize_valid(self, price):
     """Check for valid tickSize, ie if the price is an integer or .5 away."""
     if not isinstance(price, int):
         if not isinstance(price + 0.5, int):
             raise InvalidArgError(
                 price,
                 "Invalid Tick Size! Prices must be set at integer or .5 between"
             )
コード例 #3
0
ファイル: BitMEX.py プロジェクト: mikeyQuantR/TraderBot
    async def place_bulk_order(self, orders):
        """Place a bulk order via REST API."""
        if len(orders) < 1:
            raise InvalidArgError(orders, "Orders must be more than 1 order.")

        endpoint = "order/bulk"
        allOrders = {'orders': orders}
        return await self._http_request(path=endpoint, postdict=allOrders, verb="POST")
コード例 #4
0
ファイル: BitMEX.py プロジェクト: mikeyQuantR/TraderBot
    async def cancel_order(self, orderID=None, clOrdID=None, text=None):
        """Cancel an existing order(s) by submitting orderID(s) or clOrdID(s) (as a string). Supply optional text to annotate cancellation."""
        if orderID == None and clOrdID == None:
            raise InvalidArgError([orderID, clOrdID], "Must submit either orderID or clOrdID to cancel order(s).")

        path = "order"
        postdict = {}
        if orderID:   postdict['orderID'] = orderID
        elif clOrdID: postdict['clOrdID'] = clOrdID
        if text:      postdict['text'] = text

        return await self._http_request(path=path, postdict=postdict, verb="DELETE")
コード例 #5
0
ファイル: Trade.py プロジェクト: sfhemstreet/TraderBot
    def close(self, quantity=None, side=None, symbol=None, orderIDPrefix=None):
        '''
        Returns a close order ready to send to Bitmex.

        cancel other active limit orders with the same side and symbol if the open quantity exceeds the current position.

        Side or quantity required.
        --------------------------

        Parameters:

        `quantity: int`
            number of contracts to close

        `side: str`
            Buy or Sell

        `symbol: str`
            symbol to apply close to

        `orderIDPrefix: str`
            label prefix of close, if none supplied uses default

        Returns:

        `order: dict`
            close order
        
        '''
        if symbol == None:
            symbol = self.symbol

        if orderIDPrefix == None:
            orderIDPrefix = self.orderIDPrefex

        if quantity == None and side == None:
            raise InvalidArgError([quantity, side],
                                  "Side or quantity required to close.")

        clOrdID = orderIDPrefix + base64.b64encode(
            uuid.uuid4().bytes).decode('utf8').rstrip('=\n')
        order = {'symbol': symbol, 'clOrdID': clOrdID, 'execInst': 'Close'}
        if quantity: order['orderQty'] = quantity,
        if side: order['side'] = side

        return order
コード例 #6
0
    def close(self, quantity=None, side=None, symbol=None, orderIDPrefix=None):
        '''
        Returns a close order ready to send to Bitmex.
        cancel other active limit orders with the same side and symbol if the open quantity exceeds the current position.
        Side or quantity required.
        '''
        if symbol == None:
            symbol = self.symbol

        if orderIDPrefix == None:
            orderIDPrefix = self.orderIDPrefex

        if quantity == None and side == None:
            raise InvalidArgError([quantity, side],
                                  "Side or quantity required to close.")

        clOrdID = orderIDPrefix + base64.b64encode(
            uuid.uuid4().bytes).decode('utf8').rstrip('=\n')
        order = {'symbol': symbol, 'clOrdID': clOrdID, 'execInst': 'Close'}
        if quantity: order['orderQty'] = quantity,
        if side: order['side'] = side

        return order
コード例 #7
0
    async def cancel_order(self, orderID=None, clOrdID=None, text=None):
        """Cancel an existing order(s) by submitting orderID(s) or clOrdID(s). 
        
        Supply optional text to annotate cancellation.

        async func - use await
        
        Parameters:

        `orderID: str`
            orderID of order to cancel

        `clOrdID: str`
            clOrdID of order to cancel

        `text: str`
            annnotaion text

        Returns:

        `orderInfo: json data`
            bitmex response
        """
        if orderID == None and clOrdID == None:
            raise InvalidArgError(
                [orderID, clOrdID],
                "Must submit either orderID or clOrdID to cancel order(s).")

        path = "order"
        postdict = {}
        if orderID: postdict['orderID'] = orderID
        elif clOrdID: postdict['clOrdID'] = clOrdID
        if text: postdict['text'] = text

        return await self._http_request(path=path,
                                        postdict=postdict,
                                        verb="DELETE")
コード例 #8
0
    async def place_bulk_order(self, orders):
        """Place a bulk order on Bitmex exchange.

        async func - use await
        
        Parameters:

        `orders: array<dict>`
            array of orders ( use Trade class to make orders )

        Returns:
            
        `orderInfo: json data`
            bitmex response

        """
        if len(orders) < 1:
            raise InvalidArgError(orders, "Invalid bulk order number.")

        endpoint = "order/bulk"
        allOrders = {'orders': orders}
        return await self._http_request(path=endpoint,
                                        postdict=allOrders,
                                        verb="POST")
コード例 #9
0
    async def amend_order(self,
                          orderID=None,
                          origClOrdID=None,
                          clOrdID=None,
                          orderQty=None,
                          leavesQty=None,
                          price=None,
                          stopPx=None,
                          pegOffsetValue=None,
                          text=None):
        """
        Amend the quantity or price of an open order.

        Send an orderID or origClOrdID to identify the order you wish to amend.

        Both order quantity and price can be amended. Only one qty field can be used to amend.

        Use the leavesQty field to specify how much of the order you wish to remain open. 
        This can be useful if you want to adjust your position's delta by a certain amount, regardless of how much of the order has already filled.
        A leavesQty can be used to make a "Filled" order live again, if it is received within 60 seconds of the fill.

        async func - use await

        Parameters:

        `orderID: str`
            order ID

        `origClOrdID: str`
            the orginal clOrdID

        `clOrdID: str`
            clOrdID for amend

        `orderQty: int`
            order quantity

        `leavesQty: int`
            how much of the order you wish to remain open
        
        `price: float`
            price to amend to, use valid tickSize, ie integer or .5

        `stopPx: float`
            stop to amend

        `pegOffsetValue: float`
            pegOffset Value

        `text: str`
            annotate amend for records

        Returns:

        `orderInfo: json data`
            bitmex response

        """
        if orderID == None and origClOrdID == None:
            raise InvalidArgError(
                [orderID, origClOrdID],
                "Must submit either orderID or origClOrdID to amend order(s).")

        path = "order"
        postdict = {}

        if orderID: postdict['orderID'] = orderID
        if origClOrdID: postdict['origClOrdID'] = origClOrdID
        if clOrdID: postdict['clOrdID'] = clOrdID
        if orderQty: postdict['orderQty'] = orderQty
        if leavesQty: postdict['leavesQty'] = leavesQty
        if price: postdict['price'] = price
        if stopPx: postdict['stopPx'] = stopPx
        if pegOffsetValue: postdict['pegOffsetValue'] = pegOffsetValue
        if text: postdict['text'] = text

        return await self._http_request(path=path,
                                        postdict=postdict,
                                        verb='PUT')
コード例 #10
0
    def make_order(self,
                   quantity=None,
                   symbol=None,
                   price=None,
                   side=None,
                   orderType=None,
                   displayQty=None,
                   stopPx=None,
                   pegOffsetValue=None,
                   pegPriceType=None,
                   timeInForce=None,
                   execInst=None,
                   orderIDPrefix=None):
        """
            Make your own order.
            Returns an order object ready to use in bulk or single order. 
            Must supply quantity. 
            If no price supplied, order is market sell or buy.
            side is either 'Buy' or 'Sell'
            displayQty of 0 hides order
            stopPx, optional trigger price, use a price below the current price for stop-sell orders and buy-if-touched orders.
            For more info see Bitmex API explorer.
        """
        # Read Me - This creates all the orders below, so dont break it

        if price and price < 0:
            raise InvalidArgError(price, "Order Price must be positive.")

        if price:
            self.is_tickSize_valid(price)

        if not quantity and execInst != 'Close':
            raise InvalidArgError([quantity, execInst],
                                  "Must supply order quantity.")

        if side and side != 'Sell' and side != 'Buy':
            raise InvalidArgError(side, "Side must be 'Sell' or 'Buy'.")

        if orderType and (orderType != 'Market' or orderType != 'Limit'
                          or orderType != 'Stop' or orderType != 'StopLimit'
                          or orderType != 'MarketIfTouched'
                          or orderType != 'LimitIfTouched'
                          or orderType != 'Pegged'):
            raise InvalidArgError(
                orderType,
                "orderType must be Market, Limit, Stop, StopLimit, MarketIfTouched, LimitIfTouched, or Pegged"
            )

        if displayQty and displayQty < 0:
            raise InvalidArgError(
                displayQty,
                "DisplayQty is negative, must be 0 to hide order or positive.")

        if pegPriceType and (pegPriceType != 'LastPeg'
                             or pegPriceType != 'MidPricePeg'
                             or pegPriceType != 'MarketPeg'
                             or pegPriceType != 'PrimaryPeg'
                             or pegPriceType != 'TrailingStopPeg'):
            raise InvalidArgError(
                pegPriceType,
                "pegPriceType must be LastPeg, MidPricePeg, MarketPeg, PrimaryPeg, or TrailingStopPeg."
            )

        if timeInForce and (timeInForce != 'Day'
                            or timeInForce != 'GoodTillCancel'
                            or timeInForce != 'ImmediateOrCancel'
                            or timeInForce != 'FillOrKill'):
            raise InvalidArgError(
                timeInForce,
                "timeInForce must be Day, GoodTillCancel, ImmediateOrCancel, or FillOrKill"
            )

        if execInst and (execInst != 'ParticipateDoNotInitiate'
                         or execInst != 'AllOrNone' or execInst != 'MarkPrice'
                         or execInst != 'IndexPrice' or execInst != 'LastPrice'
                         or execInst != 'Close' or execInst != 'ReduceOnly'
                         or execInst != 'Fixed'):
            raise InvalidArgError(
                execInst,
                "execInst must be ParticipateDoNotInitiate, AllOrNone, MarkPrice, IndexPrice, LastPrice, Close, ReduceOnly, or Fixed."
            )

        if execInst and execInst == 'AllOrNone' and displayQty != 0:
            raise InvalidArgError(
                [execInst, displayQty],
                "execInst is 'AllOrNone', displayQty must be 0" + c[0])

        if symbol == None:
            symbol = self.symbol

        if orderIDPrefix == None:
            orderIDPrefix = self.orderIDPrefex

        # Generate a unique clOrdID with our prefix so we can identify it.
        clOrdID = orderIDPrefix + base64.b64encode(
            uuid.uuid4().bytes).decode('utf8').rstrip('=\n')
        order = {
            'symbol': symbol,
            'clOrdID': clOrdID,
        }
        # add to order if supplied
        if quantity: order['orderQty'] = quantity
        if side: order['side'] = side
        if price: order['price'] = price
        if displayQty: order['displayQty'] = displayQty
        if stopPx: order['stopPx'] = stopPx
        if pegOffsetValue: order['pegOffsetValue'] = pegOffsetValue
        if timeInForce: order['timeInForce'] = timeInForce
        if execInst: order['execInst'] = execInst
        if orderType: order['ordType'] = orderType

        return order