Spaces:
Runtime error
Runtime error
export function calculateMFI(prices, volumes, period = 14) { | |
const mfiValues = []; | |
const typicalPrices = prices.map((price, index) => { | |
return (price.high + price.low + price.close) / 3; | |
}); | |
for (let i = 0; i < typicalPrices.length; i++) { | |
if (i < period) { | |
mfiValues.push(null); | |
continue; | |
} | |
const gain = typicalPrices.slice(i - period + 1, i + 1).reduce((acc, price, index) => { | |
return acc + (price > typicalPrices[i - period + index] ? volumes[i - period + index] : 0); | |
}, 0); | |
const loss = typicalPrices.slice(i - period + 1, i + 1).reduce((acc, price, index) => { | |
return acc + (price < typicalPrices[i - period + index] ? volumes[i - period + index] : 0); | |
}, 0); | |
const moneyFlowRatio = gain / loss || 0; | |
const mfi = 100 - (100 / (1 + moneyFlowRatio)); | |
mfiValues.push(mfi); | |
} | |
return mfiValues; | |
} |