File Exchange

image thumbnail

Walk-Forward Analysis (WFA) - Files for WFA webinar

version (18.5 KB) by MathWorks Quant Team
The files are designed for walk-forward analysis of pair trading strategy using Bollinger Band


Updated 01 Feb 2018

View License

The files are designed for walk-forward analysis of pair trading strategy using Bollinger Band as entry and exit rules. In this example, you will see 5 pair of stocks tested over the period of 3 years. Performance analytic part includes profit/loss, Sharpe ratio, and maximum drawdown. I really hope that you will find this file as a starting point for backtesting your investment ideas. Definitely, you need to customize the file if you would like to add new rules, trading strategies, or market data. I would also recommend to watch the webinar on this topic.

Cite As

MathWorks Quant Team (2020). Walk-Forward Analysis (WFA) - Files for WFA webinar (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (16)

Hello, I really need the WFAToolbox. How can I download the file , please?

Thank You


Dear Kawee

Since Google Finance is not working, I have generated the opening and closing prices for each ticker in a separate excel file and adjusted the getPrices function.

For example: For the first Ticker "FANG", the first five rows in my "FANG.xlsx" file looks as follows:

Date Open Close
01.04.2013 26.93 27.40
02.04.2013 27.47 27.04
03.04.2013 27.05 24.92
04.04.2013 24.87 25.05
05.04.2013 24.69 25.18

I have adjusted your getPrices function as follows:

function [Open, Close,tDay] = getPricesNew(symbol)
filename =string(symbol) + ".xlsx";
data = readtable(filename);
data = sortrows(data);
data.Properties.VariableNames{1} = 'Date';
Open = data.Open;
Close = data.Close;
tDay = datenum(data.Date);

Now that I have managed to feed the matlab process with input prices, I still obtain errors when running runWFA1.m and runWFA2.m, see below:

Error using getHistPriceRange (line 36)
Array indices must be positive integers or logical values.

Error in strategy (line 45)
currPrice1 = getHistPriceRange(Prices,u(i,1),d,exPrice,winSize + 1);

Error in runIST (line 23)

Error in runWFA1 (line 13)
parfor j = 1:nParam

How can I fix this? It seems that other users also encounter multiple errors when running through the process. Would it be possible to provide a downloadable version that does not run into errors?

Thank you for your help.

Sorry everyone, this demo can not be run currently. At first, I developed based on Yahoo Finance. After Yahoo Finance stopped its free offering, I switched to Google Finance. Dejavu! Google Finance stoped thier free offering as well. In short, you can't run this demo. You will get an error.


Hi Thanks for sharing the great idea. I get below error on MATLAB R2019a, How do i fix this?

Total Walk is 36

nParam =


=============== In-sample test ===============
walk startIST endIST startOST endOST
____ ___________ ___________ ___________ ___________

1.00 01-Apr-2013 30-Jun-2013 01-Jul-2013 31-Jul-2013

Error using tick2ret (line 81)
Expected number of observations to be a scalar with value >= 2.

Error in runIST (line 39)
portReturn = tick2ret(portValue);

Error in runWFA1 (line 13)
parfor j = 1:nParam

Grateful for this!

Hi Babak,

The cost in updatePort function is the cost of buying or short-selling the asset. It might be a little bit confusing with transaction cost. Sorry about that.


babak znl

babak znl

hi, why do you set cost = TNew.execPrice in updatePort function?
i think it must be cost = TNew.cost

Brian Greer

Sorry for the inconvenience, I am working on fixing all Yahoo-related files. This one is on my radar too. Unfortunately, I haven't had a chance to work on this yet. I would suggest to manually download the price from Yahoo in csv format as a quick fix.


Did anyone code a workaround for the yahoo problem in getPrices.m of this demo?


Here you can learn more about another Walk-Forward Analysis (WFA) solution:


Change data source from Yahoo! Finance to Google Finance.
Remove one pair of stocks because prices are not completely available in the new data source.

Updated license

Updated license

Add copyright statement

MATLAB Release Compatibility
Created with R2016a
Compatible with any release
Platform Compatibility
Windows macOS Linux