def test_getAccount(): filename = 'config.json' with open(filename) as config_file: config = json.load(config_file) api_key = '' api_secret = '' api_passphrase = '' 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_passphrase = config['api_pass'] api_url = config['api_url'] elif 'api_key' in config['coinbasepro'] and 'api_secret' in config[ 'coinbasepro'] and 'api_passphrase' in config[ 'coinbasepro'] and 'api_url' in config['coinbasepro']: api_key = config['coinbasepro']['api_key'] api_secret = config['coinbasepro']['api_secret'] api_passphrase = config['coinbasepro']['api_passphrase'] api_url = config['coinbasepro']['api_url'] exchange = AuthAPI(api_key, api_secret, api_passphrase, api_url) assert type(exchange) is AuthAPI df = exchange.getAccounts() assert len(df) > 0 account = df[['id']].head(1).values[0][0] assert len(account) > 0 df = exchange.getAccount(account) assert type(df) is pandas.core.frame.DataFrame assert len(df) == 1 actual = df.columns.to_list() expected = [ 'id', 'currency', 'balance', 'hold', 'available', 'profile_id', 'trading_enabled' ] assert len(actual) == len(expected) assert all([a == b for a, b in zip(actual, expected)])
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': resp = self.client.get_account() if 'balances' in resp: df = pd.DataFrame(resp['balances']) df = df[(df['free'] != '0.00000000') & (df['free'] != '0.00')] df['free'] = df['free'].astype(float) df['locked'] = df['locked'].astype(float) df['balance'] = df['free'] - df['locked'] df.columns = ['currency', 'available', 'hold', 'balance'] df = df[['currency', 'balance', 'hold', 'available']] df = df.reset_index(drop=True) 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 == 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)) 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))
import json from models.CoinbasePro import AuthAPI with open('config.json') as config_file: config = json.load(config_file) model = AuthAPI(config['api_key'], config['api_secret'], config['api_pass'], config['api_url']) resp = model.authAPI('GET','orders?status=all') print (resp) accounts = model.getAccounts() print (accounts) #account = model.getAccount('00000000-0000-0000-0000-000000000000') #print (account) orders1 = model.getOrders() print (orders1) orders2 = model.getOrders('BTC-GBP') print (orders2) orders3 = model.getOrders('BTC-GBP', 'buy') print (orders3) orders4 = model.getOrders('BTC-GBP', 'buy', 'done') print (orders4) #order = model.marketBuy('BTC-GBP', 100) #print (order)
def getBalance(self, currency=''): """Retrieves balance either live or simulation Parameters ---------- currency: str, optional Filters orders by currency """ if self.mode == 'live': # if config is provided and live connect to Coinbase Pro account portfolio model = AuthAPI(self.api_key, self.api_secret, self.api_pass, self.api_url) 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 self.truncate( float(df[df['currency'] == currency] ['available'].values[0]), 2) else: return self.truncate( float(df[df['currency'] == currency] ['available'].values[0]), 4) else: # return dummy balances if currency == '': # retrieve all balances return self.balance else: # replace FIAT and CRYPTO placeholders if currency in ['EUR', 'GBP', 'USD']: self.balance = self.balance.replace('FIAT', currency) elif currency in ['BCH', 'BTC', 'ETH', 'LTC', 'XLM']: self.balance = self.balance.replace('CRYPTO', 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 self.truncate( float(df[df['currency'] == currency] ['available'].values[0]), 2) else: return self.truncate( float(df[df['currency'] == currency] ['available'].values[0]), 4)