作者:赵信的派 ;来源:维恩的派论坛

 

ctaHistoryData可以导入历史数据,但不支持wind。这里增加wind的分钟数据导入函数,并在主程序中调用即可。
 

#----------------------------------------------------------------------    
def loadWind(code,dbName, symbol):
    """将wind的历史数据插入到Mongo数据库中"""
    from WindPy import *
    import pandas as pd
    from pandas import DataFrame 

    w.start()

    start = time()

    print u'开始读取wind文件%s中的数据插入到%s的%s中' %(code, dbName, symbol)

    # 锁定集合,并创建索引
    host, port, logging = loadMongoSetting()

    client = pymongo.MongoClient(host, port)    
    collection = client[dbName][symbol]
    collection.ensure_index([('datetime', pymongo.ASCENDING)], unique=True)   

    # 读取数据和插入到数据库
    wsd_data = w.wsi(code, "open,high,low,close,volume,amt,oi", "2017-02-19 09:00:00", "2017-05-21 00:06:20", "")
    print(wsd_data)

    df1=DataFrame(wsd_data.Data,index=wsd_data.Fields,columns=wsd_data.Times)
    print '1'
    df1=df1.T
    df_wind=df1.dropna(subset=['open'])
    index=0

    print df_wind

    for d in df_wind.index:
        bar = CtaBarData()
        bar.vtSymbol = symbol
        bar.symbol = symbol
        bar.open = float(df_wind['open'].iloc[index])
        bar.high = float(df_wind['high'].iloc[index])
        bar.low = float(df_wind['low'].iloc[index])
        bar.close = float(df_wind['close'].iloc[index])
        print(type(d))
        bar.datetime=datetime.strftime(d.to_pydatetime(),'%Y-%m-%d %H:%M:%S')
        bar.date = datetime.strftime(d.to_pydatetime(),'%Y%m%d')
        bar.time = datetime.strftime(d.to_pydatetime(),'%H:%M:%S')



        bar.volume = df_wind['volume'].iloc[index]

        flt = {'datetime': bar.datetime}
        collection.update_one(flt, {'$set':bar.__dict__}, upsert=True)  
        print bar.datetime
#主函数
if __name__ == '__main__':
    ## 简单的测试脚本可以写在这里
    #from time import sleep
    #e = HistoryDataEngine()
    #sleep(1)
    #e.downloadEquityDailyBar('000001')
    #e.downloadEquityDailyBarts('000001')

    # 这里将项目中包含的股指日内分钟线csv导入MongoDB,作者电脑耗时大约3分钟
    #loadMcCsv('IF0000_1min.csv', MINUTE_DB_NAME, 'IF0000')
    start = time()
    #导入wind数据
    loadWind("MA709.CZC",MINUTE_DB_NAME, 'MA709')
    #测时
    print u'插入完毕,耗时:%s' % (time()-start)
    #导入通达信历史分钟数据
    #loadTdxCsv('CL8.csv', MINUTE_DB_NAME, 'c0000)