pricehistory
Version:
Transforms raw OHLCV series data into enriched candles with technical indicators, pattern recognition, and trend analysis.
100 lines • 3.88 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const utilN = __importStar(require("@nameer/utils"));
function setCandleColor(opt, candle, ctx, period) {
var _a;
if (opt.color !== true ||
candle.priceClose === undefined ||
candle.priceOpen === undefined) {
return;
}
const color = candle.priceClose > candle.priceOpen
? "green"
: candle.priceClose < candle.priceOpen
? "red"
: "gray";
const winKey = `color${period ?? ""}`;
(_a = ctx.color)[winKey] ?? (_a[winKey] = []);
ctx.color[winKey].push([color, candle.volume]);
if (period !== undefined && ctx.color[winKey].length > period) {
ctx.color[winKey].shift();
}
let greenCount = 0;
let redCount = 0;
let grayCount = 0;
let total = 0;
let greenVolume = 0;
let redVolume = 0;
let grayVolume = 0;
let totalVolume = 0;
for (const [c, vol] of ctx.color[winKey]) {
if (c === "green")
greenCount++;
else if (c === "red")
redCount++;
else
grayCount++;
total++;
if (vol !== undefined) {
if (c === "green")
greenVolume += vol;
else if (c === "red")
redVolume += vol;
else
grayVolume += vol;
totalVolume += vol;
}
}
if (period !== undefined) {
candle[`sma${period}ColorGreen`] = utilN.math.percent(greenCount, total);
candle[`sma${period}ColorRed`] = utilN.math.percent(redCount, total);
candle[`sma${period}ColorGray`] = utilN.math.percent(grayCount, total);
candle[`sma${period}ColorVolumeGreen`] = utilN.math.percent(greenVolume, totalVolume);
candle[`sma${period}ColorVolumeRed`] = utilN.math.percent(redVolume, totalVolume);
candle[`sma${period}ColorVolumeGray`] = utilN.math.percent(grayVolume, totalVolume);
}
else {
candle.color = color;
candle.colorGreen = utilN.math.percent(greenCount, total);
candle.colorRed = utilN.math.percent(redCount, total);
candle.colorGray = utilN.math.percent(grayCount, total);
candle.colorVolumeGreen = utilN.math.percent(greenVolume, totalVolume);
candle.colorVolumeRed = utilN.math.percent(redVolume, totalVolume);
candle.colorVolumeGray = utilN.math.percent(grayVolume, totalVolume);
}
}
exports.default = setCandleColor;
//# sourceMappingURL=candle.setColor.js.map