@barchart/chart-lib 2.201.0 → 2.201.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.
- chart-lib/barchart.chart.d.ts +2 -0
- chart-lib/barchart.chart.js +1 -1
- chart-lib/index.js +133 -8
- chart-lib/package.json +1 -1
- chart-lib/schemas/chart.schema.json +1861 -0
- chart-lib/schemas/fields.schema.json +60 -0
- chart-lib/schemas/studies.schema.json +103 -0
chart-lib/index.js
CHANGED
|
@@ -11,6 +11,10 @@ import {
|
|
|
11
11
|
Topics,
|
|
12
12
|
SeriesKind,
|
|
13
13
|
OHLCDataPointBuilder,
|
|
14
|
+
StudySeries,
|
|
15
|
+
linReg,
|
|
16
|
+
convertSeries,
|
|
17
|
+
mapSeries,
|
|
14
18
|
} from "./barchart.chart.js";
|
|
15
19
|
|
|
16
20
|
const ohlcFields = [
|
|
@@ -212,15 +216,136 @@ class ExampleFeed extends BaseDataFeed {
|
|
|
212
216
|
}
|
|
213
217
|
}
|
|
214
218
|
|
|
219
|
+
// using a built-in study and a small lambda function
|
|
220
|
+
class ChandeForecastOscillatorSeries1 extends StudySeries {
|
|
221
|
+
constructor(options, innerSeries) {
|
|
222
|
+
super(options, innerSeries);
|
|
223
|
+
const linRegSeries = linReg(innerSeries, this.period, this.source);
|
|
224
|
+
const that = this; // chart replaces `this` inside `convertSeries` with its return value
|
|
225
|
+
const pluckField = function (pos) {
|
|
226
|
+
return {
|
|
227
|
+
[that.source.id]: this.baseVal(that.source, pos),
|
|
228
|
+
};
|
|
229
|
+
};
|
|
230
|
+
const sourceSeries = convertSeries(this.source, pluckField, innerSeries);
|
|
231
|
+
const oscFunc = (_pos, src, linReg) => (null === src ? null : (100 * (src - linReg)) / src);
|
|
232
|
+
const oscSeries = mapSeries(this.target, oscFunc, sourceSeries, linRegSeries);
|
|
233
|
+
this.addInner(oscSeries);
|
|
234
|
+
}
|
|
235
|
+
// no need for `calculateAt`, just need to expose the computed value from inner series
|
|
236
|
+
getWrappers() {
|
|
237
|
+
return [this.wrapInner(this.target, 1)];
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function sum(arr) {
|
|
242
|
+
return arr.reduce((prev, curr) => prev + curr, 0);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function notEmpty(arr) {
|
|
246
|
+
return arr.every(val => val !== null);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// not using any built-in studies, fully implemented here
|
|
250
|
+
class ChandeForecastOscillatorSeries2 extends StudySeries {
|
|
251
|
+
constructor(options, innerSeries) {
|
|
252
|
+
super(options, innerSeries);
|
|
253
|
+
const per = this.inputs.Period;
|
|
254
|
+
this.xSum = (per * (per + 1.0)) / 2.0; // = sum(1,2,..,n) = (n * (n+1)) / 2
|
|
255
|
+
this.xSum2 = this.xSum * this.xSum; // xSum squared
|
|
256
|
+
this.x2Sum = (per * (per + 1.0) * (2 * per + 1.0)) / 6.0;
|
|
257
|
+
// sum of xs squared = sum(1^2, 2^2,.., n^2) = (n * (n+1)*(2*n +1)) / 6
|
|
258
|
+
}
|
|
259
|
+
calculateAt(pos) {
|
|
260
|
+
let cfosc = null;
|
|
261
|
+
const per = this.inputs.Period;
|
|
262
|
+
if (this.atLeast(pos, per)) {
|
|
263
|
+
let lastN = this.past(pos, per, this.source);
|
|
264
|
+
if (notEmpty(lastN)) {
|
|
265
|
+
const ySum = sum(lastN);
|
|
266
|
+
const xySum = sum(lastN.map((v, i) => v * (i + 1)));
|
|
267
|
+
const slope = (per * xySum - this.xSum * ySum) / (per * this.x2Sum - this.xSum2);
|
|
268
|
+
const intercept = (ySum - slope * this.xSum) / per;
|
|
269
|
+
const mlr = intercept + slope * per;
|
|
270
|
+
// y = a + b * x; a = intercept, b = slope; y = price, x = date (of bar) index
|
|
271
|
+
const src = this.baseVal(this.source, pos);
|
|
272
|
+
cfosc = (100 * (src - mlr)) / src;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return {
|
|
276
|
+
[this.target.id]: cfosc,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
class StudyProvider {
|
|
282
|
+
getFields() {
|
|
283
|
+
return [
|
|
284
|
+
{
|
|
285
|
+
id: "CFOSC",
|
|
286
|
+
category: "Study",
|
|
287
|
+
name: "ChndFcstOsc",
|
|
288
|
+
shortName: "CFOSC",
|
|
289
|
+
},
|
|
290
|
+
];
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
getStudies() {
|
|
294
|
+
return [
|
|
295
|
+
{
|
|
296
|
+
id: "CFOSC",
|
|
297
|
+
meta: {
|
|
298
|
+
title: "Chande Forecast Oscillator",
|
|
299
|
+
overlay: false,
|
|
300
|
+
},
|
|
301
|
+
defaults: {
|
|
302
|
+
source: "Close",
|
|
303
|
+
inputs: [
|
|
304
|
+
{
|
|
305
|
+
name: "Period",
|
|
306
|
+
value: 14,
|
|
307
|
+
},
|
|
308
|
+
],
|
|
309
|
+
curves: [
|
|
310
|
+
{
|
|
311
|
+
colors: ["#888"],
|
|
312
|
+
fields: ["CFOSC"],
|
|
313
|
+
},
|
|
314
|
+
],
|
|
315
|
+
},
|
|
316
|
+
},
|
|
317
|
+
];
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
factory(studyId, options, innerSeries) {
|
|
321
|
+
if (studyId === "CFOSC") {
|
|
322
|
+
return Math.random() > 0.5
|
|
323
|
+
? new ChandeForecastOscillatorSeries2(options, innerSeries)
|
|
324
|
+
: new ChandeForecastOscillatorSeries1(options, innerSeries);
|
|
325
|
+
}
|
|
326
|
+
return null;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
215
330
|
const feed = initFeed(ExampleFeed, {
|
|
216
331
|
throttleMillis: 250,
|
|
217
|
-
apiKey: "
|
|
332
|
+
apiKey: "<YOUR_API_KEY>",
|
|
333
|
+
extensions: {
|
|
334
|
+
studyProvider: new StudyProvider(),
|
|
335
|
+
},
|
|
218
336
|
});
|
|
219
337
|
|
|
220
|
-
feed.ready()
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
338
|
+
const ready = await feed.ready();
|
|
339
|
+
if (ready) {
|
|
340
|
+
const chart = feed.addChart("root", {
|
|
341
|
+
symbol: `AMZN`,
|
|
342
|
+
});
|
|
343
|
+
chart.change({
|
|
344
|
+
id: "Plot",
|
|
345
|
+
context: {
|
|
346
|
+
id: "Add",
|
|
347
|
+
type: "Study",
|
|
348
|
+
studyId: "CFOSC",
|
|
349
|
+
},
|
|
350
|
+
});
|
|
351
|
+
}
|