vnpy/trader/vtFunction.py

57 lines
1.5 KiB
Python
Raw Normal View History

2016-07-02 03:12:44 +00:00
# encoding: UTF-8
"""
2017-04-28 14:10:07 +00:00
包含一些开发中常用的函数
2016-07-02 03:12:44 +00:00
"""
2017-04-28 14:10:07 +00:00
import os
2016-07-02 03:12:44 +00:00
import decimal
import json
from datetime import datetime
MAX_NUMBER = 10000000000000
MAX_DECIMAL = 4
2016-07-03 16:59:48 +00:00
# ----------------------------------------------------------------------
2016-07-02 03:12:44 +00:00
def safeUnicode(value):
"""检查接口数据潜在的错误,保证转化为的字符串正确"""
# 检查是数字接近0时会出现的浮点数上限
if type(value) is int or type(value) is float:
if value > MAX_NUMBER:
value = 0
# 检查防止小数点位过多
if type(value) is float:
d = decimal.Decimal(str(value))
if abs(d.as_tuple().exponent) > MAX_DECIMAL:
value = round(value, ndigits=MAX_DECIMAL)
return unicode(value)
#----------------------------------------------------------------------
def loadMongoSetting():
"""载入MongoDB数据库的配置"""
2017-04-28 14:10:07 +00:00
fileName = 'VT_setting.json'
path = os.path.abspath(os.path.dirname(__file__))
fileName = os.path.join(path, fileName)
2016-07-02 03:12:44 +00:00
try:
2017-04-28 14:10:07 +00:00
f = file(fileName)
2016-07-02 03:12:44 +00:00
setting = json.load(f)
host = setting['mongoHost']
port = setting['mongoPort']
2017-04-02 14:32:36 +00:00
logging = setting['mongoLogging']
2016-07-02 03:12:44 +00:00
except:
host = 'localhost'
port = 27017
2017-04-02 14:32:36 +00:00
logging = False
2016-07-02 03:12:44 +00:00
2017-04-02 14:32:36 +00:00
return host, port, logging
2016-07-02 03:12:44 +00:00
#----------------------------------------------------------------------
def todayDate():
"""获取当前本机电脑时间的日期"""
return datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)