using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;namespace WealthLab.Strategies { public class TASC_2018_08 : WealthScript { private StrategyParameter paramMode; private StrategyParameter paramLength; private StrategyParameter paramADX; private StrategyParameter paramConsTimeout; private StrategyParameter paramConsFactor; private StrategyParameter paramEntryBreakout ; public TASC_2018_08() { paramMode = CreateParameter("A/D/ ATR/ ADX", 0, 0, 2, 1); paramLength = CreateParameter("Length", 4, 4, 10, 2); paramADX = CreateParameter("ADX Trigger", 30, 5, 40, 5); paramConsTimeout = CreateParameter("Cons. timeout", 4, 2, 20, 2); paramConsFactor = CreateParameter("Cons. factor", 0.75, 0.25, 0.75, 0.25); paramEntryBreakout = CreateParameter("Brkout/Pullback", 0, 0, 1, 1); } protected override void Execute() { int mode = paramMode.ValueInt; int Length = paramLength.ValueInt, ADXTrigger = paramADX.ValueInt, TopBar = -1, BottomBar = -1, ConsolidationBar = -1, ConsolidationTimeout = paramConsTimeout.ValueInt; double ConsolidationFactor = paramConsFactor.Value, ATRFactor = paramConsFactor.Value, Top = 0.0, Bottom = 0.0; var Range_ = Highest.Series(High,Length)-Lowest.Series(Low,Length); var ATR_ = ATR.Series(Bars,Length); var ADX_ = ADX.Series(Bars,Length); int minPeriod = Math.Max(Length * 3, 20); bool Consolidation = false, breakout = paramEntryBreakout.ValueInt == 0 ? true : false; for(int bar = GetTradingLoopStartBar(minPeriod); bar < Bars.Count; bar++) { if( !Consolidation ) { bool conditionRange = (Range_[bar] < ConsolidationFactor * Range_[bar - Length]) && paramMode.ValueInt == 0; bool conditionATR = (ATR_[bar] < ATRFactor * ATR_[Length]) && paramMode.ValueInt == 1; bool conditionADX = (ADX_[bar] < ADXTrigger) && paramMode.ValueInt == 2; if( conditionRange || conditionATR || conditionADX ) { Consolidation = true; Top = Highest.Series(High,Length)[bar]; Bottom = Lowest.Series(Low,Length)[bar]; TopBar = bar; BottomBar = bar - Length; ConsolidationBar = bar; } } if( Consolidation ) { double[] rectangle = { TopBar, Top, TopBar, Bottom, BottomBar, Bottom, BottomBar, Top }; //if( ActivePositions.Count == 0 ) DrawPolygon( PricePane, Color.Blue, Color.FromArgb(30,Color.LightSteelBlue), LineStyle.Solid, 1, true, rectangle ); if( Range_[bar] > ConsolidationFactor * Range_[bar - Length] ) Consolidation = false; if( bar > ConsolidationBar + ConsolidationTimeout ) Consolidation = false; } if (IsLastPositionActive) { SellAtTrailingStop(bar+1, LastPosition, Bottom); } else { if( Consolidation ) { if( breakout ) // Breakout entry { if( Bottom > Lowest.Series(Low,Length)[bar - 12] ) if( BuyAtStop(bar+1, Top, "Breakout") != null ) Consolidation = false; else Consolidation = bar + 1 - ConsolidationBar < ConsolidationTimeout; } else // Pullback entry { if(( Close[bar] > Bottom ) && CrossOver(bar, RSI.Series(Close, Length), 30 ) && Bottom > Lowest.Series(Low,Length)[bar - 12]) if( BuyAtMarket(bar+1, "Pullback") != null ) Consolidation = false; else Consolidation = bar + 1 - ConsolidationBar < ConsolidationTimeout; } } } } } } }