Hi,
Upon using the data and the first code you have provided I got the sharpeRatio to be 0.7873
Now coming to your 2nd snippet, I have modified the code to achieve the same result as the first one:
clear;
T = readtable("IGE.xlsx");
tday=T{1:end, 1};
tday.Format = 'yyyy-MMM-dd';
cls=T{:, end};
[tday, sortIndex] = sort(tday, 'ascend');
cls = cls(sortIndex);
dailyret=(cls(2:end)-cls(1:end-1))./cls(1:end-1);
excessRet = dailyret - 0.04/252;
sharpeRatio = sqrt(252)*mean(excessRet)/std(excessRet)
I am not sure why you are using cls as a table instead of an array, modified cls to an array.
Sortrows, sorts according to the data provided, as you have provided cls it sorted cls in ascending order, modified it to sort according to the dates. Hope this helps!
Best Answer