Helloworld with custom study

Sample of how to create a custom study calculation using the default line rendering function.

by sonylnagale

HTML

<link rel="stylesheet" type="text/css" href="https://jsfiddle.chartiq.com/chart/css/stx-chart.css" media="screen" />

<div class="chartContainer" style="width:600px;height:400px;position:relative;"></div>

JavaScript

import {
  CIQ
} from "https://jsfiddle.chartiq.com/chart/js/advanced.js";
import sampleData from "https://jsfiddle.chartiq.com/chart/examples/data/STX_SAMPLE_DAILY.js";

// Activate the License Key
import getLicenseKey from "https://jsfiddle.chartiq.com/chart/key.js";
getLicenseKey(CIQ);


/*********** y axis scale override ********/
CIQ.ChartEngine.prototype.detMinMax = CIQ.ChartEngine.prototype.determineMinMax;
CIQ.ChartEngine.prototype.determineMinMax = function (
	quotes, fields, sum, bypassTransform, length, checkArray, panel, axis, filters) {
  const yAxis = axis ? axis : this.panels[panel].yAxis;    
  if (yAxis.sum) {
    sum = yAxis.sum;
  }
  return this.detMinMax(quotes, fields, sum, bypassTransform, length, checkArray, panel, axis, filters);
}

/*********** end override ********/

CIQ.Studies.calculateSample = function(stx, sd) {

  const quotes = sd.chart.scrubbed;

  if (quotes.length < sd.days + 1) {
    // not enough data
    sd.error = true;
    return;
  }

  // iterate trough the raw data and create the required values for the display function to use
  for (let i = sd.startFrom; i < quotes.length; i++) {
    if (!quotes[i]) continue;
    if (!quotes[i].Close) continue;

    // make sure field name complies with 
    // default function https://documentation.chartiq.com/CIQ.Studies.html#.displaySeriesAsLine
    quotes[i]["line1" + " " + sd.name] = quotes[i].Close + 1;
    quotes[i]["line2" + " " + sd.name] = quotes[i].Close + 2;
    quotes[i]["line3" + " " + sd.name] = quotes[i].Close + 3;
  }
};



CIQ.Studies.studyLibrary = CIQ.extend(CIQ.Studies.studyLibrary, {
  "Sample": {
    name: "Sample",
    overlay: true,
    // note no display function is set, 
    // so the default (https://documentation.chartiq.com/CIQ.Studies.html#.displaySeriesAsLine) 
    // will be used to draw simple lines.
    calculateFN: CIQ.Studies.calculateSample,
    seriesFN: (stx, sd) => {
    	/*********** short term override ********/
      const yAxis =...