Back to website

Moving Average Distance

by TrendSpider Team
+ Add to TrendSpider

Measures the difference between two moving averages of a selected type and length, highlighting convergence and divergence. It is implemented in JavaScript.

Developer Info

Source Code


describe_indicator('Distance between MAs', 'lower');

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

const ma1 = indicators[maType](prices[priceSource], input("Length 1", 20, { min: 1, max: 200 }));
const ma2 = indicators[maType](prices[priceSource], input("Length 2", 50, { min: 1, max: 200 }));

const distance = for_every(ma1, ma2, (ma1, ma2) => ma1 - ma2);

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