Skip to content

Commit 6c917de

Browse files
committed
Merge branch 'api/plots' into dev
2 parents 859f80a + e4dea13 commit 6c917de

File tree

9 files changed

+200
-19
lines changed

9 files changed

+200
-19
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# Change Log
22

3+
## [0.7.6] - 2025-12-30 - Additional Plot Functions
4+
5+
### Added
6+
7+
- **Plot Functions**: Added support for additional Pine Script plot functions:
8+
- `plotbar()` - Renders OHLC data as traditional bar charts with horizontal ticks
9+
- `plotcandle()` - Renders OHLC data as candlesticks with filled bodies and wicks
10+
- `bgcolor()` - Fills the chart background with colors based on conditions
11+
- `barcolor()` - Colors the main chart candlesticks based on indicator conditions
12+
13+
### Changed
14+
15+
- Enhanced `Plots` namespace with support for OHLC array values and color application to main chart
16+
- Updated API coverage documentation to reflect new plot functions
17+
318
## [0.7.5] - 2025-12-29 - UDT Support
419

520
### Added

docs/api-coverage/pinescript-v6/plots.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"Plots": {
33
"plot()": true,
44
"plotarrow()": true,
5-
"plotbar()": false,
6-
"plotcandle()": false,
5+
"plotbar()": true,
6+
"plotcandle()": true,
77
"plotchar()": true,
88
"plotshape()": true,
9-
"barcolor()": false,
9+
"barcolor()": true,
1010
"bgcolor()": true
1111
}
1212
}

docs/api-coverage/plots.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ parent: API Coverage
1111
| `plot()` || Plot a series |
1212
| `plotchar()` || Plot character markers |
1313
| `plotarrow()` || Plot arrow markers |
14-
| `plotbar()` | | Plot bar chart |
15-
| `plotcandle()` | | Plot candlestick chart |
14+
| `plotbar()` | | Plot bar chart |
15+
| `plotcandle()` | | Plot candlestick chart |
1616
| `plotshape()` || Plot shape markers |
17-
| `barcolor()` | | Set bar color |
17+
| `barcolor()` | | Set bar color |
1818
| `bgcolor()` || Set background color |

src/Context.class.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export class Context {
165165

166166
const plotHelper = new PlotHelper(this);
167167
const hlineHelper = new HlineHelper(this);
168-
this.bindContextObject(plotHelper, ['plotchar', 'plotshape', 'plotarrow']);
168+
this.bindContextObject(plotHelper, ['plotchar', 'plotshape', 'plotarrow', 'plotbar', 'plotcandle', 'bgcolor', 'barcolor']);
169169
this.bindContextObject(
170170
plotHelper,
171171
[

src/namespaces/Plots.ts

Lines changed: 122 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,25 @@ const PLOT_ARROW_SIGNATURE = [
1919
'editable', 'show_last', 'display', 'format', 'precision', 'force_overlay',
2020
];
2121

22+
//prettier-ignore
23+
const PLOTBAR_SIGNATURE = [
24+
'open', 'high', 'low', 'close', 'title', 'color', 'editable', 'show_last', 'display', 'format', 'precision', 'force_overlay',
25+
];
26+
27+
//prettier-ignore
28+
const PLOTCANDLE_SIGNATURE = [
29+
'open', 'high', 'low', 'close', 'title', 'color', 'wickcolor', 'editable', 'show_last', 'bordercolor', 'display', 'format', 'precision', 'force_overlay',
30+
]
31+
//prettier-ignore
32+
const BGCOLOR_SIGNATURE = [
33+
'color', 'offset', 'editable', 'show_last', 'title', 'display', 'force_overlay',
34+
];
35+
36+
//prettier-ignore
37+
const BARCOLOR_SIGNATURE = [
38+
'color', 'offset', 'editable', 'show_last', 'title', 'display'
39+
];
40+
2241
//prettier-ignore
2342
const PLOT_ARGS_TYPES = {
2443
series: 'series', title: 'string', color: 'string', linewidth: 'number',
@@ -43,6 +62,33 @@ const PLOT_ARROW_ARGS_TYPES = {
4362
format: 'string', precision: 'number', force_overlay: 'boolean',
4463
};
4564

65+
//prettier-ignore
66+
const PLOTBAR_ARGS_TYPES = {
67+
open: 'series', high: 'series', low: 'series', close: 'series',
68+
title: 'string', color: 'string', editable: 'boolean', show_last: 'number', display: 'string',
69+
format: 'string', precision: 'number', force_overlay: 'boolean',
70+
};
71+
72+
//prettier-ignore
73+
const PLOTCANDLE_ARGS_TYPES = {
74+
open: 'series', high: 'series', low: 'series', close: 'series',
75+
title: 'string', color: 'string', wickcolor: 'string', bordercolor: 'string',
76+
editable: 'boolean', show_last: 'number', display: 'string',
77+
format: 'string', precision: 'number', force_overlay: 'boolean',
78+
};
79+
80+
//prettier-ignore
81+
const BGCOLOR_ARGS_TYPES = {
82+
color: 'string', offset: 'number', editable: 'boolean', show_last: 'number',
83+
title: 'string', display: 'string', force_overlay: 'boolean',
84+
};
85+
86+
//prettier-ignore
87+
const BARCOLOR_ARGS_TYPES = {
88+
color: 'string', offset: 'number', editable: 'boolean', show_last: 'number',
89+
title: 'string', display: 'string',
90+
};
91+
4692
export class PlotHelper {
4793
constructor(private context: any) {}
4894
private extractPlotOptions(options: PlotCharOptions | PlotShapeOptions) {
@@ -102,16 +148,19 @@ export class PlotHelper {
102148

103149
//in the current implementation, plot functions are only used to collect data for the plots array and map it to the market data
104150
plotchar(...args) {
105-
// if (!this.context.plots[title]) {
106-
// this.context.plots[title] = { data: [], options: this.extractPlotOptions(options), title };
107-
// }
108-
// const value = Series.from(series).get(0);
109-
// this.context.plots[title].data.push({
110-
// time: this.context.marketData[this.context.idx].openTime,
111-
// value: value,
112-
// options: { ...this.extractPlotOptions(options), style: 'char' },
113-
// });
114-
this.any(...args);
151+
const _parsed = parseArgsForPineParams<PlotOptions>(args, PLOT_SIGNATURE, PLOT_ARGS_TYPES);
152+
const { series, title, ...others } = _parsed;
153+
const options = this.extractPlotOptions(others);
154+
if (!this.context.plots[title]) {
155+
this.context.plots[title] = { data: [], options: { ...options, style: 'char' }, title };
156+
}
157+
158+
const value = Series.from(series).get(0);
159+
160+
this.context.plots[title].data.push({
161+
time: this.context.marketData[this.context.idx].openTime,
162+
value: value,
163+
});
115164
}
116165

117166
//this will map to plot() - see README.md for more details
@@ -184,6 +233,69 @@ export class PlotHelper {
184233
: undefined,
185234
});
186235
}
236+
237+
plotbar(...args) {
238+
const _parsed = parseArgsForPineParams<PlotBarOptions>(args, PLOTBAR_SIGNATURE, PLOTBAR_ARGS_TYPES);
239+
const { open, high, low, close, title, ...others } = _parsed;
240+
const options: PlotBarOptions = this.extractPlotOptions(others);
241+
if (!this.context.plots[title]) {
242+
this.context.plots[title] = { data: [], options: { ...options, style: 'bar' }, title };
243+
}
244+
245+
const value = [Series.from(open).get(0), Series.from(high).get(0), Series.from(low).get(0), Series.from(close).get(0)];
246+
247+
this.context.plots[title].data.push({
248+
time: this.context.marketData[this.context.idx].openTime,
249+
value: value,
250+
options: { color: options.color },
251+
});
252+
}
253+
254+
plotcandle(...args) {
255+
const _parsed = parseArgsForPineParams<PlotCandleOptions>(args, PLOTCANDLE_SIGNATURE, PLOTCANDLE_ARGS_TYPES);
256+
const { open, high, low, close, title, ...others } = _parsed;
257+
const options: PlotCandleOptions = this.extractPlotOptions(others);
258+
if (!this.context.plots[title]) {
259+
this.context.plots[title] = { data: [], options: { ...options, style: 'candle' }, title };
260+
}
261+
262+
const value = [Series.from(open).get(0), Series.from(high).get(0), Series.from(low).get(0), Series.from(close).get(0)];
263+
264+
this.context.plots[title].data.push({
265+
time: this.context.marketData[this.context.idx].openTime,
266+
value: value,
267+
options: { color: options.color, wickcolor: options.wickcolor, bordercolor: options.bordercolor },
268+
});
269+
}
270+
271+
bgcolor(...args) {
272+
const _parsed = parseArgsForPineParams<BackgroundColorOptions>(args, BGCOLOR_SIGNATURE, BGCOLOR_ARGS_TYPES);
273+
const { title, ...others } = _parsed;
274+
const options: BackgroundColorOptions = this.extractPlotOptions(others);
275+
if (!this.context.plots[title]) {
276+
this.context.plots[title] = { data: [], options: { ...options, style: 'background' }, title };
277+
}
278+
279+
this.context.plots[title].data.push({
280+
time: this.context.marketData[this.context.idx].openTime,
281+
value: options.color && options.color !== 'na' && options?.color.toString() !== 'NaN',
282+
options: { color: options.color },
283+
});
284+
}
285+
barcolor(...args) {
286+
const _parsed = parseArgsForPineParams<BarColorOptions>(args, BGCOLOR_SIGNATURE, BGCOLOR_ARGS_TYPES);
287+
const { title, ...others } = _parsed;
288+
const options: BarColorOptions = this.extractPlotOptions(others);
289+
if (!this.context.plots[title]) {
290+
this.context.plots[title] = { data: [], options: { ...options, style: 'barcolor' }, title };
291+
}
292+
293+
this.context.plots[title].data.push({
294+
time: this.context.marketData[this.context.idx].openTime,
295+
value: options.color && options.color !== 'na' && options?.color.toString() !== 'NaN',
296+
options: { color: options.color },
297+
});
298+
}
187299
}
188300

189301
export class HlineHelper {

src/namespaces/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export type PineTypeMap<T> = {
3838
* @param types - The types to parse, each type is a string representing the type of the argument.
3939
* @returns The parsed arguments, the arguments are parsed according to the signatures and types.
4040
*/
41-
export function parseArgsForPineParams<T>(args: any[], signatures: any[], types: Record<string, string>) {
41+
export function parseArgsForPineParams<T>(args: any[], signatures: any[], types: Record<string, string>, override?: Record<string, any>) {
4242
if (Array.isArray(signatures) && typeof signatures[0] === 'string') {
4343
signatures = [signatures];
4444
}
@@ -73,5 +73,5 @@ export function parseArgsForPineParams<T>(args: any[], signatures: any[], types:
7373
}
7474
}
7575

76-
return { ...options_arg, ...options };
76+
return { ...options_arg, ...options, ...override };
7777
}

src/transpiler/settings.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ export const CONTEXT_PINE_VARS = [
1818
'plotchar',
1919
'plotshape',
2020
'plotarrow',
21+
'plotbar',
22+
'plotcandle',
2123
'plot',
24+
'bgcolor',
25+
'barcolor',
2226
'hline',
2327

2428
//declarations

src/types/PineTypes.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,54 @@ type PlotArrowOptions = {
6969
force_overlay?: boolean;
7070
};
7171

72+
type PlotBarOptions = {
73+
open: number;
74+
high: number;
75+
low: number;
76+
close: number;
77+
title?: string;
78+
color?: string;
79+
editable?: boolean;
80+
show_last?: number;
81+
display?: string;
82+
format?: string;
83+
precision?: number;
84+
force_overlay?: boolean;
85+
};
86+
87+
type BackgroundColorOptions = {
88+
color?: string;
89+
offset?: number;
90+
editable?: boolean;
91+
show_last?: number;
92+
title?: string;
93+
display?: string;
94+
force_overlay?: boolean;
95+
};
96+
type BarColorOptions = {
97+
color?: string;
98+
offset?: number;
99+
editable?: boolean;
100+
show_last?: number;
101+
title?: string;
102+
display?: string;
103+
};
104+
type PlotCandleOptions = {
105+
open: number;
106+
high: number;
107+
low: number;
108+
close: number;
109+
title?: string;
110+
color?: string;
111+
wickcolor?: string;
112+
bordercolor?: string;
113+
editable?: boolean;
114+
show_last?: number;
115+
display?: string;
116+
format?: string;
117+
precision?: number;
118+
force_overlay?: boolean;
119+
};
72120
type IndicatorOptions = {
73121
title: string;
74122
shorttitle: string;

tests/indicators/Institutional_Bias.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ describe('Indicators', () => {
1717
const bull_bias = ema9 > ema18;
1818
const bear_bias = ema9 < ema18;
1919

20+
bgcolor(bear_bias ? '#FFFFFF' : '#000000', 'Bear Bias');
21+
2022
return {
2123
bull_bias,
2224
bear_bias,

0 commit comments

Comments
 (0)