Parameter
Namespace: WealthLab.Core
Parent: Object

The Parameter class represents a configurable parameter, and it most often used in C# Coded Strategies and custom indicators (derived from IndicatorBase). Each Parameter instance has a Name, a Type (see ParameterTypes in the Enums reference) and a default Value.

In C# Coded Strategies, Parameters are used to optimize a Strategy. In these cases, the Parameter instances have their MinValue, MaxValue and StepValue properties defined to specify the optimization range.

In custom indicator development, the Parameters are used when an indicator is drag and dropped onto a chart or in a Building Block Strategy. The user can change the value of the Parameter using the user interface in WealthLab or on the web site.

AsType Helper Properties
AsBarHistory
public BarHistory AsBarHistory

Returns the Parameter's Value as a BarHistory instance. This assumes the Parameter's Type is ParameterTypes.BarHistory.


AsBoolean
public bool AsBoolean

Returns the Parameter's Value as a bool. This assumes the Parameter's Type is ParameterTypes.Boolean.


AsColor
public Color AsColor

Returns the Parameter's Value as a System.Drawing.Color instance. This assumes the Parameter's Type is ParameterTypes.Color.


AsColorWpf
public System.Windows.Media.Color AsColorWpf

Returns the Parameter's Value as a System.Windows.Media.Color instance. This assumes the Parameter's Type is ParameterTypes.ColorWpf.


AsDouble
public double AsDouble

Returns the Parameter's Value as a floating point double. This assumes the Parameter's Type is ParameterTypes.Double.


AsFont
public Font AsFont

Returns the Parameter's Value as a System.Drawing.Font instance. This assumes the Parameter's Type is ParameterTypes.Font.


AsHistoryScale
public HistoryScale AsHistoryScale

Returns the Parameter's Value as a HistoryScale instance. This assumes the Parameter's Type is ParameterTypes.HistoryScale.


AsInt
public int AsInt

Returns the Parameter's Value as an int. This assumes the Parameter's Type is ParameterTypes.Int32.


AsLineStyleV2
public LineStylesV2 AsLineStyleV2

Returns the Parameter's Value as a LineStylesV2 value (see Enums reference). This assumes the Parameter's Type is ParameterTypes.LineStyleV2.


AsPriceComponent
public PriceComponents AsPriceComponent

Returns the Parameter's Value as a PriceComponents value (see Enums reference). This assumes the Parameter's Type is ParameterTypes.PriceComponent.


AsString
public string AsString

Returns the Parameter's Value as an string. This assumes the Parameter's Type is ParameterTypes.String.


AsTimeSeries
public TimeSeries AsTimeSeries

Returns the Parameter's Value as a TimeSeries instance. This assumes the Parameter's Type is ParameterTypes.TimeSeries.



Constructors
Parameter
public Parameter(string name, ParameterTypes type, object value)
public Parameter(string name, ParameterTypes type, object value, double minValue, double maxValue)
public Parameter(string name, Type enumType)

The Parameter class has three constructors.

The first two provide parameters to establish the Parameter's name, type, and initial value. The second constructor allows you to also specify a MinValue, MaxValue, and StepValue. These values are used in optimizations to determine the range or parameter values to test against.

The last constructor allows you to specify an Enum type. The constructor creates a Parameter of Type StringChoice, and automatically fills in the Choices with string values that correspond to the Enum's values.

In a C# Coded Strategy, you generally do not have to create a Parameter instance directly using a constructor. Rather, you can use the AddParameter method provided by UserStrategyBase. Likewise, when coding custom indicators (descended from IndicatorBase), you can also use the AddParameter method rather than calling the constructor directly. These methods create the Parameter instance and then add it to appropriate list.



Members
AssignExplicitValues
public void AssignExplicitValues(IEnumerable<double> values)
public void AssignExplicitValues(params object[] vals)

Let's you assign possible optimization values for this Parameter explicity, instead of lettting it assume these values based on its MinValue, MaxValue and StepValue. There are two overloaded versions of this method:

  • The first version lets you assign the values based on an instance of an IEnumerable<double>, such as a List<double> or an array of double values.
  • The second version lets you enter the values directly as parameters of the method call. Enter one or more double values.
Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using System.Drawing;

namespace WealthScript1
{
	public class MyStrategy : UserStrategyBase
	{
		public MyStrategy()
		{
			Parameter p = AddParameter("Explicit", ParameterType.Int32, 10, 10, 10000, 10);
			p.AssignExplicitValues(10, 20, 50, 100, 200);
		}

		//Initialize
		public override void Initialize(BarHistory bars)
		{
			Parameter p = Parameters[0];
			DrawHeaderText(p.ValuesDescription, WLColor.Black, 12);
		}

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

AssignFibonacciValues
public void AssignFibonacciValues()

Uses the MinVale and MaxValue range that you already established for the Parameter, and add values that include all of the Fibonacci sequence numbers that fall within that range.

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

namespace WealthScript1
{
	public class MyStrategy : UserStrategyBase
	{
		public MyStrategy()
		{
			Parameter p = AddParameter("Fibonacci", ParameterType.Int32, 10, 10, 10000, 10);
			p.AssignFibonacciValues();
		}

		//Initialize
		public override void Initialize(BarHistory bars)
		{
			Parameter p = Parameters[0];
			DrawHeaderText(p.ValuesDescription, WLColor.Black, 12);
		}

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

AssignPercentageValues
public void AssignPercentageValues(double percentageGain)

Uses the MinVale and MaxValue range that you already established for the Parameter, and add values beginning at MinValue, and increasing each time by the percentage specified in the percentageGain parameter. In this way you can produce logarithmic optimization parameter values.

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

namespace WealthScript1
{
	public class MyStrategy : UserStrategyBase
	{
		public MyStrategy()
		{
			Parameter p = AddParameter("Percentage", ParameterType.Int32, 10, 10, 1000, 10);
			p.AssignPercentageValues(20);
		}

		//Initialize
		public override void Initialize(BarHistory bars)
		{
			Parameter p = Parameters[0];
			DrawHeaderText(p.ValuesDescription, WLColor.Black, 12);
		}

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

Choices
public List<string> Choices

A list of strings that contains the possible values the Parameter can assume. This comes into play of the Parameter Type is ParameterTypes.StringChoice. When using this parameter type, add the desired string values to the Parameter instance's Choices property.


DefaultValue
public object DefaultValue

The DefaultValue property contains the initial value that was established when the Parameter instance was created. The Value property, on the other hand, can be changed either via optimization, or by a change from a user in the user interface.


Hint
public string Hint

Contains a string hint or comment that pops up as a tooltip when hovering over the Parameter's slider in the Strategy Settings. The comment is an empty string if you don't assign one.

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

namespace WealthScript4
{
	public class HintExample : UserStrategyBase
	{
		public HintExample()
		{
			Parameter p = AddParameter("Number of swings", ParameterType.Int32, 6, 1, 20, 1);
			p.Hint = "The swings parameter in the AdaptiveLookback indicator";
			p = AddParameter("Drop %", ParameterType.Double, 0.95, 0.92, 0.99, 0.01);
			p.Hint = "Percent drop below adaptiveMAL to buy";
		}

		//create indicators and other objects here, this is executed prior to the main trading loop
		public override void Initialize(BarHistory bars)
		{
			swings = Parameters[0].AsInt;
			drop = Parameters[1].AsDouble;

			//create an instance of the AdaptiveLookback indicator class
			AdaptiveLookback ap = AdaptiveLookback.Series(bars, swings, false, false, false);
			adaptiveADX = new TimeSeries(bars.DateTimes, 0);
			adaptiveMAH = new TimeSeries(bars.DateTimes, 0);
			adaptiveMAL = new TimeSeries(bars.DateTimes, 0);

			//fill the adaptive series
			for (int bar = 0; bar < bars.Count; bar++)
			{
				adaptiveMAH[bar] = FastSMA.Value(bar, bars.High, Math.Max(1, (int)ap[bar]));
				adaptiveMAL[bar] = FastSMA.Value(bar, bars.Low, Math.Max(1, (int)ap[bar]));
				adaptiveADX[bar] = ADX.Series(bars, Math.Max(1, (int)ap[bar]))[bar];
			}

			PlotTimeSeries(ap, "Adaptive Lookback", "ap", WLColor.Purple, PlotStyle.ThickLine);
			PlotTimeSeries(adaptiveADX, "Adaptive ADX", "adx", WLColor.Blue, PlotStyle.ThickLine);
			PlotTimeSeries(adaptiveMAH, "Upper Adaptive Band", "Price", WLColor.Red, PlotStyle.Line);
			PlotTimeSeries(adaptiveMAL, "Lower Adaptive Band", "Price", WLColor.Blue, PlotStyle.Line);

			StartIndex = 1;
		}

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

			if (!HasOpenPosition(bars, PositionType.Long))
			{
				//Adaptive ADX is declining and in consolidation mode
				if (adaptiveADX[bar] <= adaptiveADX[bar - 1] && adaptiveADX[bar] <= 25)
					if (Close[bar] <= adaptiveMAL[bar] * drop)
						PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, "Oversold").Weight = -Close[bar];
			}
			else
			{
				Position p = LastPosition;
				double stop = 0.8;

				if (Close[bar] > adaptiveMAH[bar])
					PlaceTrade(bars, TransactionType.Sell, OrderType.Market, 0, "Overbought");
				else
					ClosePosition(p, OrderType.Stop, p.EntryPrice * stop, "Stop Loss -20%");
			}
		}

		int swings;
		double drop;
		TimeSeries adaptiveMAH, adaptiveMAL, adaptiveADX;
	}
}

MaxValue
public double MaxValue

Contains the Parameter's maximum value, for optimization purposes.


MinValue
public double MinValue

Contains the Parameter's minimum value, for optimization purposes.


Name
public string Name

The descriptive name of the Parameter.


StepValue
public double StepValue

Contains the Parameter's increment, or step, value, for optimization purposes. An optimization will test parameter values starting at MinValue, going to MaxValue, in increments of StepValue.


Type
public ParameterTypes Type

The type of data that the Parameter's Value property contains. See the Enums reference for a list of possible ParameterTypes.


Value
public object Value

The current value of the Parameter. See the Enums reference for a list of possible ParameterTypes.


Values
public List<double> Values

Returns a List of double values containing the values that the Parameter can assume, considering its MinValue, MaxValue, and StepValue. If the Parameter was assigned values explicitly, for example through a call to AssignExplicitValues, then the Values property will return these values.

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

namespace WealthScript1
{
	public class MyStrategy : UserStrategyBase
	{
		public MyStrategy()
		{
			Parameter p = AddParameter("Param1", ParameterType.Int32, 20, 5, 100, 5);
		}

		//Initialize
		public override void Initialize(BarHistory bars)
		{
			Parameter p = Parameters[0];
			DrawHeaderText("Parameter Permutations: " + p.Permutations);
			string sVal = "";
			foreach (double val in p.Values)
				sVal += val + ",";
			DrawHeaderText("Parameter Values: " + sVal);
		}

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