Exemplo n.º 1
0
def build_ETF(ETF, ETF_dic, host='localhost:27017'):
    N = len(ETF)

    # Initialize the collection ETF
    client = MongoClient([host])
    # client.drop_database('RoboAdvisor')
    db = client.RoboAdvisor
    db.ETF.drop()
    posts = db.ETF

    # Add ticker as _id
    for e in range(N):
        posts.insert_one({'_id': ETF[e]})

    # Add Asset class
    for e in range(N):
        asset = ETF[e]
        assetClass = ETF_dic[asset]
        posts.update_one({'_id': asset}, {'$set': {"asset_class": assetClass}})
    # Add summary data
    add_summary(ETF, posts, N)

    # Add historical data
    summary_data = get_summary_data_all(host)

    for e in range(N):
        asset = ETF[e]
        today = time.strftime("%Y-%m-%d")
        print(asset + " is start!")
        ETFs_data = Share(asset).get_historical(
            summary_data["inception_date"][asset], today)
        ETFs_data = json_normalize(ETFs_data)
        ETFs_data = ETFs_data.set_index("Date")
        dates = sorted(ETFs_data["Adj_Close"].keys())
        n = len(dates)
        # date = dates[0].to_datetime().strftime('%Y-%m-%d')
        # date = dates[0].to_datetime()
        date = dates[0]
        close = ETFs_data["Adj_Close"][dates[0]]
        volume = ETFs_data["Volume"][dates[0]]
        posts.update_one({'_id': asset}, {
            '$set': {
                'hist_data': [{
                    'date':
                    datetime.datetime.strptime(date, '%Y-%m-%d'),
                    'quotes': {
                        'close': float(close),
                        'volume': float(volume)
                    }
                }]
            }
        })
        for x in range(1, n):
            # date = dates[x].to_datetime().strftime('%Y-%m-%d')
            # date = dates[x].to_datetime()
            date = dates[x]
            close = ETFs_data["Close"][dates[x]]  # "%.2f" %
            volume = ETFs_data["Volume"][dates[x]]
            histData = {
                '$push': {
                    'hist_data': {
                        'date': datetime.datetime.strptime(date, '%Y-%m-%d'),
                        'quotes': {
                            'close': float(close),
                            'volume': float(volume)
                        }
                    }
                }
            }
            posts.update_one({'_id': asset}, histData)
    return