Time Frames

The Time Frames category contains methods you can use to access different time frames (such as weekly or monthly) within your Strategy.

BuyAtMarket

AddCalendarDays

int AddCalendarDays(bool interpolate)

Adds all missing calendar days to the chart data, including weekends, holidays, and any other non-trading day. Newly added bars are considered "synthetic", and these bar numbers return true when Bars.InSynthetic is called. AddCalendarDays returns the number of new bars that were added.

The value of the inserted bars depends on the interpolate parameter. If interpolate is false, the new bars assume the OHLC values of the next actual bar. If interpolate is true, the OHLC values of the new bars are calculated using linear interpolation between the previous bar and the next actual bar. Note that interpolating values will result in the bars being created based on future information (next bar's value) so be careful if using these bars in trading system development.

Remarks


Example

protected override void Execute(){
    
    if( Bars.Scale == 0 )
    {
        DrawLabel( PricePane, Bars.Count + " bars before", Color.Black );
        int added = AddCalendarDays( true );
        DrawLabel( PricePane, Bars.Count + " bars after", Color.Black );
        DrawLabel( PricePane, "Added " + added + " bars", Color.Blue );
    } else
    DrawLabel( PricePane, "Daily data required...", Color.LightCoral );
}
BuyAtMarket

RestoreScale

void RestoreScale();

Restores the data scale that the Strategy is currently operating on back to the original scale that it was invoked on.  The internal data scale can be changed by calling the various SetScale methods.

Remarks


Example

protected override void Execute(){
// Chart SMA from 30 minute compressed data on a lower scale
    SetScaleCompressed( 30 );
    DataSeries SMA10_60 = SMA.Series( Close, 10 );
    RestoreScale();
    SMA10_60 = Synchronize( SMA10_60 );
    
    PlotSeries( PricePane, SMA10_60, Color.Blue, WealthLab.LineStyle.Solid, 1 );
}
BuyAtMarket

SetScaleCompressed

void SetScaleCompressed(int barInterval);

Changes the base time scale of the Strategy to a more highly compressed intraday scale.  The context Bars is replaced with a new Bars object that is compressed to the specified barInterval.  For example, if the source data is a 5 minute chart, you can compress the data to 10, 15, or 30 minute scale (any multiple of 5).  Any indicators, and external symbols produced will also be in the compressed scale.  If you need to plot any of the compressed DataSeries or Bars, you must first expand them to the original intraday scale using the Synchronize method.  Call RestoreScale to revert the Strategy back to the original intraday time scale.

Important!

You must call RestoreScale() to return to the original time scale particularly for plotting and executing trading signals. In general, only remain in a compressed scale to create indicators and immediately revert to the base scale by calling RestoreScale().

Remarks


Example

protected override void Execute(){
    // The chart will depict 20-minute SMA and RSI 
    // on compressed and original scales

    DataSeries SMA20 = SMA.Series( Close, 20 );
    SetScaleCompressed( 15 );
    DataSeries SMA20_15 = SMA.Series( Close, 20 );
    RestoreScale();
    SMA20_15 = Synchronize( SMA20_15 );
    
    PlotSeries( PricePane, SMA20, Color.Red, WealthLab.LineStyle.Solid, 1 );
    PlotSeries( PricePane, SMA20_15, Color.Blue, WealthLab.LineStyle.Solid, 1 );

    ChartPane RSIPane = CreatePane( 50, true, true );
    SetScaleCompressed( 15 );
    DataSeries RSI20_15 = RSI.Series( Close, 20 );
    RestoreScale();
    RSI20_15 = Synchronize( RSI20_15 );
    
    PlotSeries( RSIPane, RSI.Series( Close, 20 ), Color.Red, WealthLab.LineStyle.Solid, 1 );
    PlotSeries( RSIPane, RSI20_15, Color.Blue, WealthLab.LineStyle.Solid, 1 );
}
BuyAtMarket

SetScaleDaily

void SetScaleDaily();

Changes the base time scale of the Strategy to daily, from intraday.  The context Bars is replaced with a new Bars object compressed to the daily scale.  Any indicators, and external symbols produced will also be in daily scale.  If you need to plot any of the compressed daily DataSeries or Bars, you must first expand them to the original intraday scale using the Synchronize method.  Call RestoreScale to revert the Strategy back to the original time scale.

Important!

You must call RestoreScale() to return to the original time scale particularly for plotting and executing trading signals. In general, only remain in a compressed scale to create indicators and immediately revert to the base scale by calling RestoreScale().

Remarks

  • SetScaleDaily only works on charts using intraday scaled data.
  • SetScaleDaily operates only on the standard OHLC/V DataSeries of the Bars object and does not apply to manually created and/or Named DataSeries.

Example

protected override void Execute(){
//Look for a Daily SMA CrossOver in our intraday chart
    if ( Bars.IsIntraday )
    {
        SetScaleDaily();
        DataSeries SMA1 = SMA.Series( Close, 10 );
        DataSeries SMA2 = SMA.Series( Close, 40 ); 
        RestoreScale();
        SMA1 = Synchronize( SMA1 );
        SMA2 = Synchronize( SMA2 );
        PlotSeries( PricePane, SMA1, Color.Red, WealthLab.LineStyle.Solid, 1 );
        PlotSeries( PricePane, SMA2, Color.Blue, WealthLab.LineStyle.Solid, 1 );
        for(int bar = 20; bar < Bars.Count; bar++)
        {
            if ( CrossOver( bar, SMA1, SMA2 ) )
                SetBackgroundColor( bar, Color.Blue );
        }
    }
}
BuyAtMarket

SetScaleMonthly

void SetScaleMonthly();

Changes the base time scale of the Strategy to monthly.  The context Bars is replaced with a new Bars object compressed to the monthly scale.  Any indicators, and external symbols produced will also be in monthly scale.  If you need to plot any of the compressed DataSeries or Bars, you must first expand them to the original scale using the Synchronize method.  Call RestoreScale to revert the Strategy back to the original time scale.

Important!

You must call RestoreScale() to return to the original time scale particularly for plotting and executing trading signals. In general, only remain in a compressed scale to create indicators and immediately revert to the base scale by calling RestoreScale().

Remarks

  • SetScaleMonthly operates only on the standard OHLC/V DataSeries of the Bars object and does not apply to manually created and/or Named DataSeries.
  • Known issue: Applying SetScaleMonthly to a compressed Weekly chart (source data is Daily) of an external symbol may result in an incorrect compressed data (month has more than 4 weeks). Use a workaround from this forum thread.

Example

protected override void Execute(){
    // Plot the 5 month RSI in our daily chart
    SetScaleMonthly();
    DataSeries MonthlyRSI = RSI.Series( Close, 5 );
    RestoreScale();
    MonthlyRSI = Synchronize( MonthlyRSI );
    ChartPane RSIPane = CreatePane( 50, true, true );
    PlotSeries( RSIPane, MonthlyRSI, Color.Navy, WealthLab.LineStyle.Solid, 2 );
}
BuyAtMarket

SetScaleWeekly

void SetScaleWeekly();

Changes the base time scale of the Strategy to weekly.  The context Bars is replaced with a new Bars object compressed to the weekly scale.  Any indicators, and external symbols produced will also be in weekly scale.  If you need to plot any of the compressed DataSeries or Bars, you must first expand them to the original scale using the Synchronize method.  Call RestoreScale to revert the Strategy back to the original time scale.

Important!

You must call RestoreScale() to return to the original time scale particularly for plotting and executing trading signals. In general, only remain in a compressed scale to create indicators and immediately revert to the base scale by calling RestoreScale().

Remarks

  • SetScaleWeekly operates only on the standard OHLC/V DataSeries of the Bars object and does not apply to manually created and/or Named DataSeries.

Example

protected override void Execute(){
    // Plot the weekly MACD in our daily chart
    SetScaleWeekly();
    DataSeries WeeklyMACD = MACD.Series( Close );
    RestoreScale();
    WeeklyMACD = Synchronize( WeeklyMACD );
    ChartPane MACDPane = CreatePane( 50, true, true );
    PlotSeries( MACDPane, WeeklyMACD, Color.Maroon, WealthLab.LineStyle.Histogram, 2 );
}