using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;namespace WealthLab.Strategies { public class Leavitt : WealthScript { private StrategyParameter paramLength; private StrategyParameter paramPeak; public Leavitt() { paramLength = CreateParameter("LcP Length", 50, 10, 50, 10); paramPeak = CreateParameter("LcSlope Peak", 50, 5, 50, 5); } protected override void Execute() { bool reachedPeak = false, changedSign = false; int peakBar = -1, signBar = -1; int peakBars = paramPeak.ValueInt; int Length = paramLength.ValueInt; var LeavittProjection = LinearReg.Series(Close, Length); int LenConv = Convert.ToInt32(Math.Sqrt(Length)); // this is how EasyLanguage extracts the integer portion of a variable var LeavittConv = LinearReg.Series(LeavittProjection, LenConv); //inline function call // The slope follows the same pattern. However, step 2, which defines the line, is replaced by capturing the slope of that line. var LeavittConvSlope = LinearRegSlope.Series(LeavittProjection, LenConv); LeavittConvSlope.Description = "LeavittConvSlope"; // The acceleration is simply the difference between two consecutive values of the slope. var LcAcceleration = LeavittConvSlope - (LeavittConvSlope>>1); LcAcceleration.Description = "LcAcceleration"; var LeavittConvAcc = new DataSeries(Bars,"LeavittConvAcc"); LeavittConvAcc.Description = "LeavittConvAcc"; HideVolume(); ChartPane p3 = CreatePane(10,false,false); ChartPane p2 = CreatePane(30,true,false); ChartPane p1 = CreatePane(30,true,false); PlotSeries(p1, LcAcceleration, Color.Blue, LineStyle.Histogram, 5 ); PlotSeries(p2, LeavittConvSlope, Color.Blue, LineStyle.Histogram, 5 ); PlotSeries(p3, LeavittConvAcc, Color.Blue, LineStyle.Histogram, 5 ); PlotSeries( p2, Highest.Series( LeavittConvSlope, peakBars ), Color.Blue, LineStyle.Dashed, 1 ); for(int bar = 1; bar < Bars.Count; bar++) { SetSeriesBarColor( bar, LcAcceleration, LcAccelerationbar > 0 ? Color.DarkGreen: Color.Red ); SetSeriesBarColor( bar, LeavittConvSlope, LeavittConvSlopebar > LeavittConvSlopebar-1 ? Color.DarkGreen: Color.Red ); if( bar > 2 ) { if (Closebar - 2 - 2*Closebar - 1 + Closebar > 0) LeavittConvAccbar = 1; else LeavittConvAccbar = -1; } } for(int bar = GetTradingLoopStartBar(1); bar < Bars.Count; bar++) { if (IsLastPositionActive) { //This exit was taken on the 30-minute chart precisely when //LcSlope reached its peak and LcAcceleration changed sign. var hb = HighestBar.Series( LeavittConvSlope, peakBars )bar; if( !reachedPeak ) { if( bar == hb ) { SetBackgroundColor( bar, Color.FromArgb(50,Color.LightBlue)); reachedPeak = true; peakBar = bar; } } if( reachedPeak ) { if( !changedSign ) { if( LcAccelerationbar < 0 ) { SetBackgroundColor( bar, Color.FromArgb(50,Color.LightSalmon)); changedSign = true; signBar = bar; } if( changedSign ) { if( SellAtMarket(bar+1, LastPosition ) ) { reachedPeak = false; changedSign = false; } } } } } else { if( LeavittConvSlopebar > 0 && LcAccelerationbar > 0 ) BuyAtMarket(bar+1); } } } } }