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 (14)

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