UNPKG

@neabyte/candlestick-cli

Version:

Beautiful terminal candlestick charts with real-time trading data. Create stunning ASCII art charts directly in your terminal with support for live market data, custom colors, and professional trading visualization.

1 lines 1.34 kB
export function calculateSMA(e,t){if(t<=0||t>e.length)throw new Error(`Invalid period: ${t}. Must be between 1 and ${e.length}`);const r=[];for(let n=0;n<e.length;n++)if(n<t-1)r.push(NaN);else{const o=e.slice(n-t+1,n+1).reduce((e,t)=>e+t,0);r.push(o/t)}return r}export function calculateEMA(e,t){if(t<=0||t>e.length)throw new Error(`Invalid period: ${t}. Must be between 1 and ${e.length}`);const r=[],n=2/(t+1);let o=0;for(let r=0;r<t;r++)o+=e[r];r.push(o/t);for(let t=1;t<e.length;t++){const o=e[t]*n+r[t-1]*(1-n);r.push(o)}const l=[];for(let n=0;n<e.length;n++)n<t-1?l.push(NaN):l.push(r[n-t+1]);return l}export function calculateMultipleEMAs(e,t){const r={};for(const n of t)r[n.toString()]=calculateEMA(e,n);return r}export function calculateMultipleSMAs(e,t){const r={};for(const n of t)r[n.toString()]=calculateSMA(e,n);return r}export function validateIndicatorParams(e,t){if(!Array.isArray(e)||0===e.length)throw new Error("Prices array must be non-empty");if("number"!=typeof t||t<=0)throw new Error(`Period must be a positive number, got: ${t}`);if(t>e.length)throw new Error(`Period (${t}) cannot be greater than prices length (${e.length})`);for(let t=0;t<e.length;t++)if("number"!=typeof e[t]||isNaN(e[t]))throw new Error(`Invalid price at index ${t}: ${e[t]}`)}export function getClosingPrices(e){return e.map(e=>e.close)}