Example #1
0
def test_getAccount():
    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.getAccount()
    assert type(df) is pandas.core.frame.DataFrame

    actual = df.columns.to_list()
    expected = ['currency', 'balance', 'hold', 'available']
    assert len(actual) == len(expected)
    assert all([a == b for a, b in zip(actual, expected)])
def test_get_account(mocker):
    client_response = {
        'makerCommission': 10,
        'takerCommission': 10,
        'buyerCommission': 0,
        'sellerCommission': 0,
        'canTrade': True,
        'canWithdraw': True,
        'canDeposit': True,
        'updateTime': 1620861508183,
        'accountType': 'SPOT',
        'balances': [
            {'asset': 'BTC', 'free': '0.00000000', 'locked': '0.00000000'},
            {'asset': 'LTC', 'free': '0.00000000', 'locked': '0.24944000'}
        ],
        'permissions': ['SPOT']
    }

    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_account", return_value=client_response)
    df = exchange.getAccount()
    assert type(df) is pandas.core.frame.DataFrame

    actual = df.columns.to_list()
    expected = ['currency', 'balance', 'hold', 'available']
    assert len(actual) == len(expected)
    assert all([a == b for a, b in zip(actual, expected)])
Example #3
0
    def getBalance(self, currency=''):
        """Retrieves balance either live or simulation

        Parameters
        ----------
        currency: str, optional
            Filters orders by currency
        """

        if self.app.getExchange() == 'binance':
            if self.mode == 'live':
                model = BAuthAPI(self.app.getAPIKey(), self.app.getAPISecret())
                df = model.getAccount()
                if isinstance(df, pd.DataFrame):
                    if currency == '':
                        # retrieve all balances
                        return df
                    else:
                        # retrieve balance of specified currency
                        df_filtered = df[df['currency'] ==
                                         currency]['available']
                        if len(df_filtered) == 0:
                            # return nil balance if no positive balance was found
                            return 0.0
                        else:
                            # return balance of specified currency (if positive)
                            if currency in ['EUR', 'GBP', 'USD']:
                                return float(
                                    self.app.truncate(
                                        float(df[df['currency'] == currency]
                                              ['available'].values[0]), 2))
                            else:
                                return float(
                                    self.app.truncate(
                                        float(df[df['currency'] == currency]
                                              ['available'].values[0]), 4))
                else:
                    return 0.0
            else:
                # return dummy balances
                if currency == '':
                    # retrieve all balances
                    return self.balance
                else:
                    if self.app.getExchange() == 'binance':
                        self.balance = self.balance.replace('QUOTE', currency)
                    else:
                        # replace QUOTE and BASE placeholders
                        if currency in ['EUR', 'GBP', 'USD']:
                            self.balance = self.balance.replace(
                                'QUOTE', currency)
                        else:
                            self.balance = self.balance.replace(
                                'BASE', currency)

                    if self.balance.currency[self.balance.currency.isin(
                        [currency])].empty:
                        self.balance.loc[len(
                            self.balance)] = [currency, 0, 0, 0]

                    # retrieve balance of specified currency
                    df = self.balance
                    df_filtered = df[df['currency'] == currency]['available']

                    if len(df_filtered) == 0:
                        # return nil balance if no positive balance was found
                        return 0.0
                    else:
                        # return balance of specified currency (if positive)
                        if currency in ['EUR', 'GBP', 'USD']:
                            return float(
                                self.app.truncate(
                                    float(df[df['currency'] == currency]
                                          ['available'].values[0]), 2))
                        else:
                            return float(
                                self.app.truncate(
                                    float(df[df['currency'] == currency]
                                          ['available'].values[0]), 4))

        else:
            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())
                if currency == '':
                    # retrieve all balances
                    return model.getAccounts()[[
                        'currency', 'balance', 'hold', 'available'
                    ]]
                else:
                    df = model.getAccounts()
                    # retrieve balance of specified currency
                    df_filtered = df[df['currency'] == currency]['available']
                    if len(df_filtered) == 0:
                        # return nil balance if no positive balance was found
                        return 0.0
                    else:
                        # return balance of specified currency (if positive)
                        if currency in ['EUR', 'GBP', 'USD']:
                            return float(
                                self.app.truncate(
                                    float(df[df['currency'] == currency]
                                          ['available'].values[0]), 2))
                        else:
                            return float(
                                self.app.truncate(
                                    float(df[df['currency'] == currency]
                                          ['available'].values[0]), 4))

            else:
                # return dummy balances

                if currency == '':
                    # retrieve all balances
                    return self.balance
                else:
                    # replace QUOTE and BASE placeholders
                    if currency in ['EUR', 'GBP', 'USD']:
                        self.balance = self.balance.replace('QUOTE', currency)
                    elif currency in ['BCH', 'BTC', 'ETH', 'LTC', 'XLM']:
                        self.balance = self.balance.replace('BASE', currency)

                    if self.balance.currency[self.balance.currency.isin(
                        [currency])].empty == True:
                        self.balance.loc[len(
                            self.balance)] = [currency, 0, 0, 0]

                    # retrieve balance of specified currency
                    df = self.balance
                    df_filtered = df[df['currency'] == currency]['available']

                    if len(df_filtered) == 0:
                        # return nil balance if no positive balance was found
                        return 0.0
                    else:
                        # return balance of specified currency (if positive)
                        if currency in ['EUR', 'GBP', 'USD']:
                            return float(
                                self.app.truncate(
                                    float(df[df['currency'] == currency]
                                          ['available'].values[0]), 2))
                        else:
                            return float(
                                self.app.truncate(
                                    float(df[df['currency'] == currency]
                                          ['available'].values[0]), 4))