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.



Constructor
Parameter
public Parameter(string name, ParameterTypes type, object value)
public Parameter(string name, ParameterTypes type, object value, double minValue, double maxValue)

The Parameter class has two constructors. Both provide parameters to establish the Parameter's name, type, and initial value. The second constructor allows you to also specify a minValue and maxValue.

In a C# Coded Strategy, you generally do not have to create a Parameter instance directly using a constructor. Rather, 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
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", ParameterTypes.Int32, 6, 1, 20, 1);
			p.Hint = "The swings parameter in the AdaptiveLookback indicator";
			p = AddParameter("Drop %", ParameterTypes.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", Color.Purple, PlotStyles.ThickLine);
			PlotTimeSeries(adaptiveADX, "Adaptive ADX", "adx", Color.Blue, PlotStyles.ThickLine);
			PlotTimeSeries(adaptiveMAH, "Upper Adaptive Band", "Price", Color.Red, PlotStyles.Line);
			PlotTimeSeries(adaptiveMAL, "Lower Adaptive Band", "Price", Color.Blue, PlotStyles.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.