Search code examples
pythonfinancecryptocurrency

Getting error TypeError: '>' not supported between instances of 'int' and 'str',


I am trying to retrieve the historical hourly data from the cryptocompare API. First function retrieves the latest 2000 data points of hourly data on Bitcoin. However, after defining the times in the get_df function, I am getting an error after running it:

<ipython-input-73-81a46125c981> in get_df(from_date, to_date)
      5     # While the earliest date returned is later than the earliest date requested, keep on querying the API
      6     # and adding the results to a list.
----> 7     while date > from_date:
      8         data = get_data(date)
      9         holder.append(pd.DataFrame(data['Data']))

TypeError: '>' not supported between instances of 'int' and 'str'
def get_data(date):
    """ Query the API for 2000 days historical price data starting from "date". """
    url = "https://min-api.cryptocompare.com/data/histohour?fsym=BTC&tsym=USD&limit=2000&toTs={}".format(date)
    r = requests.get(url)
    ipdata = r.json()
    return ipdata
ipdata = get_data('1556838000')
df = pd.DataFrame(ipdata['Data'])
def get_df(from_date, to_date):
    """ Get historical price data between two dates. """
    date = to_date
    holder = []
    # While the earliest date returned is later than the earliest date requested, keep on querying the API
    # and adding the results to a list. 
    while date > from_date:
        data = get_data(date)
        holder.append(pd.DataFrame(data['Data']))
        date = data['TimeFrom']
    # Join together all of the API queries in the list.    
    df = pd.concat(holder, axis = 0)                    
    # Remove data points from before from_date
    df = df[df['time']>from_date]                       
    # Convert to timestamp to readable date format
    df['time'] = pd.to_datetime(df['time'], unit='s')   
    # Make the DataFrame index the time
    df.set_index('time', inplace=True)                  
    # And sort it so its in time order 
    df.sort_index(ascending=False, inplace=True)        
    return df

get_df('1549638000', '1556838000')

Solution

  • So if you look at the API response, it shows TimeFrom is epoch timestamp in int format so 1549638000(not '1549638000'). enter image description here

    while int(date) > int(from_date):
    

    Should work, or you can just pass the vars as int

    get_df(1549638000, 1556838000)