Chart Indicator Template

Use this template to create custom chart indicators for the Algocdk platform.

Template Code

Live Preview

How to Create Custom Indicators

1 Define the calculateIndicator Function

Your indicator must have a function named calculateIndicator that accepts two parameters:

  • candles - Array of OHLCV objects with properties: open, high, low, close, volume, time
  • params - Optional configuration object (e.g., {period: 14, color: '#FF4500'})

2 Process the Candle Data

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.

3 Return the Correct Format

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
        }
    ]
};

4 Test Your Indicator

Use the code editor above to write your indicator, then click Test Code to see it visualized with sample data.

Best Practices

  • • Handle edge cases (insufficient data)
  • • Use meaningful parameter defaults
  • • Add comments to explain logic
  • • Test with different timeframes

Common Mistakes

  • • Not returning the correct object format
  • • Accessing array indices out of bounds
  • • Forgetting to handle null values
  • • Using wrong property names

5 Save and Deploy

Once your indicator works correctly:

  1. Click Download Template to save your code as a .js file
  2. Go to Admin Dashboard → Chart Indicators
  3. Click Create Indicator
  4. Fill in the details and upload your .js file
  5. Set it as Free or Paid
  6. Users can now add it to their charts!

Technical Reference

Candle Object Structure

{
    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
}

Visualization Types

  • line - Continuous line chart (best for moving averages, trends)
  • bar - Bar chart (good for MACD histogram)
  • histogram - Volume-style bars (best for volume indicators)

Helper Functions Example

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 };
}
Back to Admin Dashboard