Log in to see Cloud of Tags

Wealth-Lab Wiki

TASC 2009-08 | DMI and Moving Avg (Kerstens)

RSS

Traders' Tip text

Wealth-Lab Version 6 code in C# for combining the DM and Moving Average indicators is presented below with the minor modification of making this a stop and reverse strategy. As the periods chosen seem quite arbitrary, it seems possible to find a sweeter spot for the strategy by optimizing. While adding complexity, it also may pay off investigating the use of a loose trailing stop in order to prevent whipsaws in the middle of a strong trend (possibly detected using ADX) like the one in September 2008 (Figure 1).


Image

Figure 1. DMI and moving average crossovers often indicate the same trend on nearly the same bar.


WealthScript Code (C#)

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

namespace WealthLab.Strategies { public class CombinedDMI_MA : WealthScript { StrategyParameter maPeriod; StrategyParameter dmPeriod; public CombinedDMI_MA() { maPeriod = CreateParameter("MA Period", 30, 10, 55, 5); dmPeriod = CreateParameter("DM Period", 14, 10, 21, 1); } protected override void Execute() { HideVolume(); int maPer = maPeriod.ValueInt; int dmPer = dmPeriod.ValueInt; bool dmiLong = false; bool dmiShort = false; bool maLong = false; bool maShort = false; DataSeries ma = SMA.Series(Close, maPer); DataSeries diplus = DIPlus.Series(Bars, dmPer); DataSeries diminus = DIMinus.Series(Bars, dmPer); ChartPane diPane = CreatePane(40, true, true); PlotSeries(diPane, diplus, Color.Green, LineStyle.Solid, 2); PlotSeries(diPane, diminus, Color.Red, LineStyle.Solid, 2); PlotSeries(PricePane, ma, Color.Blue, LineStyle.Solid, 2); for(int bar = Math.Max(maPer, dmPer); bar < Bars.Count; bar++) { if( CrossOver(bar, diplus, diminus) ) { dmiLong = true; dmiShort = false; } else if( CrossUnder(bar, diplus, diminus) ) { dmiLong = false; dmiShort = true; } if( CrossOver(bar, Close, ma) ) { maLong = true; maShort = false; } else if( CrossUnder(bar, Close, ma) ) { maLong = false; maShort = true; } if (IsLastPositionActive) { Position p = LastPosition; if( p.PositionType == PositionType.Long ) { if( dmiShort && maShort ) { SellAtClose(bar, p); ShortAtClose(bar); } } else if( dmiLong && maLong ) { CoverAtClose(bar, p); BuyAtClose(bar); } } else if( dmiLong && maLong ) BuyAtClose(bar); else if( dmiShort && maShort ) ShortAtClose(bar); } } } }

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.


ScrewTurn Wiki. Some of the icons created by FamFamFam.