+1 888 340 6572

# SharpeRatio

File Includes:
Function - LastBarOnChart
Function - LastCalcDate
Function - LastCalcTime
Function - SharpeRatio
Signal - Sharpe Demo
Strategy - Sharpe Demo

Category: Functions > SharpeRatio

Description:

This function calculates and returns the Sharpe Ratio of a series of account values. It samples the series of values on a yearly, quarterly, monthly, weekly, or daily basis as determined by an input. It also calculates average return and standard deviation. It prints the results in a form suitable for importing into an Excel spreadsheet for plotting.

Inputs:

Mode - Sampling period (0=yearly, 1=quarterly, 2=monthly, 3=weekly, 4=daily)
NetValue - The series of values to be sampled. It should be equal to the beginning equity plus accumulated net profits.
Periods - The number of yearly, quarterly, etc., periods to include in the calculation. If this value is zero, the function will use all periods up to a maximum of 1500.
PrntMode:
zero - Print one line summary only on last bar
> zero - Print values as stored in array plus summary
< zero - Do not print anything
Futures:
TRUE - For futures trading (Sharpe = Ave / SDev)
FALSE - For Stocks (Sharpe = (Ave - 5) / SDev)

Method: The function samples the value of the trading account at periodic intervals, calculates returns in each period, then calculates the average and standard deviation of returns and annualizes them. It then calculates to Sharpe Ratio as noted above.

Assumptions: The usage for stocks assumes a constant value of 5% for the risk-free return (T-Bill interest rate). This is a good assumption for recent times but may be incorrect for the distant past. The Sharpe Ratio is independent of the sampling interval if the returns are normally distributed. Returns are typically not strictly normally distributed so the sampling interval will affect the results somewhat. There should be more than about 25 samples to get reasonable accuracy so use daily samples for 1 to 6 months of trades, weekly samples for 6 months to 24 months of trades, etc.

EasyLanguage Code:
```INPUT:	MODE(NUMERICSIMPLE),
{0=YEARLY, 1=QUARTERLY, 2=MONTHLY, 3=WEEKLY, 4=DAILY}
NETVALUE(NUMERICSIMPLE),
{NET VALUE OF ACCOUNT = BEGINNING EQUITY + NETPROFIT}
PERIODS(NUMERICSIMPLE),
{NUMBER OF PERIODS TO USE IN CALCULATION, ZERO = ALL}
PRNTMODE(NUMERICSIMPLE),
{0 = PRINT SUMMARY, 1 = INCLUDE DETAIL, -1 = DON'T PRINT}
FUTURES(TRUEFALSE);
{TRUE FOR FUTURES, FALSE FOR STOCKS}

VARS:		INDEX(0), 			{INDEX USED TO INDEX RETURN ARRAY}
SINDEX(0), 			{INDEX USED TO SUM RETURN ARRAY}
LNETVAL(0),  		{NETVALUE AT END OF PREVIOUS PERIOD}
LCLOSE(0),			{CLOSE AT END OF PREVIOUS PERIOD}
YCLOSE(0),			{CLOSE AT END OF PREVIOUS BAR}
SIZE(0),				{SIXE OF DATA TO BE STORED IN ARRAY}
ILAST(0),  			{NUMBER OF ENTRIES IN ARRAY}
AVE(0),  			{AVERAGE RETURN}
ASUM(0),  			{USED TO CALC AVERAGE}
SSUM(0),  			{USED TO CALC STANDARD DEVIATION}
SDEV(0),  			{STANDARD DEVIATION}
SDMULT(0),  		{MULTIPLIER TO ANNUALIZE STANDARD DEVIATION}
MO(0),  				{MONTH FOR BAR}
MP(0),				{MARKETPOSITION}
MPX(0),				{MARKETPOSITION FLAG BECOMES 1 ON FIRST TRADE}
YMO(0),				{MONTH FOR PREVIOUS BAR}
YR(-99),				{YEAR FOR BAR}
YYR(0),				{YEAR FOR PREVIOUS BAR}
YDATE(0),			{DATE FOR PREVIOUS BAR}
AVMULT(0),  		{MULTIPLIER TO ANNUALIZE AVERAGE}
NETVAL(0),  		{NETVALUE SERIES}
YNETVAL(0),			{NETVAL FOR PREVIOUS BAR}
ACTIVE(FALSE),  	{FALSE FOR FIRST CALC THEN TRUE THEREAFTER}
RECORD(FALSE),  	{FLAG TO TRIGGER CALCULATION AT END OF PERIOD}
SUMMARY(FALSE),  	{FLAG SET IF SUMMARY PRINTED}
STDATE(0),			{START DATE}
SHARPE(0); 			{SHARPE RATIO}

ARRAY:	RETURN[1500](0);  {TABLE OF RETURNS AS A PERCENT}

SIZE   = IFF(PERIODS > 0, PERIODS, 1500);
SIZE   = MINLIST(SIZE, 1500);
NETVAL = NETVALUE;
MO     = MONTH(DATE);
YR     = YEAR(DATE);

{THIS DETERMINES MARKETPOSITION IN EITHER SYSTEMS OR INDICATORS}
IF MARKETPOSITION <> 0 THEN
MP = MARKETPOSITION
ELSE
MP = I_MARKETPOSITION;

MPX = IFF(MP <> 0, 1, MPX);

CONDITION1	= MO = 1 OR MO = 4 OR MO = 7 OR MO = 10;

BEGIN

{INITIALIZE FOR YEARLY}

IF MODE = 0 AND YR <> YYR THEN BEGIN
SDMULT 	= 1;
AVMULT 	= 1;
RECORD 	= TRUE;
END;

{INITIALIZE FOR QUARTERLY}

IF MODE = 1 AND MO <> YMO AND CONDITION1 THEN BEGIN
SDMULT 	= 2;
AVMULT 	= 4;
RECORD 	= TRUE;
END;

{INITIALIZE FOR MONTHLY}

IF MODE = 2 AND MO <> YMO THEN BEGIN
SDMULT 	= SQUAREROOT(12);
AVMULT 	= 12;
RECORD 	= TRUE;
END;

{INITIALIZE FOR WEEKLY}

IF MODE = 3 AND DAYOFWEEK(DATE) < DAYOFWEEK(YDATE) THEN BEGIN
SDMULT 	= SQUAREROOT(52);
AVMULT 	= 52;
RECORD 	= TRUE;
END;

{INITIALIZE FOR DAILY}

IF MODE = 4 AND DATE <> YDATE THEN BEGIN
SDMULT 	= SQUAREROOT(253);
AVMULT 	= 253;
RECORD 	= TRUE;
END;
END;

{ACTION IF NEW YEAR, QUARTER, MONTH, WEEK, OR DAY}

IF RECORD = TRUE THEN BEGIN
IF ACTIVE = TRUE THEN BEGIN
{EACH TIME EXCEPT FIRST TIME}
BEGIN
ILAST = ILAST + 1;
IF LNETVAL <> 0 THEN VALUE1 = YNETVAL / LNETVAL;
IF VALUE1 > 0 THEN RETURN[INDEX] = 100 * LOG(VALUE1);
IF PRNTMODE > 0 THEN PRINT(INDEX:5:0, DATE:7:0, YCLOSE:6:2,
LCLOSE:6:2, YNETVAL:7:0, LNETVAL:7:0, RETURN[INDEX]:4:2, ",");
INDEX = MOD(INDEX + 1, SIZE);
END;
END ELSE
{FIRST TIME ONLY AFTER INITIAL POSITION}
IF MPX > 0 THEN BEGIN
ACTIVE = TRUE;
STDATE = DATE;
IF PRNTMODE > 0 THEN PRINT(INDEX:5:0, DATE:7:0, YCLOSE:6:2,
LCLOSE:6:2, YNETVAL:7:0, LNETVAL:7:0, RETURN[INDEX]:4:2, ",");
END;

LCLOSE  = YCLOSE;
LNETVAL = YNETVAL;
RECORD  = FALSE;
END;

{CALCULATE AND PRINT SUMMARY}

IF ACTIVE = TRUE AND SUMMARY = FALSE AND
(LASTBARONCHART OR ILAST >= SIZE) THEN BEGIN

{CALCULATE AVERAGE RETURN IN PERIOD}
SUMMARY = TRUE;
ASUM	= 0;
ILAST = MINLIST(SIZE, ILAST);
FOR SINDEX = 0 TO ILAST - 1 BEGIN
ASUM = ASUM + RETURN[SINDEX];
END;
IF ILAST <> 0 THEN AVE = ASUM / ILAST;

{PRINT("AVE  = ", AVE:5:2);}

{CALCULATE ANNUALIZED STANDARD DEVIATION}
SSUM = 0;
FOR SINDEX = 0 TO ILAST - 1 BEGIN
SSUM = SSUM + SQUARE(RETURN[SINDEX] - AVE);
END;

{PRINT("SSUM = ", SSUM:5:2);}

IF ILAST <> 0 THEN SDEV = SDMULT * SQUAREROOT(SSUM / ILAST);

{PRINT("SDEV = ", SDEV:5:2);}

{ANNUALIZE AVERAGE}
AVE  = AVMULT * AVE;

{PRINT("AVE  = ", AVE:5:2);}

{CONVERT BACK TO RATIOS FROM LOGARITHMS}
SDEV = 100 * (EXPVALUE(SDEV / 100) - 1);
AVE  = 100 * (EXPVALUE(AVE  / 100) - 1);

{CALCULATE SHARPE RATIO}
IF SDEV <> 0 THEN BEGIN
IF FUTURES THEN
SHARPE = AVE / SDEV
ELSE
SHARPE = (AVE - 5) / SDEV;
END;

IF PRNTMODE >= 0 THEN
PRINT( ",", STDATE:6:0, ",", ILAST:6:0, ",", SDEV:6:1, "%,", AVE:6:1, "%,",
SHARPE:3:2, ",  ",GETSYMBOLNAME, ",");

END;

{PRINT(DATE:6:0, NETVAL, SHARPE:4:2, MP:2:0, ACTIVE);}

YMO     = MO;
YYR     = YR;
YDATE   = DATE;
YCLOSE  = CLOSE;
YNETVAL = NETVAL;

SHARPERATIO = SHARPE;```