using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;namespace WealthLab.Strategies { public class MyStrategy : WealthScript { protected override void Execute() { int Period = 40; bool UseAdjustedROC = true; // get a broad market index (S&P500 is used here) and its ROC data DataSeries sIndex = GetExternalSymbol( "^GSPC", true ).Close; DataSeries sIndexROC = ROC.Series( sIndex, Period ); // get the ROC of selected stock DataSeries sROC = ROC.Series( Close, Period ); if( UseAdjustedROC ) { // change from standard ROCs to the "adjusted" version sROC = EMA.Series( ROC.Series( Close, 1 ), Period, EMACalculation.Modern ); sIndexROC = EMA.Series( ROC.Series( sIndex, 1), Period, EMACalculation.Modern ); } // compute the spread between the two (adjusted) ROCs // that is, the difference: ROC(stock) - ROC(index) DataSeries sSpread = sROC - sIndexROC; sROC.Description = "sROC"; sIndexROC.Description = "sIndexROC"; sSpread.Description = "sSpread"; for(int bar = Period; bar < Bars.Count; bar++) { double R = sROCbar; if (IsLastPositionActive) { Position p = LastPosition; // exit all (long) positions when a profit target is reached, // or when the stock rises while the spread starts decreasing if( ( p.NetProfitAsOfBarPercent( bar ) > 4 ) || ( ( R > 0 ) & TurnDown( bar, sSpread ) ) ) SellAtMarket( bar + 1, Position.AllPositions ); } // enter a new long position after the stock had a dip, // as soon as the spread shows an improvement if( ( R < 0 ) & TurnUp( bar, sSpread ) ) if( BuyAtMarket( bar + 1, (-R).ToString() ) != null ) // when trading multiple stocks, and not having enough cash to // enter all alerts, instruct WL’s $imulator to trade those with // the lowest (most negative) ROC value, hoping in a faster bounce LastPosition.Priority = - R; } HideVolume(); ChartPane Pane = CreatePane( 50, false, true ); DrawHorzLine( Pane, 0, Color.Black, LineStyle.Solid, 2 ); PlotSeries( Pane, sROC, Color.Red, LineStyle.Solid, 1 ); PlotSeries( Pane, sIndexROC, Color.Blue, LineStyle.Dotted, 1 ); PlotSeries( Pane, sSpread, Color.Green, LineStyle.Solid, 2 ); } } }