using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; namespace WealthLab.Strategies { public class ScoreStrategy : WealthScript { public double ScoreIt(double val) { if (val >0.05)return 1; if (val <-0.05)return 0; return 0.5; } StrategyParameter paramDays; StrategyParameter paramScore; public ScoreStrategy() { paramDays = CreateParameter("Days",5,5,30,5); paramScore = CreateParameter("Score",3,1,4,0.5); } protected override void Execute() { int days = paramDays.ValueInt; double scoreThreshold = paramScore.Value; const int nLines = 5; DataSeries[] reglines = new DataSeries[nLines]; DataSeries[] regRoc = new DataSeries[nLines]; DataSeries score = new DataSeries(Bars, "The Score"); Color[] clr = new Color[5]; clr[0] =Color.Red; clr[1] = Color.Blue; clr[2] = Color.Green; clr[3] = Color.Fuchsia; clr[4] = Color.Black; ChartPane rocPane = CreatePane(40, true, true); // Create and plot the series for (int n = 0; n < nLines; n++) { reglines[n] = LinearReg.Series(Close, days); regRoc[n] = ROC.Series(reglines[n], 1); // 3-day rate of change days += 3; PlotSeries(PricePane, reglines[n],clr[n],LineStyle.Solid, 1); PlotSeries(rocPane,regRoc[n],clr[n],LineStyle.Solid, 1); } // Now that we have the series let's create a score series for(int bar = 5; bar < Bars.Count; bar++) { // calculate score double sum = 0; for (int n = 0; n < nLines; n++) { sum += ScoreIt(regRoc[n][bar]); } score[bar] = sum; //calculate mean turnover (bars) DataSeries turnover = Close * Volume; DataSeries meanturnover = SMA.Series(turnover, 20); //trading if (IsLastPositionActive) { Position p = LastPosition; if (score[bar] + score[bar - 1] + score[bar - 2] < (scoreThreshold + 6)) SellAtClose(bar, p); } else if (score[bar] + score[bar - 1] + score[bar - 2] >= (scoreThreshold + 6)) { BuyAtClose(bar); } } // saving the score in a series makes it easy to plot ChartPane scorePane = CreatePane(40, true, true); PlotSeries(scorePane, score, Color.Blue, LineStyle.Histogram, 2); } } }