Logo

SQL Server RunningSHARPE Function

Updated 2024-03-14 15:26:45.167000

Description

Use the scalar function RunningSHARPE to calculate the Sharpe ratio from column values in an ordered resultant table without the need to a self-join. The Sharpe ratio is calculated over all the values from the first value to the last value in the ordered group or partition. If the column values are presented to the function out of order, an error message will be generated.

The Sharpe ratio is calculated using Sharpe's 1994 revision of his original formula and is calculated as the mean difference of the returns and risk-free rate divided by the standard deviation of the differences multiplied by the square root of a scaling factor. For daily returns the scale factor might be 252; for weekly returns 52; for monthly returns 12. For the sake of consistency, the risk-free rate should be in the same units as the scaling factor. The standard deviation is the sample standard deviation.

SHARPE=\ \frac{\bar{R}-\ \bar{R_f}}{\sigma_{R-R_f}}\ast\ \sqrt{scale}

Syntax

SELECT [westclintech].[wct].[RunningSHARPE](
  <@R, float,>
 ,<@Rf, float,>
 ,<@Scale, float,>
 ,<@Prices, bit,>
 ,<@RowNum, int,>
 ,<@Id, tinyint,>)

Arguments

@R

the return or price value; if return values are being supplied, it should the percentage return in floating point format (i.e. 10% = 0.1). @R is an expression of type float or of a type that can be implicitly converted to float.

@Rf

the risk-free rate. @Rf is an expression of type float or of a type that can be implicitly converted to float.

@Scale

the scaling factor used in the calculation. @Scale is an expression of type float or of a type that can be implicitly converted to float.

@Prices

a bit value identifying whether the supplied @R values are prices (or portfolio values) or returns. If @Prices is true, then the return is calculated. @Prices is an expression of type bit or of a type that can be implicitly converted to bit.

@RowNum

the number of the row within the group for which the Sharpe ratio is being calculated. If @RowNum for the current row in a set is less than or equal to the previous @RowNum and @RowNum is not equal to 1, an error message will be generated. @RowNum is an expression of type int or of a type that can be implicitly converted to int.

@Id

a unique identifier for the RunningSHARPE calculation. @Id allows you to specify multiple Sharpe ratios within a resultant table. @Id is an expression of type tinyint or of a type that can be implicitly converted to tinyint.

Return Type

float

Remarks

If @Scale IS NULL then @Scale is set to 12.

If @Prices IS NULL then @Prices is set to 'False'.

Examples

In this example we have 12 months' worth of return data for and we want to calculate the SHARPE ratio.

SELECT CAST(eom as date) as eom,
       cast(r as float) as r,
       Cast(rf as float) as rf
into #s
FROM
(
    VALUES
        ('2012-01-31', -0.0123631941629511, 0.010733),
        ('2012-02-29', 0.021547301457008, 0.008281),
        ('2012-03-31', 0.109883487344315, 0.009401),
        ('2012-04-30', -0.135203619909502, 0.009261),
        ('2012-05-31', 0.053578903306823, 0.007886),
        ('2012-06-30', -0.0701231624950338, 0.008206),
        ('2012-07-31', 0.100833155308695, 0.009777),
        ('2012-08-31', 0.0118377644090821, 0.011936),
        ('2012-09-30', -0.0176448024549291, 0.009263),
        ('2012-10-31', -0.0300663803201874, 0.008798),
        ('2012-11-30', -0.0853462157809984, 0.013932),
        ('2012-12-31', 0.143485915492958, 0.008757)
) n (eom, r, rf);
SELECT EOM,
       r,
       rf,
       wct.RunningSHARPE(   r,                                    --@R
                            rf / 12,                              --@Rf
                            12,                                   --@Scale
                            'False',                              --@Prices
                            ROW_NUMBER() OVER (ORDER BY eom ASC), --@RowNum
                            NULL                                  --@Id
                        ) as SHARPE
FROM #s;
DROP TABLE #s;

This produces the following result.

EOMrrfSHARPE
2012-01-31-0.01236319416295110.010733NULL
2012-02-290.0215473014570080.0082810.545661833493556
2012-03-310.1098834873443150.0094012.13410450673763
2012-04-30-0.1352036199095020.009261-0.164456691190495
2012-05-310.0535789033068230.0078860.254426478335439
2012-06-30-0.07012316249503380.008206-0.244225006510992
2012-07-310.1008331553086950.0097770.346854303695086
2012-08-310.01183776440908210.0119360.384351708574908
2012-09-30-0.01764480245492910.0092630.272061894639026
2012-10-31-0.03006638032018740.0087980.113603855400751
2012-11-30-0.08534621578099840.013932-0.257896431576602
2012-12-310.1434859154929580.0087570.277573805034166

In this example we have 13 months' worth of price data for and we want to calculate the SHARPE ratio.

SELECT CAST(eom as date) as eom,
       cast(pr as float) as pr,
       cast(rf as float) as rf
into #s
FROM
(
    VALUES
        ('2011-12-31', 49.34, 0.011124),
        ('2012-01-31', 48.73, 0.010733),
        ('2012-02-29', 49.78, 0.008281),
        ('2012-03-31', 55.25, 0.009401),
        ('2012-04-30', 47.78, 0.009261),
        ('2012-05-31', 50.34, 0.007886),
        ('2012-06-30', 46.81, 0.008206),
        ('2012-07-31', 51.53, 0.009777),
        ('2012-08-31', 52.14, 0.011936),
        ('2012-09-30', 51.22, 0.009263),
        ('2012-10-31', 49.68, 0.008798),
        ('2012-11-30', 45.44, 0.013932),
        ('2012-12-31', 51.96, 0.008757)
) n (eom, pr, rf);
SELECT EOM,
       pr,
       rf,
       wct.RunningSHARPE(   pr,                                   --@R
                            rf / 12,                              --@Rf
                            12,                                   --@Scale
                            'True',                               --@Prices
                            ROW_NUMBER() OVER (ORDER BY eom ASC), --@RowNum
                            1                                     --@Id
                        ) as SHARPE
FROM #s;
DROP TABLE #s;

This produces the following result.

EOMprrfSHARPE
2011-12-3149.340.011124NULL
2012-01-3148.730.010733NULL
2012-02-2949.780.0082810.54566183349356
2012-03-3155.250.0094012.13410450673763
2012-04-3047.780.009261-0.164456691190498
2012-05-3150.340.0078860.254426478335437
2012-06-3046.810.008206-0.244225006510994
2012-07-3151.530.0097770.346854303695083
2012-08-3152.140.0119360.384351708574905
2012-09-3051.220.0092630.272061894639024
2012-10-3149.680.0087980.113603855400749
2012-11-3045.440.013932-0.257896431576604
2012-12-3151.960.0087570.277573805034163

In this example we have 13 months' worth of price data for three different portfolios and we want to calculate the SHARPE ratio.

SELECT CAST(eom as date) as eom,
       aaa,
       bbb,
       ccc,
       rf
into #s
FROM
(
    VALUES
        ('2011-12-31', 74.58, 49.34, 54.97, 0.011124),
        ('2012-01-31', 75.17, 48.73, 56.07, 0.010733),
        ('2012-02-29', 74.61, 49.78, 54.66, 0.008281),
        ('2012-03-31', 74.69, 55.25, 55.39, 0.009401),
        ('2012-04-30', 75.6, 47.78, 57.4, 0.009261),
        ('2012-05-31', 75.53, 50.34, 54.92, 0.007886),
        ('2012-06-30', 75.41, 46.81, 55.12, 0.008206),
        ('2012-07-31', 75.83, 51.53, 55.56, 0.009777),
        ('2012-08-31', 74.58, 52.14, 54.24, 0.011936),
        ('2012-09-30', 74.77, 51.22, 57.41, 0.009263),
        ('2012-10-31', 74.33, 49.68, 55.76, 0.008798),
        ('2012-11-30', 75.06, 45.44, 56.28, 0.013932),
        ('2012-12-31', 75.25, 51.96, 53.19, 0.008757)
) n (eom, aaa, bbb, ccc, rf);
SELECT EOM,
       wct.RunningSHARPE(AAA, rf / 12, 12, 'True', ROW_NUMBER() OVER (ORDER BY eom 
                 ASC), 1) as AAA,
       wct.RunningSHARPE(BBB, rf / 12, 12, 'True', ROW_NUMBER() OVER (ORDER BY eom 
                 ASC), 2) as BBB,
       wct.RunningSHARPE(CCC, rf / 12, 12, 'True', ROW_NUMBER() OVER (ORDER BY eom 
                 ASC), 3) as CCC
FROM #s;
DROP TABLE #s;

This produces the following result.

EOMAAABBBCCC
2011-12-31NULLNULLNULL
2012-01-31NULLNULLNULL
2012-02-29-0.1815429090206280.545661910626499-0.366206366233852
2012-03-31-0.1268854108920282.134104535606610.278195680508385
2012-04-301.07500165715537-0.1644566765511271.37903712868579
2012-05-310.8180011815115420.254426495294565-0.0522708164825227
2012-06-300.553334383732037-0.2442249883585720.00901909082311352
2012-07-310.8673349588831160.3468543179889810.13899666737245
2012-08-31-0.2862514168382620.384351725488128-0.27706453633369
2012-09-30-0.190609200262460.2720619140296340.48771957908082
2012-10-31-0.4537910393947070.1136038769503880.119798191788006
2012-11-30-0.0820413791871152-0.2578964117978910.19893568874376
2012-12-31-0.01379001254393970.277573820987464-0.314234829951125

In this example, we have the same data as in the previous example, except that is stored in 3rd normal form rather than in the de-normalized form. The risk-free rate is included in the table with using the symbol RF. We use the PARTITION clause in order to generate the correct row number within a symbol.

SELECT CAST(eom as date) as eom,
       sym,
       CAST(pr as float) as pr
INTO #s
FROM
(
    VALUES
        ('2011-12-31', 'AAA', 74.58),
        ('2012-01-31', 'AAA', 75.17),
        ('2012-02-29', 'AAA', 74.61),
        ('2012-03-31', 'AAA', 74.69),
        ('2012-04-30', 'AAA', 75.6),
        ('2012-05-31', 'AAA', 75.53),
        ('2012-06-30', 'AAA', 75.41),
        ('2012-07-31', 'AAA', 75.83),
        ('2012-08-31', 'AAA', 74.58),
        ('2012-09-30', 'AAA', 74.77),
        ('2012-10-31', 'AAA', 74.33),
        ('2012-11-30', 'AAA', 75.06),
        ('2012-12-31', 'AAA', 75.25),
        ('2011-12-31', 'BBB', 49.34),
        ('2012-01-31', 'BBB', 48.73),
        ('2012-02-29', 'BBB', 49.78),
        ('2012-03-31', 'BBB', 55.25),
        ('2012-04-30', 'BBB', 47.78),
        ('2012-05-31', 'BBB', 50.34),
        ('2012-06-30', 'BBB', 46.81),
        ('2012-07-31', 'BBB', 51.53),
        ('2012-08-31', 'BBB', 52.14),
        ('2012-09-30', 'BBB', 51.22),
        ('2012-10-31', 'BBB', 49.68),
        ('2012-11-30', 'BBB', 45.44),
        ('2012-12-31', 'BBB', 51.96),
        ('2011-12-31', 'CCC', 54.97),
        ('2012-01-31', 'CCC', 56.07),
        ('2012-02-29', 'CCC', 54.66),
        ('2012-03-31', 'CCC', 55.39),
        ('2012-04-30', 'CCC', 57.4),
        ('2012-05-31', 'CCC', 54.92),
        ('2012-06-30', 'CCC', 55.12),
        ('2012-07-31', 'CCC', 55.56),
        ('2012-08-31', 'CCC', 54.24),
        ('2012-09-30', 'CCC', 57.41),
        ('2012-10-31', 'CCC', 55.76),
        ('2012-11-30', 'CCC', 56.28),
        ('2012-12-31', 'CCC', 53.19),
        ('2011-12-31', 'RF', 0.011124),
        ('2012-01-31', 'RF', 0.010733),
        ('2012-02-29', 'RF', 0.008281),
        ('2012-03-31', 'RF', 0.009401),
        ('2012-04-30', 'RF', 0.009261),
        ('2012-05-31', 'RF', 0.007886),
        ('2012-06-30', 'RF', 0.008206),
        ('2012-07-31', 'RF', 0.009777),
        ('2012-08-31', 'RF', 0.011936),
        ('2012-09-30', 'RF', 0.009263),
        ('2012-10-31', 'RF', 0.008798),
        ('2012-11-30', 'RF', 0.013932),
        ('2012-12-31', 'RF', 0.008757)
) n (eom, sym, pr);
SELECT s1.EOM,
       s1.sym,
       wct.RunningSHARPE(
                            s1.pr,
                            s2.pr / 12,
                            12,
                            'True',
                            ROW_NUMBER() OVER (PARTITION BY s1.SYM ORDER BY s1.sym,
                                      s1.eom ASC),
                            1
                        ) as SHARPE
FROM #s s1
    JOIN #s s2
        ON s1.eom = s2.eom
WHERE s1.sym <> 'RF'
      AND s2.sym = 'RF';
DROP TABLE #s;

This produces the following result.

EOMsymSHARPE
2011-12-31AAANULL
2012-01-31AAANULL
2012-02-29AAA-0.181543074627337
2012-03-31AAA-0.12688566719547
2012-04-30AAA1.07500147666457
2012-05-31AAA0.818000958395123
2012-06-30AAA0.553334158265181
2012-07-31AAA0.867334743771591
2012-08-31AAA-0.286251571435952
2012-09-30AAA-0.190609377952824
2012-10-31AAA-0.453791227700159
2012-11-30AAA-0.0820415528633414
2012-12-31AAA-0.0137901803260117
2011-12-31BBBNULL
2012-01-31BBBNULL
2012-02-29BBB0.54566183349356
2012-03-31BBB2.13410450673763
2012-04-30BBB-0.164456691190498
2012-05-31BBB0.254426478335437
2012-06-30BBB-0.244225006510994
2012-07-31BBB0.346854303695083
2012-08-31BBB0.384351708574905
2012-09-30BBB0.272061894639024
2012-10-31BBB0.113603855400749
2012-11-30BBB-0.257896431576604
2012-12-31BBB0.277573805034163
2011-12-31CCCNULL
2012-01-31CCCNULL
2012-02-29CCC-0.36620642343843
2012-03-31CCC0.278195603450318
2012-04-30CCC1.37903706990828
2012-05-31CCC-0.0522708651764702
2012-06-30CCC0.00901903880240953
2012-07-31CCC0.13899661821098
2012-08-31CCC-0.277064588884071
2012-09-30CCC0.487719530266477
2012-10-31CCC0.119798141071374
2012-11-30CCC0.198935640030015
2012-12-31CCC-0.31423487026325

Using the same data, we could PIVOT the results into a tabular format.

SELECT EOM,
       AAA,
       BBB,
       CCC
FROM
(
    SELECT s1.EOM,
           s1.sym,
           wct.RunningSHARPE(
                                s1.pr,
                                s2.pr / 12,
                                12,
                                'True',
                                ROW_NUMBER() OVER (PARTITION BY s1.SYM ORDER BY 
                                          s1.sym, s1.eom ASC),
                                1
                            ) as SHARPE
    FROM #s s1
        JOIN #s s2
            ON s1.eom = s2.eom
    WHERE s1.sym <> 'RF'
          AND s2.sym = 'RF'
) d
PIVOT
(
    sum(SHARPE)
    for sym in (AAA, BBB, CCC)
) as P;

This produces the following result.

EOMAAABBBCCC
2011-12-31NULLNULLNULL
2012-01-31NULLNULLNULL
2012-02-29-0.1815430746273370.54566183349356-0.36620642343843
2012-03-31-0.126885667195472.134104506737630.278195603450318
2012-04-301.07500147666457-0.1644566911904981.37903706990828
2012-05-310.8180009583951230.254426478335437-0.0522708651764702
2012-06-300.553334158265181-0.2442250065109940.00901903880240953
2012-07-310.8673347437715910.3468543036950830.13899661821098
2012-08-31-0.2862515714359520.384351708574905-0.277064588884071
2012-09-30-0.1906093779528240.2720618946390240.487719530266477
2012-10-31-0.4537912277001590.1136038554007490.119798141071374
2012-11-30-0.0820415528633414-0.2578964315766040.198935640030015
2012-12-31-0.01379018032601170.277573805034163-0.31423487026325