using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;namespace WealthLab.Strategies { public class MyStrategy : WealthScript { StrategyParameter _nviLevel; StrategyParameter _atrPeriod; StrategyParameter _maxPositions; public MyStrategy() { _nviLevel = CreateParameter("Trigger Level", 3.7, 1, 5, 0.1); _atrPeriod = CreateParameter("ATR Period", 64, 30, 90, 2); _maxPositions = CreateParameter("Max Positions", 5, 1, 10, 1); } protected override void Execute() { double nviTrigger = _nviLevel.Value; int maxPositions = _maxPositions.ValueInt; int per = _atrPeriod.ValueInt; // Smoothed "nvi" DataSeries nvi = EMA.Series( ATRP.Series(Bars, per), 21, EMACalculation.Modern); ChartPane nviPane = CreatePane(40, true, true); PlotSeries(nviPane, nvi, Color.Blue, LineStyle.Solid, 2); DrawHorzLine(nviPane, nviTrigger, Color.Fuchsia, LineStyle.Dashed, 2); for(int bar = Math.Min(3 * per, 64); bar < Bars.Count; bar++) { if (nvi[bar] > nviTrigger ) { if ( CrossOver(bar, nvi, nviTrigger) ) { ExitAtMarket(bar + 1, Position.AllPositions); ShortAtMarket( bar + 1, "Crossover" ); } else if ( CrossUnder(bar, Momentum.Series(Low, 4), 0) ) { int ap = ActivePositions.Count; if ( ap > 0 && ap < maxPositions) { Position p = LastPosition; if ( bar - p.EntryBar > 5 && Close[bar] < p.EntryPrice ) ShortAtMarket( bar + 1, "Add" ); } } } if (nvi[bar] < nviTrigger ) { if ( CrossUnder(bar, nvi, nviTrigger) ) { ExitAtMarket(bar + 1, Position.AllPositions); BuyAtMarket( bar + 1, "Crossover" ); } else if ( CrossOver(bar, Momentum.Series(High, 4), 0) ) { int ap = ActivePositions.Count; if ( ap > 0 && ap < maxPositions) { Position p = LastPosition; if ( bar - p.EntryBar > 5 && Close[bar] > p.EntryPrice ) BuyAtMarket( bar + 1, "Add" ); } } } } } } }