using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;namespace WealthLab.Strategies { public class SVETrendsStopStrategy : WealthScript { StrategyParameter atrFactor; StrategyParameter period; public SVETrendsStopStrategy() { atrFactor = CreateParameter("ATR Factor", 2.8, 1, 10, 0.2); period = CreateParameter("Period", 10, 2, 100, 2); } public DataSeries TrndsStop(Bars b, int period, double factor ) { DataSeries loss = factor * TASCIndicators.ATRModified.Series(b, period); DataSeries resistance = b.Close + loss; DataSeries support = new DataSeries(b, "Support(" + period + "," + factor + ")"); DataSeries trends = new DataSeries(b, "TrendsStop(" + period + "," + factor + ")"); for (int bar = 0; bar < period; bar++) supportbar = b.Lowbar; for (int bar = period; bar < b.Count; bar++) { double b2 = b.Lowbar-2; double prev = trendsbar-1; if( (b.Lowbar >= b2) && (b.Lowbar-1 >= b2) && (b.Lowbar-3 >= b2) && (b.Lowbar-4 >= b2) ) supportbar = b2; else if ( b.Lowbar > b.Highbar-1 * 1.0013 ) supportbar = b.Highbar-1 * 0.9945; else if ( b.Lowbar > supportbar-1 * 1.1 ) supportbar = supportbar-1 * 1.05; else supportbar = supportbar-1; if( (b.Highbar > prev) && (b.Highbar-1 > prev) ) trendsbar = Math.Max(prev, supportbar); else if( (b.Highbar < prev) && (b.Highbar-1 < prev) ) trendsbar = Math.Min(prev, resistancebar); else if( (b.Highbar >= prev) ) trendsbar = supportbar; else trendsbar = resistancebar; } return trends; } protected override void Execute() { DataSeries stop = TrndsStop(Bars, period.ValueInt, atrFactor.Value); PlotSeries(PricePane, stop, Color.Blue, LineStyle.Solid, 2); for(int bar = 20; bar < Bars.Count; bar++) { if (IsLastPositionActive) { if( CrossUnder(bar, High, stop) ) SellAtMarket(bar + 1, LastPosition); } else if( CrossOver(bar, Close, stop) ) BuyAtMarket(bar + 1); } } } }