Class SharpeRatioRTIndicator

All Implemented Interfaces:
Resettable, RTIndicator

public class SharpeRatioRTIndicator extends AbstractWindowSeriesRTIndicator
Rolling Sharpe Ratio with O(1) per-tick updates.

Computes the risk-adjusted return over a rolling window of log returns:

  Sharpe = (meanReturn - riskFreeRate) / stddev(returns)

Each tick produces a log return r = ln(price / prevPrice). Dual accumulators (sumR and sumRSq) maintain the running variance of these returns. The first tick establishes the initial price.

The risk-free rate defaults to 0 (excess returns). Set via withRiskFreeRate(double) — should be expressed per period (e.g. daily rate for daily data).

Annualization is supported via withAnnualizationFactor(double). The standard annualized Sharpe is Sharpe * sqrt(periodsPerYear).

Returns 0 when standard deviation is zero or window has fewer than 2 returns.

  • Constructor Details

    • SharpeRatioRTIndicator

      public SharpeRatioRTIndicator(int periods)
  • Method Details

    • onEvict

      protected void onEvict(double evicted)
      Description copied from class: AbstractWindowSeriesRTIndicator
      Called when the window evicts the oldest value. Override to update accumulators.
      Overrides:
      onEvict in class AbstractWindowSeriesRTIndicator
    • onReset

      protected void onReset()
      Description copied from class: AbstractWindowSeriesRTIndicator
      Called after window is cleared. Override to zero accumulators.
      Overrides:
      onReset in class AbstractWindowSeriesRTIndicator
    • withRiskFreeRate

      public SharpeRatioRTIndicator withRiskFreeRate(double rate)
      Sets the per-period risk-free rate. Default is 0.

      For example, if using daily bars and the annual risk-free rate is 5%, set this to 0.05 / 252.

    • withAnnualizationFactor

      public SharpeRatioRTIndicator withAnnualizationFactor(double factor)
      Sets the annualization factor. The output is multiplied by this factor.

      Common values:

      • Math.sqrt(252) — daily bars
      • Math.sqrt(52) — weekly bars
      • Math.sqrt(12) — monthly bars
    • getMeanReturn

      public double getMeanReturn()
      Returns the current mean log return over the window.
    • getStdDev

      public double getStdDev()
      Returns the current standard deviation of log returns over the window.
    • update

      public double update(double price)
      Specified by:
      update in interface RTIndicator
      Overrides:
      update in class AbstractRTIndicator