예제 #1
0
def load_data(code, type, period, index_name):
    """
    按照周期加载三类数据:基金/股票;指数(市场);国债(无风险)
    :param code: 代码
    :param type: stock|fund
    :param period: 期间:day,week,month,year
    :param index_name:指数名称,中文的
    :return:
    """
    # 加载基金/股票数据
    if type == const.FUND:
        data = data_utils.load_fund_data(code)
        if data is None:
            logger.warning("[%s]数据有问题,忽略它...", code)
            return -999, None, None
        data_rate = calculate_rate(data, const.COL_ACCUMULATIVE_NET, period,
                                   'price')
    elif type == const.STOCK:
        data = data_utils.load_stock_data(code)
        data_rate = calculate_rate(data, 'close', period, 'price')
    else:
        raise ValueError("type不合法:" + type)

    # 加载指数数据
    index_data = data_utils.load_index_data_by_name(index_name, period)
    index_rate = data_utils.calculate_rate(index_data, 'close', period)

    # 加载无风险利率(/365=每天利率)
    bond_rate = data_utils.load_bond_interest_data() / PERIOD_NUM[period]
    bond_rate = calculate_rate(bond_rate, '收盘', period, 'rate')

    return data_rate, index_rate, bond_rate
예제 #2
0
def calculate_one_fund_by_period(fund, period):
    # # 不计算今年才开始的基金
    # if fund.start_date > datetime.strptime('2020-1-1', DATE_FORMAT).date():
    #     logger.debug("此基金开始日期[%r],太新了,不具备分析价值")
    #     return None

    start_year = fund.start_date.year
    end_year = datetime.now().date().year

    periods = []
    for year in range(start_year, end_year + 1):
        periods += date_utils.get_peroid(year, period)

    trade_data = data_utils.load_fund_data(fund.code)
    if trade_data is None:
        return None

    data = filter_trade_by_period(trade_data, periods)
    logger.debug("过滤出%d条基金净值记录,%r~%r", len(data), data.index[0],
                 data.index[-1])

    bond_interests = data_utils.load_bond_interest_data(data.index)
    bond_interests = calculate_rate(bond_interests, '收盘', period)
    logger.debug("过滤出%d条基准利率记录", len(bond_interests))

    # assert len(data) == len(bond_interests), "基金净值数据 ~ 基准利率 个数不一致"
    sharpe_ratio = calculate_sharpe(data, bond_interests, period)

    return sharpe_ratio
예제 #3
0
    def load_data(self, args):
        data = data_utils.load_fund_data(args.code)
        if data is None:
            raise ValueError("数据不存在,代码:" + args.code)

        index_data = data_utils.load_index_data_by_name('上证指数')
        index_rate = data_utils.calculate_rate(index_data, 'close')

        self.load_info(args.code)

        return data, index_data, index_rate