Search Framework:
BacktestSettings
Namespace: WealthLab.Backtest
Parent: Object

The BacktestSettings class contains information about how a backtest was conducted, including the benchmark symbol used, and whether cash interest and dividends were considered.

Members
CashInterestRate
public double CashInterestRate

The percentage amount of interest that simulated cash in the backtest should generate.


CollectDividends
public bool CollectDividends

If set to true, simulated dividends will be collected during the backtest. Dividends are calculated based on EventDataPoint instances in the BarHistory instances being tested, and data points with the Name "dividend" are considered. Deduping logic is in place to ensure that only one dividend per date is accounted for, even if multiple Event Data Providers that supply dividends are enabled.


CommissionAmount
public double CommissionAmount

Specifies the amount to use for backtest simulated commissions. Works in conjunction with the CommissionType property.


CommissionType
public CommissionTypes CommissionType

Specifies the commission type used in the backtest. Possible values are:

  • None
  • Flat - CommissionAmount specifies total commission per trade
  • PerShare - CommissionAmount specifies commission per share/contract traded

FuturesMode
public bool FuturesMode

If set to true, the backtest will be run in "futures mode", and take account of each symbol's Margin, PointValue, and Tick values when calculating gains and losses.


IsLimitSlippageEnabled
public bool IsLimitSlippageEnabled

Allows you to read or override the Backtest Preference that enables Limit Order Slippage. Works in conjunction with SlippagePercentStocks and SlippageTickFutures properties.


IsSlippageEnabled
public bool IsSlippageEnabled

Allows you to read or override the Backtest Preference Adjust Entry/Exit Prices that enables Slippage for Market, Stop, and AtClose orders. Works in conjunction with SlippagePercentStocks and SlippageTickFutures properties.


MarginInterestRate
public double MarginInterestRate

The percentage amount that simulated margin interest should be charged during the backtest.


RetainNSFPositions
public bool RetainNSFPositions

This setting overrides the Backtest Preference: Retain NSF Positions

If set to true, the Backtester will internally retain and continue to process positions for which there was insufficient capital to complete the simulated fill. This preserves the internal integrity of a Strategy, preventing it from potentially signaling new buy orders as an NSF position continues to be held until its eventual exit conditions are met. It also allows you to see exit signals for positions that might have been NSF in the simulation, but that you have actually filled in your live trading.

Set this to false to cause the Backtester to ignore NSF positions. This can reduce the overhead for certain Strategies that results in a faster backtest, but can also cause new buy signals to trigger at different places, depending on available simulated capital.

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

namespace WealthScript
{
	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)
		{
			//override the Retain NSF Positions backtest setting
			BacktestSettings.RetainNSFPositions = false;

			//begin the backtest at bar 13
			StartIndex = 13;

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

			PlotIndicatorLine(_sma1, WLColor.Blue);
			PlotIndicatorLine(_sma2, WLColor.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)
		{
			if (!HasOpenPosition(bars, PositionType.Long))
			{
				if (_sma1.CrossesOver(_sma2, idx))
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);

			}
			else
			{
				if (_sma1.CrossesUnder(_sma2, idx))
					PlaceTrade(bars, TransactionType.Sell, OrderType.Market);
			}
		}

		//declare private variables below
		IndicatorBase _sma1;
		IndicatorBase _sma2;

	}
}

RoundLots
public bool RoundLots

If set to true, Transaction Quantity will be rounded to the nearest 100 shares/contracts, or the nearest 10 if under 100.


SlippagePercentStocks
public double SlippagePercentStocks

Allows you to read or override the Backtest Preference SlippagePercentStocks that sets the Slippage percentage amount for Market, Stop, and AtClose orders. Works in conjunction with IsSlippageEnabled and IsLimitSlippageEnabled properties.

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

namespace WealthScript123
{
	public class SlippageOverrideExample : UserStrategyBase
	{
		public override void Initialize(BarHistory bars)
		{
			// Enable and set market order slippage for stocks to 0.1%
			BacktestSettings.IsSlippageEnabled = true;
			BacktestSettings.SlippagePercentStocks = 0.1;

			StartIndex = _slowPer;

			_fast = SMA.Series(bars.Close, 20);
			_slow = SMA.Series(bars.Close, _slowPer);
			PlotIndicatorLine(_fast, WLColor.Blue);
			PlotIndicatorLine(_slow, WLColor.Black);
		}

		public override void Execute(BarHistory bars, int idx)
		{
			if (!HasOpenPosition(bars, PositionType.Long))
			{
				if (_fast.CrossesOver(_slow, idx))
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
			}
			else
			{
				if (_fast.CrossesUnder(_slow, idx))
					PlaceTrade(bars, TransactionType.Sell, OrderType.Market);
			}
		}

		SMA _slow;
		SMA _fast;
		int _slowPer = 50;
	}
}

SlippageTickFutures
public int SlippageTickFutures

Allows you to read or override the Backtest Preference SlippagePercentStocks that sets the amount of Slippage in ticks for futures contracts for Market, Stop, and AtClose orders. Works in conjunction with IsSlippageEnabled and IsLimitSlippageEnabled properties.

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

namespace WealthScript124
{
	public class SlippageOverrideExample : UserStrategyBase
	{
		public override void Initialize(BarHistory bars)
		{
			// Enable and set market order slippage for futures to 2 ticks
			BacktestSettings.IsSlippageEnabled = true;
			BacktestSettings.SlippageTickFutures = 2;

			StartIndex = _slowPer;

			_fast = SMA.Series(bars.Close, 20);
			_slow = SMA.Series(bars.Close, _slowPer);
			PlotIndicatorLine(_fast, WLColor.Blue);
			PlotIndicatorLine(_slow, WLColor.Black);
		}

		public override void Execute(BarHistory bars, int idx)
		{
			if (!HasOpenPosition(bars, PositionType.Long))
			{
				if (_fast.CrossesOver(_slow, idx))
					PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
			}
			else
			{
				if (_fast.CrossesUnder(_slow, idx))
					PlaceTrade(bars, TransactionType.Sell, OrderType.Market);
			}
		}

		SMA _slow;
		SMA _fast;
		int _slowPer = 50;
	}
}

UseMarginInBenchmark
public bool UseMarginInBenchmark

By default, the comparison benchmark buy and hold backtest does not employ margin. Set this to true to have it employ the same Margin value in the backtest's PositionSize property.


UseUSTRateAsInterest
public bool UseUSTRateAsInterest

If set to true, the CashInterest property will be ignored, and the percentage rate for simulated interest on cash will be determined by the yield of the US Treasury. The US Treasury period to use for this purpose is set in the YieldPeriod property.


VolumeLimit
public double VolumeLimit

If greater than zero, will limit the Quantity of a Transaction to that percentage of the entry bar's volume.


YieldPeriod
public YieldPeriods YieldPeriod

The period of US Treasury yield to use when UseUSTRateAsInterest is set to true. Possible values are:

  • OneMonth
  • ThreeMonth
  • SixMonth
  • OneYear
  • TwoYear
  • ThreeYear
  • FiveYear
  • SevenYear
  • TenYear
  • TwentyYear
  • ThirtyYear