#NEWLINE# def get_historical_price( self , crypto , fiat , at_time ) : #NEWLINE# #INDENT# '\n        Using the quandl.com API, get the historical price (by day).\n        The CRYPTOCHART source claims to be from multiple exchange sources\n        for price (they say best exchange is most volume).\n        ' #NEWLINE# interval = datetime . timedelta ( hours = 48 ) #NEWLINE# crypto = crypto . lower ( ) #NEWLINE# fiat = fiat . lower ( ) #NEWLINE# at_time = arrow . get ( at_time ) . datetime #NEWLINE# data = crypto_data [ crypto ] #NEWLINE# ( name , date_created ) = ( data [ 'name' ] , data [ 'genesis_date' ] ) #NEWLINE# if ( date_created . replace ( tzinfo = pytz . utc ) > at_time ) : #NEWLINE# #INDENT# raise Exception ( ( '%s (%s) did not exist on %s' % ( name , crypto , at_time ) ) ) #NEWLINE# #UNINDENT# if ( crypto == 'btc' ) : #NEWLINE# #INDENT# if ( fiat == 'usd' ) : #NEWLINE# #INDENT# if ( at_time < datetime . datetime ( 2013 , 2 , 1 , tzinfo = pytz . utc ) ) : #NEWLINE# #INDENT# exchange = 'MtGox' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# exchange = 'Bitstamp' #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# exchange = quandl_exchange_btc_to_fiat [ fiat . upper ( ) ] #NEWLINE# #UNINDENT# source = ( 'BITCOIN/%s%s' % ( exchange . upper ( ) , fiat . upper ( ) ) ) #NEWLINE# price_index = 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( fiat != 'btc' ) : #NEWLINE# #INDENT# raise Exception ( 'Altcoins are only available via BTC base fiat' ) #NEWLINE# #UNINDENT# if ( crypto == 'ltc' ) : #NEWLINE# #INDENT# ( source , price_index ) = [ 'BTCE/BTCLTC' , 4 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( source , price_index ) = [ ( 'CRYPTOCHART/' + crypto . upper ( ) ) , 1 ] #NEWLINE# #UNINDENT# #UNINDENT# url = ( 'https://www.quandl.com/api/v1/datasets/%s.json' % source ) #NEWLINE# trim = '?trim_start={0:%Y-%m-%d}&trim_end={1:%Y-%m-%d}' . format ( ( at_time - interval ) , ( at_time + interval ) ) #NEWLINE# response = self . get_url ( ( url + trim ) ) . json ( ) #NEWLINE# closest_distance = interval #NEWLINE# best_price = None #NEWLINE# for line in response [ 'data' ] : #NEWLINE# #INDENT# date = fiat [ 0 ] #NEWLINE# price = line [ price_index ] #NEWLINE# tick_date = arrow . get ( date ) . datetime #NEWLINE# distance = ( at_time - tick_date ) #NEWLINE# if ( distance . total_seconds ( ) == 0 ) : #NEWLINE# #INDENT# return ( price , source , tick_date ) #NEWLINE# #UNINDENT# if ( distance < closest_distance ) : #NEWLINE# #INDENT# closest_distance = distance #NEWLINE# best_price = price #NEWLINE# best_date = tick_date #NEWLINE# #UNINDENT# #UNINDENT# if ( not best_price ) : #NEWLINE# #INDENT# msg = ( "Quandl's data source is incomplete. Could not get best price for %s/%s on %s." % ( crypto , fiat , at_time ) ) #NEWLINE# raise NoData ( msg ) #NEWLINE# #UNINDENT# return ( best_price , source , best_date )