Distance between MAs

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

Chart featuring the Distance between MAs indicator

Measures the difference between two moving averages of a selected type and length, highlighting convergence and divergence. By default this indicator using the 20 and 50 SMA. However, you can adjust the length and moving average type.

Source code

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

describe_indicator('Distance between MAs', 'lower', { warmup: 250 });

const maType = input('MA type', 'sma', constants.ma_types);
const priceSource = input('Price', 'close', constants.price_source_options);

// Define the lengths of the moving averages
const length1 = input("Length 1", 20, { min: 1, max: 200 });
const length2 = input("Length 2", 50, { min: 1, max: 200 });

// Calculate the moving averages
const ma1 = indicators[maType](prices[priceSource], length1);
const ma2 = indicators[maType](prices[priceSource], length2);

// Calculate the distance between the two moving averages
const distance = for_every(ma1, ma2, (ma1, ma2) => ma1 - ma2);

// Paint the distance as a histogram
paint(distance, {
    style: 'histogram',
    color: for_every(distance, d => d > 0 ? 'green' : 'red')
});