Backtesting Code for Algorithmic Trading Strategy

Code to Backtest trading strategy
Updated 17 Dec 2010

View License

%Author: Moeti Ncube
%This is code that can be used to backtest a trading strategy. The example strategy used was partially used in the development of a medium-frequency algorithmic trading strategy; this is a some of the backtesting coding we use to analyze tick data.

This code can be used to backtest a trading strategy for a time series that has the price vector in the first column and trading indicator in
second column. I will be using NG futures contracts for trading and will track pnl in terms of ticks (NG trades in ticks, so .001 ticks on ICE would be about $70/contract, $10/contract on NYMEX)

Over 17 days this strategy on this dataset would make about $1060 on NYMEX, or $7427 on ICE.

Data is stored in the first column and a (proprietery) indicator, that basically tracks the speed of market, is stored in second column.

%This code can be adjusted to incorporate another dataset/indicator as long as it assumes the basic strategy outline I will describe here. This is actually a simplification of my real strategy. For example, my real Buy/Sell indicator updates to be vt=max(v1,....vt-1) whereas here I make it static.

%Buy/Sell Indicator:
Whenever the indicator is less than value "v1", you buy one contract at the current price in market. Whenever indicater is greater than value "v1' you sell one contract at
current price in market.

%Profit Target:
%If the weighted average long/short position is "pt" ticks in the money you close out your position, make "pt" and strategy starts over from current price/ind.

%Stop Loss:
If the weighted average long/short position is "st" ticks out of the money, your strategy depends on you double down indicator "dd", If dd=0, you take the loss the first time this happens and strategy starts over. If dd=1, you add 1 contract to your long/short postion and obtain a new weighted average long/short price. Now, If your new long/short position becomes "pt" ticks in the money you double your profit to "2*pt", however if it becomes "st" ticks out of the money, you double your losses to "2*st"; Unless dd=2, in which once again you would buy another contract for potential "3*pt" gain or "3*st" loss.

%val(d,:) (matrix)
Here I use intraday data from 17 days in the market, val provides output from each day of the [pnl,percent profitability, sharpe ratio, number of trades]

Here I track the profit or loss realized on each trade on a given day 'd'

Cite As

Moeti Ncube (2024). Backtesting Code for Algorithmic Trading Strategy (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2009b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Transaction Cost Analysis in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes