API Reference
Namespaces
Classes
Events
Global
Externals

Class: StudyCalculator

StudyCalculator


new StudyCalculator(ciq [, params])

Study Calculator is a Node.js module which can be included in your JavaScript applications.

The Study Calculator module:

  • Requires a specially locked version the ChartIQ library to run
  • Will not work with your browser-based locked package
  • Is available in the modules directory if you have purchased this feature

The purpose of this module is to calculate the results of one or more studies applied to market data provided to the module. The results can then be retrieved and used by your applications.

This module should be used as a "one pass state machine". Data must be pushed into it. This data is processed, and the requested study data then outputted. The module then stops processing and awaits new data to be pushed into it.

The complete list of studies generated by the module can be extracted from the CIQ.Studies.studyLibrary. See Using and Customizing Studies for more details.

Included in the modules/studycalc directory is a detailedExample.js file which is an in-depth example of how to interface with the Study Calculator module. You can modify this file or incorporate it into your own code. The study-calculator.js file itself should not be modified.

A package.json file is included in the modules/studycalc folder to illustrate the use of the Study Calculator. Simply point the path to the tarball from the extracted library package (chartiq-x.x.x.tgz file from your license package) in the dependencies section of the package.json, and run npm install. You will then be able to run the example files provided.

See the examples below.

Parameters:
Name Type Argument Description
ciq object

The ChartIQ namespace.

params object <optional>

Contains the function parameters.

Properties
Name Type Argument Default Description
marketFactory function <optional>

A market factory, required by some studies. If this parameter is omitted, the Study Calculator uses CIQ.Market.Symbology.factory.

symbolObject object <optional>

A stock symbol, required by some studies. This object should look like: {symbol:xxx}.

periodicity number <optional>

A time series period, required by some studies (for example, 1). See CIQ.ChartEngine~PeriodicityParameters.

interval string <optional>

A time series interval, required by some studies (for example, "day"). See CIQ.ChartEngine~PeriodicityParameters.

timeUnit string <optional>

A time series time unit, required by some studies (for example, "seconds"). See CIQ.ChartEngine~PeriodicityParameters.

maxRecords number <optional>
1000

The maximum number of records to keep in the calculator. If additional records are pushed in, the oldest records are dropped. Set this parameter to 0 to remove the limitation so that no records are dropped.

Since:
  • 7.1.0

Examples

Here is a very simple example of how to use the Study Calculator module to compute the results of a five-period moving average. It is included as simpleExample.js in the ChartIQ library.

import { CIQ as ciq } from "chartiq/js/standard.js";
import StudyCalculator from "chartiq/modules/studycalc/study-calculator.js";

const sampledata = [
    {"DT":new Date("2015-04-16 16:00"),"Close":152.11}, {"DT":new Date("2015-04-17 09:30"),"Close":151.79},
    {"DT":new Date("2015-04-17 09:35"),"Close":151.75}, {"DT":new Date("2015-04-17 09:40"),"Close":151.84},
    {"DT":new Date("2015-04-17 09:45"),"Close":151.95}, {"DT":new Date("2015-04-17 09:50"),"Close":152.07},
    {"DT":new Date("2015-04-17 09:55"),"Close":151.91}, {"DT":new Date("2015-04-17 10:00"),"Close":151.95},
    {"DT":new Date("2015-04-17 10:05"),"Close":151.98}, {"DT":new Date("2015-04-17 10:10"),"Close":151.73},
    {"DT":new Date("2015-04-17 10:15"),"Close":151.82}, {"DT":new Date("2015-04-17 10:20"),"Close":151.75},
    {"DT":new Date("2015-04-17 10:25"),"Close":151.73}, {"DT":new Date("2015-04-17 10:30"),"Close":151.82},
    {"DT":new Date("2015-04-17 10:35"),"Close":151.84}, {"DT":new Date("2015-04-17 10:40"),"Close":151.95},
    {"DT":new Date("2015-04-17 10:45"),"Close":152.03}, {"DT":new Date("2015-04-17 10:50"),"Close":152.03}
];

const studyCalculator=new StudyCalculator(ciq, {interval:5, symbolObject:{symbol:"IBM"}});
const maHandle=studyCalculator.addStudy("ma", {Period:5});

studyCalculator.addData(sampledata);
studyCalculator.calculate();

for(let record=0;record<sampledata.length;record++){
	const results=studyCalculator.getResults(record, maHandle);
	for(let f in results) console.log(sampledata[record].DT.toISOString()+"   "+f+":  "+results[f]);
}

To run the above on Node.js 13.x or higher, execute the following command:

npm run simple

To run the above on older versions of Node.js, execute the following command:

npm run simple:old

Methods


addData(data)

Adds input data to the calculator.

Parameters:
Name Type Description
data object

The data on which the studies are computed.

Since:
  • 7.1.0


addStudy(type [, inputs])

Adds a study to the calculator.

Parameters:
Name Type Argument Description
type string

The study type to add.

inputs object <optional>

Input parameters for the study, if different from the default.

Since:
  • 7.1.0

Returns:

A handle to the study just added.

Type
string

calculate()

Calculates all added studies using the given data.

Since:
  • 7.1.0

Example

Create a study calculator.

const studyCalculator = new StudyCalculator(ciq);
studyCalculator.addData(sampleData);
const sd = studyCalculator.addStudy("rsi");
studyCalculator.calculate();

const results = [];
for (let i = 0; i < sampleData.length; i++) {
    results.push(studyCalculator.getResults(i, sd));
}

clearData()

Clears the calculator input data.

Since:
  • 7.1.0


destroy()

Removes the calculator instance.

Since:
  • 7.1.0


getErrors()

Gets the error object.

Errors are returned in an object, keyed by the study name. Use this function to determine whether any special remediation processes are required before sending calculation results to the final destination.

Since:
  • 7.1.0

Returns:

The error object.

Type
object

getResults(index [, handle])

Gets the results from the calculator.

Results are returned in an object representing a record in the time series.

Parameters:
Name Type Argument Description
index number

Index in the calculator results array to the record to be returned.

handle string <optional>

A handle to the study from which to return fields. If omitted, all fields are included in the record.

Since:
  • 7.1.0

Returns:

Result object (the record itself).

Type
object