UserStrategyBase
Namespace: WealthLab.Backtest
Parent: StrategyBase

UserStrategyBase represents a trading Strategy in WealthLab. When you work with C# Code-Based Strategies, you are actually coding a custom class that is derived from UserStrategyBase. The class provides properties and methods that let you control the logic of the trading system, including placing orders and examining the current state of the system.

You hook into the trading system logic by overriding several virtual methods in UserStrategyBase. These methods pass as a parameter the instance of the BarHistory object that contains the historical data to backtest. The following are the most important methods:

  • Initialize - the backtester calls this once at the start of processing
  • Execute - the backtester calls this once for each data point in the BarHistory being backtested, and you are supplied the index that is should be processed in each iteration
Chart Rendering
DrawBarAnnotation
public void DrawBarAnnotation(string text, int bar, bool aboveBar, Color color, int fontSize, bool center = false, Font font = null)

Draws the specified text above or below the specified bar on the chart, using the specified color and fontSize. The text is centered, unless you pass false to the optional center parameter. The final optional font parameter allows you to specify a different System.Drawing.Font to use.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
	    //annotate bullish and bearish "outside bars"
        public override void Execute(BarHistory bars, int idx)
        {
	        if (idx == 0)
				return;
			if (bars.High[idx] > bars.High[idx - 1] && bars.Low[idx] < bars.Low[idx - 1])
			{
				bool isBullish = bars.Close[idx] > bars.Open[idx];
				string annotation = isBullish ? "Bullish" : "bearish";
				Color c = isBullish ? Color.Green : Color.Red;
				DrawBarAnnotation(annotation, idx, isBullish, c, 6);
			}
        }

    }
}

DrawDot
public void DrawDot(int bar, double value, Color color, int radius, string paneTag = "Price")

Draw a dot on that chart at the specified bar number and price value. The color parameter determines the color of the dot, and the radius parameter determines its size.

Remarks

  • See PlotStopsAndLimits to automatically draw dots at stop and limit prices on bars where the Transactions (orders) are active.
Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy14 : UserStrategyBase
	{
		private BBLower lowerBollingerBand;
		private BBUpper upperBollingerBand;
		private ATR atr;
		private double stopLevel;

		double atrStop;

		public override void Initialize(BarHistory bars)
		{
			lowerBollingerBand = new BBLower(bars.Close, 20, 2.0);
			upperBollingerBand = new BBUpper(bars.Close, 20, 2.0);
			PlotIndicator(lowerBollingerBand, Color.Navy, PlotStyles.Bands);
			atr = new ATR(bars, 14);
			PlotIndicator(atr);
		}

		public override void Execute(BarHistory bars, int idx)
		{
			if (idx < 20)
				return;

			if (HasOpenPosition(bars, PositionType.Long) == false)
			{
				if (bars.Low[idx] <= lowerBollingerBand[idx] && bars.Close[idx] > lowerBollingerBand[idx])
				{
					SetBackgroundColor(bars, idx, Color.LightGreen, "Price");
					PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, bars.Close[idx]);
					atrStop = bars.Close[idx] - atr[idx] * 2;
					DrawDot(idx, atrStop, Color.Red, 2);
					PlaceTrade(bars, TransactionType.Sell, OrderType.Stop, atrStop);
				}
			}
			else
			{
				//uncomment this line to use a fixed stop level calculated at entry time
				//atrStop = bars.Close[idx] - atr[idx] * 2;
				DrawDot(idx, atrStop, Color.Red, 2);
				PlaceTrade(bars, TransactionType.Sell, OrderType.Stop, atrStop);
				SetBackgroundColor(bars, idx, Color.LightPink, "Price");
				PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, upperBollingerBand[idx]);
			}
		}
	}
}

DrawEllipse
public void DrawEllipse(int startBar, double startValue, int endBar, double endValue, Color color, int lineWidth = 1, LineStyles lineStyle = LineStyles.Solid, string paneTag = "Price")

Draws an ellipse on the chart using the specified parameters, color, line width, and style.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
        //draw an ellipse arounf the last 100 bar range
        public override void Initialize(BarHistory bars)
        {
	        if (bars.Count < 100)
				return;
			double highest = bars.High.GetHighest(bars.Count - 1, 100);
			double lowest = bars.Low.GetLowest(bars.Count - 1, 100);
			DrawEllipse(bars.Count - 99, highest, bars.Count - 1, lowest, Color.Navy, 3);
        }

        //execute the strategy rules here, this is executed once for each bar in the backtest history
        public override void Execute(BarHistory bars, int idx)
        {
        }

        //declare private variables below

    }
}

DrawHeaderText
public void DrawHeaderText(string text, Color color, int fontSize, string paneTag = "Price")
public void DrawHeaderText(string txt, string paneTag = "Price")

Draws the specified text on the chart, under the upper left hand corner box that displays the symbol and price values. The first overload allows you to control the color and fontSize of the text. Both overloads have a final, optional, parameter paneTag that defaults to "Price". Specify a different paneTag to draw the text in a different pane.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
        //display some information on the chart
        public override void Initialize(BarHistory bars)
        {
			double smaVal = SMA.Value(bars.Count - 1, bars.Close, 20);
			double closingVal = bars.Close[bars.Count - 1];
			double diff = closingVal - smaVal;
			double pctDiff = (diff * 100.0) / smaVal;
	        if (diff < 0)
				DrawHeaderText("Price is " + (-pctDiff).ToString("N2") + "% below SMA(20)", Color.Red);
	        else
				DrawHeaderText("Price is " + pctDiff.ToString("N2") + "% above SMA(20)", Color.Green);
        }

        public override void Execute(BarHistory bars, int idx)
        {
        }

    }
}

DrawHorzLine
public void DrawHorzLine(double value, Color color, int lineWidth = 1, LineStyles lineStyle = LineStyles.Solid, string paneTag = "Price")

Draws a horizontal line on the chart at the specified value, using the specified color, line width, and style.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
using System.Collections.Generic;

namespace WealthScript123
{
	public class DrawHorzLineExample : UserStrategyBase
	{
		IndicatorBase _rsi;

		public override void Initialize(BarHistory bars)
		{
			//put a line at the most recent closing price
			DrawHorzLine(bars.Close[bars.Count - 1], Color.Green);

			//put a line on the volume pane at 10,000,000 
			DrawHorzLine(10e6, Color.Blue, 1, LineStyles.Dashed, "Volume");

			_rsi = RSI.Series(bars.Close, 14);
			PlotIndicatorLine(_rsi);

			//put a line at an oversold level, say 40
			DrawHorzLine(40, Color.Red, 1, LineStyles.Solid, _rsi.PaneTag);
		}

		public override void Execute(BarHistory bars, int idx)
		{
			if (!HasOpenPosition(bars, PositionType.Long))
			{
				//code your buy conditions here
			}
			else
			{
				//code your sell conditions here
			}
		}


	}
}

DrawImage
public void DrawImage(Image img, int idx, double value, string paneTag = "Price")

Draws the specified Image on the chart, at the specified location.

Remarks

  • To run the example, modify the Bitmap.FromFile() statement to point to an image file on your computer.
Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			cmo = new CMO(bars.Close, 20);
			PlotIndicatorLine(cmo);
			img = (Bitmap)Bitmap.FromFile(@"C:\Images\BuyArrow.bmp");
			img.MakeTransparent(Color.White);
		}

		//draw arrows on the chart where CMO oscillator turns up from oversold area
		public override void Execute(BarHistory bars, int idx)
		{
			if (cmo[idx] < cmo.OversoldLevel)
				if (cmo.TurnsUp(idx))
				{
					DrawImage(img, idx, bars.Low[idx], "Price", 5);
					SetBackgroundColor(bars, idx, c);
				}
		}

		//declare private variables below
		private CMO cmo;
		private Bitmap img;
		Color c = Color.FromArgb(32, 255, 0, 0);
	}
}

DrawImageAt
public void DrawImageAt(Image img, double x, double y, string paneTag = "Price")

Draws the specified Image on the chart, at the specified location.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using System.Drawing;
using ScottPlot;

namespace WealthScript1
{
	public class ScottPlot_DrawImageAt : UserStrategyBase
	{
		//create a bitmap with an OHLC chart using ScottPlot
		Bitmap getBitmap(BarHistory bars)
		{
			//a ScottPlot object for charting
			Plot plt = new ScottPlot.Plot(300, 200);
			plt.Title(bars.Symbol);
			plt.YLabel("Price"); plt.XAxis.Ticks(false);

			double[] _bars = new double[bars.Count];
			OHLC[] _ohlc = new OHLC[bars.Count];

			for (int i = 0; i < bars.Count; i++)
				_bars[i] = bars.DateTimes[i].ToOADate();

			//fill OHLC values
			for (int i = 0; i < bars.Count; i++)
				_ohlc[i] = new OHLC(bars.Open[i], bars.High[i], bars.Low[i], bars.Close[i], bars.DateTimes[i], new TimeSpan(1, 0, 0, 0, 0));

			//make the bar plot
			plt.AddOHLCs(_ohlc);

			//render chart and draw image at the top left corner
			Bitmap bmp = plt.Render();
			bmp.MakeTransparent(Color.White);

			return bmp;
		}

		public override void Initialize(BarHistory bars)
		{
			//get 4 BarHistory objects
			BarHistory qqq = GetHistory(bars, "QQQ");
			BarHistory spy = GetHistory(bars, "SPY");
			BarHistory dia = GetHistory(bars, "DIA");
			BarHistory gld = GetHistory(bars, "GLD");

			//create 4 bitmaps
			var _qqq = getBitmap(qqq);
			var _spy = getBitmap(spy);
			var _dia = getBitmap(dia);
			var _gld = getBitmap(gld);

			//draw a panel of the 4 stacked charts
			DrawImageAt(_qqq, 10, 10);
			DrawImageAt(_dia, 10, 210);
			DrawImageAt(_spy, 310, 10);
			DrawImageAt(_gld, 310, 210);
		}

		public override void Execute(BarHistory bars, int idx)
		{
		}
	}
}

DrawLine
public void DrawLine(int startBar, double startValue, int endBar, double endValue, Color color, int lineWidth = 1, LineStyles lineStyle = LineStyles.Solid, string paneTag = "Price", bool extendLeft = false, bool extendRight = false)

Draws a line on the chart using the specified parameters, line width, color, and style.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
        //draw extended trendlines between two most recent peaks/troughs
        public override void Initialize(BarHistory bars)
        {
			PeakTroughCalculator pt = new PeakTroughCalculator(bars, 5.0);
			if (pt.PeakTroughs.Count > 4)
			{
				int ptc = pt.PeakTroughs.Count - 1;
				
				//render peak trendline
				PeakTrough peak1 = pt.PeakTroughs[ptc].Type == PeakTroughTypes.Peak ? pt.PeakTroughs[ptc] : pt.PeakTroughs[ptc - 1];
				PeakTrough peak2 = pt.GetPeak(peak1.DetectedAtIndex - 1);
				if (peak1 != null && peak2 != null)
				{
					DrawLine(peak2.PeakTroughIndex, peak2.Value, peak1.PeakTroughIndex, peak1.Value, Color.Red, 2, LineStyles.Dotted, "Price", false, true);
				}

				//render trough trendline
				PeakTrough trough1 = pt.PeakTroughs[ptc].Type == PeakTroughTypes.Peak ? pt.PeakTroughs[ptc - 1] : pt.PeakTroughs[ptc];
				PeakTrough trough2 = pt.GetTrough(trough1.DetectedAtIndex - 1);
				if (trough1 != null && trough2 != null)
				{
					DrawLine(trough2.PeakTroughIndex, trough2.Value, trough1.PeakTroughIndex, trough1.Value, Color.Green, 2, LineStyles.Dotted, "Price", false, true);
				}
			}
        }

        public override void Execute(BarHistory bars, int idx)
        {
        }

		//declare private variables below
    }
}

DrawPolygon
public void DrawPolygon(List<IChartPoint> pts, Color color, int lineWidth = 1, LineStyles lineStyle = LineStyles.Solid, string paneTag = "Price")

Draws a polygon based on the list of points in the pts parameter, using the specific color, line width, and style. The ChartPoint class represents a point on the chart. It contains 2 properties; an int Index and a double Value.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
		//draw a triangle around most recent 3 peak/troughs
		public override void Initialize(BarHistory bars)
		{
			PeakTroughCalculator pt = new PeakTroughCalculator(bars, 7.0);
			if (pt.PeakTroughs.Count < 3)
				return;
			PeakTrough pt1 = pt.PeakTroughs[pt.PeakTroughs.Count - 1];
			PeakTrough pt2 = pt.PeakTroughs[pt.PeakTroughs.Count - 2];
			PeakTrough pt3 = pt.PeakTroughs[pt.PeakTroughs.Count - 3];
			List<IChartPoint> points = new List<IChartPoint>();
			points.Add(pt1);
			points.Add(pt2);
			points.Add(pt3);
			DrawPolygon(points, Color.DarkCyan, 3);
		}
	    
        public override void Execute(BarHistory bars, int idx)
        {
        }

    }
}

DrawRectangle
public void DrawRectangle(int startBar, double startValue, int endBar, double endValue, Color color, int lineWidth = 1, LineStyles lineStyle = LineStyles.Solid, string paneTag = "Price")

Draws a rectangle on the chart using the specified parameters, color, line width, and style.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//draw rectangle around recent peak/troughs
		public override void Initialize(BarHistory bars)
		{
			PeakTroughCalculator pt = new PeakTroughCalculator(bars, 10.0);
			if (pt.PeakTroughs.Count == 0)
				return;
			PeakTrough pt1 = pt.PeakTroughs[pt.PeakTroughs.Count - 1];
			if (pt1 != null)
			{
				PeakTrough pt2 = pt.GetPeakTrough(pt1.DetectedAtIndex - 1);
				if (pt2 != null)
				{
					DrawRectangle(pt2.PeakTroughIndex, pt2.Value, pt1.PeakTroughIndex, pt1.Value, Color.Blue, 3);
				}
			}
		}

		public override void Execute(BarHistory bars, int idx)
		{
		}
	}
}

DrawText
public void DrawText(string txt, int idx, double value, Color color, int fontSize, string paneTag = "Price", bool centerText = false, Font font = null)

Draws the specified text on the chart, at the location specified by the idx (x-axis) and value (y-axis) parameters. The idx parameter corresponds to an index in the BarHistory being charted. The value parameter corresponds to a numeric value along the y-axis.

The color parameter determines the text's color, and the fontSize parameter determines its size. A size of 12 is standard for chart text rendering.

DrawText has several optional parameters that let you further customize the text:

  • paneTag - lets you control which pane the text is rendered to. Possible values are "Price", "Volume", or an indicator pane tag such as "RSI" or "CMO".
  • centerText - if true, specified that the text should be centered along the idx parameter. If false (the default value) it renders with the left edge beginning at that point.
  • font - specifies the Font to use to render the text, as an instance of the System.Drawing.Font class.
Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthScript1
{
	public class MyStrategy : UserStrategyBase
	{
		//create moving averages
		public override void Initialize(BarHistory bars)
		{
			sma50 = new SMA(bars.Close, 50);
			sma200 = new SMA(bars.Close, 200);
			PlotIndicatorLine(sma50);
			PlotIndicatorLine(sma200, Color.Red);
		}

		//label where moving average crossover occur
		public override void Execute(BarHistory bars, int idx)
		{
			if (sma50.CrossesOver(sma200, idx))
				DrawText("Cross Over", idx, sma200[idx], Color.Black, 8);
			if (sma50.CrossesUnder(sma200, idx))
				DrawText("Cross Under", idx, sma200[idx], Color.Black, 8);
		}

		//declare private variables below
		SMA sma50;
		SMA sma200;
	}
}

ExtendLine
public double ExtendLine(double x1, double y1, double x2, double y2, double x, bool isLog = false)

Does not actually plot, but rather calculates the extension of a line specified by two points (x1,x2 and y1,y2), and returns the value of y extened to the point along the line where the x parameter occurs. Pass true for isLog for a straight line on a semi-log chart.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//create an indicator that projects price 5 bars into the future based on the line
		//that connects the close 5 bars ago with the current close
		public override void Initialize(BarHistory bars)
		{
			TimeSeries projected = new TimeSeries(bars.DateTimes);
			for (int n = 5; n < bars.Count; n++)
			{
				double projectedValue = ExtendLine(n - 5, bars.Close[n - 5], n, bars.Close[n], n + 5);
				projected[n] = projectedValue;
			}
			PlotTimeSeries(projected, "Projected Price", "Price");
		}

		public override void Execute(BarHistory bars, int idx)
		{
		}
	}
}

FillEllipse
public void FillEllipse(int startBar, double startValue, int endBar, double endValue, Color color, string paneTag = "Price")

Fills an ellipse on the chart using the specified parameters, color, and style.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
        //fill 3 ellipses around descending high/low ranges
        public override void Initialize(BarHistory bars)
        {
	        if (bars.Count < 100)
				return;
			DrawMyEllipse(bars, 100);
	        DrawMyEllipse(bars, 70);
	        DrawMyEllipse(bars, 40);
        }
		private void DrawMyEllipse(BarHistory bars, int barRange)
		{
			Color c = Color.FromArgb(32, 0, 0, 128);
			double highest = bars.High.GetHighest(bars.Count - 1, barRange);
			double lowest = bars.Low.GetLowest(bars.Count - 1, barRange);
			FillEllipse(bars.Count - barRange + 1, highest, bars.Count - 1, lowest, c);
		}

        //execute the strategy rules here, this is executed once for each bar in the backtest history
        public override void Execute(BarHistory bars, int idx)
        {
        }

        //declare private variables below

    }
}

FillPolygon
public void FillPolygon(List<IChartPoint> pts, Color color, string paneTag = "Price")

Fills a polygon based on the list of points in the pts parameter, using the specific color. The ChartPoint class represents a point on the chart. It contains 2 properties; an int Index and a double Value.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//draw a triangle around most recent 3 peak/troughs
		public override void Initialize(BarHistory bars)
		{
			PeakTroughCalculator pt = new PeakTroughCalculator(bars, 7.0);
			if (pt.PeakTroughs.Count < 3)
				return;
			PeakTrough pt1 = pt.PeakTroughs[pt.PeakTroughs.Count - 1];
			PeakTrough pt2 = pt.PeakTroughs[pt.PeakTroughs.Count - 2];
			PeakTrough pt3 = pt.PeakTroughs[pt.PeakTroughs.Count - 3];
			List<IChartPoint> points = new List<IChartPoint>();
			points.Add(pt1);
			points.Add(pt2);
			points.Add(pt3);
			FillPolygon(points, Color.DarkCyan);
		}

		public override void Execute(BarHistory bars, int idx)
		{
		}

	}
}

FillRectangle
public void FillRectangle(int startBar, double startValue, int endBar, double endValue, Color color, string paneTag = "Price")

Fills a rectangle on the chart using the specified parameters, color, and style.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
		//for intraday charts - draw a rectangle around the zone of yesterday's high/low range
		public override void Initialize(BarHistory bars)
		{
			BarHistory daily = BarHistoryCompressor.ToDaily(bars);
			if (daily == null || daily.Count < 2)
				return;
			for (int n = bars.Count - 1; n > 0; n--)
			{
				if (bars.DateTimes[n].Day != bars.DateTimes[n - 1].Day)
				{
					int dailyIdx = daily.Count - 1;
					double dailyLow = daily.Low[dailyIdx];
					double dailyHigh = daily.High[dailyIdx];
					Color c = Color.FromArgb(32, 0, 255, 255);
					FillRectangle(n, dailyHigh, bars.Count - 1, dailyLow, c);
					return;
				}
			}
		}
	    
        public override void Execute(BarHistory bars, int idx)
        {
        }

    }
}

PlotBarHistory
public void PlotBarHistory(BarHistory bh, string paneTag, Color color = default(Color), bool fitInAxis = false)

Plots a BarHistory instance on the chart. BarHistory instances are typically obtained as a result of the GetHistory method. Provide the BarHistory to be plotted in the bh parameter.

The paneTag parameter should specify what pane to plot the data. You can specify Price for the price pane, Volume for the volume pane, or some other unique string for a custom pane.

The color parameter is optional. If not specified, WealthLab will plot the data in black.

The fitInAxis parameter is also optional, with a default of false. If set to true, it will cause the BarHistory data to fit within the existing scale of the pane, so as not to distort it. If false, the pane's scale will adjust to accommodate the BarHistory data.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using System.Drawing;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
        //create indicators and other objects here, this is executed prior to the main trading loop
        public override void Initialize(BarHistory bars)
        {
			//Plot QQQ in its own pane below price
			BarHistory qqq = GetHistory(bars, "QQQ");
			PlotBarHistory(qqq, "QQQ");

			//Plot SPY in the same pane, using the dual scale
			BarHistory spy = GetHistory(bars, "SPY");
			PlotBarHistory(spy, "QQQ", Color.Red, true);
        }

        //execute the strategy rules here, this is executed once for each bar in the backtest history
        public override void Execute(BarHistory bars, int idx)
        {
        }
    }
}

PlotBarHistoryStyle
public void PlotBarHistoryStyle(BarHistory bh, string paneTag, string styleName, Color color = default(Color), bool fitInAxis = false)

Plots the specified BarHistory instance in bh in a chart pane with the specified paneTag, using the specified color. The fitInAxis parameter is optional, with a default of false. If set to true, it will cause the BarHistory data to fit within the existing scale of the pane, so as not to distort it. If false, the pane's scale will adjust to accommodate the BarHistory data.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
using System.Collections.Generic;

namespace WealthScript1
{
	public class MyStrategy : UserStrategyBase
	{
		//Initialize
		public override void Initialize(BarHistory bars)
		{
			PlotBarHistoryStyle(bars, "Bars", "Heikin Ashi", Color.Navy);
		}

		//Execute
		public override void Execute(BarHistory bars, int idx)
		{
			if (OpenPositions.Count == 0)
			{

			}
			else
			{

			}
		}

		//private members

	}
}

PlotIndicator
public virtual void PlotIndicator(IndicatorBase ib, Color color = default(Color), PlotStyles? plotStyle = null, bool suppressLabels = false, string paneTag = null))

Plots an indicator on the chart. The ib parameter is an indicator is an instance of the IndicatorBase class, the class which all indicators in WealthLab are derived from. Any time you create an indicator in code, you're returned an instance of the indicator's specialized class, which is a descendant of IndicatorBase.

Parameter remarks

  • color is optional. If not specified WealthLab will use the indicator's default color.
  • plotStyle is optional, and is a member of the PlotStyles enum. If not specified, Wealth-Lab uses the indicator's default plot style.
  • suppressLabels lets you disable to indicator pane labels and the value markers along the chart's right margin for this plot.
  • paneTag is optional for indicators. Pass your own paneTag to plot multiple indicators of different types in the same pane, for example.
Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
        //create indicators and other objects here, this is executed prior to the main trading loop
        public override void Initialize(BarHistory bars)
        {
	        //plot a MACD and signal line
			IndicatorBase macd = new MACD(bars.Close);
			PlotIndicator(macd, Color.Firebrick, PlotStyles.ThickHistogram);
			IndicatorBase signalLine = new SMA(macd, 9);
			PlotIndicator(signalLine);

			//plot an SMA, semi-transparent
			IndicatorBase sma = new SMA(bars.AveragePriceOHLC, 9);
			PlotIndicator(sma, Color.FromArgb(64, Color.CadetBlue), PlotStyles.ThickLine);
        }

        //execute the strategy rules here, this is executed once for each bar in the backtest history
        public override void Execute(BarHistory bars, int idx)
        {
        }
    }
}

PlotIndicatorLine
public virtual void PlotIndicatorLine(IndicatorBase ib, Color color = default(Color), int lineWidth = 2, LineStyles lineStyle = LineStyles.Solid, bool suppressLabels = false, string paneTag = null)

Similar to PlotIndicator, but instead of specifying a plot style, this version always plots a line. You can control the thickness and style of the line using the lineThickness and lineStyle parameters.

Parameter remarks

  • color is optional. If not specified WealthLab will use the indicator's default color.
  • *lineStyle * is optional.
  • suppressLabels lets you disable to indicator pane labels and the value markers along the chart's right margin for this plot.
  • paneTag is optional for indicators. Pass your own paneTag to plot multiple indicators of different types in the same pane, for example.

PlotIndicatorOscillator
public virtual void PlotIndicatorOscillator(IndicatorBase ib, Color color = default(Color), Color colorOversold = default(Color), Color colorOverbought = default(Color), int opacity = 25, bool suppressLabels = false, string paneTag = null)

Plots the specified IndicatorBase series ib as a solid line using the color, if specified. When ib moves below its oversold area, this area of the chart is filled using colorOversold. Conversely, when ib moves above the overbought area, that area of the chart is filled with colorOverbought. Overbought/Oversold colors are subject to the opacity percentage setting, a number from 0 (transparent) to 100 (opaque).

Remarks

  • Except for the IndicatorBase parameter, all others are optional.
  • Standard overbought and and oversold levels are pre-assigned to oscillator-type indicators. To override these levels, use PlotTimeSeriesOscillator() instead.
Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
using System.Collections.Generic;

namespace WealthScript1
{
	public class MyStrategy : UserStrategyBase
	{
		RSI _myRSI;
		TimeSeries _ts;

		public override void Initialize(BarHistory bars)
		{
			_myRSI = RSI.Series(bars.Close, 4);
			PlotIndicatorOscillator(_myRSI);

			_ts = SMA.Series(_myRSI, 3);
			PlotTimeSeriesOscillator(_ts, "Smoothed", _myRSI.PaneTag, 40, 60, Color.Black, Color.Navy, Color.Maroon, 10);
		}

		public override void Execute(BarHistory bars, int idx)
		{
			if (!HasOpenPosition(bars, PositionType.Long))
			{
				//code your buy conditions here
			}
			else
			{
				//code your sell conditions here
			}
		}

	}
}

PlotStopsAndLimits
public void PlotStopsAndLimits(int dotSize = 6, bool stops = true, bool limits = true, Color colorBuy = default(Color), Color colorSell = default(Color), Color colorShort = default(Color), Color colorCover = default(Color))

Call PlotStopsAndLimits once in Initialize() to cause stop and/or limit orders to be plotted on the chart. Stop and limit orders are drawn as colored dots on the chart at the bar and price levels at which they are active. All parameters are optional but can be specified for customization.

Remarks
Default color codes by order type as follows:

  • Buy = blue
  • Sell = red
  • Short = fuchsia
  • Cover = green
Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
using System.Collections.Generic;

namespace WealthScript1
{
	public class PlotStopsAndLimitsSampler : UserStrategyBase
	{
		IndicatorBase _bbU;
		IndicatorBase _bbL;
		IndicatorBase _sma;

		public override void Initialize(BarHistory bars)
		{
			PlotStopsAndLimits();

			_bbU = BBUpper.Series(bars.Close, 10, 2.5);
			_bbL = BBLower.Series(bars.Close, 10, 2.0);
			_sma = SMA.Series(bars.Close, 10);

			//advance the series plot 1 bar to coincide with where the stop/limits are active
			PlotTimeSeriesLine(_bbU >> 1, "bbu", "Price", Color.Blue, 1);
			PlotTimeSeriesLine(_bbL >> 1, "bbu", "Price", Color.Blue, 1);
			PlotTimeSeriesLine(_sma >> 1, "sma", "Price", Color.Black, 1);
		}

		public override void Execute(BarHistory bars, int idx)
		{

			if (HasOpenPosition(bars, PositionType.Long))
			{
				PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, _bbU[idx]);
				PlaceTrade(bars, TransactionType.Sell, OrderType.Stop, _bbL[idx]);
			}
			else
			{			
				if (bars.Close.CrossesOver(_sma, idx))
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
			}
		}
	}
}

PlotTimeSeries
public void PlotTimeSeries(TimeSeries ts, string name, string paneTag, Color color = default(Color), PlotStyles plotStyle = PlotStyles.Line, bool suppressLabels = false)

Plots a TimeSeries on the chart. The ts parameter is a TimeSeries instance. PlotTimeSeries is useful when plotting the results of mathematical operations on indicators and other TimeSeries. These operations always return instances of the TimeSeries class.

The name parameter should be a descriptive name of the data being plotted. This appears in the pane label.

The paneTag specifies which chart pane to plot the data in. You can specify Price for the price pane, Volume for the volume pane, or some other string value for a custom pane.

The color parameter is optional. If not provided, WealthLab will use a default color.

The plotStyle parameter is also optional, and is a member of the PlotStyles enum. If not specified, WealthLab will use PlotStyles.Line.

The suppressLabels parameter lets you disable to indicator pane labels and the value markers along the chart's right margin for this plot.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
using System;

namespace WealthLab
{
	public class MyStrategy218 : UserStrategyBase
	{
		TimeSeries _ratio;

		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			//plot a ratio of QLD and QID momentum
			BarHistory qld = GetHistory(bars, "QLD");
			BarHistory qid = GetHistory(bars, "QID");
			_ratio = Momentum.Series(qld.Close, 20) / Momentum.Series(qid.Close, 20);

			for (int bar = 20; bar < bars.Count; bar++)
			{
				if (Math.Abs(_ratio[bar]) > 20)
					_ratio[bar] = _ratio[bar - 1];
			}

			PlotTimeSeries(_ratio, "QLD/QID Momentum Ratio", "Ratio", Color.Maroon, PlotStyles.ThickHistogram);
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
			//use _ratio here in the trading rules
		}
	}
}

PlotTimeSeriesLine
public void PlotTimeSeriesLine(TimeSeries ts, string name, string paneTag, Color color = default(Color), int lineWidth = 2, LineStyles lineStyle = LineStyles.Solid, bool suppressLabels = false)

Similar to PlotTimeSeries, but instead of specifying a plot style, this version always plots a line. You can control the thickness and style of the line using the lineThickness and lineStyle parameters.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
using System;

namespace WealthLab
{
	public class MyStrategy218 : UserStrategyBase
	{
		TimeSeries _ratio;

		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			//plot a ratio of QLD and QID momentum
			BarHistory qld = GetHistory(bars, "QLD");
			BarHistory qid = GetHistory(bars, "QID");
			_ratio = Momentum.Series(qld.Close, 20) / Momentum.Series(qid.Close, 20);

			for (int bar = 20; bar < bars.Count; bar++)
			{
				if (Math.Abs(_ratio[bar]) > 20)
					_ratio[bar] = _ratio[bar - 1];
			}

			PlotTimeSeriesLine(_ratio, "QLD/QID Momentum Ratio", "Ratio", Color.Blue, 3, LineStyles.Dotted);
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
			//use _ratio here in the trading rules
		}
	}
}

PlotTimeSeriesOscillator
public void PlotTimeSeriesOscillator(TimeSeries ts, string name, string paneTag, double oversold, double overbought, Color color = default(Color), Color colorOversold = default(Color), Color colorOverbought = default(Color), int opacity = 25, bool suppressLabels = false)

Plots the specified TimeSeries ts as a solid line in the specified paneTag using the color, if specified. When ts moves below the oversold value, this area of the chart is filled using colorOversold. Conversely, when ts moves above the overbought value, that area of the chart is filled with colorOverbought. Overbought/Oversold colors are subject to the opacity percentage setting, a number from 0 (transparent) to 100 (opaque).

Remarks

  • Colors and opacity parameters are optional.
  • The specified name label is displayed in the pane specified by paneTag. Pass true to suppressLabels to hide the label.
  • Also see PlotIndicatorOscillator()
Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
using System.Collections.Generic;

namespace WealthScript1
{
	public class MyStrategy : UserStrategyBase
	{
		RSI _myRSI;
		TimeSeries _ts;

		public override void Initialize(BarHistory bars)
		{
			_myRSI = RSI.Series(bars.Close, 4);
			PlotIndicatorOscillator(_myRSI);

			_ts = SMA.Series(_myRSI, 3);
			PlotTimeSeriesOscillator(_ts, "Smoothed", _myRSI.PaneTag, 40, 60, Color.Black, Color.Navy, Color.Maroon, 10);
		}

		public override void Execute(BarHistory bars, int idx)
		{
			if (!HasOpenPosition(bars, PositionType.Long))
			{
				//code your buy conditions here
			}
			else
			{
				//code your sell conditions here
			}
		}

	}
}

SetBackgroundColor
public void SetBackgroundColor(BarHistory bars, int bar, Color color, string paneTag = "Price")

Sets the background of the specified chart pane, at the specified bar index, to the color you specify.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//create indicators
		public override void Initialize(BarHistory bars)
		{
			roc = new ROC(bars.Close, 10);
			PlotIndicator(roc, Color.Black, PlotStyles.ThickHistogram);
		}

		//set color of ROC indicator based on its value
		public override void Execute(BarHistory bars, int idx)
		{
			//transform RSI value into 0-100 range after correcting tails
			double value = roc[idx];
			if (value < -20)
				value = -20;
			if (value > 20)
				value = 20;
			value += 20;
			value *= 2.5;
			Color c = ColorExtensions.InterpolateColors(Color.Red, Color.LimeGreen, (int)value);
			SetBackgroundColor(bars, idx, c, "ROC");
		}

		//declare private variables below
		ROC roc;
	}
}

SetBackgroundColorAllPanes
public void SetBackgroundColorAllPanes(BarHistory bh, int bar, Color color)

Sets the background to the color specified from the top to bottom of the chart at the specified bar index.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
using System.Collections.Generic;

namespace WealthScript2
{
	public class MyStrategy : UserStrategyBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			_ma1 = SMA.Series(bars.Close, 20);
			_ma2 = SMA.Series(bars.Close, 50);
			StartIndex = 50;
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
			if (!HasOpenPosition(bars, PositionType.Long))
			{
				//code your buy conditions here
				if (_ma1.CrossesOver(_ma2, idx))
				{
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);

					//paint a light blue background on the signal bar
					SetBackgroundColorAllPanes(bars, idx, Color.FromArgb(40, Color.Blue));
				}
			}
			else
			{
				//code your sell conditions here
				Position p = LastPosition;
				if (idx + 1 - p.EntryBar == 2)
					ClosePosition(p, OrderType.Market);
			}
		}

		//declare private variables below
		SMA _ma1;
		SMA _ma2;

	}
}

SetBarColor
public void SetBarColor(BarHistory bars, int bar, Color color)

Sets the chart bar color at the specified index to the color you specify.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//create indicators
		public override void Initialize(BarHistory bars)
		{
			rsi = new RSI(bars.Close, 20);
			PlotIndicator(rsi);
		}

		//set bar color depending on position of RSI
		public override void Execute(BarHistory bars, int idx)
		{
			//transform RSI value into 0-100 range after correcting tails
			double value = (int)rsi[idx];
			if (value < 30)
				value = 30;
			if (value > 70)
				value = 70;
			value -= 30;
			value *= 2.5;
			Color c = ColorExtensions.InterpolateColors(Color.Red, Color.LimeGreen, (int)value);
			SetBarColor(bars, idx, c);
		}

		//declare private variables below
		RSI rsi;
	}
}

SetChartDrawingOptions
public void SetChartDrawingOptions(ChartDisplaySettings cds)

Lets you control various chart rendering settings via code. The cds parameter should be an instance of the ChartDisplaySettings class, with properties assigned for the various chart settings you want to change. Properties that you do not assign a value to will default to the current chart preferences.

The ChartDisplaySettings class has the following properties:

  • public Color? ColorBackground - the chart background color
  • public Color? ColorAxisBackground - the chart axis background color
  • public Color? ColorUpBar - the color of the chart's "up" bars
  • public Color? ColorDownBar - the color of the chart's "down" bars
  • public Color? ColorPaneSeparator - the color of the chart's pane separator lines
  • public Color? ColorGridLines - the color of the chart's grid lines
  • public Color? ColorAxisLabels - the color of the chart's axis labels
  • public Color? ColorCursors - the color of the vertical and crosshair cursors
  • public Color? ColorWatermark - the color of the chart watermark that displays the symbol and security name
  • public bool? ShowVolumePane - whether or not the volume pane should be displayed
  • public bool? ShowEventIcons - whether or not icons for Events (fundamental, etc.) should be displayed
  • public bool? DrawTradeArrows - controls whether the entry/exit arrows are displayed for trades
  • public bool? LogScale - whether or not the chart price pane should be log scale
  • public bool? ShowStatusBar - whether or not the chart status bar should appear
  • public bool? AlwaysShowToolbar - whether the chart toolbar should always appear, or appear only when the mouse is near the top area of the chart
  • public double? BarSpacing - the number of pixels between chart bars
  • public int? Margin - how much space should appear above and below chartable data
  • public int? PaneSeparatorWidth - the size of the pane separator lines, setting this to zero hides the separator lines
  • public int? CursorThickness - how thick the vertical and crosshair cursor lines should be
Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;

namespace WealthScript1
{
	public class MyStrategy : UserStrategyBase
	{
		//Initialize
		public override void Initialize(BarHistory bars)
		{
			//plot an RSI and a CMO
			RSI rsi = RSI.Series(bars.Close, 20);
			PlotIndicator(rsi);
			CMO cmo = CMO.Series(bars.Close, 20);
			PlotIndicator(cmo);

			//Create a ChartDisplaySettings for customized chart display
			ChartDisplaySettings cds = new ChartDisplaySettings();

			//hide the volume pane and separator lines
			cds.ShowVolumePane = false;
			cds.PaneSeparatorWidth = 0;

			//set the default bar colors
			cds.ColorUpBar = Color.Gray;
			cds.ColorDownBar = Color.Silver;

			//put these settings into effect
			SetChartDrawingOptions(cds);
		}

		//Execute
		public override void Execute(BarHistory bars, int idx)
		{
		}
	}
}

SetPaneDrawingOptions
public void SetPaneDrawingOptions(string paneTag, int height, int sortValue = 50)

Controls the height and sort order of the chart pane with the specified paneTag (case sensitive). The price pane has a paneTag of "Price", and the volume pane "Volume". Indicator panes typically have a paneTag equal to the plotted indicator's abbreviation. But, to be sure, you can access the PaneTag property of a plotted indicator to obtain its paneTag.

The height parameter determines the relative vertical size of the chart pane. For example, given two panes, one with a height of 100 and one with a height of 50, the first pane will take up twice as much vertical space on the chart. By default, the price pane has a height of 100 and volume and indicator panes 33.

The optional sortValue parameter controls the order that the panes appear in the chart. The price pane has a default sortValue of 0, the volume pane 100, and indicator panes 50. This causes the price pane to sort to the top, the volume pane to the bottom, and indicator panes in between.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;

namespace WealthScript1
{
	public class MyStrategy : UserStrategyBase
	{
		//Initialize - plot an RSI above the price pane, and make it an equal height
		public override void Initialize(BarHistory bars)
		{
			RSI rsi = RSI.Series(bars.Close, 20);
			PlotIndicator(rsi);
			SetPaneDrawingOptions("Price", 100, 2);
			string rsiPaneTag = rsi.PaneTag;
			SetPaneDrawingOptions(rsiPaneTag, 100, 1);
			SetPaneDrawingOptions("Volume", 30, 3);
		}

		//Execute
		public override void Execute(BarHistory bars, int idx)
		{
		}
	}
}

SetSeriesBarColor
public void SetSeriesBarColor(TimeSeries bars, int bar, Color color)

Sets the series bar color at the specified index to the color you specify.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			rsi = RSI.Series(bars.Close, 14);
			PlotIndicator(rsi);
			for (int i = 0; i < bars.Count; i++)
			{
				SetSeriesBarColor(rsi, i, rsi[i] > 70 ? Color.Green : rsi[i] < 30 ? Color.Red : Color.Black);
			}
		}

		//annotate bars when prices cross lower bollinger band
		public override void Execute(BarHistory bars, int idx)
		{
		}

		//declare private variables below
		RSI rsi;
	}
}

SetTextDrawingFont
public void SetTextDrawingFont(Font font)

Changes the font that will be used in subsequent text rendering calls in your Strategy code.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;

namespace WealthScript1
{
	public class MyStrategy : UserStrategyBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			bbLower = new BBLower(bars.Close, 20, 2.00);
			bbUpper = new BBUpper(bars.Close, 20, 2.00);
			PlotIndicator(bbLower);
			PlotIndicator(bbUpper);
			SetTextDrawingOptions(Color.FromArgb(255, 255, 220, 220), Color.DarkRed, 2);
			FontFamily ff = new FontFamily("Impact");
			Font f = new Font(ff, 12);
			SetTextDrawingFont(f);
		}

		//annotate bars when prices cross lower bollinger band
		public override void Execute(BarHistory bars, int idx)
		{
			if (bars.Close.CrossesUnder(bbLower, idx))
				DrawBarAnnotation("Look out!", idx, false, Color.Red, 12, true);
		}

		//declare private variables below
		BBLower bbLower;
		BBUpper bbUpper;
	}
}

SetTextDrawingOptions
public void SetTextDrawingOptions(Color backgroundColor, Color borderColor, int borderWidth)

Lets you control how text is rendered to the chart by any of the text drawing methods. You can specify a background color (backgroundColor parameter), and/or a border color (borderColor parameter) and width (borderWidth parameter) to use when text is rendered. If you don't want a background color and/or border, specify Color.Transparent in these parameters. The settings affect all subsequent calls to text drawing methods.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			bbLower = new BBLower(bars.Close, 20, 2.00);
			bbUpper = new BBUpper(bars.Close, 20, 2.00);
			PlotIndicator(bbLower);
			PlotIndicator(bbUpper);
			SetTextDrawingOptions(Color.FromArgb(32, 255, 0, 0), Color.DarkRed, 2);
		}

		//annotate bars when prices cross lower bollinger band
		public override void Execute(BarHistory bars, int idx)
		{
			if (bars.Close.CrossesUnder(bbLower, idx))
				DrawBarAnnotation("Look out!", idx, false, Color.Red, 8, true);
		}

		//declare private variables below
		BBLower bbLower;
		BBUpper bbUpper;
	}
}


Miscellaneous
BacktestData
public List<BarHistory> BacktestData

Returns a List of BarHistory objects that contain the historical data being backtested.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class ExampleModel11 : UserStrategyBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			//how correlated is this symbol to all of the symbols in the universe?
			TimeSeries tsSumCorr = new TimeSeries(bars.DateTimes, 0.0);
			int n = 0;
			foreach (BarHistory bh in BacktestData)
			{
				Corr c = new Corr(bars, bh, PriceComponents.Close, 20);
				if (c != null && c.Count > 0)
				{
					tsSumCorr += c;
					n++;
				}
			}
			if (n > 0)
				tsSumCorr /= n;
			PlotTimeSeries(tsSumCorr, "Avg Corr", "Corr", Color.DarkOrange);
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
			if (!HasOpenPosition(bars, PositionType.Long))
			{
				//code your buy conditions here
			}
			else
			{
				//code your sell conditions here
			}
		}

		//declare private variables below

	}
}

BacktestSettings
public BacktestSettings BacktestSettings

Exposes the instance of the BacktestSettings class that contains the backtest settings used by the Backtester. You can override one or more of the backtest settings by changing the properties of the BacktestSettings instance. These changes affect your Strategy run only, and not the overall backtest settings established in Preferences.

Remarks

  • You should make assignments to BacktestSettings properties in the Initialize() method.

CurrentCash
public double CurrentCash

Returns the current cash level available in the simulation. The cash level decreases as your Strategy opens new positions, and increases as it exits positions.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			SetTextDrawingOptions(Color.Wheat, Color.Black, 2);
			sma200 = new SMA(bars.Close, 200);
			PlotIndicator(sma200);
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
			if (!HasOpenPosition(bars, PositionType.Long))
			{
				if (bars.Close.CrossesOver(sma200, idx))
				{
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
					string s = "Portfolio cash at entry bar = $" + CurrentCash.ToString("N2");
					DrawText(s, idx, bars.Low[idx], Color.Navy, 8, "Price", true);
				}
			}
			else
			{
				if (bars.Close.CrossesUnder(sma200, idx))
					PlaceTrade(bars, TransactionType.Sell, OrderType.Market);
			}
		}

		//declare private variables below
		SMA sma200;
	}
}

CurrentEquity
public double CurrentEquity

Returns the current equity level in the simulation. This is initially determined by the Starting Equity that you established in Strategy Settings. As your Strategy executes bar by bar, and enters and exits positions, the equity level will rise and fall.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			sma200 = new SMA(bars.Close, 200);
			equity = new TimeSeries(bars.DateTimes);
			PlotIndicator(sma200);
			PlotTimeSeries(equity, "Equity", "Equity", Color.DarkGreen, PlotStyles.ThickHistogram);
		}

		//populate equity as Strategy executes bar by bar
		public override void Execute(BarHistory bars, int idx)
		{
			equity[idx] = CurrentEquity;
			if (HasOpenPosition(bars, PositionType.Long) && bars.Close.CrossesUnder(sma200, idx))
				PlaceTrade(bars, TransactionType.Sell, OrderType.Market);
			else if (bars.Close.CrossesOver(sma200, idx))
				PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
		}

		//declare private variables below
		SMA sma200;
		TimeSeries equity;
	}
}

GetCurrentIndex
public override int GetCurrentIndex(BarHistory bh)

This method is intended to be called in either the PreExecute or PostExecute methods. These two methods provide you a list of BarHistory objects that are being processed during the current Execute cycle. GetCurrentIndex takes a BarHistory parameter, which should be one of the instances in the list mentioned above. It returns the int index into that BarHistory that represents the bar currently being processed by Execute.


GetGlobal
public object GetGlobal(string key)

Gets a value out of the global Dictionary that is available throughout the current WL7 session. Specify a key and a value. If the keyed item is not present, returns null.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;

namespace WealthScript1
{
	public class MyStrategy : UserStrategyBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			int runs = HasGlobal("runs") ? (int)GetGlobal("runs") : 0;
			runs++;
			SetGlobal("runs", runs);
			DrawHeaderText(runs + " Runs so far!");
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
		}

		//declare private variables below

	}
}

GetHistory
public BarHistory GetHistory(BarHistory synchWith, string symbol, string dataSetName = null)

Lets you access data for another symbol from within your Strategy. Returns a BarHistory object for the specified symbol. The synchWith parameter specifies a BarHistory object to synch the historical data with. Generally, this will be the bars parameter from the Initialize or Execute method. You can optionally pass a DataSet name in the dataSetName parameter to retrieve the data from that DataSet, if it's found there.

Remarks

  • GetHistory does not use statically cached data collected by the Data Manager. It's primary intent is to allow a Strategy to make decisions using data that does not reside in the DataSet being tested. For example, buy a stock if the corresponding index is oversold.
  • For statically cached data, use the BacktestData property, which returns a List containing the BarHistory instances being backtested.
Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthScript123
{
	public class MyStrategy : UserStrategyBase
	{
		//plot a market benchmark in its own pane and don't trade unless the benchmark is above its 50-day SMA
		public override void Initialize(BarHistory bars)
		{
			_spy = GetHistory(bars, "SPY");
			PlotBarHistory(_spy, "SPYPane", Color.Silver);
			_smaSpy = SMA.Series(_spy.Close, 50);
			PlotIndicator(_smaSpy, default, default, default, "SPYPane");

		}

		public override void Execute(BarHistory bars, int idx)
		{
			if (!HasOpenPosition(bars, PositionType.Long))
			{
				if (_spy.Close[idx] > _smaSpy[idx])
				{
					//code your buy conditions here
				}
			}
			else
			{
				//code your sell conditions here
			}
		}

		BarHistory _spy;
		SMA _smaSpy;
	}
}

GetHistoryUnsynched
public BarHistory GetHistoryUnsynched(string symbol, HistoryScale scale, string dataSetName = null)

Returns an instance of the BarHistory class the represents historical data for the symbol and scale you specify. If WealthLab could not locate historical data for the symbol/scale, the method returns null. You can optionally pass a DataSet name in the dataSetName parameter to retrieve the data from that DataSet, if it's found there. The resulting data is not synched to the BarHistory currently being processed on the chart. Therefore, you should not plot indicators or TimeSeries derived from this data, unless you first synchronize these series using the TimeSeriesSynchronizer helper class.

Remarks

  • GetHistoryUnsynched does not use statically cached data collected by the Data Manager. It's primary intent is to allow a Strategy to make decisions using data that does not reside in the DataSet being tested. For example, buy a stock if the corresponding index is oversold.
  • For statically cached data, use the BacktestData property, which returns a List containing the BarHistory instances being backtested.
Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//create indictors based on an external data series
		//synchronize them AFTER they are created to preserve consistency despite possible irregularities
		//between the primary and external symbol histories
		public override void Initialize(BarHistory bars)
		{
			//create the unsynchronized indicator (RSI of SPY)
			BarHistory spy = GetHistoryUnsynched("SPY", HistoryScale.Daily);
			TimeSeries spyRSI = new RSI(spy.Close, 14);

			//sychrnize it with the data being processed
			spyRSI = TimeSeriesSynchronizer.Synchronize(spyRSI, bars);

			//Plot it
			PlotTimeSeries(spyRSI, "RSI(SPY,14)", "RSI", Color.Red);

			//Plot RSI of symbol being processed
			RSI rsi = new RSI(bars.Close, 14);
			PlotIndicator(rsi);
		}

		public override void Execute(BarHistory bars, int idx)
		{
		}
	}
}

HasGlobal
public bool HasGlobal(string key)

Returns true if the specified key item is in the global Dictionary.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;

namespace WealthScript1
{
	public class MyStrategy : UserStrategyBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			int runs = HasGlobal("runs") ? (int)GetGlobal("runs") : 0;
			runs++;
			SetGlobal("runs", runs);
			DrawHeaderText(runs + " Runs so far!");
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
		}

		//declare private variables below

	}
}

SendEmail
public void SendEmail(string fromEmail, string smtpServer, string login, string password, int smtpPort, string toEmail, string messageSubject, string messageText, bool smtpEnableSSL = true)

Call this method to send e-mail from your Strategy code. The example code illustrates how to do it using a secure (SSL) mail server (e.g. Google).

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
using System.Collections.Generic;

namespace WealthScript123
{
	public class MyStrategy : UserStrategyBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
			if(idx == bars.Count - 1)
			{
				Transaction t = PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
				// for Gmail, port can be 587 (TLS) or 465 (SSL)
				int port = 587;
				SendEmail("from@domain.com", "smtp.domain.com", "from@domain.com", "myPassword", port, "send-to@domain.com", "Hello World from Wealth-Lab 7", t.Description);
			}
		}

		//declare private variables below

	}
}

SetGlobal
public void SetGlobal(string key, object value)

Sets a value in the global Dictionary that is available throughout the current WL7 session. Specify a key and a value.

Tip: To make a variable available for all symbols during script execution, declare a static class variable instead of using global variables.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;

namespace WealthScript1
{
	public class MyStrategy : UserStrategyBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			int runs = HasGlobal("runs") ? (int)GetGlobal("runs") : 0;
			runs++;
			SetGlobal("runs", runs);
			DrawHeaderText(runs + " Runs so far!");
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
		}

		//declare private variables below

	}
}

StrategyName
public string StrategyName

Returns the class name of the executing strategy that derives from UserStrategyBase.


WriteToDebugLog
public void WriteToDebugLog(string txt)
public void WriteToDebugLog(obj txt)

Writes a line of output to the Debug Log tab.

Remarks

  • The Debug Log is refreshed for each Backtest run.
  • The Log is explicitly segregrated by writes for the symbol under test - you don't need to add bars.Symbol to know for which symbol the write occurred.
  • If you WriteToDebugLog in BacktestBegin, BacktestComplete, PreExecute, or PreExecute virtual methods, Wealth-Lab will generally write to the same random symbol, but you can't count on it especially when using Wealth-Data dynamic DataSets. In this case, the order of writes may not appear intuitive. One way around this is to save writes to a private static List and WriteToDebugLog while iterating through the list in BacktestComplete() as shown in the example.
Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
using System.Collections.Generic;

namespace WealthScript123
{
	public class MyStrategy : UserStrategyBase
	{
		//private variables
		IndicatorBase _sma1;
		IndicatorBase _sma2;
		static List<string> _output = new List<string>();

		public override void Initialize(BarHistory bars)
		{
			//begin the backtest at bar 13
			StartIndex = 13;

			_sma1 = SMA.Series(bars.Close, 8);
			_sma2 = SMA.Series(bars.Close, 13);

			PlotIndicatorLine(_sma1, Color.Blue);
			PlotIndicatorLine(_sma2, Color.Red);
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
			//write the last date for exach symbol
			if (idx == bars.Count - 1)
				WriteToDebugLog(bars.DateTimes[idx].ToShortDateString());


			if (!HasOpenPosition(bars, PositionType.Long))
			{
				if (_sma1.CrossesOver(_sma2, idx))
				{
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);

					//save the date of the Alert
					_output.Add(bars.Symbol + "\t" + bars.DateTimes[idx].ToString("yyyyMMdd"));
				}
			}
			else
			{
				if (_sma1.CrossesUnder(_sma2, idx))
					PlaceTrade(bars, TransactionType.Sell, OrderType.Market);
			}
		}

		public override void BacktestBegin()
		{
			base.BacktestBegin();
			WriteToDebugLog("BEGIN");
		}

		public override void BacktestComplete()
		{
			base.BacktestComplete();
			WriteToDebugLog("COMPLETE");

			foreach (string str in _output)
			{
				WriteToDebugLog(str);
			}
		}
	}
}


Parameter Related
AddParameter
public Parameter AddParameter(string label, ParameterTypes paramType, object defaultValue, double minValue = -999999999, double maxValue = 999999999, double stepValue = 1)

Call this from within the Strategy's constructor to add a parameter to the Strategy. The method returns an instance of the Parameter class that represents the parameter you added.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//constructor
		public MyStrategy() : base()
		{
			AddParameter("Short Period", ParameterTypes.Int32, 50, 30, 70, 10);
			AddParameter("Long Period", ParameterTypes.Int32, 200, 150, 300, 50);
		}

		//create indicators, note obtaining period from parameters
		public override void Initialize(BarHistory bars)
		{
			smaShort = new SMA(bars.Close, Parameters[0].AsInt);
			smaLong = new SMA(bars.Close, Parameters[1].AsInt);
			PlotIndicator(smaShort);
			PlotIndicator(smaLong, Color.Red);
		}

		//a parameter-driven SMA crossover Strategy
		public override void Execute(BarHistory bars, int idx)
		{
			if (!HasOpenPosition(bars, PositionType.Long))
			{
				if (smaShort.CrossesOver(smaLong, idx))
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
			}
			else
			{
				if (smaShort.CrossesUnder(smaLong, idx))
					PlaceTrade(bars, TransactionType.Sell, OrderType.Market);
			}
		}

		//declare private variables below
		SMA smaShort;
		SMA smaLong;
	}
}

Parameters
public ParameterList Parameters

Returns a List<Parameter> containing the Strategy's parameters, all instances of the Parameter class. Strategy parameters should be added in the constructor, by calling the AddParameter method.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//constructor
		public MyStrategy() : base()
		{
			AddParameter("Short Period", ParameterTypes.Int32, 50, 30, 70, 10);
			AddParameter("Long Period", ParameterTypes.Int32, 200, 150, 300, 50);
		}

		//create indicators, note obtaining period from parameters
		public override void Initialize(BarHistory bars)
		{
			smaShort = new SMA(bars.Close, Parameters[0].AsInt);
			smaLong = new SMA(bars.Close, Parameters[1].AsInt);
			PlotIndicator(smaShort);
			PlotIndicator(smaLong, Color.Red);
		}

		//a parameter-driven SMA crossover Strategy
		public override void Execute(BarHistory bars, int idx)
		{
			if (!HasOpenPosition(bars, PositionType.Long))
			{
				if (smaShort.CrossesOver(smaLong, idx))
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
			}
			else
			{
				if (smaShort.CrossesUnder(smaLong, idx))
					PlaceTrade(bars, TransactionType.Sell, OrderType.Market);
			}
		}

		//declare private variables below
		SMA smaShort;
		SMA smaLong;
	}
}


Positions
AverageEntryPrice
public double AverageEntryPrice()

Returns the average entry price of open positions at the moment.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
using System.Collections.Generic;

namespace WealthScript1
{
	public class MyStrategy : UserStrategyBase
	{
		public override void Initialize(BarHistory bars)
		{
			rsi = new RSI(bars.Close, 14);
			PlotIndicator(rsi);
		}

		public override void Execute(BarHistory bars, int idx)
		{
			if(OpenPositions.Count < 6)
			{
				if (rsi[idx] < 30)
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
			}
			else
			{
				//Exit all positions if the Close price is below the average entry price for all open positions in the symbol minus X points...
				double stopValue = AverageEntryPrice() - pointStopLoss;
				bool stopped = bars.Close[idx] < stopValue;

				//...or if RSI is overbought
				bool rsiHigh = rsi[idx] > 70;

				if (stopped || rsiHigh)
				{
					for (int i = 0; i < OpenPositions.Count; i++)
						ClosePosition(OpenPositions[i], OrderType.Market, 0, stopped ? "AvgPrice Stop" : "RSI > 70");
				}
			}
		}

		RSI rsi;
		double pointStopLoss = 5.0;
	}
}

BarsSinceLastExit
public bool BarsSinceLastExit(int idx, int bars, string exitSignal = "")

Returns true if bars since last Position has been exited exceeds given bars. Used if you wish a Strategy to wait for a certain period of time before entering a new position once the preceding position is closed. Optionally, pass on an exitSignal to apply to only certain exit signal name.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
using System.Collections.Generic;

namespace WealthScript1
{
	public class MyStrategy1 : UserStrategyBase
	{
		public override void Initialize(BarHistory bars)
		{
		}

		public override void Execute(BarHistory bars, int idx)
		{
			if (!HasOpenPosition(bars, PositionType.Long))
			{
				if (GetPositions().Count == 0)
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
				else
				{
					//wait 100 bars before making 2nd trade because its exit is labeled "longDelay"
					if (BarsSinceLastExit(idx, longDelay, "longDelay"))
						PlaceTrade(bars, TransactionType.Buy, OrderType.Market);

					//subsequent positions are entered 10 bars since
					if (GetPositions().Count >= 2)
						if (BarsSinceLastExit(idx, normalDelay))
							PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
				}
			}
			else
			{
				//code your sell conditions here
				PlaceTrade(bars, TransactionType.Sell, OrderType.Market, 0, GetPositions().Count == 1 ? "longDelay" : "");
			}
		}

		//how many bars to wait
		int longDelay = 100; //delay before 2nd trade
		int normalDelay = 10; //subsequent trades

	}
}

FindOpenPosition
public Position FindOpenPosition(int positionTag)
public Position FindOpenPosition(PositionType pt)

Looks for an open Position with the specified numeric positionTag. Positions that were created as a result of passing a value in the PlaceTrade method's positionTag parameter can be found in this way.

The second version of FindOpenPosition allows you to look for positions by type, where the pt parameter can contain PositionType.Long or PositionType.Short.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
        //create indicators 
        public override void Initialize(BarHistory bars)
        {
			rsi = new RSI(bars.Close, 20);
        }

        //example of seasonal entries/exits
        public override void Execute(BarHistory bars, int idx)
        {
			//open positions in February and March
			Position febPosition = FindOpenPosition(2);
			Position marchPosition = FindOpenPosition(3);
			if (rsi[idx] < 30)
			{
				if (bars.DateTimes[idx].Month == 2)
				{
					if (febPosition == null)
						PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 2);
				}
				else if (bars.DateTimes[idx].Month == 3)
				{
					if (marchPosition == null)
						PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 3);
				}
			}

			//sell February position in December, sell March position in November
			if (febPosition != null)
				if (bars.DateTimes[idx].Month == 12)
					ClosePosition(febPosition, OrderType.Market);
			if (marchPosition != null)
				if (bars.DateTimes[idx].Month == 11)
					ClosePosition(marchPosition, OrderType.Market);
        }

		//declare private variables below
		RSI rsi;
    }
}

FindOpenPositionAllSymbols
public Position FindOpenPositionAllSymbols(int positionTag)

Returns the open position with the specified positionTag among the entire set of currently open postions, regardless of symbol. This method was created to support a Strategy that stops normal trading during a specific technical event, and instead swaps the entire portfolio into a bond ETF. When the technical condition is over, and normal trading can begin again, the Strategy calls FindOpenPositionAllSymbols to locate the position in this bond ETF that it had opened previously, and closes it.


GetPositions
public override List<Position> GetPositions(bool includeNSF = false)

Returns the list of positions (instances of the Position class) for the current symbol being processed. Use the includeNSF parameter to determine if NSF Positions should be included.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
		//create indicators
		public override void Initialize(BarHistory bars)
        {
			downDays = new ConsecDown(bars.Close, 4);
			upDays = new ConsecUp(bars.Close, 4);
        }

        //buy when 9 consecutive days where close is < close of 4 days ago, sell reverse
        public override void Execute(BarHistory bars, int idx)
        {
            if (!HasOpenPosition(bars, PositionType.Long))
            {
                if (downDays[idx] == 9)
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
            }
            else
            {
                if (upDays[idx] == 9)
					PlaceTrade(bars, TransactionType.Sell, OrderType.Market);
            }
        }

		//report how many trades were filled and not filled
		public override void Cleanup(BarHistory bars)
		{
			int filled = GetPositions().Count;
			int all = GetPositions(true).Count;
			int notFilled = all - filled;
			DrawHeaderText("Filled Positions in " + bars.Symbol + " = " + filled);
			DrawHeaderText("Unfilled Positions in " + bars.Symbol + "= " + notFilled);
		}

		//declare private variables below
		private ConsecDown downDays;
		private ConsecUp upDays;
    }
}

GetPositionsAllSymbols
public List<Position> GetPositionsAllSymbols(bool includeNSF = false)

Returns the list of positions (instances of the Position class) for all symbols in the backtest. Use the includeNSF parameter to determine if NSF Positions should be included.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
		//create indicators
		public override void Initialize(BarHistory bars)
        {
			downDays = new ConsecDown(bars.Close, 4);
			upDays = new ConsecUp(bars.Close, 4);
        }

        //buy when 9 consecutive days where close is < close of 4 days ago, sell reverse
        public override void Execute(BarHistory bars, int idx)
        {
            if (!HasOpenPosition(bars, PositionType.Long))
            {
                if (downDays[idx] == 9)
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
            }
            else
            {
                if (upDays[idx] == 9)
					PlaceTrade(bars, TransactionType.Sell, OrderType.Market);
            }
        }

		//report how many trades were filled and not filled
		public override void Cleanup(BarHistory bars)
		{
			int filled = GetPositionsAllSymbols().Count;
			int all = GetPositionsAllSymbols(true).Count;
			int notFilled = all - filled;
			DrawHeaderText("Filled Positions = " + filled);
			DrawHeaderText("Unfilled Positions in = " + notFilled);
		}

		//declare private variables below
		private ConsecDown downDays;
		private ConsecUp upDays;
    }
}

HasOpenPosition
public bool HasOpenPosition(BarHistory bars, PositionType pt)

Lets you determine if there is currently an open Position of the current type (PositionType.Long or PositionType.Short). For the bars parameter, pass the value of the bars parameter that you received in the Execute method.


LastOpenPosition
public virtual Position LastOpenPosition

Returns the most-recently created open Position object. Returns null if there are no open Positions currently.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			cmo = new CMO(bars.Close, 14);
			PlotIndicator(cmo);
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
			if (LastOpenPosition == null)
			{
				if (cmo.CrossesOver(cmo.OversoldLevel, idx))
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
			}
			else
			{
				if (cmo.CrossesUnder(cmo.OverboughtLevel, idx))
					ClosePosition(LastOpenPosition, OrderType.Market);
			}
		}

		//declare private variables below
		private CMO cmo;
	}
}

LastPosition
public virtual Position LastPosition

Returns the most-recently created Position object or null if Positions have been created.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			cmo = new CMO(bars.Close, 14);
			PlotIndicator(cmo);
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
			if (LastPosition == null)
			{
				if (cmo.CrossesOver(cmo.OversoldLevel, idx))
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
			}
			else
			{
				Position p = LastPosition;
				
				//exit after 3 days
				if (idx + 1 - p.EntryBar >= 3)
					ClosePosition(p, OrderType.Market);
			}
		}

		//declare private variables below
		private CMO cmo;
	}
}

OpenPositions
public virtual List<Position> OpenPositions

Returns a list of the open positions (instances of the Position class) for the BarHistory currently being processed. This includes positions that are marked NSF.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
		//create indicators
		public override void Initialize(BarHistory bars)
        {
			rsi = new RSI(bars.Close, 20);
			PlotIndicator(rsi);
        }

        //keep buying positions when the RSI is oversold
        public override void Execute(BarHistory bars, int idx)
        {
			//see if we need to sell positions
			if (rsi.CrossesOver(50.0, idx))
			{
				foreach(Position pos in OpenPositions)
					ClosePosition(pos, OrderType.Market);
			}
	        
	        //time to buy?
	        if (rsi[idx] < 30)
				PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
        }

		//declare private variables below
		private RSI rsi;
    }
}

OpenPositionsAllSymbols
public List<Position> OpenPositionsAllSymbols

Returns a list of open positions (instances of the Position class) for all symbols being backtested. You generally won't need to access other symbol positions during normal Strategy processing, but this can be handy for certain summary processing you might perform during the BacktestComplete method.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
        //create indicators 
        public override void Initialize(BarHistory bars)
        {
			roc = new ROC(bars.Close, 20);
        }

        //fade the momentum
        public override void Execute(BarHistory bars, int idx)
        {
	        //close all positions when momentum reaches zero
			if (roc[idx] >= 2)
	        	foreach(Position pos in OpenPositions)
					ClosePosition(pos, OrderType.Market);

			//scale in when momentum descreases
			if (roc[idx] < -2)
			{
				//no more than 20 total open positions in portfolio
				if (OpenPositionsAllSymbols.Count < 20)
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
			}
        }

		//declare private variables below
		ROC roc;
    }
}

TrailingStopPrice
public double TrailingStopPrice

Contains the current value of the trailing stop for the position. Trailing stops are managed by the UserStrategyBase CloseAtTrailingStop method.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			//create a TimeSeries to plot trailing stop
			stops = new TimeSeries(bars.DateTimes);
			PlotTimeSeries(stops, "Trailing Stop", "Price", Color.Navy, PlotStyles.Dots);
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
			if (HasOpenPosition(bars, PositionType.Long))
			{
				CloseAtTrailingStop(LastPosition, TrailingStopTypes.PercentHL, 10);
				stops[idx] = LastPosition.TrailingStopPrice;
			}
			else if (idx == bars.Count - 50)
			{
				PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
			}
		}

		//declare private variables below
		private TimeSeries stops;
	}
}


Strategy Execution
AssignAutoStopTargetPrices
public virtual void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice)

You can override this method to assign values to the AutoProfitTargetPrice and AutoStopLossPrice properties of the Transaction instance passed in the t parameter. See the corresponding entries for these properties in the Transaction class for more details on same-bar exits. The advantage of assigning these properties within this method is that it provides you the actual executionPrice, upon which you can base the desired profit target and stop loss prices.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using System.Drawing;
using System.Collections.Generic;

namespace WealthScript5
{
	public class SameBarAuto : UserStrategyBase
	{
		TimeSeries _band;

		public override void Initialize(BarHistory bars)
		{
			double pct = 0.95;
			_band = SMA.Series(bars.Close, 10) * pct;
			PlotTimeSeriesLine(_band, "band", "Price");
		}

		public override void Execute(BarHistory bars, int idx)
		{
			if (HasOpenPosition(bars, PositionType.Long))
			{
				//sell at open next bar
				PlaceTrade(bars, TransactionType.Sell, OrderType.Market);
			}
			else
			{
				//buy at market if the low goes below the band, but closes above it
				if (bars.Low.CrossesUnder(_band[idx], idx) && bars.Close[idx] > _band[idx])
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
			}
		}

		//same bar exits: 3% profit or loss from execution price
		public override void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice)
		{
			t.AutoProfitTargetPrice = executionPrice * 1.03;
			t.AutoStopLossPrice = executionPrice * 0.97;
		}
	}
}

BacktestBegin
public virtual void BacktestBegin()

The backtester calls this method for the first symbol (sorted alphabetically) in the universe prior to the backtest beginning its processing.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
		//intialize the sum variable to zero, this only gets called once per backtest
		public override void BacktestBegin()
		{
			sumClose = 0;
			sumObs = 0;
		}
	    
        //we add the closing price to the sum and increment the number of observations
        public override void Execute(BarHistory bars, int idx)
        {
			sumObs++;
			sumClose += bars.Close[idx];
        }

	    //display average close of entire universe on chart
		public override void Cleanup(BarHistory bars)
		{
			double avg = sumClose / sumObs;
			DrawHeaderText(avg.ToString("N2"));
		}

		//declare private variables below
		private static double sumClose;
		private static int sumObs;
    }
}

BacktestComplete
public virtual void BacktestComplete()

The backtester calls this method for the last symbol (sorted alphabetically) in the universe after the backtest processing is completed for all symbols.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using System.IO;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
		//intialize the sum variable to zero, this only gets called once per backtest
		public override void BacktestBegin()
		{
			sumClose = 0;
			sumObs = 0;
		}
	    
        //we add the closing price to the sum and increment the number of observations
        public override void Execute(BarHistory bars, int idx)
        {
			sumObs++;
			sumClose += bars.Close[idx];
        }

		//save the information that we gathered to a file
		public override void BacktestComplete()
		{
			double avg = sumClose / sumObs;
			string output = "Average Closing Price of DataSet: " + avg.ToString("N2");
			File.WriteAllText(@"C:\My Folder\My File.txt", output);
		}

		//declare private variables below
		private static double sumClose;
		private static int sumObs;
    }
}

Cleanup
public virtual void Cleanup(BarHistory bars)

The backtester calls the Strategy's Cleanup method after all processing is complete for a symbol. Override the Cleanup method to dispose of any necessary objects or data that are not handled by normal .NET garbage collection.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
        //this example illustrates the Execute method by adding the closing price to a 
	    //summation variable each time it is called, then displaying the average close
        public override void Execute(BarHistory bars, int idx)
        {
			sumClose += bars.Close[idx];
        }

	    //display average close on chart
		public override void Cleanup(BarHistory bars)
		{
			double avg = sumClose / bars.Count;
			DrawHeaderText(avg.ToString("N2"));
		}

		//declare private variables below
		private double sumClose = 0;
    }
}

Execute
public abstract void Execute(BarHistory bars, int idx)

The backtester calls the Strategy's Execute method for each bar of data being processed. The BarHistory being processed is passed in the bars parameter. The numeric index being processed is passed in the idx parameter. Override the Execute method to implement the Strategy's primary logic.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
        //this example illustrates the Execute method by adding the closing price to a 
	    //summation variable each time it is called, then displaying the average close
        public override void Execute(BarHistory bars, int idx)
        {
			sumClose += bars.Close[idx];
        }

	    //display average close on chart
		public override void Cleanup(BarHistory bars)
		{
			double avg = sumClose / bars.Count;
			DrawHeaderText(avg.ToString("N2"));
		}

		//declare private variables below
		private double sumClose = 0;
    }
}

ExecutionDataSetName
public string ExecutionDataSetName

Returns the name of the DataSet that is currently being processed. If the Strategy was not executed on a DataSet (single symbol mode), returns null.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using System.Drawing;

namespace WealthScript1
{
	public class MyStrategy : UserStrategyBase
	{
		//Initialize
		public override void Initialize(BarHistory bars)
		{
			string dsn = ExecutionDataSetName;
			if (dsn == null)
				DrawHeaderText("Symbol: " + bars.Symbol, Color.Black, 20);
			else
				DrawHeaderText(ExecutionDataSetName, Color.Black, 20);
		}

		//Execute
		public override void Execute(BarHistory bars, int idx)
		{
		}
	}
}

ExecutionMode
public StrategyExecutionModes ExecutionMode

Returns the execution mode of the backtester.

For details and an example, see Enums > StrategyExecutionModes


Initialize
public virtual void Initialize(BarHistory bars)

The backtester calls the Strategy's Initialize method prior to beginning the main trading logic loop. Initialize is called once for each symbol being backtested, and the symbol's BarHistory is passed in the bars parameter. Override this method to create any instances of indicators and other objects your Strategy will need.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
        //create indicators and other objects here, this is executed prior to the main trading loop
        public override void Initialize(BarHistory bars)
        {
            myRSI = new RSI(bars.Close, 14);
            PlotIndicator(myRSI);
        }

        //execute the strategy rules here, this is executed once for each bar in the backtest history
        public override void Execute(BarHistory bars, int idx)
        {
        }

        //declare private variables below
        RSI myRSI;
    }
}

PostExecute
public virtual void PostExecute(DateTime dt, List<BarHistory> participants)

Executes immediately after the main backtesting loop that processed each symbol via the Execute method. PostExecute gives you a chance to operate on the list of symbols that have been processed during this backtesting loop iteration. You are provided the date/time being processed via the dt parameter, and a list of BarHistory instances containing the data being processed this iteration in the participants parameter. Use the GetCurrentIndex method to determine the index to use for a particular BarHistory instance.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
		}

		//report on symbols that were processed
		public override void PostExecute(DateTime dt, List<BarHistory> participants)
		{
			string s = dt.ToShortDateString() + ": Symbols=" + participants.Count + " ";
			foreach (BarHistory bh in participants)
				s += bh.Symbol + ",";
			WriteToDebugLog(s);
		}
	}
}

PreExecute
public virtual void PreExecute(DateTime dt, List<BarHistory> participants)

Executes immediately prior to the main backtesting loop that processed each symbol via the Execute method. PreExecute gives you a chance to operate on the list of symbols that are being processed during this backtesting loop iteration. You are provided the date/time being processed via the dt parameter, and a list of BarHistory instances containing the data being processed this iteration in the participants parameter. Use the GetCurrentIndex method to determine the index to use for a particular BarHistory instance.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//the list of symbols that we should buy each bar
		private static List<BarHistory> buys = new List<BarHistory>();

		//create the weight indicator and stash it into the BarHistory object for reference in PreExecute
		public override void Initialize(BarHistory bars)
		{
			rsi = new RSI(bars.Close, 14);
			bars.Cache["RSI"] = rsi;
		}

		//this is called prior to the Execute loop, determine which symbols have the lowest RSI
		public override void PreExecute(DateTime dt, List<BarHistory> participants)
		{
			//store the symbols' RSI value in their BarHistory instances
			foreach (BarHistory bh in participants)
			{
				RSI rsi = (RSI)bh.Cache["RSI"];
				int idx = GetCurrentIndex(bh);  //this returns the index of the BarHistory for the bar currently being processed
				double rsiVal = rsi[idx];
				bh.UserData = rsiVal; //save the current RSI value along with the BarHistory instance
			}

			//sort the participants by RSI value (lowest to highest)
			participants.Sort((a, b) => a.UserDataAsDouble.CompareTo(b.UserDataAsDouble));

			//keep the top 3 symbols
			buys.Clear();
			for (int n = 0; n < 3; n++)
			{
				if (n >= participants.Count)
					break;
				buys.Add(participants[n]);
			}
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
			bool inBuyList = buys.Contains(bars);
			if (!HasOpenPosition(bars, PositionType.Long))
			{
				//buy logic - buy if it's in the buys list
				if (inBuyList)
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
			}
			else
			{
				//sell logic, sell if it's not in the buys list
				if (!inBuyList)
					PlaceTrade(bars, TransactionType.Sell, OrderType.Market);
			}
		}

		//declare private variables below
		private RSI rsi;
	}
}

StartIndex
public int StartIndex

This property controls the starting point of your backtest, specified as a bar number index. By default, StartIndex return 0, which means that your Strategy's Execute method will first get called with a bar index of zero. If you assign a higher value to StartIndex, the Execute method will first get called at the index you specified.

This can be useful to avoid unnecessary calls to Execute. For example, if your Strategy uses a 200 bar moving average, you can set StartIndex to 199 to prevent the Execute method from being called 199 times before the moving average is available.

The backtester also uses StartIndex to determine the entry bar for the benchmark buy & hold comparison. In the example above, the buy & hold comparison would have also entered its position at bar 199, making the benchmark a more fair comparison.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
		//first 200 bars not needed
		public override void Initialize(BarHistory bars)
        {
			sma200 = new SMA(bars.Close, 200);
			StartIndex = 199;
        }

        //a price/SMA crossover
        public override void Execute(BarHistory bars, int idx)
        {
            if (!HasOpenPosition(bars, PositionType.Long))
            {
                if (bars.Close.CrossesOver(sma200, idx))
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
            }
            else
            {
                if (bars.Close.CrossesUnder(sma200, idx))
					PlaceTrade(bars, TransactionType.Sell, OrderType.Market);
            }
        }

		//declare private variables below
		private SMA sma200;
    }
}


Trading
CloseAtTrailingStop
public void CloseAtTrailingStop(Position pos, TrailingStopTypes tst, double amount)

Use this method to close the specified Position object (pos) at a trailing stop. Specify the type of trailing stop in the tst parameter. You can specify percentage, point or ATR based (Chandelier) trailing stops, and specify whether the trailing stop is based off closing prices or off high/lows. The amount parameter specifies the size of the stop, either in percentage, point or ATR value.

TrailingStopTypes

  • ATR - A Chandelier stop using a 22-period Average True Range.
  • PercentC - A percentage of closing prices. For example, pass 7.5 for amount for a 7.5% trailing stop based on closing prices.
  • PercentHL - Like PercentC, but adjusts based on intraday progress; uses highs for long positions and lows for short positions.
  • PointC - amount becomes a point-based stop with respect to closing prices.
  • PointHL - Like PointC, but amount is subtracted from highs (long) or added to lows (short) to adjust the trailing stop value.
Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			//create a TimeSeries to plot trailing stop
			stops = new TimeSeries(bars.DateTimes);
			PlotTimeSeries(stops, "Trailing Stop", "Price", Color.Navy, PlotStyles.Dots);
		}

		//execute the strategy rules here, this is executed once for each bar in the backtest history
		public override void Execute(BarHistory bars, int idx)
		{
			if (HasOpenPosition(bars, PositionType.Long))
			{
				CloseAtTrailingStop(LastPosition, TrailingStopTypes.PercentHL, 10);
				stops[idx] = LastPosition.TrailingStopPrice;
			}
			else if (idx == bars.Count - 50)
			{
				PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
			}
		}

		//declare private variables below
		private TimeSeries stops;
	}
}

ClosePosition
public void ClosePosition(Position pos, OrderType orderType, double price = 0, string exitSignalName = "")

Use this method to explicitly close a Position object (pos). Specify the orderType to use to close the position. If you use a limit or stop order, specify the order price in the price parameter.

OrderType is one of the following enumerations (see Enums for more info):

  • OrderType.Market
  • OrderType.Limit
  • OrderType.Stop
  • OrderType.LimitMove
  • OrderType.MarketClose
  • OrderType.FixedPrice

You can pass an optional exitSignalName parameter, which will be assigned to the Transaction object's SignalName property, and will ultimately be assigned to the resulting Position's ExitSignalName property.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
    public class MyStrategy : UserStrategyBase
    {
        //create indicators and other objects here, this is executed prior to the main trading loop
        public override void Initialize(BarHistory bars)
        {
			rsi = new RSI(bars.Close, 14);
			PlotIndicator(rsi);
        }

        //buy multiple positions when RSI oversold
        public override void Execute(BarHistory bars, int idx)
        {
			if (rsi[idx] < 30)
				PlaceTrade(bars, TransactionType.Buy, OrderType.Market);

			if (rsi[idx] > 60)
			{
				//sell open positions one by one, sell the least profitable position first
				Position pos = null;
				double lowestProfit = Double.MaxValue;
				foreach (Position openPos in OpenPositions)
					if (openPos.ProfitAsOf(idx) < lowestProfit)
					{
						lowestProfit = openPos.ProfitAsOf(idx);
						pos = openPos;
					}
				if (pos != null)
					ClosePosition(pos, OrderType.Market);
			}
        }

		//declare private variables below
		private RSI rsi;
    }
}

PlaceTrade
public Transaction PlaceTrade(BarHistory bars, TransactionType transType, OrderType orderType, double price = 0, int positionTag = -1)
public Transaction PlaceTrade(BarHistory bars, TransactionType transType, OrderType orderType, double price, string signalName)

Places a simulated order, and returns an instance of the Transaction class the represents it. The WealthLab backtester will determine the number of shares based on the position size settings you established in the Strategy Settings. The backtester will attempt to fill the trade at the start of the following bar, and the simulation must have sufficient simulated capital to do so. The method returns an instance of the Transaction class that represents the transaction.

  • For the bars parameter, pass the same bars parameter value that you received in the call to the Execute method.

  • For the transType parameter, specify TransactionType.Buy, TransactionType.Sell, TransactionType.Short, or TransactionType.Cover.

  • For the orderType parameter, specify OrderType.Market, OrderType.Limit or OrderType.Stop.

  • For limit and stop orders, supply an order price in the price parameter.

  • The positionTag parameter allows you to optionally maintain groups of positions, using integer codes that you specify. You can locate an open position with a matching positionTag by calling the FindOpenPosition method. This method is used internally by Building Block strategies.

The second constructor allows you to pass a signalName parameter. This string will be assigned to the Transaction's SignalName property, and will ultimately end up assigned to the resulting Position's EntrySignalName or ExitSignalName properties.

Example Code
using WealthLab.Backtest;
using System;
using WealthLab.Core;
using WealthLab.Indicators;
using WealthLab.ChartWPF;
using System.Drawing;
using System.Collections.Generic;

namespace WealthLab
{
	public class MyStrategy : UserStrategyBase
	{
		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			hh = new Highest(bars.High, 33);
			ll = new Lowest(bars.Low, 33);
			PlotIndicator(hh, Color.Green);
			PlotIndicator(ll, Color.Red);
		}

		//a basic stop and reverse system
		public override void Execute(BarHistory bars, int idx)
		{
			PlaceTrade(bars, TransactionType.Cover, OrderType.Stop, hh[idx]);
			PlaceTrade(bars, TransactionType.Sell, OrderType.Stop, ll[idx]);
			if (!HasOpenPosition(bars, PositionType.Long))
				PlaceTrade(bars, TransactionType.Buy, OrderType.Stop, hh[idx]);
			if (!HasOpenPosition(bars, PositionType.Short))
				PlaceTrade(bars, TransactionType.Short, OrderType.Stop, ll[idx]);
		}

		//declare private variables below
		IndicatorBase hh;
		IndicatorBase ll;
	}
}