Code covered by the BSD License

# Statistical Backtest Toolbox

### Benjamin Heelan (view profile)

15 Nov 2012 (Updated )

A Toolbox that allows the user to backtest trading strategies on the FTSE100.

...
```function [Algo_Performance, Buy_Hold_Performance] = ...

%       Given the results of a trading strategy, we can calculate the
%       following measures to evaluate the performance of the strategy.
%
%       -   Annualised Return = geometric average of rate of return
%       -   Annualised Volatility = volatility of rate of return during
%       -   SR = risk adjusted rate of return (Sharpe Ratio) during
%       -   p_in = proportion of in the market periods to the whole
%           horizon

%% Algorithm Performance

Algo_Performance = struct ('Annualised_Rate_of_Return',[],...
'Annualised_Volatility',[],...
'Sharpes_Ratio',[],...
'P_in',[],...

%       Rate of return of the asset during a single period r_k is
%       calculated at time T/N(k) from the asset prices at time T/N(k-1)
%       and T/N(k) (P_k-1 and P_k respectively) as ln(P_k)-ln(P_k-1)
%
%       Rate of return during the whole trading periods R is a sum of
%       rates of return in each period, and a geometric mean of a single
%       period return r is obtained by dividing R by the number of
%       periods N.

r = log(P_n_L.exit_long_price) - log(P_n_L.enter_long_price);

Algo_Performance.Annualised_Rate_of_Return = sum(r) / n;

%       The annualsied volatility (sigma) is the standard deviation of the
%       instruments yearly logarithmic return. The generalised volatility
%       sigma_T for time horizon T in years is expressed as:
%
%                       sigma_T = sigma * sqrt(T)
%
%       Therefore, if the daily logarithmic returns of a stock have a
%       standard deviation of sigma_SD and the time period of returns is P,
%       the annualized volatility is:
%
%                       sigma = sigma_SD / sqrt(P)
%
%       A common assumption is that P = 1/252 (there are 252 trading days
%       in any given year). Then, if sigma_SD = 0.01 the annualized
%       volatility is:
%
%                   sigma_annual = 0.01 / sqrt(1/252)

Algo_Performance.Annualised_Volatility = std(r)*sqrt(252);

Algo_Performance.Sharpes_Ratio = sharpe(r,0);

Algo_Performance.P_in = sum(P_n_L.exit_long_i - ...
P_n_L.enter_long_i)/length(C);

'Annualised_Volatility',[],...
'Sharpes_Ratio',[],...
'P_in',[],...

r_b     = [];
r_b(1)  = 0;

for j = 2:length(C)

r_b(j) = log(C(j)) - log(C(j-1));

end