Wealth-Lab Wiki

API ChartStyles Community Components Community Indicators IndexDefinitions Knowledge Base Misc Optimizers Pending Deletion PosSizers Providers Standard Indicators TASC Traders Tips TASCIndicators Tutorial Videos Visualizers
RSS

Navigation


Quick Search
»
Advanced Search »


TASC 2016-12 | Pair Trading With a Twist (D'Errico)

RSS

Traders' Tip text

To help traders using Wealth-Lab draw their own conclusions on how Domenico D'Errico's pair trading systems work, we've coded all the three pair trading ideas into one system. To build your stock pair versus the SPY, run it on a single symbol's chart which is a S&P 500 component. It’s possible to apply the strategy to any instruments.

To switch between mean-reverting, trend & pullback and trend-following modes you can drag respective slider “Strategy mode” on the bottom left part of the screen. Likewise it's possible to adjust the fast and the slow moving averages periods. Wealth-Lab will apply changed parameters so you have a chance to see how entries and exits change interactively on the chart.

Image

Figure 1 stacks three chart panes: individual stocks' candlesticks (top), the price ratio with its moving averages (middle) and the first stock of the pair (bottom). Trade arrows are displayed in the bottom pane.

If you need more trading ideas, you could compare the strategies from this month's article to similar strategies already available for download from our website wealth-lab.com: Pairs Trading (from S&C March 2001 issue), Developing a Multi-Level Strategy (from S&C September 2012 issue), Timing The Market With Pairs (from S&C March 2014 issue) and Market-neutral VIX-based pair strategy (from Active Trader Magazine October 2010 issue).

On a closing note, to scan for different pairs combinations in any markets you can use a study like the one created by a Wealth-Lab community member. Called Correlation Matrix, it calculates and displays the correlations of all symbols in a watchlist.

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;

namespace WealthLab.Strategies { public class TASC201612 : WealthScript { StrategyParameter fastPeriod; StrategyParameter slowPeriod; StrategyParameter strategyMode;

public TASC201612() { fastPeriod = CreateParameter("Fast Period", 4, 1, 100, 1); slowPeriod = CreateParameter("Slow Period", 40, 20, 300, 5); strategyMode = CreateParameter("Strategy Mode", 0, -1, 1, 1); } protected override void Execute() { // Enter your pair symbols here string stock1, stock2, longSymbol, shortSymbol; longSymbol = stock1 = Bars.Symbol; shortSymbol = stock2 = "SPY";

int MAPeriod = fastPeriod.ValueInt, MAPeriod2 = slowPeriod.ValueInt, mode = strategyMode.ValueInt; // Compute DataSeries DataSeries close1, close2, open1, open2, high1, high2, low1, low2; SetContext( stock1, true ); Bars bars1 = Bars; close1 = Close; open1 = Open; high1 = High; low1 = Low; SetContext( stock2, true ); Bars bars2 = Bars; close2 = Close; open2 = Open; high2 = High; low2 = Low; RestoreContext();

DataSeries Ratio = close1 / close2; Ratio.Description = "Ratio"; DataSeries RatioO = open1 / open2; RatioO.Description = "RatioOpen"; DataSeries RatioH = high1 / high2; RatioH.Description = "RatioHigh"; DataSeries RatioL = low1 / low2; RatioL.Description = "RatioLow"; DataSeries RatioSMA = SMA.Series( Ratio, MAPeriod ); RatioSMA.Description = "Ratio Fast SMA"; DataSeries RatioSMA2 = SMA.Series( Ratio, MAPeriod2 ); RatioSMA2.Description = "Ratio Slow SMA";

// Graphics ChartPane RatioPane = CreatePane( 6000, true, true ); ChartPane StocksPane = CreatePane( 2000, true, true ); PlotSymbol(StocksPane,bars1,Color.Green,Color.Red); PlotSymbol(StocksPane,bars2,Color.Green,Color.Red); HideVolume(); PlotSyntheticSymbol( RatioPane, "Price Ratio " + stock1 + "/" + stock2 + " as OHLC", RatioO, RatioH, RatioL, Ratio, new DataSeries("Bogus"), Color.Navy, Color.Salmon ); PlotSeries( RatioPane, RatioSMA, Color.Gray, WealthLab.LineStyle.Solid, 2 ); PlotSeries( RatioPane, RatioSMA2, Color.Salmon, WealthLab.LineStyle.Solid, 2 );

for(int bar = MAPeriod; bar < Bars.Count; bar++) { // Trend following strategy if( mode == 1 ) { if( IsLastPositionActive ) { if (CrossUnder(bar, RatioSMA, RatioSMA2 )) ExitAtClose(bar, Position.AllPositions); } else { if( CrossOver(bar, RatioSMA, RatioSMA2 ) ) { longSymbol = stock1; shortSymbol = stock2; SetContext( longSymbol, true ); BuyAtClose( bar, "TF Buy " + shortSymbol ); SetContext( shortSymbol, true ); ShortAtClose( bar, "TF Short " + longSymbol ); } } } else // Mean reverting strategy if( mode == -1 ) { if( IsLastPositionActive ) { if (Ratio[bar] > RatioSMA[bar]) ExitAtClose(bar, Position.AllPositions); } else { if (Ratio[bar] < RatioSMA[bar]) { longSymbol = stock1; shortSymbol = stock2; SetContext( longSymbol, true ); BuyAtClose( bar, "MR Buy " + shortSymbol ); SetContext( shortSymbol, true ); ShortAtClose( bar, "MR Short " + longSymbol ); } } } else // Trend pullback strategy if( mode == 0 ) { if( IsLastPositionActive ) { if( (RatioSMA[bar] < RatioSMA2[bar]) && (Ratio[bar] > RatioSMA[bar]) ) ExitAtClose(bar, Position.AllPositions); } else { if( (RatioSMA[bar] > RatioSMA2[bar]) && (Ratio[bar] < RatioSMA[bar]) ) { longSymbol = stock1; shortSymbol = stock2; SetContext( longSymbol, true ); BuyAtClose( bar, "PB Buy " + shortSymbol ); SetContext( shortSymbol, true ); ShortAtClose( bar, "PB Short " + longSymbol ); } } }

RestoreContext(); } } } }

Eugene
Wealth-Lab team
www.wealth-lab.com

Important Disclaimer: The information provided by Wealth-Lab is strictly for informational purposes and is not to be construed as advice or solicitation to buy or sell any security.  The owner of Wealth-Lab.com assumes no liability resulting from the use of the material contained herein for investment purposes. By using this web site, you agree to the terms of this disclaimer and our Terms of Use.

Used under license from FMR Corp. Copyright 2008 FMR Corp. All rights reserved.


ScrewTurn Wiki. Some of the icons created by FamFamFam.