def test_forward_date(self): ref_date = '2017-12-04' tenor = ['1b', '2d', '3w', '4m', '5y'] expected = [ '2017-12-04', '2017-12-05', '2017-11-14', '2017-08-07', '2012-12-05' ] calculated = [ WindDataProvider.forward_date(ref_date, tenor[i]) for i in range(len(tenor)) ] self.assertEqual(calculated, expected) ref_date = '20170101' tenor = ['1b', '2d', '3w', '4m', '5y'] expected = [ '2017-01-03', '2017-01-03', '2016-12-13', '2016-09-02', '2012-01-05' ] calculated = [ WindDataProvider.forward_date(ref_date, tenor[i], date_format='%Y%m%d') for i in range(len(tenor)) ] self.assertEqual(calculated, expected)
def test_forward_date(self): # WIND_DATA_PROVIDER = WindDataProvider() ref_date = '2017-12-04' tenor = ['1b', '2d', '3w', '4m', '5y'] expected = [ '2017-12-04', '2017-12-05', '2017-11-14', '2017-08-07', '2012-12-05' ] # WindDataProvider.forward_date = mock.Mock(return_value=ret_value) # result = WindDataProvider.forward_date(ref_date, tenor) calculated = [ WindDataProvider.forward_date(ref_date, tenor[i]) for i in range(len(tenor)) ] self.assertEqual(calculated, expected) ref_date = '20170101' tenor = ['1b', '2d', '3w', '4m', '5y'] expected = [ '2017-01-03', '2017-01-03', '2016-12-13', '2016-09-02', '2012-01-05' ] calculated = [ WindDataProvider.forward_date(ref_date, tenor[i], date_format='%Y%m%d') for i in range(len(tenor)) ] self.assertEqual(calculated, expected)
def get_universe(index_id, date=None, output_weight=False): """ :param index_id: str, 可以为指数代码或者'fullA' or 'ashare'(指全市场股票),不区分大小写 :param date: str, optional, YYYYMMDD/YYYY-MM-DD,默认为None,即返回最近交易日的成分股列表 :param output_weight: bool, optional, 是否返回对应的个股权重 :return: 如果output_weight=False, 返回list, 成分股列表 如果output_weight=True, 返回DataFrame """ LOGGER.info('Loading the constituent stocks of index {0} at date {1}'. format(index_id, datetime.date.today() if date is None else date)) ret = WindDataProvider.get_universe(index_id, date, output_weight) LOGGER.info('Number of the loaded constituent stocks is {0}'.format(len(ret))) return ret
except ImportError: from mock import patch try: from WindPy import w except ImportError: pass from WindAdapter.data_provider import WindDataProvider from WindAdapter.helper import WindQueryHelper from WindAdapter.factor_loader import FactorLoader from WindAdapter.enums import Header from datetime import (datetime, date) wind_query_helper = WindQueryHelper() wind_data_provider = WindDataProvider() """ 该文件用以测试WindPy接口数据结构是否与从前保持一致,以确保WindAdapter数据处理得以照常进行 """ class WindData(object): def __init__(self, data, codes, error_code, fields, times): self.Data = data self.Codes = codes self.ErrorCode = error_code self.Fields = fields self.Times = times def __eq__(self, other): if (self.Data == other.Data and self.Codes == other.Codes
import re import pandas as pd from argcheck import expect_types import numpy as np from WindAdapter.data_provider import WindDataProvider from WindAdapter.enums import FreqType from WindAdapter.enums import Header from WindAdapter.enums import OutputFormat from WindAdapter.helper import WindQueryHelper from WindAdapter.utils import date_convert_2_str from WindAdapter.utils import py_assert WIND_QUERY_HELPER = WindQueryHelper() WIND_DATA_PROVIDER = WindDataProvider() class FactorLoader: def __init__(self, start_date, end_date, factor_name, **kwargs): self.start_date = start_date self.end_date = end_date self.factor_name = factor_name self.sec_id = kwargs.get('sec_id', 'fulla') self.freq = kwargs.get('freq', FreqType.EOM) self.tenor = kwargs.get('tenor', None) self.output_data_format = kwargs.get('output_data_format', OutputFormat.MULTI_INDEX_DF) self.is_index = kwargs.get('is_index', True) self.date_format = kwargs.get('date_format', '%Y-%m-%d') self.block_size = kwargs.get('block_size', 400)
import os import datetime import pandas as pd from toolz import merge from argcheck import expect_types from WindAdapter.factor_loader import FactorLoader from WindAdapter.utils import save_data_to_file from WindAdapter.utils import print_table from WindAdapter.utils import handle_wind_query_exception from WindAdapter.custom_logger import CustomLogger from WindAdapter.data_provider import WindDataProvider from WindAdapter.helper import WindQueryHelper from WindAdapter.enums import OutputFormat LOGGER = CustomLogger() WIND_DATA_PRODIVER = WindDataProvider() WIND_QUERY_HELPER = WindQueryHelper() def reset_log_level(log_level): """ :param log_level: enum, 可选择'info', 'critical' 'notset' :return: 设置WindAdapter函数输出信息的等级, 项目默认为'info'等级 """ LOGGER.critical('Reset path of data dict to {0}'.format(log_level)) LOGGER.set_level(log_level) def reset_data_dict_path(path, path_type_abs): """ :param path: str, 自定义的data_dict 路径