From 1907803a3e67ac2ce6c63b2dd6ba3e15facd6732 Mon Sep 17 00:00:00 2001 From: msincenselee Date: Sat, 30 Nov 2019 10:45:28 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=A2=9E=E5=BC=BA]=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=80=A7=E8=83=BDwrapper,=E7=9F=AD=E5=90=88=E7=BA=A6=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=EF=BC=8C=E5=85=A8=E5=90=88=E7=BA=A6=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +- vnpy/trader/utility.py | 101 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bc2e37ed..35dfcc16 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,9 @@ # “当你想放弃时,想想你为什么开始。埃隆·马斯克” ###Fork版本主要改进如下 - - +1. 事件引擎,增加运行效率调试功能 +2. 增加rabbitMQ通信组件 +3. 增加tdx 免费数据源 大佳 diff --git a/vnpy/trader/utility.py b/vnpy/trader/utility.py index 3df538a9..c54564dc 100644 --- a/vnpy/trader/utility.py +++ b/vnpy/trader/utility.py @@ -5,11 +5,14 @@ General utility functions. import json import logging import sys +import re from pathlib import Path from typing import Callable, Dict from decimal import Decimal from math import floor, ceil - +from time import time +from datetime import datetime, timedelta +from functools import wraps, lru_cache import numpy as np import talib @@ -20,6 +23,102 @@ from .constant import Exchange, Interval log_formatter = logging.Formatter('[%(asctime)s] %(message)s') +def func_time(over_ms: int = 0): + """ + 简单记录执行时间 + :param :over_ms 超过多少毫秒, 提示信息 + :return: + """ + def run(func): + @wraps(func) + def wrapper(*args, **kwargs): + start = time() + result = func(*args, **kwargs) + end = time() + execute_ms = (int(round(end * 1000))) - (int(round(start * 1000))) + if execute_ms > over_ms: + print('{} took {} ms'.format(func.__qualname__, execute_ms)) + return result + return wrapper + return run + +@lru_cache() +def get_underlying_symbol(symbol: str): + """ + 取得合约的短号. rb2005 => rb + :param symbol: + :return: 短号 + """ + # 套利合约 + if symbol.find(' ') != -1: + # 排除SP SPC SPD + s = symbol.split(' ') + if len(s) < 2: + return symbol + symbol = s[1] + + # 只提取leg1合约 + if symbol.find('&') != -1: + s = symbol.split('&') + if len(s) < 2: + return symbol + symbol = s[0] + + p = re.compile(r"([A-Z]+)[0-9]+", re.I) + underlying_symbol = p.match(symbol) + + if underlying_symbol is None: + return symbol + + return underlying_symbol.group(1) + +@lru_cache() +def get_full_symbol(symbol: str): + """ + 获取全路径得合约名称, MA005 => MA2005, j2005 => j2005 + """ + if symbol.endswith('SPD'): + return symbol + + underlying_symbol = get_underlying_symbol(symbol) + if underlying_symbol == symbol: + return symbol + + symbol_month = symbol.replace(underlying_symbol, '') + if len(symbol_month) == 3: + if symbol_month[0] == '0': + # 支持2020年合约 + return '{0}2{1}'.format(underlying_symbol, symbol_month) + else: + return '{0}1{1}'.format(underlying_symbol, symbol_month) + else: + return symbol + + +def get_trading_date(dt: datetime = None): + """ + 根据输入的时间,返回交易日的日期 + :param dt: + :return: + """ + if dt is None: + dt = datetime.now() + + if dt.isoweekday() in [6, 7]: + # 星期六,星期天=>星期一 + return (dt + timedelta(days=8 - dt.isoweekday())).strftime('%Y-%m-%d') + + if dt.hour >= 20: + if dt.isoweekday() == 5: + # 星期五=》星期一 + return (dt + timedelta(days=3)).strftime('%Y-%m-%d') + else: + # 第二天 + return (dt + timedelta(days=1)).strftime('%Y-%m-%d') + else: + return dt.strftime('%Y-%m-%d') + + def extract_vt_symbol(vt_symbol: str): """ :return: (symbol, exchange)