DataSeries Object

DataSeries(string description)
DataSeries(Bars bars, string description)
DataSeries(DataSeries source, string description)

The DataSeries object represents a series of values (double type) with corresponding dates.  You can perform mathematical operations on DataSeries as if they were primary types:

//Get average of high and low price
DataSeries avg = (High + Low) / 2;

You can also create offset copies of a DataSeries by using the shift operators, >> and <<:

//Shift a moving average 2 bars to the right
DataSeries shiftedMA = SMA.Series(Close, 14) >> 2;

The most practical use of the DataSeries function is to return a new zero-filled DataSeries that has the same number of elements as the Bars parameter. Use the result to fill the series bar by bar with calculated values:

protected override void Execute()
{    
    // Create a zero-filled series
    DataSeries binSeries = new DataSeries(Bars, "Binary Series");
    // Fill the series with values based on some logic
    for(int bar = 1; bar < Bars.Count; bar++)
    {
        if( High[bar] > High[bar-1] )
            binSeries[bar] = 1;
        else if( Low[bar] < Low[bar - 1] )
            binSeries[bar] = -1;
    }            
    ChartPane cp = CreatePane(40, true, false);
    PlotSeries(cp, binSeries, Color.Black, LineStyle.Histogram, 20);
}
BuyAtMarket

[] indexer Property

double this[int i]

Allows you to access one of the values in the DataSeries.  The DataSeries contains a number of values, indexed between 0 and Count - 1.  Provide the index number of the value you want to access in the i parameter.

//Access the first and last values of a DataSeries (ds)
double firstValue = ds[0];
double lastValue = ds[ds.Count - 1];

BuyAtMarket

Abs

static DataSeries Abs(DataSeries source);

This class level (static) method returns a new DataSeries that is the absolute value of the specified source DataSeries.


Example

protected override void Execute(){    //Return the absolute value of Rate of Change
    //DataSeries roc = ROC.Series( Close, 20 );
    DataSeries absRoc = DataSeries.Abs( ROC.Series( Close, 20 ) );
    DrawLabel( PricePane, "Abs(ROC) value on last bar: " + String.Format( "{0:f}", absRoc[absRoc.Count-1] ), Color.RoyalBlue );
}
BuyAtMarket

Count Property

int Count

Returns the number of values contained in the DataSeries.  The values are accessed by index, starting at index 0 and ending at index Count - 1.


Example

protected override void Execute(){
    for(int bar = 20; bar < Bars.Count; bar++)
    {
        // Your trading system rules
    }
}
BuyAtMarket

Date Property

IList<DateTime> Date

Returns the list of DateTimes that are associated with the values contained in the DataSeries.  The number of DateTimes in the Date list is always equal to the number of values contained in the DataSeries.

//Access the first and last dates contained in the DataSeries (ds)
DateTime dtFirst = ds.Date[0];
DateTime dsLast = ds.Date[ds.Count - 1];


Example

protected override void Execute(){
    //Access the first and last dates
    DateTime firstDate     = Date[0];
    DateTime lastDate     = Date[Bars.Count-1];
    DrawLabel( PricePane, "First trading date: " + String.Format( "{0:d}", firstDate ), Color.Black );
    DrawLabel( PricePane, "Last trading date: " + String.Format( "{0:d}", lastDate ), Color.Black );
}
BuyAtMarket

Description Property

string Description

Represents the description associated with the DataSeries.  You can change the description by assigning a different string value to this property.  The Description is shown as a label in charts when the DataSeries is plotted, and appears in the tooltip that is visible when you move the mouse over an indicator on the chart.


Example

protected override void Execute(){
    // Average price series
    DataSeries average = (High + Low) / 2;
    average.Description = "Average Price" ;
    PrintDebug ( average.Description );
}
BuyAtMarket

FirstValidValue Property

int FirstValidValue

Returns the bar number of the first "valid" value contained in the DataSeries.  When an indicator (all indicators are DataSeries) is plotted, the plotting actually begins at the FirstValidValue.  For previous bars, the indicator is not plotted.  For example, the FirstValidValue of a 30 bar moving average would be bar number 29.


Example

protected override void Execute(){
    // Some price series
    DataSeries mySeries = SMA.Series( Close, 100 );
    PlotSeries( PricePane, mySeries, Color.Blue, WealthLab.LineStyle.Solid, 2 );
    ClearDebug();
    PrintDebug( mySeries.FirstValidValue );
    
    // Sets the trading loop to the first "valid" value of the DataSeries
    for(int bar = mySeries.FirstValidValue; bar < Bars.Count; bar++)
    {
        if (IsLastPositionActive)
        {
            if( CrossUnder( bar, High, mySeries[bar] ) )
                SellAtMarket( bar+1, LastPosition, "Exit Long" );
        }
        else
        {
            if( CrossOver( bar, Low, mySeries[bar] ) )
            BuyAtMarket( bar+1, "Enter Long" );
        }
    }
}
BuyAtMarket

MaxValue Property

double MaxValue

Returns the maximum (highest) value that exists in the entire DataSeries.


Example

protected override void Execute(){
    // Show highest price
    DataSeries mySeries = Highest.Series( High, 1 );
    PrintDebug( "Highest price registered = " + mySeries.MaxValue );
}
BuyAtMarket

MinValue Property

double MinValue

Returns the minimum (lowest) value that exists in the entire DataSeries.


Example

protected override void Execute(){
    // Show lowest price
    DataSeries mySeries = Lowest.Series( Low, 1 );
    PrintDebug( "Lowest price registered = " + mySeries.MinValue );
}
BuyAtMarket

Partial Property

double PartialValue

Contains the value based on the partial bar in a streaming chart.  In a streaming chart, a partial bar is visible at the far right end of the chart, containing the partial values for open, high, low, and close.  Certain indicators can also update based on partial values.

Remarks

  • If the DataSeries does not have a partial value available, the PartialValue property returns Double.NaN (not a number).

Example

protected override void Execute(){
// How to access the opening price of an incomplete (Ghost) bar
    // Helpful when trading gaps and in Opening Range Breakout strategies etc.
    
    // Run this Strategy on Daily scale shortly after market opens in Streaming mode
    // Your Streaming data provider should support partial values (e.g. Fidelity, Yahoo)
    
    if( Bars.Scale != BarScale.Daily )    {
        DrawLabel(PricePane, "To be used on Daily scale", Color.Red);
        return;
    }

    if (!IsStreaming)    {
        DrawLabel( PricePane, "Enable Streaming first", Color.Red);
    }
    else {
        for (int bar = 1; bar < Bars.Count; bar++)
        {
            double OpenPrice = (bar < Bars.Count - 1 ? Open[bar + 1] : Open.PartialValue);
            
            // Open.PartialValue equals double.NaN if streaming is not enabled
            if (OpenPrice == double.NaN)
                continue;

            if( bar == Bars.Count-1 )
                DrawLabel( PricePane, OpenPrice.ToString() );
        }
    }
}