EMA Crossover Candle Colors

A custom indicator created by TrendSpider on TrendSpider. You can import this script into your TrendSpider account. Don't have TrendSpider? Create an account first, then import the script.

Chart featuring the EMA Crossover Candle Colors indicator

Paints candles green when the shorter MA crosses up the longer MA, paints candles red when shorter MA crosses down the longer MA.

Source code

This indicator had been implemented by TrendSpider in JavaScript on TrendSpider. Check out the developer documentation to learn more about JS on TrendSpider.

describe_indicator('EMA Crossover Candle Colors');

// Input parameters for EMA lengths
const fastLength = input.number('Fast EMA Length', 8, { min: 1 });
const slowLength = input.number('Slow EMA Length', 21, { min: 1 });

// Input parameters for candle colors
const bullishColor = input.color('Bullish Candle Color', 'green');
const bearishColor = input.color('Bearish Candle Color', 'red');
const neutralColor = input.color('Neutral Candle Color', 'gray');

// Calculate EMAs
const fastEMA = ema(close, fastLength);
const slowEMA = ema(close, slowLength);

// Register signals for strategy tester and market scanner
register_signal(for_every(fastEMA, slowEMA, (_fast, _slow) => _fast > _slow), 'Bullish');
register_signal(for_every(fastEMA, slowEMA, (_fast, _slow) => _fast < _slow), 'Bearish');

// Determine candle colors based on EMA crossover
const candleColors = for_every(fastEMA, slowEMA, (_fast, _slow) => {
    if (_fast > _slow) {
        return bullishColor;
    } else if (_fast < _slow) {
        return bearishColor;
    }
    return neutralColor; // Use neutral color if EMAs are equal
});

// Color the candles
color_candles(candleColors);

// Paint EMAs for reference
paint(fastEMA, { color: 'blue', name: `${fastLength} EMA` });
paint(slowEMA, { color: 'orange', name: `${slowLength} EMA` });