def get_market_cap_history(start_ts=None, end_ts=None):
    now_ts = calendar.timegm(time.gmtime())
    if not end_ts:  # default to current datetime
        end_ts = now_ts
    if not start_ts:  # default to 30 days before the end date
        start_ts = end_ts - (30 * 24 * 60 * 60)

    data = {}
    results = {}
    #^ format is result[market_cap_as][asset] = [[block_time, market_cap], [block_time2, market_cap2], ...]
    for market_cap_as in (config.XCP, config.BTC):
        caps = config.mongo_db.asset_marketcap_history.aggregate([
            {"$match": {
                "market_cap_as": market_cap_as,
                "block_time": {
                    "$gte": datetime.datetime.utcfromtimestamp(start_ts)
                } if end_ts == now_ts else {
                    "$gte": datetime.datetime.utcfromtimestamp(start_ts),
                    "$lte": datetime.datetime.utcfromtimestamp(end_ts)
                }
            }},
            {"$project": {
                "year":  {"$year": "$block_time"},
                "month": {"$month": "$block_time"},
                "day":   {"$dayOfMonth": "$block_time"},
                "hour":  {"$hour": "$block_time"},
                "asset": 1,
                "market_cap": 1,
            }},
            {"$sort": {"block_time": pymongo.ASCENDING}},
            {"$group": {
                "_id":   {"asset": "$asset", "year": "$year", "month": "$month", "day": "$day", "hour": "$hour"},
                "market_cap": {"$avg": "$market_cap"},  # use the average marketcap during the interval
            }},
        ])
        data[market_cap_as] = {}
        for e in caps:
            interval_time = int(calendar.timegm(datetime.datetime(e['_id']['year'], e['_id']['month'], e['_id']['day'], e['_id']['hour']).timetuple()) * 1000)
            data[market_cap_as].setdefault(e['_id']['asset'], [])
            data[market_cap_as][e['_id']['asset']].append([interval_time, e['market_cap']])
        results[market_cap_as] = []
        for asset in data[market_cap_as]:
            #for z in data[market_cap_as][asset]: assert z[0] and z[0] > 0 and z[1] and z[1] >= 0
            results[market_cap_as].append(
                {'name': asset, 'data': sorted(data[market_cap_as][asset], key=operator.itemgetter(0))})
    return results