示例#1
0
def loadDayTxt(fileName, dbName, symbol):
    """将Multicharts导出的csv格式的历史数据插入到Mongo数据库中"""
    import csv
    import pandas as pd
    import pymongo

    from cyvn.trader.vtObject import VtBarData

    getcvs()

    start = time()
    print (u'开始读取CSV文件%s中的数据插入到%s的%s中' %(fileName, dbName, symbol))

    # 锁定集合,并创建索引


    client = pymongo.MongoClient('149.28.56.155',
                                            27017,
                                            username = '******',
                                            password = '******',
                                            connectTimeoutMS=500)
    collection = client[dbName][symbol]
    collection.ensure_index([('datetime', pymongo.ASCENDING)], unique=True)

    # 读取数据和插入到数据库

    reader = csv.DictReader(open(fileName, 'r',))
    rows = [row for row in reader]
    for d in rows[-200:-1]:
        bar = VtBarData()
        bar.vtSymbol = symbol
        bar.symbol = symbol
        bar.exchange = 'SHFE'
        bar.gatewayName = 'CTP'
        bar.open = float(d['open'])/10000
        bar.high = float(d['high'])/10000
        bar.low = float(d['low'])/10000
        bar.close = float(d['close'])/10000
        bar.date = datetime.strptime(d['date'], '%Y%m%d').strftime('%Y%m%d')
        bar.time = "15:00"
        bar.datetime = datetime.strptime(bar.date, '%Y%m%d')
        bar.volume = d['volume']
        bar.openInterest = d['open_interest']

        flt = {'datetime': bar.datetime}
        collection.update_one(flt, {'$set':bar.__dict__}, upsert=True)
        print(bar.date)


    print (u'插入完毕,耗时:%s' % (time()-start))
示例#2
0
def loadRqCsv(fileName, dbName, symbol):
    """将RQ导出的csv格式的历史分钟数据插入到Mongo数据库中"""
    start = time()
    print('开始读取CSV文件%s中的数据插入到%s的%s中' %(fileName, dbName, symbol))

    # 锁定集合,并创建索引
    client = pymongo.MongoClient(globalSetting['mongoHost'],
                                            globalSetting['mongoPort'],
                                            username = globalSetting['mongoUser'],
                                            password = globalSetting['mongoPwd'],
                                            connectTimeoutMS=500)
    collection = client[dbName][symbol]
    collection.ensure_index([('datetime', pymongo.ASCENDING)], unique=True)

    # 读取数据和插入到数据库
    reader = csv.DictReader(open(fileName, 'r', ))
    rows = [row for row in reader]
    for d in rows:
            bar = VtBarData()
            bar.vtSymbol = symbol
            bar.symbol = symbol

            bar.datetime = datetime.strptime(d['datetime'], '%Y-%m-%d %H:%M:%S')
            bar.date = bar.datetime.date().strftime('%Y%m%d')
            bar.time = bar.datetime.time().strftime('%H:%M:%S')
            bar.exchange = 'SHFE'
            bar.gatewayName = 'CTP'
            bar.open = float(d['open'])
            bar.high = float(d['high'])
            bar.low = float(d['low'])
            bar.close = float(d['close'])

            bar.volume = float(d['volume'])
            bar.openInterest  = float(d['open_interest'])

            flt = {'datetime': bar.datetime}
            collection.update_one(flt, {'$set':bar.__dict__}, upsert=True)
            print(('%s \t %s' % (bar.date, bar.time)))

    print('插入完毕,耗时:%s' % (time()-start))