Skip to content

Your First Indicator

Build a Dual EMA indicator step by step.

Step 1: Create a New Script

Open CodeLab and select the Blank Script template.

Step 2: Define Inputs

typescript
import { Script, input, plot, plotColor, color } from '@chartlabs/script-sdk';

export default class DualMA extends Script {
  fastLen = input.number('Fast Period', 9, { min: 1, max: 200 });
  slowLen = input.number('Slow Period', 21, { min: 1, max: 500 });
  source = input.source('Source', 'close');

  calculate() {
    // Next step
  }
}

Step 3: Calculate EMAs

typescript
calculate() {
  const fast = this.ema(this.source, this.fastLen);
  const slow = this.ema(this.source, this.slowLen);
}

Step 4: Plot with Dynamic Coloring

typescript
calculate() {
  const fast = this.ema(this.source, this.fastLen);
  const slow = this.ema(this.source, this.slowLen);

  plotColor('Fast EMA', fast,
    [color.green, color.red],
    fast > slow ? 0 : 1,
    { width: 2 }
  );
  plot('Slow EMA', slow, { color: color.orange, width: 2 });
}

The fast EMA turns green when above the slow EMA and red when below. Save, compile, and add to a chart to see it in action.

ChartLabs Documentation