using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; using Community.Components;namespace WealthLab.Strategies { public class TASCJan2021 : WealthScript { private StrategyParameter paramPattern; private StrategyParameter paramExitDays; public TASCJan2021() { paramPattern = CreateParameter("Pattern", 1, 1, 6, 1); paramExitDays = CreateParameter("Exit after", 3, 1, 10, 1); } protected override void Execute() { var _pattern = paramPattern.ValueInt; var _exitAfter = paramExitDays.ValueInt; int atrPeriod = 20, maPeriod = 80; double tick = Bars.SymbolInfo.Tick; var atr = ATR.Series(Bars, atrPeriod); var trendFilter = SMA.Series(Close, maPeriod); for(int bar = GetTradingLoopStartBar(Math.Max(atrPeriod,maPeriod)); bar < Bars.Count; bar++) { //key reversal bool keyRevBear = High[bar] > High[bar - 1] && Low[bar] < Low[bar - 1] && Close[bar] < Low[bar - 1]; bool keyRevBull = High[bar] > High[bar - 1] && Low[bar] < Low[bar - 1] && Close[bar] > High[bar - 1]; //island reversal bool islRevBear = Low[bar] > High[bar - 1] && Close[bar] < Open[bar]; bool islRevBull = High[bar] < Low[bar - 1] && Close[bar] > Open[bar]; //outside day bool outsideBull = this.isOutsideBar(bar) && Close[bar] > Low[bar] + ( 0.75 * (High[bar] - Low[bar])); bool outsideBear = this.isOutsideBar(bar) && Close[bar] < Low[bar] + ( 0.25 * (High[bar] - Low[bar])); //wide range day var ratio = TrueRange.Series(Bars)[bar] / atr[bar]; bool isWRBBull = outsideBull && (ratio > 1.5); bool isWRBBear = outsideBear && (ratio > 1.5); //3-day compression bool compression = CumDown.Series(TrueRange.Series(Bars), 1)[bar] >= 3; //gap open bool isGapUp = (this.isGap(bar) == CommonSignalsEx.GapType.FullUp) && (Open[bar] > Close[bar] + 0.5 * atr[bar]); bool isGapDown = (this.isGap(bar) == CommonSignalsEx.GapType.FullDown) && (Open[bar] < Close[bar] + 0.5 * atr[bar]); //trend filter bool isBullish = Close[bar] > trendFilter[bar]; bool isBearish = Close[bar] < trendFilter[bar]; if (IsLastPositionActive) { /* Exit after N days */ Position p = LastPosition; if (bar + 1 - p.EntryBar >= _exitAfter) ExitAtMarket(bar + 1, p, string.Format("After {0}", _exitAfter)); } else { switch (_pattern) { case 1: if( keyRevBear && isBearish) ShortAtMarket(bar + 1, "KeyRevBear"); if( keyRevBull && isBullish) BuyAtMarket(bar + 1, "KeyRevBull"); break; case 2: if (islRevBear && isBearish) ShortAtMarket(bar + 1, "IslRevBear"); if (islRevBull && isBullish) BuyAtMarket(bar + 1, "IslRevBull"); break; case 3: if (outsideBear && isBearish) ShortAtMarket(bar + 1, "OutsideBear"); if (outsideBull && isBullish) BuyAtMarket(bar + 1, "OutsideBull"); break; case 4: if (isWRBBear && isBearish) ShortAtMarket(bar + 1, "WRBBear"); if (isWRBBull && isBullish) BuyAtMarket(bar + 1, "WRBBull"); break; case 5: if (compression) { if(BuyAtStop(bar+1, Highest.Series(High,3)[bar], "CompressionBull") ==null) ShortAtStop(bar + 1, Lowest.Series(Low, 3)[bar], "CompressionBear"); } break; case 6: if (isGapUp && isBullish) BuyAtClose(bar, "GapUp"); if (isGapDown && isBearish) ShortAtClose(bar, "GapDown"); break; default: break; } } } } } }