Multibars

Multibars

An indicator or a strategy will always have the same underlying timeframe-units as those units being displayed within the chart. The values of an SMA(14) indicator displayed in a 5 minute chart will be calculated based on the last fourteen 5 minute bars. A daily chart, on the other hand, would use the closing prices of the past 14 days in order to calculate this value. The same method applies for your self-programmed indicators. A 5 minute chart will call up the OnCalculate() for each 5 minute bar. If you want your self-created indicator to use a different timeframe, this is possible using multibars.

Example

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;
using AgenaTrader.API;
using AgenaTrader.Custom;
using AgenaTrader.Plugins;
using AgenaTrader.Helper;
namespace AgenaTrader.UserCode
{
    [Description("Multibar Demo")]
    // The indicator requires daily and weekly data
    [TimeFrameRequirements("1 Day", "1 Week")]
    public class MultiBarDemo : UserIndicator
    {
        private static readonly TimeFrame TF_Day = new TimeFrame(DatafeedHistoryPeriodicity.Day, 1);
        private static readonly TimeFrame TF_Week = new TimeFrame(DatafeedHistoryPeriodicity.Week, 1);

        protected override void OnBarsRequirements()
        {
            Add(TF_Day);
            Add(TF_Week);
        }

        protected override void OnInit()
        {
            CalculateOnClosedBar = true;
        }
        protected override void OnCalculate()
        {
            // The current value for the SMA 14 in the timeframe of the chart
            Print("TF0: " + SMA(Closes[0], 14)[0]);
            // The current value for the SMA 14 in a daily timeframe
            Print("TF1: " + SMA(Closes[1], 14)[0]);
            // Current value for the SMA 14 in a weekly timeframe
            Print("TF2: " + SMA(Closes[2], 14)[0]);
        }
    }
}

Additional Notes

When using additional timeframes, a further entry with the respective data series for the bars of the new timeframe will be added to the arrays Opens, Highs, Lows, Closes, Medians, Typicals, Weighteds, Times and Volumes. The indexing will occur in the order of the addition of the new timeframes. Closes[0][0] is equivalent to Close[0]. Closes[1][0] equals the current closing price for the daily data series Closes[2][0] equals the current closing price for the weekly data series

"Closes" is, of course, interchangeable with Opens, Highs, Lows etc.

See ProcessingBarIndexes, ProcessingBarSeriesIndex, TimeFrames.

Additional syntax methods are available for multibars:

// Declare the variable TF_DAY and define it
private static readonly TimeFrame TF_Day = new TimeFrame(DatafeedHistoryPeriodicity.Day, 1);
private static readonly TimeFrame TF_Week = new TimeFrame(DatafeedHistoryPeriodicity.Week, 1);
// The following instruction is identical to double d = Closes[1][0];
double d = MultiBars.GetBarsItem(TF_Day).Close[0];
// The following instruction is identical to double w = Closes[2][0];
double w = MultiBars.GetBarsItem(TF_Week).Close[0];

ProcessingBarIndexes

Description

ProcessingBarIndexes is an array of int values that contains the number of ProcessingBarIndex for each bar.

This array is only of value for indicators or strategies that use data from multiple timeframes.

A new entry is added to the array whenever a new timeframe is added to an indicator or strategy.

With [TimeFrameRequirements(("1 Day"), ("1 Week"))] the array will contain 3 entries:

ProcessingBarIndexes[0] Current bar for the primary data series (chart timeframe) ProcessingBarIndexes[1] Current bar for the daily bars ProcessingBarIndexes[2] Current bar for the weekly bars

ProcessingBarIndexes[0] is equivalent to ProcessingBarIndex.

Also see MultiBars.

Parameter

barSeriesIndex Index value for the various timeframes

Usage

ProcessingBarIndexes[int barSeriesIndex]

Example

//Ensure that a minimum of 20 bars is loaded
for (int i=0; i<ProcessingBarIndexes.Count; i++)
if (ProcessingBarIndexes[i] < 20) return;

ProcessingBarSeriesIndex

Description

Within a multibars script, multiple bars objects are available. The OnCalculate() method will therefore also be called up for every bar within your script. In order to include/exclude events of specific data series, you can use the ProcessingBarSeriesIndex property.

ProcessingBarSeriesIndex is only of value for indicators or strategies that use data from multiple timeframes. With [TimeFrameRequirements("1 Day", "1 Week")] two timeframes will be added to the primary chart timeframe.

If OnCalculate() is called up by the primary data series, then ProcessingBarSeriesIndex will equal zero. If OnCalculate() is called up by the daily bars, then ProcessingBarSeriesIndex will equal 1. Weekly bars will have a value of 2.

See Multibars and ProcessingBarIndexes.

Parameter

none

Usage

ProcessingBarSeriesIndex

More Information

Within a script that only works with primary timeframes, the value will always equal zero.

Example

// To demonstrate the methodology
// set CalculateOnClosedBar=false
Print(Time[0] + " " + ProcessingBarSeriesIndex);
// Calculate only for the chart timeframe
protected override void OnCalculate()
{
if (ProcessingBarSeriesIndex > 0) return;
// Logic for the primary data series
}

Last updated