@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 • 3.01 kB
JavaScript
import{ValidationError}from"../types/errors.js";function validateNonNegativeNumber(e,a){if("number"!=typeof e||e<0)throw new ValidationError(`${a} must be a non-negative number`,a.toLowerCase())}export function validateCandle(e){if(!e)throw new ValidationError("Candle cannot be null or undefined","candle");if("number"!=typeof e.open||isNaN(e.open))throw new ValidationError("Candle open price must be a valid number","open");if("number"!=typeof e.high||isNaN(e.high))throw new ValidationError("Candle high price must be a valid number","high");if("number"!=typeof e.low||isNaN(e.low))throw new ValidationError("Candle low price must be a valid number","low");if("number"!=typeof e.close||isNaN(e.close))throw new ValidationError("Candle close price must be a valid number","close");if("number"!=typeof e.timestamp||isNaN(e.timestamp))throw new ValidationError("Candle timestamp must be a valid number","timestamp");if(e.high<Math.max(e.open,e.close))throw new ValidationError("Candle high must be >= max(open, close)","high");if(e.low>Math.min(e.open,e.close))throw new ValidationError("Candle low must be <= min(open, close)","low");if(void 0!==e.volume){if("number"!=typeof e.volume||isNaN(e.volume))throw new ValidationError("Candle volume must be a valid number","volume");if(e.volume<0)throw new ValidationError("Candle volume cannot be negative","volume")}}export function validateCandles(e){if(!Array.isArray(e))throw new ValidationError("Candles must be an array","candles");if(0===e.length)throw new ValidationError("Candles array cannot be empty","candles");if(e.length<5)throw new ValidationError(`Too few candles: ${e.length}. Minimum required: 5 candles.`,"candles");if(e.length>1e4)throw new ValidationError(`Too many candles: ${e.length}. Maximum allowed: 10,000 candles.`,"candles");for(let a=0;a<e.length;a++)try{validateCandle(e[a])}catch(e){if(e instanceof ValidationError)throw new ValidationError(`${e.message} at index ${a}`,e.field);throw e}}function validateColorComponent(e,a){if("number"!=typeof e||e<0||e>255)throw new ValidationError(`${a} component must be a number between 0 and 255`,a.toLowerCase())}export function validateRGBColor(e,a,n){validateColorComponent(e,"Red"),validateColorComponent(a,"Green"),validateColorComponent(n,"Blue")}export function validateChartDimensions(e,a){validateNonNegativeNumber(e,"Chart width"),validateNonNegativeNumber(a,"Chart height")}export function validateMargins(e,a,n,o){validateNonNegativeNumber(e,"Top margin"),validateNonNegativeNumber(a,"Right margin"),validateNonNegativeNumber(n,"Bottom margin"),validateNonNegativeNumber(o,"Left margin")}export function validateTimeRange(e,a,n){if(validateNonNegativeNumber(e,"Start index"),validateNonNegativeNumber(a,"End index"),e>=n)throw new ValidationError("Start index must be less than array length","startIndex");if(a>=n)throw new ValidationError("End index must be less than array length","endIndex");if(e>a)throw new ValidationError("Start index must be <= end index","startIndex")}