Exemple #1
0
    def _calculate_volatility(self):
        self.annualised_vol = get_volatility(self.returns_tms, self.frequency, annualise=True)

        positive_returns_tms = self.returns_tms[self.returns_tms > 0]
        negative_returns_tms = self.returns_tms[self.returns_tms < 0]

        self.annualised_upside_vol = get_volatility(positive_returns_tms, self.frequency, annualise=True)
        self.annualised_downside_vol = get_volatility(negative_returns_tms, self.frequency, annualise=True)
Exemple #2
0
def sorino_ratio(qf_series: QFSeries,
                 frequency: Frequency,
                 risk_free: float = 0) -> float:
    """
    Calculates the Sorino ratio for a given timeseries of returns.
    sorino_ratio = (CAGR - risk free) / annualised downside volatility

    Parameters
    ----------
    qf_series: QFSeries
        financial series
    frequency: Frequency
        frequency of the qf_series
    risk_free: float
        risk free rate

    Returns
    -------
    float
    """

    annualised_growth_rate = cagr(qf_series, frequency)
    negative_returns = qf_series[qf_series < 0]
    annualised_downside_vol = get_volatility(negative_returns,
                                             frequency,
                                             annualise=True)

    ratio = (annualised_growth_rate - risk_free) / annualised_downside_vol
    return ratio
Exemple #3
0
def sharpe_ratio(qf_series: QFSeries,
                 frequency: Frequency,
                 risk_free: float = 0) -> float:
    """
    Calculates the Sharpe Ratio for a given timeseries of returns and given frequency.

    Parameters
    ----------
    qf_series: QFSeries
        financial series
    frequency: Frequency
        frequency of the series
    risk_free: float
        risk free rate

    Returns
    -------
    float
        Sharpe Ratio for given series and frequency
    """
    annual_simple_return = cagr(qf_series, frequency)
    annual_log_return = np.log(annual_simple_return + 1)
    annual_vol = get_volatility(qf_series, frequency, annualise=True)

    return (annual_log_return - risk_free) / annual_vol
 def _compute_volatility(self, prices_tms) -> float:
     """Compute the annualised volatility of the last self._number_of_samples days"""
     prices_tms = prices_tms.dropna().iloc[-self._number_of_samples:]
     prices_tms = PricesSeries(prices_tms)
     try:
         volatility = get_volatility(prices_tms,
                                     frequency=Frequency.DAILY,
                                     annualise=True)
     except (AssertionError, AttributeError):
         volatility = float('nan')
     return volatility
def _calculate_single_values(data_provider):
    ticker = QuandlTicker('AAPL', 'WIKI')
    fields = [
        PriceField.Open, PriceField.High, PriceField.Low, PriceField.Close
    ]
    prices_df = data_provider.get_price(ticker, fields, start_date, end_date)

    di_vol = DriftIndependentVolatility.get_volatility(prices_df,
                                                       Frequency.DAILY)
    print("drift_independent_vol = {}".format(di_vol))

    close_price_tms = prices_df[PriceField.Close]
    simple_vol = get_volatility(close_price_tms, Frequency.DAILY)
    print("simple_vol = {}".format(simple_vol))
def rolling_volatility(qf_series: QFSeries,
                       frequency: Frequency = None,
                       annualise: bool = True,
                       window_size: int = None) -> QFSeries:
    """
    Calculates the rolling volatility for the given series of returns. If the annualise parameter is set to be True,
    then it is obligatory to specify frequency.

    Parameters
    ----------
    qf_series: QFSeries
        series of returns or prices
    frequency: Frequency
        the frequency of samples in the returns series; it is only obligatory to specify frequency if the annualise
        parameter is set to True, which is a default value
    annualise: bool
        True if the volatility values should be annualised; False otherwise. If it is set to True, then it is obligatory
        to specify a frequency of the returns series.
    window_size: int
        number of samples from which the rolling volatility will be calculated. If it is not set, then only overall
        volatility (of the whole series) will be calculated

    Returns
    -------
    QFSeries
        Series of volatility values for each day concerning last window_size days.
    """
    returns_tms = qf_series.to_log_returns()
    if annualise:
        assert frequency is not None

    volatility_values = []
    for i in range(window_size - 1, len(returns_tms)):
        start_index = i - window_size + 1
        end_index = i + 1

        returns_from_window = returns_tms[start_index:end_index]
        volatility = get_volatility(returns_from_window, frequency, annualise)

        volatility_values.append(volatility)

    first_date_idx = window_size - 1
    dates = returns_tms.index[first_date_idx::]
    volatility_tms = QFSeries(data=volatility_values, index=dates)

    return volatility_tms
Exemple #7
0
 def volatility_fun(window):
     return get_volatility(SimpleReturnsSeries(window), self.frequency)
Exemple #8
0
 def volatility(window):
     return get_volatility(PricesSeries(window), freq)
Exemple #9
0
 def volatility(window):
     return get_volatility(PricesSeries(window), Frequency.DAILY)
Exemple #10
0
 def test_get_volatility(self):
     expected_volatility = 9.513791146776931
     actual_volatility = get_volatility(self.log_returns_tms, frequency=Frequency.DAILY)
     self.assertAlmostEqual(expected_volatility, actual_volatility)
Exemple #11
0
 def volatility(window):
     return get_volatility(PricesSeries(window),
                           Frequency.DAILY,
                           annualise=True)
Exemple #12
0
 def volatility(window):
     return get_volatility(PricesSeries(window),
                           self.frequency,
                           annualise=True)
 def simple_vol(close_tms):
     return get_volatility(close_tms, Frequency.DAILY)