using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;namespace WealthLab.Strategies { public class TAC_DMI_Strategy : WealthScript { #region Convergence functions double Average(List lst) { double total = 0.0; double average = 0.0; int period = lst.Count; if( period > 0 ) { for (int x = 0; x < period; x++) total += lstx; average = total / period; } return average; } bool Convergence(List lst, double scatter, double threshold, bool greater) { bool result = false; if( lst.Count > 0 ) { lst.Sort(); double first = lst0; double last = lstlst.Count-1; double average = Average(lst); double diff = last - first; double pct = diff / average * 100; if( !double.IsInfinity(average)) { if( greater ) result = ( (first >= threshold && last >= threshold) && pct <= scatter ); else result = ( (first <= threshold && last <= threshold) && pct <= scatter ); } } return result; } #endregion private StrategyParameter paramADXThDn; private StrategyParameter paramADXThUp; public TAC_DMI_Strategy() { paramADXThDn = CreateParameter("ADX Up.Th.", 70, 70, 90, 10); paramADXThUp = CreateParameter("ATR Lo.Th.", 30, 10, 30, 10); } protected override void Execute() { #region DataSeries ADX adx4 = ADX.Series(Bars,4); ADX adx5 = ADX.Series(Bars,5); ADX adx6 = ADX.Series(Bars,6); DIPlus dip5 = DIPlus.Series(Bars,5); DIPlus dip8 = DIPlus.Series(Bars,8); DIPlus dip14 = DIPlus.Series(Bars,14); DIMinus dim5 = DIMinus.Series(Bars,5); DIMinus dim8 = DIMinus.Series(Bars,8); DIMinus dim14 = DIMinus.Series(Bars,14); LineStyle ls = LineStyle.Solid; #endregion #region Plotting ChartPane adxPane = CreatePane(35,true,true); PlotSeries(adxPane,adx4,Color.Purple,ls,1); PlotSeries(adxPane,adx5,Color.Green,ls,1); PlotSeries(adxPane,adx6,Color.Violet,ls,1); DrawHorzLine(adxPane,30,Color.Blue,LineStyle.Dashed,1); DrawHorzLine(adxPane,70,Color.Red,LineStyle.Dashed,1); ChartPane dipPane = CreatePane(35,true,true); PlotSeries(dipPane,dip5,Color.Purple,ls,1); PlotSeries(dipPane,dip8,Color.Green,ls,1); PlotSeries(dipPane,dip14,Color.Violet,ls,1); DrawHorzLine(dipPane,50,Color.Red,LineStyle.Dashed,1); DrawHorzLine(dipPane,10,Color.Black,LineStyle.Dashed,1); DrawHorzLine(dipPane,50,Color.Black,LineStyle.Dashed,1); ChartPane dimPane = CreatePane(35,true,true); PlotSeries(dimPane,dim5,Color.Purple,ls,1); PlotSeries(dimPane,dim8,Color.Green,ls,1); PlotSeries(dimPane,dim14,Color.Violet,ls,1); DrawHorzLine(dimPane,50,Color.Red,LineStyle.Dashed,1); DrawHorzLine(dimPane,10,Color.Black,LineStyle.Dashed,1); DrawHorzLine(dimPane,50,Color.Black,LineStyle.Dashed,1); HideVolume(); Font font = new Font("Arial", 12, FontStyle.Bold); #endregion Plotting for(int bar = GetTradingLoopStartBar(14 * 3); bar < Bars.Count; bar++) { List lstADX = new List() { adx4bar-1,adx5bar-1,adx6bar-1 }; List lstDIP = new List() { dip5bar-1,dip8bar-1,dip14bar-1 }; List lstDIM = new List() { dim5bar-1,dim8bar-1,dim14bar-1 }; bool adxConvergingUp = Convergence( lstADX, 10, paramADXThUp.ValueInt, false ); bool adxConvergingDown = Convergence( lstADX, 10, paramADXThDn.ValueInt, true ); bool adxTurnup = TurnUp(bar, adx4) && TurnUp(bar, adx5) && TurnUp(bar, adx6); bool adxTurndown = TurnDown(bar, adx4) && TurnDown(bar, adx5) && TurnDown(bar, adx6); bool dipTurnup = TurnUp(bar, dip5) && TurnUp(bar, dip8) && TurnUp(bar, dip14); bool dimTurnup = TurnUp(bar, dim5) && TurnUp(bar, dim8) && TurnUp(bar, dim14); // Signal 1: start of an uptrend if( adxConvergingUp && adxTurnup ) { SetBackgroundColor( bar, Color.FromArgb( 20, Color.Green ) ); AnnotateBar( "Possible uptrend start", bar, true, Color.Green, Color.Transparent, font ); //BuyAtMarket(bar + 1, "Signal-1-Long"); } // Signal 2: start of an uptrend if( adxConvergingDown && adxTurndown ) { SetBackgroundColor( bar, Color.FromArgb( 20, Color.Red ) ); AnnotateBar( "Possible downtrend start", bar, true, Color.Red, Color.Transparent, font ); //ShortAtMarket(bar + 1, "Signal-2-Short"); } // Signal 3: market bottom (DI+/ADX cluster reversal) if( adxConvergingDown && Average(lstDIP) <= 10 && dipTurnup ) { SetBackgroundColor( bar, Color.FromArgb( 40, Color.Green ) ); AnnotateBar( "Possible bottom", bar, true, Color.Green, Color.Transparent, font ); //BuyAtMarket(bar + 1, "Signal-3-Long"); } // Signal 4: market top (DI-/ADX cluster reversal) if( adxConvergingDown && Average(lstDIM) <= 10 && dimTurnup ) { SetBackgroundColor( bar, Color.FromArgb( 40, Color.Red ) ); AnnotateBar( "Possible top", bar, true, Color.Red, Color.Transparent, font ); //ShortAtMarket(bar + 1, "Signal-4-Short"); } } } } }