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(truncate(float(df[df['currency'] == currency]['available'].values[0]), 2)) else: return float(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(truncate(float(df[df['currency'] == currency]['available'].values[0]), 2)) else: return float(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(truncate(float(df[df['currency'] == currency]['available'].values[0]), 2)) else: return float(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(truncate(float(df[df['currency'] == currency]['available'].values[0]), 2)) else: return float(truncate(float(df[df['currency'] == currency]['available'].values[0]), 4))
def getBalance(self, currency=""): """Retrieves balance either live or simulation Parameters ---------- currency: str, optional Filters orders by currency """ if self.app.getExchange() == Exchange.KUCOIN: if self.mode == 'live': model = KAuthAPI( self.app.getAPIKey(), self.app.getAPISecret(), self.app.getAPIPassphrase(), self.app.getAPIURL(), use_cache=self.app.useKucoinCache(), ) trycnt, maxretry = (0, 5) while trycnt <= maxretry: df = model.getAccounts() if isinstance(df, pd.DataFrame) and len(df) > 0: 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( truncate( float( df[df['currency'] == currency] ['available'].values[0]), 2)) else: return float( truncate( float( df[df['currency'] == currency] ['available'].values[0]), 4)) else: time.sleep(5) trycnt += 1 else: return 0.0 else: # return dummy balances if currency == '': # retrieve all balances return self.balance else: self.balance = self.balance.replace('QUOTE', 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( truncate( float(df[df['currency'] == currency] ['available'].values[0]), 2)) else: return float( truncate( float(df[df['currency'] == currency] ['available'].values[0]), 4)) elif self.app.getExchange() == Exchange.BINANCE: if self.mode == "live": model = BAuthAPI( self.app.getAPIKey(), self.app.getAPISecret(), self.app.getAPIURL(), recv_window=self.app.getRecvWindow(), ) df = model.getAccount() if isinstance(df, pd.DataFrame): if currency == "": # retrieve all balances return df else: # return nil if dataframe is empty if len(df) == 0: return 0.0 # 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( truncate( float(df[df['currency'] == currency] ['available'].values[0]), 2)) else: return float( 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() == Exchange.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( truncate( float(df[df['currency'] == currency] ['available'].values[0]), 2)) else: return float( truncate( float(df[df['currency'] == currency] ['available'].values[0]), 4)) elif 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(), ) trycnt, maxretry = (0, 5) while trycnt <= maxretry: df = model.getAccounts() if len(df) > 0: # retrieve all balances, but check the resp if currency == "" and "balance" not in df: time.sleep(5) trycnt += 1 # retrieve all balances and return elif currency == "": 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( truncate( float( df[df['currency'] == currency] ['available'].values[0]), 2)) else: return float( truncate( float( df[df['currency'] == currency] ['available'].values[0]), 4)) else: time.sleep(5) trycnt += 1 else: return 0.0 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( truncate( float(df[df['currency'] == currency] ['available'].values[0]), 2)) else: return float( truncate( float(df[df['currency'] == currency] ['available'].values[0]), 4)) else: # dummy account if currency == "": # retrieve all balances return self.balance else: # 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) return float( df[df["currency"] == currency]["available"].values[0])
def test_truncate(f, n, expected): assert truncate(f, n) == expected # make sure nothing breaks compatibility with partial() assert functools.partial(truncate, n=n)(f) == expected