MARGINPAD
Home / Blog / Node.js trading bot

Node.js Crypto Trading Bot Tutorial (2026): Build & Test One Free with JavaScript

Guides · 7 min read · Updated June 2026

Prefer JavaScript over Python? You can build a full crypto trading bot in Node.js just as easily — and test it against real live prices with fake money, free, no deposit. Here's a complete, running bot with zero npm dependencies. (Educational only — not financial advice.)

What you'll build

A momentum bot: it watches a coin, opens a small long when the short-term trend flips up, and closes when it flips down. Every order hits a paper-trading API that fills at the real live price and simulates leverage, stop-loss, take-profit and liquidation — so a bug costs nothing.

Step 1 — get a free API key

Go to the MarginPad trading API, sign in with an email (no password, no KYC), and generate your key. Price and candle endpoints are keyless, so nothing stops you testing the connection first.

Step 2 — price feed & strategy (no dependencies)

Node 18+ has fetch built in. Pull candles and compute an EMA-cross signal:

const API = "https://marginpad.io/api/bot";
const KEY = "YOUR_KEY";
const H = { "X-API-Key": KEY, "content-type": "application/json" };

const price = async (s="BTC") =>
  (await (await fetch(`${API}/v1/price?symbol=${s}`)).json()).price;

const closes = async (s="BTC", interval="60", n=50) => {
  const k = await (await fetch(`${API}/v1/klines?symbol=${s}&interval=${interval}`)).json();
  return k.slice(-n).map(c => c.close);
};

const ema = (v, p) => { const k = 2/(p+1); return v.reduce((e,x)=>x*k+e*(1-k), v[0]); };

const signal = async (s="BTC") => {
  const c = await closes(s);
  return ema(c.slice(-9), 9) > ema(c.slice(-21), 21) ? "long" : "flat";
};

Step 3 — open, close & the loop

const openLong = async (s="BTC", usd=50, lev=10) => {
  const p = await price(s);
  return (await fetch(`${API}/v1/open`, { method:"POST", headers:H, body: JSON.stringify({
    symbol:s, side:"long", margin_usd:usd, leverage:lev, sl:p*0.97, tp:p*1.06 }) })).json();
};
const closeAll = async () => (await fetch(`${API}/v1/close_all`, { method:"POST", headers:H })).json();
const positions = async () => (await fetch(`${API}/v1/positions`, { headers:H })).json();

let state = "flat";
setInterval(async () => {
  const sig = await signal("BTC");
  if (sig === "long" && state === "flat") { console.log("open", await openLong()); state = "long"; }
  else if (sig === "flat" && state === "long") { console.log("close", await closeAll()); state = "flat"; }
  const pos = (await positions()).positions || [];
  const pnl = pos.reduce((a,p) => a + (p.unrealized_pnl_usd||0), 0);
  console.log(`signal=${sig} open=${pos.length} uPnL=$${pnl.toFixed(2)}`);
}, 60000);

That's a complete crypto trading bot in JavaScript — and it never touches real money. Positions liquidate exactly like a real exchange, and SL/TP execute automatically.

Step 4 — measure, then decide

Let it run across different market conditions, check GET /v1/account for win rate and realised P&L, and review the results in the Trading Journal. Compare it against a plain backtest; only fund a real account once the paper bot proves itself. For the full workflow see how to test a crypto trading bot, and the Python version if you want both.

TEST YOUR BOT FOR FREE

A free paper-trading REST API with real live prices — leverage, SL/TP and liquidation simulation. Works with any language. Get a key in 30 seconds.

Get your free API key →

Comments