SMA smooths price data. EMA weights recent prices more heavily. Golden cross (50 > 200 MA) is bullish; death cross is bearish.
SMA = (P₁ + P₂ + ... + Pₙ) / n
EMA = P(t) × k + EMA(t-1) × (1-k), k = 2/(n+1)
// Simple Moving Average
function SMA(data, period) {
const result = [];
for (let i = period - 1; i < data.length; i++) {
let sum = 0;
for (let j = 0; j < period; j++) {
sum += data[i - j];
}
result.push(sum / period);
}
return result;
}
// Exponential Moving Average
function EMA(data, period) {
const k = 2 / (period + 1);
const result = [data[0]];
for (let i = 1; i < data.length; i++) {
result.push(data[i] * k + result[i - 1] * (1 - k));
}
return result;
}