Multibars

Description

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 TradersYardX.API;
using TradersYardX.Custom;
using TradersYardX.Plugins;
using TradersYardX.Helper;
namespace TradersYardX.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.
Additional syntax methods are available for multi bars:
// 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];
Copy link
On this page
Description
Example
Additional Notes