trading-signals
Version:
Technical indicators to run technical analysis with JavaScript / TypeScript.
28 lines • 912 B
JavaScript
export function getStreaks(prices, keepSide) {
const streaks = [];
let currentStreak = 0;
function saveStreak(i) {
const endPrice = prices[i - 1];
const startPrice = prices[i - currentStreak - 1];
const percentage = ((endPrice - startPrice) / startPrice) * 100;
streaks.push({ length: currentStreak, percentage });
}
for (let i = 1; i < prices.length; i++) {
const isUpward = keepSide === 'up' && prices[i] > prices[i - 1];
const isDownward = keepSide === 'down' && prices[i] < prices[i - 1];
if (isUpward || isDownward) {
currentStreak++;
}
else {
if (currentStreak > 0) {
saveStreak(i);
}
currentStreak = 0;
}
}
if (currentStreak > 0) {
saveStreak(prices.length);
}
return streaks;
}
//# sourceMappingURL=getStreaks.js.map