name: 生成资产价格图表 description: 基于现有OHLC数据为任何资产生成K线价格图表,不处理数据获取。
生成资产价格图表(基于OHLC数据)
从预加载的OHLC蜡烛数据渲染K线图图像。此技能仅专注于图表生成逻辑(无API调用)。
何时使用
- 您已有OHLC蜡烛数据并需要视觉图表
- 您想在后台作业或机器人中生成PNG图表
- 您需要一个可重用的图表渲染器,适用于任何资产/时间框架
所需工具/API
- 无需外部API
- Node.js选项:
canvas - Python选项:
matplotlib
安装:
# Node.js
npm install canvas
# Python
python -m pip install matplotlib
输入OHLC格式(两个示例均期望):
- 行数组:
[时间戳, 开盘, 最高, 最低, 收盘] 时间戳可以是Unix毫秒或任何x轴标签值
技能
generate_candlestick_chart_with_nodejs
import { createCanvas } from "canvas";
import { writeFile } from "node:fs/promises";
function validateOhlc(data) {
if (!Array.isArray(data) || data.length === 0) {
throw new Error("OHLC数据必须是非空数组");
}
data.forEach((row, index) => {
if (!Array.isArray(row) || row.length < 5) {
throw new Error(`索引${index}处的行无效。期望格式:[时间戳, 开盘, 最高, 最低, 收盘]`);
}
const [, open, high, low, close] = row;
[open, high, low, close].forEach((v) => {
if (!Number.isFinite(v)) {
throw new Error(`行${index}处的OHLC值非数字`);
}
});
});
}
function generateCandlestickChart(ohlcData, options = {}) {
validateOhlc(ohlcData);
const width = options.width ?? 1200;
const height = options.height ?? 600;
const padding = options.padding ?? 60;
const canvas = createCanvas(width, height);
const ctx = canvas.getContext("2d");
// 背景
ctx.fillStyle = "#1e1e2e";
ctx.fillRect(0, 0, width, height);
const chartWidth = width - padding * 2;
const chartHeight = height - padding * 2;
const highs = ohlcData.map((d) => d[2]);
const lows = ohlcData.map((d) => d[3]);
const minPrice = Math.min(...lows);
const maxPrice = Math.max(...highs);
const priceRange = Math.max(maxPrice - minPrice, 1e-9);
const xStep = chartWidth / Math.max(ohlcData.length, 1);
const yScale = chartHeight / priceRange;
// 网格
ctx.strokeStyle = "#333";
ctx.lineWidth = 1;
for (let i = 0; i <= 5; i++) {
const y = padding + (chartHeight / 5) * i;
ctx.beginPath();
ctx.moveTo(padding, y);
ctx.lineTo(width - padding, y);
ctx.stroke();
}
// 蜡烛
ohlcData.forEach(([, open, high, low, close], index) => {
const x = padding + index * xStep + xStep / 2;
const highY = height - padding - (high - minPrice) * yScale;
const lowY = height - padding - (low - minPrice) * yScale;
const openY = height - padding - (open - minPrice) * yScale;
const closeY = height - padding - (close - minPrice) * yScale;
const bullish = close >= open;
ctx.strokeStyle = bullish ? "#4caf50" : "#f44336";
ctx.fillStyle = ctx.strokeStyle;
// 灯芯
ctx.beginPath();
ctx.moveTo(x, highY);
ctx.lineTo(x, lowY);
ctx.stroke();
// 身体
const bodyTop = Math.min(openY, closeY);
const bodyHeight = Math.max(Math.abs(openY - closeY), 2); // 保持扁平蜡烛可见
const bodyWidth = Math.max(xStep * 0.6, 1);
ctx.fillRect(x - bodyWidth / 2, bodyTop, bodyWidth, bodyHeight);
});
return canvas.toBuffer("image/png");
}
// 使用现有OHLC数组的示例
const sample = [
[1700000000000, 100, 110, 95, 108],
[1700000600000, 108, 112, 104, 106],
[1700001200000, 106, 115, 103, 113],
[1700001800000, 113, 118, 109, 111],
[1700002400000, 111, 119, 110, 117],
];
const image = generateCandlestickChart(sample, { width: 1200, height: 600 });
await writeFile("candlestick.png", image);
console.log("已保存:candlestick.png");
代理提示
您正在从现有OHLC数据生成K线图图像。
不要获取市场数据,不要添加API逻辑。
输入格式是数组:[时间戳, 开盘, 最高, 最低, 收盘]。
使用Node.js(canvas)或Python(matplotlib)渲染蜡烛,包括:
- 暗色背景,
- 简单水平网格,
- 绿色看涨蜡烛,
- 红色看跌蜡烛,
- 可见灯芯和身体。
返回:
1) 使用的代码,
2) 输出文件名,
3) 简短验证说明(例如,渲染的蜡烛数量)。
最佳实践
- 在渲染前验证OHLC形状
- 确保蜡烛身体有最小可见高度以处理扁平蜡烛
- 保持渲染纯粹:图表函数接受数据并返回/保存图像
- 将获取/ETL与可视化分离
故障排除
Module not found: canvas→ 运行npm install canvas- Python导入matplotlib错误 → 运行
python -m pip install matplotlib - 空白/扁平图表 → 验证OHLC值是数字且在蜡烛间有变化
- 感觉y轴颠倒 → 确认转换公式将更高价格向上映射
另请参阅
- trading-indicators-from-price-data.md — 在绘图前推导指标
- get-crypto-price.md — 单独获取数据,然后将OHLC传递到此图表技能