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

The BacktestSettings class contains the settings and preferences that control how WealthLab 9 conducts a backtest, including commissions, slippage, interest, dividends, futures handling, position matching, signal ordering, and multi-currency simulation.

Advanced Strategy Settings
CountMarketExitAsFullBar
public bool CountMarketExitAsFullBar

Controls how WealthLab counts the holding period of Positions that exit using a Market order at the market open. Set this to true to count the market-exit bar as a full bar for holding-period calculations.


ExitSort
public ExitPriority ExitSort

Specifies the priority WealthLab uses when sorting competing exit orders.


IntradayWeightScale
public HistoryScale IntradayWeightScale

Specifies the intraday HistoryScale WealthLab uses for granular processing of Limit and Stop entry Signals. The default is HistoryScale.Daily, which disables granular intraday processing. This setting is not included in the normal persisted BacktestSettings string.


MatchLogic
public PositionMatchLogic MatchLogic

Specifies how WealthLab matches an exit Signal with an open Position when more than one Position is eligible. The default is LIFO. Possible values are:

  • FIFO
  • LIFO
  • Smallest

ProcessCosmeticInSM
public bool ProcessCosmeticInSM

Controls whether cosmetic Strategy methods are processed when the Strategy is executed by the Strategy Monitor.


SignalSort
public SignalSort SignalSort

Specifies how WealthLab sorts the final set of Signals generated by the Backtester. Possible values are:

  • Symbol
  • TransTypeSymbol
  • TransTypeWeightSymbol

UseMarketsSymbolsCommission
public bool UseMarketsSymbolsCommission

If set to true, WealthLab uses commission settings defined for the applicable Market or Symbol instead of relying exclusively on the general BacktestSettings commission configuration.



Members
CashInterestRate
public double CashInterestRate

The annual percentage rate of interest earned on positive simulated cash balances. This value is ignored when UseUSTRateAsInterest is true.


CollectDividends
public bool CollectDividends

If set to true, simulated dividends are collected during the backtest. WealthLab obtains dividend information from EventDataPoint instances associated with the BarHistory data.


Commission
public Commission Commission

Returns a Commission instance configured from CommissionType, CommissionAmount, and the optional minimum and maximum commission settings. You can also assign a custom Commission instance to override the Commission generated from these properties.


CommissionAmount
public double CommissionAmount

Specifies the commission amount used during the backtest. The interpretation of this value depends on CommissionType.


CommissionType
public CommissionType CommissionType

Specifies the commission method used during the backtest.


FuturesMode
public bool FuturesMode

If set to true, WealthLab uses futures-specific SymbolInfo values such as Margin, PointValue, and TickSize when performing applicable backtest calculations.


FuturesProfitMarginBased
public bool FuturesProfitMarginBased

Controls how WealthLab calculates futures profit when Futures Mode is active. When true, futures profit calculations use the futures margin-based behavior. When false, WealthLab uses the raw position value behavior.


Instance
public static BacktestSettings Instance

Returns the global BacktestSettings instance used by WealthLab 9.


IsLimitSlippageEnabled
public bool IsLimitSlippageEnabled

If set to true, WealthLab applies simulated slippage to Limit orders. The amount is controlled by SlippagePercentStocks for stocks and SlippageTickFutures for futures.


IsSlippageEnabled
public bool IsSlippageEnabled

If set to true, WealthLab applies simulated slippage to applicable Market, Stop, and AtClose orders. The amount is controlled by SlippagePercentStocks for stocks and SlippageTickFutures for futures.


MarginInterestRate
public double MarginInterestRate

The annual percentage rate charged on simulated margin balances.


MaximumCommission
public double MaximumCommission

Specifies the maximum commission that can be charged for a Transaction when UseMaximumCommission is true.


MinimumCommission
public double MinimumCommission

Specifies the minimum commission that can be charged for a Transaction when UseMinimumCommission is true.


RetainNSFPositions
public bool RetainNSFPositions

Controls whether WealthLab retains Positions for which there was insufficient simulated capital or buying power to complete the fill. When true, these NSF Positions continue to participate in Strategy logic so that their eventual exit conditions and Signals can still be generated. Setting this to false can reduce backtest overhead, but can also alter subsequent Strategy behavior because the NSF Position is no longer considered open. This setting is an Advanced Strategy setting and is not included in the normal persisted BacktestSettings string.

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
    {
        public override void Initialize(BarHistory bars)
        {
            BacktestSettings.RetainNSFPositions = false;
            StartIndex = 13;
            _sma1 = SMA.Series(bars.Close, 8);
            _sma2 = SMA.Series(bars.Close, 13);
            PlotIndicatorLine(_sma1, WLColor.Blue);
            PlotIndicatorLine(_sma2, WLColor.Red);
        }
        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);
            }
        }
        IndicatorBase _sma1;
        IndicatorBase _sma2;
    }
}

RoundLots
public bool RoundLots

If set to true, WealthLab applies round-lot sizing rules to Transaction quantities.


SlippagePercentStocks
public double SlippagePercentStocks

Specifies the percentage slippage applied to stock Transactions when slippage is enabled. Works in conjunction with IsSlippageEnabled and IsLimitSlippageEnabled.

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)
        {
            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

Specifies the number of ticks of slippage applied to futures Transactions when slippage is enabled. Works in conjunction with IsSlippageEnabled and IsLimitSlippageEnabled.

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)
        {
            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 benchmark Buy & Hold backtest does not use margin. Set this property to true to allow the benchmark to use the Margin Factor configured in the backtest's PositionSize.


UseMaximumCommission
public bool UseMaximumCommission

If set to true, WealthLab applies MaximumCommission when calculating simulated commissions.


UseMinimumCommission
public bool UseMinimumCommission

If set to true, WealthLab applies MinimumCommission when calculating simulated commissions.


UseUSTRateAsInterest
public bool UseUSTRateAsInterest

If set to true, WealthLab uses U.S. Treasury yields instead of CashInterestRate when calculating simulated interest earned on cash. The Treasury maturity is specified by YieldPeriod.


VolumeLimit
public double VolumeLimit

If greater than zero, limits the Quantity of a Transaction based on the specified percentage of the entry bar's volume.


YieldPeriod
public YieldPeriod YieldPeriod

Specifies the U.S. Treasury maturity to use when UseUSTRateAsInterest is true. Possible values are:

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


Multi-Currency Simulation
BaseCurrency
public string BaseCurrency

Specifies the base currency used when MultiCurrency is enabled. The default value is "USD".


MultiCurrency
public bool MultiCurrency

If set to true, WealthLab enables multi-currency backtest processing.



Persistence
Clone
public BacktestSettings Clone()

Creates and returns a copy of the BacktestSettings instance. The method persists and parses the normal BacktestSettings properties and also explicitly copies RetainNSFPositions and IntradayWeightScale.


Parse
public static BacktestSettings Parse(string s)

Creates and returns a BacktestSettings instance from the persisted configuration string in s. The method also supports BacktestSettings strings saved by older WealthLab versions.


Persist
public string Persist()

Returns a string containing the persisted BacktestSettings configuration. Advanced Strategy settings such as RetainNSFPositions and IntradayWeightScale are not included in this persisted string.