def main(event, context): COINBASE_API_KEY = os.environ['COINBASE_API_KEY'] COINBASE_API_SECRET = os.environ['COINBASE_API_SECRET'] DYNAMODB_TABLE = os.environ['bitcoin_data_table'] dynamodb_client = boto3.client('dynamodb') sns_client = boto3.client('sns') # Initialises coinbase client client = Client(COINBASE_API_KEY, COINBASE_API_SECRET, api_version='2017-12-30') btc_buy_price = client.get_buy_price(currency_pair='BTC-GBP') # Retrieves current time time = client.get_time() # Puts the item in the DynamoDB table data_put = dynamodb_client.put_item(TableName=DYNAMODB_TABLE, Item={ 'timestamp': { 'S': time.iso }, 'btc_buy_price': { 'S': btc_buy_price.amount } }) return {"statusCode": 200}
def collect_coinbase(): # ------------------------------ # collect data from Coinbase API # ------------------------------ # Before implementation, set environmental variables with the names API_KEY_COINBASE and API_SECRET_COINBASE # API_KEY_COINBASE = '7wFz0CndMuduPhaO' # API_SECRET_COINBASE = 'SwN2NPlrak3t6gVrNpQmxphTSC40lRNH' client = Client(API_KEY_COINBASE, API_SECRET_COINBASE) #api_version='YYYY-MM-DD' currency_code = 'USD' # can also use EUR, CAD, etc. # currency=currency_code # Make the request price = client.get_spot_price(currency=currency_code, date='2017-04-11') currencies = client.get_currencies() rates = client.get_exchange_rates(currency='BTC') time = client.get_time() # print ('Current bitcoin price in %s: %s %s' % (currencies, price,rates)) def daterange(start_date, end_date): for n in range(int((end_date - start_date).days)): yield start_date + timedelta(n) start_date = date(2013, 1, 1) end_date = date(2018, 1, 13) coinbase_d_cur_his = [] for single_date in daterange(start_date, end_date): pre_date = single_date.strftime("%Y-%m-%d") pre_price = client.get_spot_price(currency=currency_code, date=pre_date) # sell_price = client.get_sell_price(currency=currency_code, date=pre_date) # buy_price = client.get_buy_price(currency=currency_code, date=pre_date) print([ pre_date, pre_price['base'], pre_price['currency'], pre_price['amount'], single_date.day, single_date.month, single_date.year ]) # print(pre_price['amount'], sell_price['amount'], buy_price['amount']) coinbase_d_cur_his.append([ pre_date, pre_price['base'], pre_price['currency'], pre_price['amount'], single_date.day, single_date.month, single_date.year ])
def test_get_time(self): client = Client(api_key, api_secret) server_time = client.get_time() self.assertIsInstance(server_time, APIObject) self.assertEqual(server_time, mock_item)
- Reference: https://github.com/coinbase/coinbase-python # Signup Coinbase account to get API key & Secret-> https://www.coinbase.com/ - Install Coinbase Python Module in CMD or Terminal. >>> pip install coinbase --upgrade ''' from coinbase.wallet.client import Client print("\n *** CoinBase Using Python *** \n") api_key = input(' Enter API Key : ') api_secret = input(' Enter API Secret : ') client = Client(api_key, api_secret) print('\n Current User information : {}'.format(client.get_current_user())) print('\n Coinbase Accounts Information : {}'.format(client.get_accounts())) print('\n Coinbase Primary Account Information : {}'.format(client.get_primary_account())) print('\n Get supported native currencies : {}'.format(client.get_currencies())) print('\n Get exchange rates : {}'.format(client.get_exchange_rates())) print('\n Buy Prices : {}'.format(client.get_buy_price(currency_pair = 'BTC-USD'))) print('\n Sell Price : {}'.format(client.get_sell_price(currency_pair = 'BTC-USD'))) print('\n Spot Price : {}'.format(client.get_spot_price(currency_pair = 'BTC-USD'))) print('\n Current Server Time : {}'.format(client.get_time())) print('\n Get Authorization Information: {}'.format(client.get_auth_info())) print('\n Get Transaction : {}'.format(account.get_transactions())) print('\n Get Reports : {}'.format(client.get_reports())) print('\n Get Buys : {}'.format(account.get_buys())) print('\n Get Sells : {}'.format(account.get_sells())) print('\n Get Sells: {}'.format(account.get_deposits())) print('\n Get Withdrawls : {}'.format(account.get_withdrawals())) print('\n Get Orders : {}'.format(client.get_orders())) print('\n Get Checkouts : {}'.format(client.get_checkouts()))
buy_price_B = json_data['data']['buy_price'] sell_price_B = json_data['data']['sell_price'] buy_BTCUSD_KRW = float(buy_price_B) / float(USDKRW) sell_BTCUSD_KRW = float(sell_price_B) / float(USDKRW) #coinbase data extraction api_key_C = 'place you api key for coinbase' api_secret_C = 'place your api secret for coinbase' client = Client(api_key_C, api_secret_C) buy_BTCUSD = float(client.get_buy_price(currency_pair='BTC-USD')) sell_BTCUSD = float(client.get_sell_price(currency_pair='BTC-USD')) buy_price = float(buy_price['amount']) sell_price = float(sell_price['amount']) date_time_C = client.get_time() date_time_C = date_time_C["iso"] abs_diff = abs(buy_BTCUSD_KRW, buy_BTCUSD) abs_dif_per = abs_diff / max(buy_BTCUSD_KRW, buy_BTCUSD) data = [ date_time_C, USDKRW, buy_BTCUSD_KRW, sell_BTCUSD_KRW, buy_BTCUSD, sell_BTCUSD, abs_diff, abs_dif_per ] if "12:00" in date_time_C: #insert hourly data into the database Database.Database.WriteHourlyData(data) #I can trade only to coinbase because I cannot open a ba
accounts = client.get_accounts() # accounts.refresh() # print accounts exchange_rates = client.get_exchange_rates() data = pd.DataFrame( columns=['time', 'currency_pair', 'spot_price', 'buy_price', 'sell_price']) log_num = 1 while True: for entry in range(14400): for currency in currencies: currency_pair = currency + '-USD' server_time = client.get_time() buy_price = client.get_buy_price(currency_pair=currency_pair) sell_price = client.get_sell_price(currency_pair=currency_pair) spot_price = client.get_spot_price(currency_pair=currency_pair) data = data.append( { 'time': server_time['iso'], 'currency_pair': currency_pair, 'spot_price': spot_price['amount'], 'buy_price': buy_price['amount'], 'sell_price': sell_price['amount'] }, ignore_index=True) with open('logs/log_{}.csv'.format(log_num), 'a') as f:
# Instructions: # - pip install coinbase # - add your api key in the api_key variable # - add your api secretin the api_secret variable from coinbase.wallet.client import Client import os os.system("clear") api_key = "" # insert your api key here api_secret = "" # insert your api secret here client = Client(api_key, api_secret) time = client.get_time().iso user = client.get_current_user() balance = client.get_primary_account().balance print("+------------------------------------------------+") print("| Coinbase Bitcoin Manager v1.0 |") print("+------------------------------------------------+") print print("Server time: %s " % time) print("Name: %s " % user.name) print("Primary Wallet Balance: %s " % balance) print("--------------------------------------------------") buy = client.get_buy_price(currency_pair = 'BTC-USD') print("BTC Buy Price: %s " % buy) sale = client.get_sell_price(currency_pair = 'BTC-USD') print("BTC Sales Price: %s " % sale)
from coinbase.wallet.client import Client import time client = Client('NTkXQa54Zgry1bcl', 'gj1x6XnWEsX7T6nKmlfyD9Cg0jGOSiI3', api_version='2020-12-06') currency_code = 'INR' # can also use EUR, CAD, etc. #print ('Current bitcoin price in %s: %s' % (currency_code, price.amount)) while (True): price = client.get_spot_price(currency=currency_code) t = client.get_time() with open('price.txt', 'a') as f: f.write('\n') f.write(t['iso']) f.write("@") f.write(price.amount) time.sleep(3)
def Server_time(): client = Client('https://api.coinbase.com/v2/time', '0') time = client.get_time() print('Server time: ', time.get('iso'))