Skip to main content
Version: v27

Extending the time scale

The tick marks on the time scale are defined by the times in the main series.

Most studies calculate their values from the bars of the main series. The simplest example of this is the Volume study - each bar of the main series has a volume associated with it and the Volume study simply shows that volume at the same time point as the bar. Because of this it is natural that the times of study values correspond 1-to-1 with the times of the main series.

For example, on the chart of weekly data, you can see there is one Volume bar for each bar of the main series.

Image of a simple volume study on a chart

Mapping times

In some cases a study needs to calculate its values from a series of bars that are not the same as the main series. In this case it is necessary to map each study value to the time of one of the main series' bars.

Consider the example of the built-in Moving Average study. You can provide a symbol input that is different to the symbol of the main study. The library will resolve and then use this input to calculate the Moving Average.

The chart below has a symbol "A" for the main series and a Moving Average study for symbol "B". You can see 5 daily bars from 2022-12-26 to 2022-12-31.

Image of a moving average study on a chart

Although you cannot tell by looking at the chart in this example the symbol B actually has more bars than symbol A: symbol A is missing a bar for 2022-12-29.

A timeB timeShown on chart?
2022-12-262022-12-26Yes
2022-12-272022-12-27Yes
2022-12-282022-12-28Yes
2022-12-29No
2022-12-302022-12-30Yes
2022-12-312022-12-31Yes

By default studies can only display data at a time point on the chart if the main series has a bar at that time point. This is done by mapping (or "adopting") the values of the study that correspond to a time in the main series. There are two ways of mapping times from one series to another: precise and continuous.

Precise mapping

Precise mapping means that each bar time of the main series will be mapped to at most once.

Main series timeStudy timeStudy valueDisplayed value
2023-01-012022-01-011.111.11
2023-01-022022-01-021.501.50
2022-01-022.30
2023-01-032022-01-033.003.00

Continuous mapping

Continuous mapping means that each main series bar time may be mapped to more than once. If the study time does not exist in the main series then the closest time greater than the study time is used.

Main series timeStudy timeStudy valueDisplayed value
2023-01-012022-01-011.111.11
2023-01-022022-01-021.501.50
2022-01-022.303.00
2023-01-032022-01-033.003.00

Extending the time scale feature

There is a feature that allows studies to define their points on a time scale. For studies that use this feature, the restriction that the time exists in the main series is removed. This allows you to create custom studies that show bars at a higher resolution than the main series. For example, you can create a study showing the weekly average of a monthly symbol.

Image of a monthly symbol and a weekly study on a chart

There are two options available to enable the time scale extension feature:

For any custom study

To enable the feature for any custom study, follow the steps below.

  1. Enable the studies_extend_time_scale featureset.
  2. Add canExtendTimeScale: true to the study's metainfo.

In the example below, if a symbol input is chosen that contains more bars than the main series, the time scale will be extended to show those extra bars. Note that the example will resolve the other symbol with the same resolution as the main series. However, you can customize it by changing the resolution parameter.

{
name: 'Extend Time Scale Example',
metainfo: {
id: 'Extend Time Scale Example@tv-basicstudies-1',
name: 'Extend Time Scale Example',
_metainfoVersion: 51,
description: 'Extend Time Scale Example',
shortDescription: 'Extend Time Scale Example',
is_price_study: true,
defaults: {
styles: {
plot_0: {
plottype: 0,
visible: true,
color: 'orange'
},
},
inputs: {
symbol: '',
}
},
plots: [{
id: 'plot_0',
type: 'line'
}],
styles: {
plot_0: {
title: 'Plot',
},
},
inputs: [
{
id: 'symbol',
name: 'Other Symbol',
type: 'symbol',
},
],
format: { type: 'inherit' },
canExtendTimeScale: true, // important!
},
constructor:
function () {
this.init = function (ctx, inputCallback) {
this._context = ctx;

var symbol = inputCallback(0);
if (symbol !== '') {
const resolution = PineJS.Std.period(this._context);
this._context.new_sym(symbol, resolution);
}
}

this.main = function (ctx, inputCallback) {
this._context = ctx;
this._input = inputCallback;

var symbol = inputCallback(0);

if (symbol !== '') {
this._context.select_sym(1);
}

var close = this._context.new_var(PineJS.Std.close(this._context));

return [close];
};
}
}

For Overlay study

The library includes the built-in Overlay study, which allows you to display multiple symbols on a single chart for easy comparison. To make compared symbols extend the time scale, enable the secondary_series_extend_time_scale featureset. This featureset only applies to the Overlay study and enables the Allow extend time scale option within the Compare symbol dialog.

For example, when comparing BTCUSD to AAPL, you can see the points on the time scale where only BTCUSD has data. The image below shows that the upper chart allows the Overlay study to extend the time scale, while the bottom chart does not.

Extending time scale when comparing BTCUSD with AAPL