@barchart/chart-lib 2.201.0 → 2.201.1

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