The point of this post is to provide some basic functions to non professional Matlab users that may help backtest a simple long only system the way Amibroker (and most other software) backtest.
If you are an Amibroker user you are used to something this easy:
Wish you could do that in Matlab?
Here's the Matlab Code for a simple Moving Average Crossover system using some custom functions
%You need these files in the "Path" for the code to work
%sma.m, iif.m, Cross.m, backtestlongAmount.m, GetTickerV.m, stock.m
%Download SPY from Yahoo, 5 years history, daily bars. We get a "fints" object
% Function TickerV() is similar to function stock() except that
%it adjusts for OHLC to Adjusted Close Data.
%%Get the close price from fints and make it a vector
buy=Cross(sma9,sma21); % custom function, NOT Matlab's native cross( )
%Signal should be 1 for Buy, -1 for Sell and 0 for noAction (or Hold)
% Buy and Sell on next bar Open
[pnl,pnlvector, sh]= backtestlongAmount(close,signal,open,1,100000);
% [pnl,pnlvector, sh]= backtestlongAmount(close,signal,close,0,100000);
You are free to copy the code and download the functions. I am not a professional coder. Most functions include pre-existing code that I combined and edited. It's very probable that mistakes exist.
I am sharing these m files in hope others will check and improve them.
Disclaimer: This code may not provide accurate results. You will have to check yourself to make sure the code works as it should.
The function are:
here's the link: