Be the first to rate this file! 59 Downloads (last 30 days) File Size: 395.29 KB File ID: #29762
image thumbnail

Backtesting Code for Algorithmic Trading Strategy

by Moeti Ncube

 

17 Dec 2010

Code to Backtest trading strategy

| Watch this File

File Information
Description

%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]

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

MATLAB release MATLAB 7.9 (2009b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (1)
15 Mar 2012 Lascu Roman

Hello.
How fast is this code given it contains the for loop? I'm searching for a fast algo to use in a large optimization. I'm wondering if more vectorisation is possible in this kind of problem? thanks.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
algorithmic trading Moeti Ncube 17 Dec 2010 15:39:37
backtesting Moeti Ncube 17 Dec 2010 15:39:37
backtesting Goryn 27 Dec 2010 07:57:00
algorithmic trading George Tzallas 02 Apr 2011 13:32:00
algorithmic trading Revelino 09 Nov 2011 02:29:12

Contact us at files@mathworks.com