Files used in the webinar - Algorithmic Trading with MATLAB Products for Financial Applications broadcast on November 18, 2010. This webinar can be viewed at http://www.mathworks.com/wbnr52491.
The download includes an additional demo, not shown in the webinar, that shows how to generate C-code from MATLAB.
I run the Algotrading1 and Algotrading2 with different data, but the result for the best ledlag is the same for different datas, such as 10:394 and 2:396.
why do my best estimators not change when I use different datas?
This is a lot of stuff in very compact form. Thanks for this good example.
Just one comment: If you publish examples you should take the work to make them correct or even tell the user that it is delivering wrong results and can not be used (see Javier comment).
Hello Justinas: 250 days, 11 trading hours, 60 minutes in one hour.
Trading signal as exposed here, are wrong for the performance measure and sharpe calculation (lines 76 leadlag.m). In the same m file, plot result section, the signal helps to identify that you keep long or short.
It is important to differentiate variables definition with a graphical purpose and variables definition for calculation purpose.
I used the AlgoTradingDemo1.m with SMA+RSI to find an optimal parametres for few currencies and I received the following error.
Error in @(x) marsiFun(x,BundClose,annualScaling,cost);
Error in parametersweep at 57
resp=fun(cell2mat(var));
Error in AlgoTradingDemo1 at 35
[maxSharpe,param,sh] = parameterSweep(fun,range);
The loop change the FX price and gives each time different parametres for each currency.
Could you please give me an idea of what could cause this error?
The 2009 version of this has some sample stop code. Fwiw, I haven't found a good array way of doing it, as opposed to looping through the whole period. The biggest drawback being that you cannot use parallel computing.
Any thoughts on the best way to make this a long/short/flat system, ie you can be 1=long, 0=flat, -1=short? So far you're either long or short, ie always in the market. Any thoughts on what the best way is to run the L/S conditions through the tradesignal file using bit operations?
Interesting approach, still working through this. Bring on the curve fitting!
The tradeSignal.m definitely has a mistake. Couldn't quite get Ivan's solution to work. I fixed it as follows: change to idx = indLoc(:); and add
if (ind2use(iInd)==0) sigC=sigB;
elseif (ind2use(iInd+1)==0) sigC=sigA;
I also renamed some vars and got rid of that eval business, seems to work faster. Full code below, if there are more mistakes feel free to comment.
Re the Sharpe annualization - OMG guys! When you use that Sharpe maximization, you should also realize that you're not really maximizing the Sharpe ratio unless you are working with indexed prices. The PnL function gives prices differences (not returns), Sharpe ratio works with returns!
function sigOut = tradeSignal(pop,ind)
%% BitString Length
cntBString = size(pop,2);
%% Calculate Indicators
% Length = 4*NoIndicators - 2
cntInd = (cntBString+2)/4;
% indicator locations
indLoc = 1:3:cntBString-cntInd;
indLocEnd = max(indLoc);
%% Generate Signal
sigOut = zeros(size(ind,1),size(pop,1));
for iPop = 1:size(pop,1)
ind2use = logical(pop(iPop,cntBString-cntInd+1:end));
idx = indLoc(:);
for iInd = 1:length(idx)-1
% indicator 1
if (iInd == 1) %evaluate first indicator
sigA=[ind(:,iInd)==pop(iPop,idx(iInd))];
else %for all other indicators, combine with the results generated so far
sigA = sigC;
end
Following Ivan, it seems that we need to transform raw signal -1/+1 into 0/1 in order to use tradeSignal.m accurately. suppose pop=[1 0 0 0 1 1], tradeSignal(pop,[1 -1])=0 while tradeSignal(pop, [1 0])=1, the latter being the correct signal given the rule.
pop = [ 1 0 0 1 0 0 0 0 1 1]
,which means not to use indicator1, and return True if indicator2 == 1 and indicator3 == 0
signals = [ 0 1 0], which must satisfy the conditions of pop.
Now, I run tradeSignal(pop, signals) and get 0.
So there is a mistake. I'm thinking of line 50 and 57. But first we should declare
line 46: filteredSignals = ind(:,ind2use);
line 51: A = eval(['(filteredSignals(:,1) == pop(r,',idxstr,'));']);
line 57: B = eval(['(filteredSignals(:,',num2str(i+1),') == pop(r,',idxstr,'));']);
%%
% Develop a trading signal and performance measures. We'll assume 250
% trading days per year.
s = zeros(size(BundClose));
s(lead>lag) = 1; % Buy (long)
s(lead<lag) = -1; % Sell (short)
r = [0; s(1:end-1).*diff(BundClose)]; % Return
sh = sqrt(250)*sharpe(r,0); % Annual Sharpe Ratio
Hi Stuart,
if I am to use minute data(NOT DAILY as used in the example) does that mean i have to adjust the sharpe annual scaling as well?
I am having difficulty trying to load my own high frequency data in line 181 of AlgoTradingDemo1.m. I get the following error message:
??? Error using ==> movavg at 41
Lead and lag arguments must be positive <= 23.
I believe the error stems from line 39 of movavg.m:
r = length(asset);
somehow, input 'asset' is being passed as zero to function movavg. I notice that when bund1min.mat is loaded using the load function, it automatically generates a variable 'data'. When I load my own .mat file, it does not generate a 'data' variable. Could this be the problem?