在调用CsvToMgDB.py时出现错误:
line 59, in <module>
loadMC12Csv('j0000.csv',MINUTE_DB_NAME,'j0000')
NameError: name 'MINUTE_DB_NAME' is not defined
请问怎么解决?
VNPY2.1.8,CsvToMgDB.py源码:
倒入MongoDB数据库
倒入库
import csv
from datetime import datetime,timedelta
from time import time
import pymongo
from vnpy.trader.vtGlobal import globalSetting #VN 1.0版本的写法
from vnpy.trader.setting import Settings #VN 2.0版本的写法
from vnpy.trader.setting import SETTINGS #VN 2.1.8版本的写法
from vnpy.trader.vtConstant import * #VN 1.0版本的写法
from vnpy.trader.constant import * #VN 2.0版本的写法
from vnpy.trader.vtObject import VtBarData #VN 1.0版本的写法
from vnpy.trader.object import BarData #VN 2.0版本的写法
from vnpy.trader.app.ctaStrategy.ctaBase import SETTING_DB_NAME,TICK_DB_NAME,MINUTE_DB_NAME,DAILY_DB_NAME #VN 1.0版本的写法
构建函数
def loadMC12Csv(fileName,dbName,symbol):
"""将MC12版本Csv导出的历史数据插入MongoDB数据库中"""
import csv
start = time()
print(u'开始读取CSV文件%s中的数据插入到%s的%s中:'%(fileName,dbName,symbol))
#锁定集合并创建索引
client = pymongo.MongoClient(globalSetting['mongoHost'],globalSetting['mongoPort'])
collection = client[dbName][symbol]
collection.ensure_index([('datetime',pymongo.ASCENDING)],unique=True)
#读取数据和插入数据库
reader = csv.DictReader(file(fileName,'r'))
for d in reader:
bar = VtBarData()
bar.vtSymbol = symbol
bar.symbol = symbol
bar.open = float(d['Open'])
bar.high = float(d['High'])
bar.low = float(d['Low'])
bar.close = float(d['Close'])
bar.date = datetime.strptime(d['Date'].split(' ')[0],'%Y/%m/%d').strftime('%Y%m%d')
"""日期格式可能有问题"""
bar.time = d['Time']
bar.datetime = datetime.strptime(bar.date + ' ' + bar.time,'%Y/%m/%d %H:%M:%S')
bar.volume = d['Volume']
flt = {'datetime':bar.datetime}
collection.update_one(flt,{'$set':bar._dict_},upsert=True)
print(u'插入完毕,耗时:%s:'% (time()-start))
在Jupyter Notebook中输入下面的代码即可开始导入
loadMC12Csv('j0000.csv',MINUTE_DB_NAME,'j0000') #出现错误的代码