The TimeSeries class manages a series of numeric double values keyed to a list of DateTimes. The two primary properties of TimeSeries are:
- Values - a list of
doublevalues - DateTimes - a list of DateTime values These two lists are synchronized, so a value and DateTime at the same index represent the same point in the series. When accessing values, you can use the TimeSeries default indexer rather than explicitly accessing Values. For example:
//assume ts is an instance of a TimeSeries
double n1 = ts.Values[10];
double n2 = ts[10];
These two statements are functionally equivalent. public void WriteToBinaryFile(string fileName)
Writes the DateTimes and Values of the TimeSeries to a WealthLab binary TimeSeries file.
The resulting file can be loaded using **ReadFromBinaryFile**.
TimeSeries overloads many C# operators so calculations and comparisons can be performed directly on complete series.
Arithmetic operations return a new TimeSeries containing the result for each bar. Comparison and logical operations return TimeSeries values that can be used as Boolean series in subsequent calculations.
The parameterless constructor creates a TimeSeries that manages its own DateTimes. When adding values to this instance, use an Add method that supplies both a value and DateTime.
The second constructor associates the TimeSeries with an existing List of DateTimes. By default, its Values are initialized to Double.NaN. Pass false for fillNaN if you intend to populate the values yourself.
The third constructor associates the TimeSeries with an existing List of DateTimes and initializes every value to fillValue.
Returns a new TimeSeries containing the absolute value of each value in the source TimeSeries.
Returns the annualized percentage return of the TimeSeries, based on its TotalReturnPct and the amount of time represented by the series.
A generic cache Dictionary that can be used to store objects associated with the TimeSeries. Indicators can use the Cache to store related calculated objects and avoid performing the same calculations repeatedly.
Returns a new TimeSeries containing copies of the DateTimes and Values in the source TimeSeries.
Returns a new TimeSeries containing the number of consecutive bars that the source TimeSeries has remained above another TimeSeries or a constant value.
The count resets to zero whenever the source value is equal to or below the comparison value. Double.NaN values also reset the count.
Returns a new TimeSeries containing the number of consecutive bars that the source TimeSeries has remained below another TimeSeries or a constant value.
The count resets to zero whenever the source value is equal to or above the comparison value. Double.NaN values also reset the count.
Returns the covariance between this TimeSeries and other.
The two TimeSeries must contain the same number of values. Returns Double.NaN if they do not have the same Count or there is insufficient data.
Returns true if the TimeSeries crosses over a constant value or another TimeSeries at idx. For a crossover to occur, the source must be above the comparison value at idx and must previously have been below it. Bars where the two values are equal do not prevent detection of the crossover. When comparing two TimeSeries, they must contain the same number of values.
Returns true if the TimeSeries crosses under a constant value or another TimeSeries at idx. For a crossunder to occur, the source must be below the comparison value at idx and must previously have been above it. When comparing two TimeSeries, they must contain the same number of values.
Returns a new TimeSeries containing 1 at each point where the source TimeSeries crosses over ts, and 0 otherwise. The two TimeSeries must contain the same number of values.
Returns a new TimeSeries containing 1 at each point where the source TimeSeries crosses under ts, and 0 otherwise. The two TimeSeries must contain the same number of values.
Contains the DateTime corresponding to each value in the TimeSeries.
Assigning a new DateTimes List causes the Values of the TimeSeries to be initialized to Double.NaN.
Contains a text description of the TimeSeries. Indicators typically set this automatically. When plotting a plain TimeSeries from Strategy code, WealthLab can use the supplied plot name as its Description.
Fills all values in the TimeSeries with Double.NaN.
Returns the first index containing valid data rather than Double.NaN.
Indicators commonly contain Double.NaN in their initial values until enough source data is available to perform their calculation.
You can also assign FirstValidIndex. If the assigned index does not actually contain valid data, WealthLab determines the first valid index from the series.
Returns the highest value in the TimeSeries looking backward from bar for up to range values.
Returns Double.NaN if bar is outside the TimeSeries.
Returns the index at which the highest value was found when looking backward from bar for up to range values. Returns -1 if a valid result cannot be obtained.
Returns the lowest value in the TimeSeries looking backward from bar for up to range values.
Returns Double.NaN if bar is outside the TimeSeries.
Returns the index at which the lowest value was found when looking backward from bar for up to range values. Returns -1 if a valid result cannot be obtained.
Returns a new TimeSeries whose values contain the bar indexes at which the highest value over the specified range occurred.
Returns a new TimeSeries whose values contain the bar indexes at which the lowest value over the specified range occurred.
Determines whether WealthLab should adjust the plotted color of the TimeSeries to improve contrast with the currently selected Light or Dark Theme. New TimeSeries instances initialize this value using the current IndicatorFactory setting.
Returns the maximum drawdown, expressed as an absolute value, that occurred in the TimeSeries.
Returns the DateTime on which MaxDrawDown occurred.
Returns the maximum percentage drawdown that occurred in the TimeSeries.
Returns the DateTime on which MaxDrawDownPct occurred.
Creates and returns a TimeSeries from a string previously generated by Persist.
Returns a string representation containing the TimeSeries' DateTimes and Values. The resulting string can subsequently be restored using Parse.
Indicates that an exception occurred while the TimeSeries was being populated. WealthLab uses this property internally to prevent indicators that encountered population errors from being plotted.
Populates the TimeSeries with data from a binary file created by WriteToBinaryFile. You can optionally restrict the data using startDate, endDate, or maxBars.
Populates the TimeSeries with data from a text file.
By default, the method reads the format produced by WriteToFile. You can also read external files containing DateTime and numeric value pairs by specifying the appropriate dateTimeFormat and separator.
Numeric values are parsed using invariant culture, so a period (.) is used as the decimal separator.
The optional startDate, endDate, and maxBars parameters can restrict the amount of data loaded. skipLines can be used when reading external files containing header lines.
Returns the percentage change between the value at idx and the value corresponding to approximately days calendar days earlier.
Returns Double.NaN if the requested index or earlier DateTime cannot be located.
Returns a new TimeSeries containing the year-to-date percentage change relative to the final value of the previous calendar year.
Contains optional bar-specific colors associated with the TimeSeries.
Contains the value associated with the current partial streaming bar.
The default value is Double.NaN.
The parameterless version returns a new TimeSeries containing the cumulative sum of the source TimeSeries. The period version returns a new TimeSeries containing the rolling sum of the source values over the specified number of bars.
Returns the difference between the final and first values of the TimeSeries. Returns zero if the TimeSeries contains fewer than two values.
Returns the total return of the TimeSeries as a percentage of its first value. Returns zero if the TimeSeries contains fewer than two values.
Returns true when the TimeSeries turns downward at idx. The method considers intervening equal values, so a series that rises, remains flat for one or more bars, and then declines is considered to have turned down.
Returns true when the TimeSeries turns upward at idx. The method considers intervening equal values, so a series that declines, remains flat for one or more bars, and then rises is considered to have turned up.
Returns the sample variance of the valid values in the TimeSeries.
### WriteToBinaryFile
Writes the DateTimes and Values of the TimeSeries to a text file.
Each record contains the DateTime in yyyyMMddHHmmss format followed by the numeric value. Values are written using invariant culture.
The resulting file can be loaded using ReadFromFile.
Compare the TimeSeries values to another TimeSeries or constant value and return a new TimeSeries representing the Boolean result. These Boolean TimeSeries can be combined with logical operators or passed to methods such as BarsSince and BooleanTest.
Combine Boolean TimeSeries using logical AND or OR operations. For example:
TimeSeries aboveBoth = bars.Close > SMA.Series(bars.Close, 200) & bars.Close > SMA.Series(bars.Close, 50);
Performs subtraction between two TimeSeries instances or between a TimeSeries and a constant value.
Performs multiplication between two TimeSeries instances or between a TimeSeries and a constant value.
Performs division between two TimeSeries instances or between a TimeSeries and a constant value. When division by zero occurs, the corresponding result is zero.
Performs addition between two TimeSeries instances or between a TimeSeries and a constant value.
Returns a new TimeSeries shifted to the right by period bars. This effectively delays the information in the TimeSeries, making the shifted series safe for use in backtesting. For example, when shifting a series one bar to the right, the value at index X comes from index X-1 of the original series.
Returns a new TimeSeries containing the number of bars since the most recent positive value in s. This is particularly useful with TimeSeries generated by WealthLab's comparison operators.
Returns a new TimeSeries whose value is taken from resultTrue when the corresponding value in s is greater than zero, and from resultFalse otherwise.
Returns a new TimeSeries containing the natural logarithm of each value in s.
Returns a new TimeSeries containing the highest value in s over the specified rolling period.
Returns a new TimeSeries containing the lowest value in s over the specified rolling period.
Returns a new TimeSeries containing each value in s raised to the specified power.
Returns a new TimeSeries containing the values in s rounded using Math.Round.
Returns a new TimeSeries containing the square root of each valid, non-zero value in s.
Returns a new TimeSeries containing the rolling sum of s over the specified length. This is equivalent to calling:
s.Sum(length);
Returns a new TimeSeries containing the kurtosis of the source TimeSeries over the specified period. Kurtosis describes characteristics of the distribution of values, particularly the weight of its tails relative to a normal distribution.
Returns a new TimeSeries containing the median absolute deviation over the specified period. Median absolute deviation is a robust measure of variability that is less affected by extreme values than standard deviation.
Returns a new TimeSeries containing the percent rank of each value relative to the values within the specified period.
Returns a new TimeSeries containing the skewness of the source TimeSeries over the specified period. Skewness measures the asymmetry of the distribution of values.