Skip to content

Rank Correlation Index ​

Spearman rank correlation of price and time, -100 to 100.

Kind indicator · Drawn in its own pane

Try it ​

On the chart, press / and choose Rank Correlation Index under Oscillators. To change it, copy the source below into the AlphaScript editor.

Inputs ​

InputTypeDefault
Lengthint10
Sourcesource"close"
Colorcolor"#2962FF"

Source ​

From packages/alphascript/indicators/rci.as.js.

js
indicator({ name: "Rank Correlation Index", shortTitle: "RCI", overlay: false, precision: 2 });
const length = input.int("Length", 10, { min: 2, max: 500 });
const src = input.source("Source", "close");
const rci = ta.calc([src], () => {
  /** @type {number[]} */
  const v = [];
  for (let k = 0; k < length; k++) { const x = src.at(k); if (x !== x) return NaN; v.push(x); }
  let d2 = 0;
  for (let k = 0; k < length; k++) {
    let above = 0, same = 0;
    for (let j = 0; j < length; j++) { if (v[j] > v[k]) above++; else if (v[j] === v[k]) same++; }
    const priceRank = above + (same + 1) / 2;
    d2 += (k + 1 - priceRank) ** 2;
  }
  return (1 - (6 * d2) / (length * (length * length - 1))) * 100;
});
plot(rci, { title: "RCI", color: input.color("Color", "#2962FF", { group: "Style" }) });
hline(80, { title: "Upper band", color: "#787B86", style: "dashed" });
hline(0, { title: "Middle band", color: "rgba(120,123,134,0.5)", style: "dotted" });
hline(-80, { title: "Lower band", color: "#787B86", style: "dashed" });
fill(80, -80, { title: "Background", color: "rgba(33,150,243,0.1)" });

AlphaScript language v0.1 · runtime 0.2.0