Use this template to create custom chart indicators for the Algocdk platform.
Your indicator must have a function named calculateIndicator that accepts two parameters:
candles - Array of OHLCV objects with properties: open, high, low, close, volume, timeparams - Optional configuration object (e.g., {period: 14, color: '#FF4500'})Loop through the candles array and calculate your indicator values:
for (let i = 0; i < candles.length; i++) {
// Access candle data
const close = candles[i].close;
const high = candles[i].high;
const low = candles[i].low;
// Your calculation logic here
const value = yourCalculation(candles, i);
values.push(value);
}
Tip: Use null for periods where calculation isn't possible yet.
Your function must return an object with these properties:
return {
name: 'My Indicator', // Display name
type: 'line', // 'line', 'bar', or 'histogram'
data: values, // Array of calculated values
color: '#FF4500', // Hex color code
lineWidth: 2, // Line thickness
// Optional: Additional lines/series
series: [
{
name: 'Signal Line',
data: signalValues,
color: '#10B981',
lineWidth: 1
}
]
};
Use the code editor above to write your indicator, then click Test Code to see it visualized with sample data.
Best Practices
Common Mistakes
Once your indicator works correctly:
{
time: 1234567890000, // Unix timestamp in milliseconds
open: 100.50, // Opening price
high: 101.20, // Highest price
low: 99.80, // Lowest price
close: 100.90, // Closing price
volume: 15000 // Trading volume
}
line - Continuous line chart (best for moving averages, trends)bar - Bar chart (good for MACD histogram)histogram - Volume-style bars (best for volume indicators)You can define helper functions within your code:
function calculateSMA(data, period) {
const result = [];
for (let i = 0; i < data.length; i++) {
if (i < period - 1) {
result.push(null);
} else {
let sum = 0;
for (let j = 0; j < period; j++) {
sum += data[i - j];
}
result.push(sum / period);
}
}
return result;
}
function calculateIndicator(candles, params = {}) {
const closes = candles.map(c => c.close);
const sma = calculateSMA(closes, params.period || 20);
return { name: 'SMA', type: 'line', data: sma, color: '#FF4500', lineWidth: 2 };
}