Sharpe and Sortino ratios

Hello,

I want to access and plot Sortino ratio and Sharpe ratio along with the time series of any of the equity/index, I tried looking at the forum but could not get any information. Kindly help in this regard.

Below is a code for Sortino ratio "used by some other analysis software", experts here can get an idea to translate it to an AFL for AB.

Thanks & Regards

VectorD ret = TA.Perf(close, 1);

for(int i=0;i<result.Length;i++)
{
int index = i - (int)period[0] + 1;
double avg = 0;
int count = 0;
if(index <= 0)
{
result[i] = double.NaN;
}
else
{
for(int j=index;j<=i;j++)
{
avg = avg + ret[j];
count++;
}
if(count != 0)
{
avg = avg / count;

double dsdv = 0;
for(int j=index;j<=i;j++)
{
if(ret[j] < avg)
{
dsdv = dsdv + Math.Pow(ret[j] - avg, 2);
}
}
dsdv = (Math.Sqrt(dsdv / (count - 1)) * Math.Sqrt(250)) / 100;
double ret1 = Math.Pow(close[i] / close[index-1], (365.5 / (double)count)) - 1;
if(dsdv != 0)
{
result[i] = ret1 / dsdv;
}
}
else
{
result[i] = 0;
}
}
}

cFunctions.SetForwardAndBackwardBars((int)period[0], 0);

Sharpe ratio is available right after backtest out of the box, see Reading backtest report

You don't need code for that.

Note however that both Sharpe and Sortio are single numbers (scalars) calculated from backtest. It is a single metric describing the WHOLE backtest period. A single number can't really be used for chart.

To display a chart you need ARRAY of numbers.

1 Like