oakscriptjs 0.12.1 → 0.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1226,6 +1226,15 @@ function ohlcRows(o: Series, h: Series, l: Series, cl: Series): OhlcRow[] {
1226
1226
 
1227
1227
  const staticColor = (col: ScriptCandleColor | undefined): string | undefined => (typeof col === 'string' ? col : undefined);
1228
1228
 
1229
+ /**
1230
+ * Colours of a plotcandle / plotbar argument left out, as the PineScript compiler declares them and the chart draws
1231
+ * them (reference: body / bar #2962FF, wick #737375, border #000000; the wick and border do not follow the body).
1232
+ */
1233
+ export const DEFAULT_CANDLE_COLORS = { color: '#2962FF', wickColor: '#737375', borderColor: '#000000' } as const;
1234
+
1235
+ /** A colour argument, or the default when it is left out. */
1236
+ const orDefault = (col: ScriptCandleColor | undefined, fallback: string): ScriptCandleColor => (col === undefined ? fallback : col);
1237
+
1229
1238
  /**
1230
1239
  * PineScript `plotcandle(open, high, low, close, title?, { color, wickcolor, bordercolor, display, force_overlay })`:
1231
1240
  * candles in `result.plotCandles[id]`, the declaration in `candleConfig`. A bar with an na value has no candle.
@@ -1239,15 +1248,18 @@ export function plotcandle(
1239
1248
  options: PlotCandleOptions = {}
1240
1249
  ): void {
1241
1250
  const c = collector();
1251
+ const color = orDefault(options.color, DEFAULT_CANDLE_COLORS.color);
1252
+ const wickcolor = orDefault(options.wickcolor, DEFAULT_CANDLE_COLORS.wickColor);
1253
+ const bordercolor = orDefault(options.bordercolor, DEFAULT_CANDLE_COLORS.borderColor);
1242
1254
  const id = `plotcandle${c.candleConfig.filter((x) => x.kind === 'candle').length}`;
1243
1255
  title ??= DEFAULT_OUTPUT_TITLES.candle;
1244
1256
  c.candleConfig.push({
1245
1257
  id,
1246
1258
  kind: 'candle',
1247
1259
  title,
1248
- color: staticColor(options.color),
1249
- wickColor: staticColor(options.wickcolor),
1250
- borderColor: staticColor(options.bordercolor),
1260
+ color: staticColor(color),
1261
+ wickColor: staticColor(wickcolor),
1262
+ borderColor: staticColor(bordercolor),
1251
1263
  display: options.display,
1252
1264
  forceOverlay: options.force_overlay,
1253
1265
  ...(options.editable === false ? { editable: false } : {}),
@@ -1256,10 +1268,10 @@ export function plotcandle(
1256
1268
  const b = bars();
1257
1269
  c.plotCandles[id] = ohlcRows(open, high, low, close).map(({ i, ...ohlc }) => {
1258
1270
  const candle: PlotCandleData = { time: b[i]!.time, ...ohlc };
1259
- const color = candleColor(options.color, i);
1260
- const wickColor = candleColor(options.wickcolor, i);
1261
- const borderColor = candleColor(options.bordercolor, i);
1262
- if (color !== undefined) candle.color = color;
1271
+ const body = candleColor(color, i);
1272
+ const wickColor = candleColor(wickcolor, i);
1273
+ const borderColor = candleColor(bordercolor, i);
1274
+ if (body !== undefined) candle.color = body;
1263
1275
  if (wickColor !== undefined) candle.wickColor = wickColor;
1264
1276
  if (borderColor !== undefined) candle.borderColor = borderColor;
1265
1277
  if (options.force_overlay) candle.forceOverlay = true;
@@ -1280,13 +1292,14 @@ export function plotbar(
1280
1292
  options: PlotBarOptions = {}
1281
1293
  ): void {
1282
1294
  const c = collector();
1295
+ const barColor = orDefault(options.color, DEFAULT_CANDLE_COLORS.color);
1283
1296
  const id = `plotbar${c.candleConfig.filter((x) => x.kind === 'bar').length}`;
1284
1297
  title ??= DEFAULT_OUTPUT_TITLES.bar;
1285
1298
  c.candleConfig.push({
1286
1299
  id,
1287
1300
  kind: 'bar',
1288
1301
  title,
1289
- color: staticColor(options.color),
1302
+ color: staticColor(barColor),
1290
1303
  display: options.display,
1291
1304
  forceOverlay: options.force_overlay,
1292
1305
  ...(options.editable === false ? { editable: false } : {}),
@@ -1295,7 +1308,7 @@ export function plotbar(
1295
1308
  const b = bars();
1296
1309
  c.plotBars[id] = ohlcRows(open, high, low, close).map(({ i, ...ohlc }) => {
1297
1310
  const bar: PlotBarData = { time: b[i]!.time, ...ohlc };
1298
- const color = candleColor(options.color, i);
1311
+ const color = candleColor(barColor, i);
1299
1312
  if (color !== undefined) bar.color = color;
1300
1313
  if (options.force_overlay) bar.forceOverlay = true;
1301
1314
  return bar;