Skip to main content
Version: v25

Studies 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 this chart of weekly data we 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.

Let's look at the example of the built-in Moving Average study: it is possible to provide a symbol input that is different to the symbol of the main study which the library will resolve and then use to calculate the moving average.

In this chart we have a symbol "A" for the main series, and a Moving Average study for symbol "B". We 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

Studies extending the time scale

There is a feature that allows studies to also define their own points on the 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 a custom study that shows bars at a higher resolution than the main series, for example a study that shows the weekly average of a monthly symbol.

Image of a monthly symbol on a chart

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

Enabling the extending time scale feature

To enable the feature for a custom study:

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

Example study

In this example 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, but this can be customised 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];
};
}
}