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 ])
class Command(BaseCommand): comparison_currency = 'USD' coin_list_url = 'https://api.coinbase.com/v2/currencies' api_version = '2018-02-14' xchange = Xchange.objects.get(pk=XCHANGE['COINBASE']) def handle(self, *args, **options): # instance variable unique to each instance try: self.client = Client(self.xchange.api_key, self.xchange.api_secret, api_version='2018-01-14') except ObjectDoesNotExist as error: logging.debug('Client does not exist:{0}'.format(error)) xchange_coins = self.client.get_currencies() for xchange_coin in xchange_coins['data']: if xchange_coin is None: continue coins = Coins.Coins(xchange_coin['id']) try: currency = Currency.objects.get(symbol=xchange_coin['id']) logger.info("{0} exists".format(xchange_coin['id'])) except ObjectDoesNotExist as error: logger.info( "{0} does not exist in our currency list..Adding".format( xchange_coin['id'], error)) coins.createClass() now = datetime.now() start_date = now.replace(second=0, minute=0, hour=0) end_date = start_date - timedelta(days=10) while end_date < start_date: start_date_ts = calendar.timegm(start_date.timetuple()) prices = self.getPrice(xchange_coin['id'], date=start_date.strftime('%Y-%m-%d')) if len(prices) != 0: if prices is None: break coin = coins.getRecord(time=start_date_ts, xchange=self.xchange) coin.time = int(calendar.timegm(start_date.timetuple())) coin.close = float(prices['amount']) coin.xchange = self.xchange coin.currency = currency coin.save() start_date = start_date - timedelta(days=1) def getCoins(self): headers = { 'content-type': 'application/json', 'user-agent': 'your-own-user-agent/0.0.1' } params = {} currencies = requests.get(self.coin_list_url, params=params, headers=headers) return currencies.text def getPrice(self, currency_symbol, date): try: res = self.client.get_spot_price(currency_pair=currency_symbol + '-USD', date=date) except Exception as error: logger.error('{0}'.format(error)) res = {} return res
def test_get_currencies(self): client = Client(api_key, api_secret) currencies = client.get_currencies() self.assertIsInstance(currencies, APIObject) self.assertEqual(currencies.data, mock_collection)
def test_get_currencies(self): client = Client(api_key, api_secret) currencies = client.get_currencies() self.assertIsInstance(currencies, APIObject) self.assertEqual(currencies.data, mock_collection)
- 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()))
class CryptoGUI(): graph_position = [0, 5] remove_ticker_entry_position = [6, 1] currency_fields_position = [1, 1] new_ticker_entry_position = [4, 1] tracking_cryptos_chart = [1, 3] save_button_position = [] THEME_COLOR = "#7befb2" def __init__(self, master_frame, user_manager): master_frame.geometry("1000x600") #CONFIGS apiVersion = '2017-08-07' apiKey = "GENERIC" apiVal = "GENERIC" self.threshold = .0001 self.quotes_position_counter = 1 self.currency_type = "USD" #Frames self.frame = master_frame self.frame.configure(background=self.THEME_COLOR) self.ticker_frame = Tkinter.Frame(self.frame) self.ticker_frame.config(bg=self.THEME_COLOR) self.ticker_frame.grid(column=1, row=0, sticky='n', padx=(0, 50)) self.graph_frame = Tkinter.Frame(self.frame, width=500, height=500) self.graph_frame.grid(column=5, row=0) #Threads self.update_pricing_thread = threading.Thread( target=self.update_prices_thread_wrapper, args=()) self.update_pricing_thread.daemon = True #Clients self.client = CoinbaseClient(apiKey, apiVal, api_version=apiVersion) self.cc_client = CryptoCompareClient.CryptoCompareClient() #Plot Generator self.plot_generator = PlotGenerator.PlotGenerator( self.graph_frame, self.cc_client) self.cryptos_tracking = [ Crypto('BTC', 0), Crypto('USDT', 0), Crypto('TRX', 0), Crypto('XMR', 0), Crypto('DASH', 0) ] self.ticker_chart_labels = {} #Currency selection Tkinter.Label(self.ticker_frame, bg=self.THEME_COLOR, text="Ticker Quotes").grid( row=self.currency_fields_position[0], column=self.currency_fields_position[1]) self.currency = Tkinter.Label(self.ticker_frame, bg=self.THEME_COLOR, text="Currency") self.currency.grid(row=self.currency_fields_position[0] + 1, column=self.currency_fields_position[1]) self.currency_selection_var = StringVar(self.ticker_frame) self.currency_selection_var.set("USD") self.currency_selection_var.trace('w', self.change_currency) self.currency_selection_dropdown = Combobox( self.ticker_frame, textvariable=self.currency_selection_var, values=self.get_currency_types()) self.currency_selection_dropdown.grid( row=self.currency_fields_position[0] + 2, column=self.currency_fields_position[1], padx=(5, 0)) self.BTC_price_label = Tkinter.Label(self.ticker_frame, bg=self.THEME_COLOR, text="0.00") self.BTC_price_label.grid(row=self.currency_fields_position[0] + 2, column=self.currency_fields_position[1] + 1) #Enter new ticker to track Tkinter.Label(self.ticker_frame, bg=self.THEME_COLOR, text="Enter new ticker to track:").grid( row=self.new_ticker_entry_position[0], column=self.new_ticker_entry_position[1]) self.ticker_entry = Entry(self.ticker_frame) self.ticker_entry.grid(row=self.new_ticker_entry_position[0] + 1, column=self.new_ticker_entry_position[1]) Button(self.ticker_frame, text="Add Ticker", command=self.add_crypto).grid( row=self.new_ticker_entry_position[0] + 1, column=self.new_ticker_entry_position[1] + 1) #Remove Ticker Tkinter.Label(self.ticker_frame, bg=self.THEME_COLOR, text="Enter ticker to remove:").grid( row=self.remove_ticker_entry_position[0], column=self.remove_ticker_entry_position[1]) self.remove_ticker_entry = Entry(self.ticker_frame) self.remove_ticker_entry.grid( row=self.remove_ticker_entry_position[0] + 1, column=self.remove_ticker_entry_position[1]) # Button(self.ticker_frame, text="Remove Ticker", command=self.remove_crypto).grid( row = self.remove_ticker_entry_position[0]+1, column = self.remove_ticker_entry_position[1]+1) #Cryptos Tracking Tkinter.Label(self.ticker_frame, bg=self.THEME_COLOR, font='Helvetica 12 bold', text=" Cryptos").grid( row=self.tracking_cryptos_chart[0], column=self.tracking_cryptos_chart[1]) Tkinter.Label(self.ticker_frame, bg=self.THEME_COLOR, font='Helvetica 12 bold', text=" Ticker ").grid( row=self.tracking_cryptos_chart[0] + 1, column=self.tracking_cryptos_chart[1]) Tkinter.Label(self.ticker_frame, bg=self.THEME_COLOR, font='Helvetica 12 bold', text="Price").grid( row=self.tracking_cryptos_chart[0] + 1, column=self.tracking_cryptos_chart[1] + 1) self.update_pricing_thread.start() self.write_quotes() #Graph Settings self.graph_ticker_selector_var = StringVar(self.graph_frame) crypto_names = [ cryptoObject.get_ticker() for cryptoObject in self.cryptos_tracking ] self.graph_ticker_selector = OptionMenu( self.graph_frame, self.graph_ticker_selector_var, crypto_names[0], *crypto_names, command=self.generate_plot).grid(column=self.graph_position[0], row=self.graph_position[1], sticky='n') def generate_plot(self): self.plot_generator.generate(self.graph_ticker_selector_var.get(), self.currency_selection_var.get(), 60).get_tk_widget().grid( row=self.graph_position[0] + 1, column=self.graph_position[1], pady=(20, 0), sticky='n') def write_quotes(self): for crypto in self.cryptos_tracking: current_ticker = crypto.get_ticker() if current_ticker not in self.ticker_chart_labels: ticker_label_var = StringVar(self.ticker_frame, value=current_ticker) ticker_price_var = StringVar(self.ticker_frame, value=0.0) self.ticker_chart_labels[current_ticker] = { "TickerLabel": Tkinter.Label(self.ticker_frame, textvariable=ticker_label_var, font='Helvetica 12 bold', bg=self.THEME_COLOR), "TickerPrice": Tkinter.Label(self.ticker_frame, textvariable=ticker_price_var, font='Helvetica 12 bold', bg=self.THEME_COLOR), "TickerLabelVar": ticker_label_var, "TickerPriceVar": ticker_price_var } self.ticker_chart_labels[current_ticker]["TickerLabel"].grid( row=self.tracking_cryptos_chart[0] + 1 + self.quotes_position_counter, column=self.tracking_cryptos_chart[1]) self.ticker_chart_labels[current_ticker]["TickerPrice"].grid( row=self.tracking_cryptos_chart[0] + 1 + self.quotes_position_counter, column=self.tracking_cryptos_chart[1] + 1) self.quotes_position_counter += 1 else: self.ticker_chart_labels[current_ticker]["TickerPriceVar"].set( crypto.get_price()) self.ticker_frame.after(1000, self.write_quotes) def update_prices(self): for crypto in self.cryptos_tracking: new_price = self.cc_client.request_price( crypto.get_ticker(), self.currency_selection_var.get()) if crypto.price != 0.0 and new_price != crypto.price and ( abs(crypto.price - new_price) / crypto.price > self.threshold): self.alert_routine(crypto.get_ticker()) crypto.price = new_price def alert_routine(self, crypto): alert_message_thread = threading.Thread(target=self.alert_dialog, args=(crypto, )) alert_message_thread.daemon = True alert_sound_thread = threading.Thread(target=self.alert_sound) alert_sound_thread.daemon = True alert_message_thread.start() alert_sound_thread.start() def alert_dialog(self, crypto): tkMessageBox.showinfo( "ALERT", "%s has changed by more than %f%%" % (crypto, self.threshold * 100)) def alert_sound(self): os.system( 'play --no-show-progress --null --channels 1 synth 1 sine 440') def update_prices_thread_wrapper(self): self.update_prices() time.sleep(1) self.update_prices_thread_wrapper() def get_price(self, crypto): return self.cc_client.request_price(crypto, self.currency_selection_var.get()) def add_crypto(self): crypto_name = self.ticker_entry.get() if crypto_name and crypto_name not in self.cryptos_tracking: if self.get_price(crypto_name) == 0.0: tkMessageBox.showinfo("ERROR", "Coin does not exist.") else: self.cryptos_tracking.append(Crypto(crypto_name, 0)) def get_currency_types(self): currency_list = self.client.get_currencies()['data'] return [a['id'] for a in currency_list] def remove_crypto(self): removing_ticker = self.remove_ticker_entry_position.get() if removing_ticker in self.ticker_chart_labels: self.cryptos_tracking = filter( lambda coin: coin.get_ticker() != removing_ticker, self.cryptos_tracking) self.ticker_chart_labels[removing_ticker][ "TickerLabel"].grid_forget() self.ticker_chart_labels[removing_ticker][ "TickerPrice"].grid_forget() del self.ticker_chart_labels[removing_ticker] self.reframe_quotes_chart() else: tkMessageBox.showinfo("ERROR", "Not tracking that coin.") def reframe_quotes_chart(self): self.quotes_position_counter = 1 for crypto, props in self.ticker_chart_labels.iteritems(): props["TickerLabel"].grid_forget() props["TickerPrice"].grid_forget() props["TickerLabel"].grid(row=self.tracking_cryptos_chart[0] + 1 + self.quotes_position_counter, column=self.tracking_cryptos_chart[1]) props["TickerPrice"].grid(row=self.tracking_cryptos_chart[0] + 1 + self.quotes_position_counter, column=self.tracking_cryptos_chart[1] + 1) self.quotes_position_counter += 1 def change_currency(self, *args): self.generate_plot() self.currency_selection = self.currency_selection_var.get()