Example #1
0
def test_getOrdersInvalidStatus():
    filename = 'config.json'

    with open(filename) as config_file:
        config = json.load(config_file)

        api_key = ''
        api_secret = ''
        api_url = ''
        if 'api_key' in config and 'api_secret' in config and 'api_pass' in config and 'api_url' in config:
            api_key = config['api_key']
            api_secret = config['api_secret']
            api_url = config['api_url']
            AuthAPI(api_key, api_secret, api_url)
        elif 'api_key' in config['binance'] and 'api_secret' in config[
                'binance'] and 'api_url' in config['binance']:
            api_key = config['binance']['api_key']
            api_secret = config['binance']['api_secret']
            api_url = config['binance']['api_url']
            AuthAPI(api_key, api_secret, api_url)

    exchange = AuthAPI(api_key, api_secret, api_url)
    assert type(exchange) is AuthAPI

    with pytest.raises(ValueError) as execinfo:
        exchange.getOrders(status='ERROR')
    assert str(execinfo.value) == 'Invalid order status.'
Example #2
0
    def getOrders(self, market='', action='', status='all'):
        """Retrieves orders either live or simulation

        Parameters
        ----------
        market : str, optional
            Filters orders by market
        action : str, optional
            Filters orders by action
        status : str
            Filters orders by status, defaults to 'all'
        """

        # validate market is syntactically correct
        self._checkMarketSyntax(market)

        if action != '':
            # validate action is either a buy or sell
            if not action in ['buy', 'sell']:
                raise ValueError('Invalid order action.')

        # validate status is open, pending, done, active or all
        if not status in [
                'open', 'pending', 'done', 'active', 'all', 'filled'
        ]:
            raise ValueError('Invalid order status.')

        if self.app.getExchange() == 'binance':
            if self.mode == 'live':
                # if config is provided and live connect to Binance account portfolio
                model = BAuthAPI(self.app.getAPIKey(), self.app.getAPISecret(),
                                 self.app.getAPIURL())
                # retrieve orders from live Binance account portfolio
                self.orders = model.getOrders(market, action, status)
                return self.orders
            else:
                # return dummy orders
                if market == '':
                    return self.orders
                else:
                    return self.orders[self.orders['market'] == market]
        if self.app.getExchange() == 'coinbasepro':
            if self.mode == 'live':
                # if config is provided and live connect to Coinbase Pro account portfolio
                model = CBAuthAPI(self.app.getAPIKey(),
                                  self.app.getAPISecret(),
                                  self.app.getAPIPassphrase(),
                                  self.app.getAPIURL())
                # retrieve orders from live Coinbase Pro account portfolio
                self.orders = model.getOrders(market, action, status)
                return self.orders
            else:
                # return dummy orders
                if market == '':
                    return self.orders
                else:
                    return self.orders[self.orders['market'] == market]
Example #3
0
def test_getOrdersValidActionSell():
    filename = 'config.json'

    with open(filename) as config_file:
        config = json.load(config_file)

        api_key = ''
        api_secret = ''
        api_url = ''
        if 'api_key' in config and 'api_secret' in config and 'api_pass' in config and 'api_url' in config:
            api_key = config['api_key']
            api_secret = config['api_secret']
            api_url = config['api_url']
            AuthAPI(api_key, api_secret, api_url)
        elif 'api_key' in config['binance'] and 'api_secret' in config[
                'binance'] and 'api_url' in config['binance']:
            api_key = config['binance']['api_key']
            api_secret = config['binance']['api_secret']
            api_url = config['binance']['api_url']
            AuthAPI(api_key, api_secret, api_url)

    exchange = AuthAPI(api_key, api_secret, api_url)
    assert type(exchange) is AuthAPI

    df = exchange.getOrders(market=VALID_ORDER_MARKET, action='sell')

    assert len(df) >= 0

    actual = df.columns.to_list()
    expected = [
        'created_at', 'market', 'action', 'type', 'size', 'filled', 'status',
        'price'
    ]
    assert len(actual) == len(expected)
    assert all([a == b for a, b in zip(actual, expected)])
Example #4
0
def test_getOrdersValidStatusActive():
    filename = 'config.json'

    with open(filename) as config_file:
        config = json.load(config_file)

        api_key = ''
        api_secret = ''
        api_url = ''
        if 'api_key' in config and 'api_secret' in config and 'api_pass' in config and 'api_url' in config:
            api_key = config['api_key']
            api_secret = config['api_secret']
            api_url = config['api_url']
            AuthAPI(api_key, api_secret, api_url)
        elif 'api_key' in config['binance'] and 'api_secret' in config['binance'] and 'api_url' in config['binance']:
            api_key = config['binance']['api_key']
            api_secret = config['binance']['api_secret']
            api_url = config['binance']['api_url']
            AuthAPI(api_key, api_secret, api_url)

    exchange = AuthAPI(api_key, api_secret, api_url)
    assert type(exchange) is AuthAPI

    df = exchange.getOrders(market=VALID_ORDER_MARKET, status='active')

    if len(df) == 0:
        pass
    else:
        actual = df.columns.to_list()
        expected = ['created_at', 'market', 'action', 'type', 'size', 'filled', 'status', 'price']
        #  order is not important, but no duplicate
        assert len(actual) == len(expected)
        diff = set(actual) ^ set(expected)
        assert not diff
Example #5
0
def test_get_orders(mocker):
    client_response = [{
        'symbol': 'CHZUSDT',
        'orderId': 123456789,
        'orderListId': -1,
        'clientOrderId': 'SOME-CLIENT-ORDER-ID',
        'price': '0.00000000',
        'origQty': '31.30000000',
        'executedQty': '31.30000000',
        'cummulativeQuoteQty': '15.68161300',
        'status': 'FILLED',
        'timeInForce': 'GTC',
        'type': 'MARKET',
        'side': 'SELL',
        'stopPrice': '0.00000000',
        'icebergQty': '0.00000000',
        'time': 1616845743872,
        'updateTime': 1616845743872,
        'isWorking': True,
        'origQuoteOrderQty': '0.00000000'
    }]

    with open(_get_config_file()) as config_file:
        config = json.load(config_file)

        api_key = ''
        api_secret = ''
        api_url = ''
        if 'api_key' in config and 'api_secret' in config and 'api_pass' in config and 'api_url' in config:
            api_key = config['api_key']
            api_secret = config['api_secret']
            api_url = config['api_url']
            AuthAPI(api_key, api_secret, api_url)
        elif 'api_key' in config['binance'] and 'api_secret' in config[
                'binance'] and 'api_url' in config['binance']:
            api_key = config['binance']['api_key']
            api_secret = config['binance']['api_secret']
            api_url = config['binance']['api_url']
            AuthAPI(api_key, api_secret, api_url)

    exchange = AuthAPI(api_key, api_secret, api_url)
    assert type(exchange) is AuthAPI
    mocker.patch("models.exchange.binance.Client.get_all_orders",
                 return_value=client_response)
    df = exchange.getOrders(MOCK_MARKET)

    assert len(df) > 0

    actual = df.columns.to_list()
    expected = [
        'created_at', 'market', 'action', 'type', 'size', 'filled', 'status',
        'price'
    ]
    #  order is not important, but no duplicate
    assert len(actual) == len(expected)
    diff = set(actual) ^ set(expected)
    assert not diff
Example #6
0
def test_get_orders():
    global app
    exchange = AuthAPI(app.api_key, app.api_secret)
    assert type(exchange) is AuthAPI

    df = exchange.getOrders()
    assert type(df) is pandas.core.frame.DataFrame
    assert len(df) > 0

    actual = df.columns.to_list()
    expected = [
        "created_at",
        "market",
        "action",
        "type",
        "size",
        "filled",
        "status",
        "price",
    ]
    assert len(actual) == len(expected)
    diff = set(actual) ^ set(expected)
    assert not diff
Example #7
0
    def getOrders(self, market="", action="", status="all"):
        """Retrieves orders either live or simulation

        Parameters
        ----------
        market : str, optional
            Filters orders by market
        action : str, optional
            Filters orders by action
        status : str
            Filters orders by status, defaults to 'all'
        """

        # validate market is syntactically correct
        self._checkMarketSyntax(market)

        if action != "":
            # validate action is either a buy or sell
            if not action in ["buy", "sell"]:
                raise ValueError("Invalid order action.")

        # validate status is open, pending, done, active or all
        if not status in [
                "open", "pending", "done", "active", "all", "filled"
        ]:
            raise ValueError("Invalid order status.")

        if self.app.getExchange() == Exchange.BINANCE:
            if self.mode == "live":
                # if config is provided and live connect to Binance account portfolio
                model = BAuthAPI(
                    self.app.getAPIKey(),
                    self.app.getAPISecret(),
                    self.app.getAPIURL(),
                    recv_window=self.app.getRecvWindow(),
                )
                # retrieve orders from live Binance account portfolio
                self.orders = model.getOrders(market, action, status)
                return self.orders
            else:
                # return dummy orders
                if market == "":
                    return self.orders
                else:
                    return self.orders[self.orders["market"] == market]

        if self.app.getExchange() == Exchange.KUCOIN:
            if self.mode == 'live':
                # if config is provided and live connect to Kucoin account portfolio
                model = KAuthAPI(
                    self.app.getAPIKey(),
                    self.app.getAPISecret(),
                    self.app.getAPIPassphrase(),
                    self.app.getAPIURL(),
                    use_cache=self.app.useKucoinCache(),
                )
                # retrieve orders from live Kucoin account portfolio
                self.orders = model.getOrders(market, action, status)
                return self.orders
            else:
                if market == '':
                    return self.orders
                else:
                    return self.orders[self.orders['market'] == market]

        if self.app.getExchange() == Exchange.COINBASEPRO:
            if self.mode == "live":
                # if config is provided and live connect to Coinbase Pro account portfolio
                model = CBAuthAPI(
                    self.app.getAPIKey(),
                    self.app.getAPISecret(),
                    self.app.getAPIPassphrase(),
                    self.app.getAPIURL(),
                )
                # retrieve orders from live Coinbase Pro account portfolio
                self.orders = model.getOrders(market, action, status)
                return self.orders
            else:
                # return dummy orders
                if market == "":
                    return self.orders
                else:
                    if "market" in self.orders:
                        return self.orders[self.orders["market"] == market]
                    else:
                        return pd.DataFrame()
        if self.app.getExchange() == Exchange.DUMMY:
            return self.orders[[
                "created_at",
                "market",
                "action",
                "type",
                "size",
                "filled",
                "fees",
                "price",
                "status",
            ]]