Search Framework:
HistoryScale
Namespace: WealthLab.Core
Parent: Object

The HistoryScale class represents a historical data scale, such as daily, weekly, monthly, or a 5-minute intraday scale. It implements the IComparable interface, allowing HistoryScale instances to be compared according to their relative granularity.

Constructors
HistoryScale
public HistoryScale()
public HistoryScale(Frequency scale, int interval = 1)

The parameterless constructor creates an uninitialized HistoryScale instance. The second constructor assigns the Frequency and Interval properties. The scale parameter specifies the Frequency, while interval specifies the interval for scales that use one, such as Minute, Hour, Second, Tick, Volume, and NDays.



Members
Abbreviation
public string Abbreviation

Returns an abbreviated description of the scale suitable for charting and other compact displays. Examples include "(D)" for Daily, "(W)" for Weekly, "(M5)" for 5-minute, "(H2)" for 2-hour, and "W(Monday)" for a WeeklyStartDay scale.


AddInterval
public DateTime AddInterval(DateTime dt)

Returns the DateTime that results from adding one interval of this HistoryScale to dt. For example, a Daily scale adds one day, a Quarterly scale adds three months, and a 5-minute scale adds five minutes.


AddLeadBars
public DateTime AddLeadBars(DateTime dt, int leadBars)

Returns a DateTime adjusted backward by the number of bars specified in leadBars, using the current HistoryScale. This can be used to determine an earlier starting date when additional lead bars are required.


Clone
public HistoryScale Clone()

Returns a new HistoryScale instance having the same Frequency, Interval, and FilterPrePost values as this instance.


Description
public string Description

Returns a description of the scale. Scales that use an interval include the interval in the description, for example "5 Minute", "2 Hour", or "1000 Volume". Other scales return the name of their Frequency.

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
    {
        public override void Initialize(BarHistory bars)
        {
            DrawHeaderText("The current scale is: " + bars.Scale.Description);
        }
        public override void Execute(BarHistory bars, int idx)
        {
        }
    }
}

DivisibleBy
public bool DivisibleBy(HistoryScale scale)

Returns whether this HistoryScale is divisible by the specified scale. If the two scales have different Frequency values, the method returns true. If they have the same Frequency, the method returns true when this instance's Interval is evenly divisible by the specified scale's Interval.


FilterPrePost
public bool FilterPrePost

Determines whether pre-market and post-market data should be filtered from intraday historical data. The default value is true.


FormatDateTime
public string FormatDateTime(DateTime dt)

Formats the specified DateTime according to this HistoryScale. Intraday scales include the time, yearly and higher scales display the year, monthly scales display the month and year, and other scales use a short date representation.


Frequency
public Frequency Frequency

The Frequency represented by this HistoryScale. Supported Frequency values include intraday and higher scales such as Tick, Volume, Second, Minute, Hour, Daily, NDays, Weekly, WeeklyStartDay, BiWeekly, Monthly, Quarterly, SemiAnnually, and Yearly.

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
    {
        public override void Initialize(BarHistory bars)
        {
            DrawHeaderText("Bar Frequency = " + bars.Scale.Frequency);
            DrawHeaderText("Bar Interval = " + bars.Scale.Interval);
        }
        public override void Execute(BarHistory bars, int idx)
        {
        }
    }
}

FromString
public static HistoryScale FromString(string s)

Creates a HistoryScale from its string representation. Recognized values include scales such as "Daily", "Weekly", "Monthly", "Quarterly", "SemiAnnually", and "Yearly", as well as interval-based descriptions such as "5 Minute", "2 Hour", "1000 Volume", and "3 Day". WeeklyStartDay values such as "Weekly(Monday)" are also supported. Returns null if s cannot be converted to a valid HistoryScale.


GetRangeBarCount
public int GetRangeBarCount(DateTime startDate, DateTime endDate)

Returns an estimated maximum number of bars that can occur between startDate and endDate, assuming a 24/7 market. This method supports Daily, Weekly, WeeklyStartDay, Monthly, Quarterly, SemiAnnually, Yearly, and Minute scales. It throws a NotSupportedException for unsupported scales.


GetRangeStartDate
public DateTime GetRangeStartDate(DateTime endDate, int numBars)

Returns an estimated starting DateTime for a range ending at endDate and containing numBars bars. This method supports Daily, Weekly, WeeklyStartDay, Monthly, Quarterly, SemiAnnually, Yearly, and Minute scales. It throws a NotSupportedException for unsupported scales.


GetSymbolKeyString
public static string GetSymbolKeyString(string symbol, HistoryScale scale, bool includeInterval = true)

Returns a string containing the specified symbol and scale, separated by a semicolon. If includeInterval is false, only the Frequency portion of the scale is included.


Interval
public int Interval

Represents the interval for scales that use an interval component, such as Minute, Hour, Second, Tick, Volume, and NDays.

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
    {
        public override void Initialize(BarHistory bars)
        {
            DrawHeaderText("Bar Scale = " + bars.Scale);
            DrawHeaderText("Bar Interval = " + bars.Scale.Interval);
        }
        public override void Execute(BarHistory bars, int idx)
        {
        }
    }
}

IsIntraday
public bool IsIntraday

Returns true if this HistoryScale represents an intraday scale. Intraday scales are Tick, Volume, Second, Minute, and Hour.

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
    {
        public override void Initialize(BarHistory bars)
        {
            if (bars.Scale.IsIntraday)
                DrawHeaderText("Intraday Chart!", WLColor.Red);
        }
        public override void Execute(BarHistory bars, int idx)
        {
        }
    }
}

IsTickBased
public bool IsTickBased

Returns true if the HistoryScale is based on individual market activity rather than elapsed time. This is true for Tick and Volume scales.


IsTimeBased
public bool IsTimeBased

Returns true if the HistoryScale is time based. Time-based scales include Second, Minute, Hour, Daily, NDays, Weekly, WeeklyStartDay, Monthly, Quarterly, SemiAnnually, and Yearly.


Parse
public static HistoryScale Parse(string s)

Creates a HistoryScale from its persisted string representation.


Persist
public string Persist()

Returns a persisted string representation of the HistoryScale, including its Frequency, Interval, and FilterPrePost values.


SubtractIntervals
public DateTime SubtractIntervals(DateTime dt, int count)

Returns the DateTime that results from subtracting count intervals of this HistoryScale from dt.


ToString
public override string ToString()

Returns the Description of the HistoryScale.


UsesIntervalForSort
public bool UsesIntervalForSort

Returns true when the Interval property is used when comparing and sorting HistoryScale instances. This is true for intraday scales and NDays.



Static Helpers
Predefined Scales
public static HistoryScale Daily
public static HistoryScale Weekly
public static HistoryScale BiWeekly
public static HistoryScale Monthly
public static HistoryScale Quarterly
public static HistoryScale SemiAnnually
public static HistoryScale Yearly
public static HistoryScale Minute1
public static HistoryScale Minute5
public static HistoryScale Minute10
public static HistoryScale Minute15
public static HistoryScale Minute30
public static HistoryScale Minute60

These predefined HistoryScale instances provide convenient shortcuts for commonly used scales. For example, HistoryScale.Daily represents a daily scale, while HistoryScale.Minute5 represents a 5-minute scale.