Skip to content
All library documents

Monthly Returns, Sharpe Ratio, and Sortino Ratio Calculation

Article TradingView scripts

Summary

This Pine library provides functions for keeping a capped queue of observations, converting timestamps to month counts, and compiling chart-level percentage changes into monthly returns. It compounds non-missing bar returns within each month and can also calculate monthly excess returns against a benchmark. When months are missing between observations, it inserts zero-return periods; the benchmark version requires aligned histories and valid observations from both series.

The library calculates Sharpe and Sortino ratios from a return array using an annual benchmark divided by the selected periods per year, defaulting to a monthly basis. Sharpe divides average excess return by total standard deviation; Sortino uses downside deviation relative to the benchmark. The example samples returns at a selectable timeframe, limits the return history, plots sampled changes, and displays both ratios. It is an implementation example rather than evidence of strategy performance; ratio values depend on return sampling, benchmark assumptions, and the supplied data history.

Key ideas

  • Monthly returns are formed by compounding non-missing bar percentage changes within each period.
  • A benchmark overload compounds both series and stores their difference as monthly excess return.
  • The Sharpe calculation scales an annual benchmark to the selected return period and divides excess average return by standard deviation.
  • The Sortino calculation substitutes downside deviation relative to the benchmark for total standard deviation.
  • The example requires a requested return timeframe at least as large as the chart timeframe.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.