def get_current_gains(user): api_key, api_secret = os.environ.get( f'API_KEY_{user.upper()}'), os.environ.get( f'API_SECRET_{user.upper()}') client = Client(api_key, api_secret) nat_curr = os.environ.get('NAT_CURRENCY', 'EUR') accounts = client.get_accounts() gains = [] for acc in accounts.data: # Getting general info id, curr, name = acc.id, acc.balance.currency, acc.name # Getting coin balance coin_balance = float(acc.balance.amount) # Getting market price if curr == 'EUR' or coin_balance == 0.0: continue sell_price = float( client._make_api_object( client._get('v2', 'prices', f'{curr.upper()}-{nat_curr}', 'sell'), APIObject).amount) buy_price = float( client._make_api_object( client._get('v2', 'prices', f'{curr.upper()}-{nat_curr}', 'buy'), APIObject).amount) # Getting fiat payments and balance fiat_balance = coin_balance * sell_price * (1 - SELL_FEE) fiat_buys = sum([ float(b['total']['amount']) for b in client.get_buys(id).data if b['status'] == 'completed' ]) gains.append({ 'currency': curr, 'name': name, 'fiat_buys': fiat_buys, 'fiat_balance': fiat_balance, 'coin_balance': coin_balance, 'buy_price': buy_price, 'sell_price': sell_price, }) return gains
def get_eth_price(self): client = Client(os.environ.get('COINBASE_KEY'), os.environ.get('COINBASE_SECRET'), api_version='2017-05-26') # rates = client.get_spot_price(currency_pair='ETH-USD') spot = client._make_api_object( client._get('v2', 'prices', 'ETH-USD', 'spot'), APIObject) current_time = datetime.datetime.utcnow().strftime( "%a %b %d %Y %H:%M:%S UTC") print('{} ETH price is {}'.format(current_time, spot.amount)) self.check_buy_price(spot.amount) return spot
class Coinbase: def __init__(self, api_key, api_secret, currency_pairs): self.client = Client(api_key, api_secret) self.currencyPair = currency_pairs self.startingBalUSD = 5000 self.startingBalETH = 0 self.balanceUSD = 5000 self.balanceBTC = 0 self.balanceETH = 0 self.balanceLTC = 0 self.profit = 0 self.buys = [] self.sells = [] self.coinbaseFee = 1.49 self.tempBuyPrice = self.GetBuyPrice() self.tempSellPrice = self.GetSellPrice() print 'Starting Balances: ETH %s USD %s Profit $%s' % ( self.startingBalETH, '{0:,.2f}'.format(self.startingBalUSD), '0') def GetBuyPrice(self): # Returns Buy Price including fees price = self.client._make_api_object( self.client._get('v2', 'prices', self.currencyPair, 'buy'), APIObject) return float(price["amount"]) + self.GetFee(float(price["amount"])) def GetSellPrice(self): # Returns Sell Price including fees price = self.client._make_api_object( self.client._get('v2', 'prices', self.currencyPair, 'sell'), APIObject) return float(price["amount"]) - self.GetFee(float(price["amount"])) def GetFee(self, price): # Calculates the fee for a given price fee = float((price / (100 / self.coinbaseFee))) return fee def Buy(self, amount): # Buy crypto total = self.GetBuyPrice() * amount if (self.balanceUSD - total) > 0: # If there is enough fiat in the account self.buys.append(total) self.balanceUSD -= total self.balanceETH += amount self.GetCurrentBalance() def Sell(self, amount): # Sell crypto total = self.GetSellPrice() * amount self.sells.append(total) self.profit = float((self.balanceUSD + total) - self.startingBalUSD) self.balanceUSD += total self.balanceETH -= amount self.GetCurrentBalance() def GetCurrentBalance(self): print 'Current Balances: ETH %s USD %s Profit $%s' % ( self.balanceETH, '{0:,.2f}'.format(self.balanceUSD), self.profit) def GetSpread(self): if self.tempBuyPrice != self.GetBuyPrice() or \ self.tempSellPrice != self.GetSellPrice(): # Print spread if Buy or Sell price has changed self.tempBuyPrice = self.GetBuyPrice() self.tempSellPrice = self.GetSellPrice() print ' Buy: $%s Sell: $%s' % (self.GetBuyPrice(), self.GetSellPrice())