当前位置: 首页>>代码示例>>Python>>正文


Python TimeSeriesCalcs.rolling_volatility方法代码示例

本文整理汇总了Python中pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs.rolling_volatility方法的典型用法代码示例。如果您正苦于以下问题:Python TimeSeriesCalcs.rolling_volatility方法的具体用法?Python TimeSeriesCalcs.rolling_volatility怎么用?Python TimeSeriesCalcs.rolling_volatility使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs的用法示例。


在下文中一共展示了TimeSeriesCalcs.rolling_volatility方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: calculate_leverage_factor

# 需要导入模块: from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs [as 别名]
# 或者: from pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs import rolling_volatility [as 别名]
    def calculate_leverage_factor(self, returns_df, vol_target, vol_max_leverage, vol_periods = 60, vol_obs_in_year = 252,
                                  vol_rebalance_freq = 'BM', returns = True, period_shift = 0):
        """
        calculate_leverage_factor - Calculates the time series of leverage for a specified vol target

        Parameters
        ----------
        returns_df : DataFrame
            Asset returns

        vol_target : float
            vol target for assets

        vol_max_leverage : float
            maximum leverage allowed

        vol_periods : int
            number of periods to calculate volatility

        vol_obs_in_year : int
            number of observations in the year

        vol_rebalance_freq : str
            how often to rebalance

        returns : boolean
            is this returns time series or prices?

        period_shift : int
            should we delay the signal by a number of periods?

        Returns
        -------
        pandas.Dataframe
        """

        tsc = TimeSeriesCalcs()

        if not returns: returns_df = tsc.calculate_returns(returns_df)

        roll_vol_df = tsc.rolling_volatility(returns_df,
                                        periods = vol_periods, obs_in_year = vol_obs_in_year).shift(period_shift)

        # calculate the leverage as function of vol target (with max lev constraint)
        lev_df = vol_target / roll_vol_df
        lev_df[lev_df > vol_max_leverage] = vol_max_leverage

        # only allow the leverage change at resampling frequency (eg. monthly 'BM')
        lev_df = lev_df.resample(vol_rebalance_freq)

        returns_df, lev_df = returns_df.align(lev_df, join='left', axis = 0)

        lev_df = lev_df.fillna(method='ffill')

        return lev_df
开发者ID:humdings,项目名称:pythalesians,代码行数:57,代码来源:cashbacktest.py

示例2: calculate_leverage_factor

# 需要导入模块: from pythalesians.timeseries.calcs.timeseriescalcs import TimeSeriesCalcs [as 别名]
# 或者: from pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs import rolling_volatility [as 别名]
    def calculate_leverage_factor(self, returns_df, vol_target, vol_max_leverage, vol_periods = 60, vol_obs_in_year = 252,
                                  vol_rebalance_freq = 'BM', data_resample_freq = None, data_resample_type = 'mean',
                                  returns = True, period_shift = 0):
        """
        calculate_leverage_factor - Calculates the time series of leverage for a specified vol target

        Parameters
        ----------
        returns_df : DataFrame
            Asset returns

        vol_target : float
            vol target for assets

        vol_max_leverage : float
            maximum leverage allowed

        vol_periods : int
            number of periods to calculate volatility

        vol_obs_in_year : int
            number of observations in the year

        vol_rebalance_freq : str
            how often to rebalance

        vol_resample_freq : str
            do we need to resample the underlying data first? (eg. have we got intraday data?)

        returns : boolean
            is this returns time series or prices?

        period_shift : int
            should we delay the signal by a number of periods?

        Returns
        -------
        pandas.Dataframe
        """

        tsc = TimeSeriesCalcs()

        if data_resample_freq is not None:
            return
            # TODO not implemented yet

        if not returns: returns_df = tsc.calculate_returns(returns_df)

        roll_vol_df = tsc.rolling_volatility(returns_df,
                                        periods = vol_periods, obs_in_year = vol_obs_in_year).shift(period_shift)

        # calculate the leverage as function of vol target (with max lev constraint)
        lev_df = vol_target / roll_vol_df
        lev_df[lev_df > vol_max_leverage] = vol_max_leverage

        # should we take the mean, first, last in our resample
        if data_resample_type == 'mean':
            lev_df = lev_df.resample(vol_rebalance_freq).mean()
        elif data_resample_type == 'first':
            lev_df = lev_df.resample(vol_rebalance_freq).first()
        elif data_resample_type == 'last':
            lev_df = lev_df.resample(vol_rebalance_freq).last()
        else:
            # TODO implement other types
            return

        returns_df, lev_df = returns_df.align(lev_df, join='left', axis = 0)

        lev_df = lev_df.fillna(method='ffill')
        lev_df.ix[0:vol_periods] = numpy.nan    # ignore the first elements before the vol window kicks in

        return lev_df
开发者ID:Sahanduiuc,项目名称:pythalesians,代码行数:74,代码来源:cashbacktest.py


注:本文中的pythalesians.timeseries.calcs.timeseriescalcs.TimeSeriesCalcs.rolling_volatility方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。