Algorithmic trading is often associated with Python, C++, or specialized trading platforms.
But JavaScript can also be a powerful language for building trading indicators and automated trading strategies — especially when the trading platform itself runs in the browser.
In this article, we'll look at how JavaScript can be used to:
- Process market data
- Calculate technical indicators
- Render indicators on charts
- Detect trading signals
- Build automated trading bots
- Create custom strategies
I'll also introduce Algocdk, an algorithmic trading platform I built that allows developers to create custom indicators and trading bots using JavaScript.
Why JavaScript for Algorithmic Trading?
When you think about algorithmic trading, Python is probably one of the first languages that comes to mind.
Python has an incredible ecosystem for quantitative analysis, machine learning, and data science.
However, JavaScript has another important advantage:
It runs natively in the browser.
That makes JavaScript particularly interesting for web-based trading applications.
A developer can write code that:
- Receives market data
- Processes the data
- Calculates an indicator
- Renders the result on a chart
- Generates a trading signal
- Sends the signal to a trading engine
All within a web application.
For example, imagine receiving candle data like this:
const candles = [
{
open: 100,
high: 105,
low: 98,
close: 103,
time: 1710000000
},
{
open: 103,
high: 108,
low: 101,
close: 107,
time: 1710000060
}
];
JavaScript can immediately process these candles.
const closes = candles.map(candle => candle.close);
console.log(closes);
Output:
[103, 107]
From there, we can calculate almost any technical indicator we want.
What Is a Trading Indicator?
A trading indicator is essentially a mathematical function that transforms market data into useful information.
For example, a Moving Average calculates the average price over a specific number of candles.
A simple moving average with a period of 5 can be represented as:
SMA = (Price1 + Price2 + Price3 + Price4 + Price5) / 5
The indicator can then be displayed on a price chart.
Other popular indicators include:
- SMA
- EMA
- MACD
- RSI
- Bollinger Bands
- Stochastic Oscillator
- ATR
- VWAP
- Moving Average Crossovers
The interesting part is that these indicators don't have to be hardcoded into a trading platform.
They can be written as JavaScript.
Building a Moving Average in JavaScript
Let's start with a simple Simple Moving Average.
function sma(values, period) {
const result = new Array(values.length).fill(null);
for (let i = period - 1; i < values.length; i++) {
let sum = 0;
for (let j = i - period + 1; j <= i; j++) {
sum += values[j];
}
result[i] = sum / period;
}
return result;
}
We can use it with candle data:
const closes = candles.map(c => c.close);
const movingAverage = sma(closes, 20);
console.log(movingAverage);
Now we have a 20-period moving average calculated from the closing prices.
Moving Average Crossover
One of the simplest algorithmic trading concepts is the Moving Average Crossover.
The idea is to compare two moving averages.
For example:
Fast MA = 9 periods
Slow MA = 21 periods
When the fast moving average crosses above the slow moving average:
Fast MA
/
/
/____ Slow MA
/
the strategy can generate a bullish signal.
When the fast moving average crosses below the slow moving average:
Slow MA
______
\
\
\ Fast MA
the strategy can generate a bearish signal.
Here's a simple JavaScript implementation:
function crossover(fast, slow, index) {
if (index < 1) {
return null;
}
const previousFast = fast[index - 1];
const previousSlow = slow[index - 1];
const currentFast = fast[index];
const currentSlow = slow[index];
if (
previousFast <= previousSlow &&
currentFast > currentSlow
) {
return "BUY";
}
if (
previousFast >= previousSlow &&
currentFast < currentSlow
) {
return "SELL";
}
return null;
}
This is already the foundation of an algorithmic strategy.
The important part is that we're not manually looking at the chart.
The computer is detecting the crossover for us.
Building the Same Idea in Algocdk
This is where Algocdk comes in.
I built Algocdk as a platform where developers can write their own JavaScript indicators and trading bots.
The platform handles the market-data connection and chart environment while your JavaScript code handles the strategy logic.
An Algocdk indicator is a JavaScript object.
For example:
({
name: "Moving Average",
color: "#FF4500",
lineWidth: 2,
defaultParams: {
period: 20
},
calculate(data, params) {
const period = params.period || 20;
const values = new Array(data.length).fill(null);
for (let i = period - 1; i < data.length; i++) {
let sum = 0;
for (
let j = i - period + 1;
j <= i;
j++
) {
sum += data[j].close;
}
values[i] = sum / period;
}
return values;
}
})
The important function here is:
calculate(data, params)
data contains the candle history and params contains the indicator parameters.
Algocdk calls the function as new market data becomes available and uses the returned values to render the indicator.
You don't have to build the WebSocket connection or chart renderer yourself.
You focus on the trading logic.
Creating a MACD Indicator
Let's move to something more interesting.
MACD stands for:
Moving Average Convergence Divergence
MACD commonly uses:
Fast EMA = 12
Slow EMA = 26
Signal EMA = 9
The MACD line is:
MACD = EMA(12) - EMA(26)
Then we calculate the signal line:
Signal = EMA(9) of MACD
And finally:
Histogram = MACD - Signal
Let's implement the EMA first.
function ema(values, period) {
const result = new Array(values.length).fill(null);
const multiplier = 2 / (period + 1);
let previous = values[0];
for (let i = 0; i < values.length; i++) {
if (i === 0) {
result[i] = previous;
continue;
}
previous =
(values[i] - previous) * multiplier +
previous;
result[i] = previous;
}
return result;
}
Now we can calculate MACD:
function calculateMACD(
closes,
fastPeriod = 12,
slowPeriod = 26,
signalPeriod = 9
) {
const fastEMA = ema(closes, fastPeriod);
const slowEMA = ema(closes, slowPeriod);
const macd = closes.map((_, i) => {
return fastEMA[i] - slowEMA[i];
});
const signal = ema(macd, signalPeriod);
const histogram = macd.map((value, i) => {
return value - signal[i];
});
return {
macd,
signal,
histogram
};
}
We can now use it:
const closes = candles.map(c => c.close);
const result = calculateMACD(closes);
console.log(result.macd);
console.log(result.signal);
console.log(result.histogram);
This is the core logic behind a MACD indicator.
MACD Inside Algocdk
We can turn the same logic into an Algocdk indicator.
({
name: "MACD",
hasWindow2: true,
defaultParams: {
fastPeriod: 12,
slowPeriod: 26,
signalPeriod: 9
},
calculate(data, params) {
const closes = data.map(c => c.close);
const fast = ema(
closes,
params.fastPeriod
);
const slow = ema(
closes,
params.slowPeriod
);
const macd = closes.map((_, i) => {
return fast[i] - slow[i];
});
const signal = ema(
macd,
params.signalPeriod
);
return {
macd,
signal
};
}
})
The hasWindow2 property can be used when the indicator should appear in a separate pane below the main price chart.
This is useful for indicators such as:
- MACD
- RSI
- Stochastic
- CCI
- Momentum
Instead of drawing everything directly over the price candles.
From Indicators to Trading Bots
This is where things become even more interesting.
An indicator tells us what is happening in the market.
A trading bot can take the next step:
If a condition happens → generate a trading signal.
For example:
if (fastMA > slowMA) {
return "BUY";
}
if (fastMA < slowMA) {
return "SELL";
}
Inside Algocdk, a bot can implement this using getSignalAt().
A simplified example:
({
name: "MA Crossover Bot",
defaultParams: {
fastPeriod: 9,
slowPeriod: 21,
stake: 1,
duration: 5,
duration_unit: "t"
},
getSignalAt(candles, index, params) {
if (candles.length < params.slowPeriod) {
return null;
}
const closes =
candles.map(c => c.close);
const fast =
ema(closes, params.fastPeriod);
const slow =
ema(closes, params.slowPeriod);
if (
fast[index] > slow[index] &&
fast[index - 1] <= slow[index - 1]
) {
return {
signal: "buy",
stake: params.stake,
duration: params.duration,
duration_unit: params.duration_unit,
label: "MA Cross Up"
};
}
if (
fast[index] < slow[index] &&
fast[index - 1] >= slow[index - 1]
) {
return {
signal: "sell",
stake: params.stake,
duration: params.duration,
duration_unit: params.duration_unit,
label: "MA Cross Down"
};
}
return null;
}
})
The important distinction is:
Indicator
↓
Analyze market data
↓
Return calculated values
↓
Render on chart
while a bot can do:
Market data
↓
Calculate indicators
↓
Evaluate strategy
↓
Generate signal
↓
Execute trade
Algocdk's bot system supports this type of JavaScript strategy architecture.
Why I Built Algocdk
The idea behind Algocdk started with a simple question:
What if traders and developers could create their own trading tools instead of being limited to the indicators provided by a platform?
That led me to build Algocdk.
Algocdk is an algorithmic trading platform focused on giving developers more control over their trading logic.
Instead of requiring developers to build an entire trading infrastructure, the platform provides the environment where JavaScript code can interact with market data, charts, indicators, and trading strategies.
Developers can create:
- Custom JavaScript indicators
- Moving averages
- Oscillators
- Trading signals
- Automated trading bots
- Digit trading strategies
- Custom chart visualizations
- Backtesting strategies
- Bot marketplace projects
The platform currently provides a Developer Guide with examples for custom indicators and trading bots.
The Interesting Part: You Don't Need a Build System
One of the things I wanted to keep simple was the developer experience.
An indicator can be a plain JavaScript file.
For example:
({
name: "My Indicator",
calculate(data, params) {
return data.map(candle => {
return candle.close;
});
}
})
No React.
No npm installation.
No bundler.
No complicated configuration.
Just JavaScript.
Upload the file and the platform can load it as a custom indicator.
This makes experimentation much faster.
JavaScript Gives You More Than Technical Indicators
Once the market data is available inside JavaScript, you aren't restricted to traditional indicators.
You can build your own logic.
For example:
const closes = data.map(c => c.close);
const recent = closes.slice(-20);
const average =
recent.reduce((sum, value) => sum + value, 0)
/ recent.length;
You could combine this with:
- Price action
- Moving averages
- Volatility
- Momentum
- Candle patterns
- Statistical analysis
- Market regimes
- Custom mathematical models
You can even create completely custom chart visualizations using JavaScript and Canvas.
Algocdk supports custom drawing through the indicator draw() function, giving developers control over how their indicator is rendered.
From Code to Trading Platform
The bigger vision behind Algocdk isn't simply:
"Here is another chart."
It's:
Give developers the ability to build their own trading logic.
A developer should be able to go from:
Idea
↓
JavaScript
↓
Indicator
↓
Backtest
↓
Strategy
↓
Trading Bot
without having to build the entire trading infrastructure from scratch.
That is the direction I'm building Algocdk toward.
Example Project
If you're interested in experimenting with this approach, you can visit:
The platform includes a trading interface, custom indicators, JavaScript bots, developer documentation, strategy tools, and other algorithmic trading features.
The developer documentation is also available directly on the platform:
https://algocdk.site/dev-guide
Final Thoughts
JavaScript isn't traditionally the first language people mention when they talk about algorithmic trading.
But for web-based trading systems, it has an interesting advantage:
The language that powers the interface can also power the trading logic.
With a relatively small amount of JavaScript, we can calculate:
SMA
EMA
MACD
RSI
Bollinger Bands
and then use those calculations to create trading strategies and automated bots.
The important part isn't the programming language itself.
It's the ability to turn an idea into a repeatable algorithm.
That's what I'm trying to make easier with Algocdk.
If you're a JavaScript developer interested in algorithmic trading, I'd love to see what you build.
Build the indicator. Test the strategy. Automate the idea.
Explore Algocdk: https://algocdk.site
Disclaimer: Algorithmic trading involves financial risk. The examples in this article are educational and are not financial advice or a guarantee of trading profits.
Top comments (0)