-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
calculate ohlcvs from trades warmup
github-actions[bot] edited this page Jun 11, 2026
·
2 revisions
'use strict';
import ccxt from '../../js/ccxt.js';
console.log('CCXT Version:', ccxt.version);
let ohlcvs = {};
async function log(exchange, symbol, timeframe) {
const market = exchange.market(symbol);
const duration = exchange.parseTimeframe(timeframe) * 1000;
console.log(exchange.iso8601(exchange.milliseconds()), 'Warming up, waiting for at least one trade...');
while (!Object.keys(ohlcvs).length) {
await exchange.throttle(1);
}
let now = exchange.milliseconds();
const start = (Math.floor(now / duration) + 1) * duration;
console.log(exchange.iso8601(exchange.milliseconds()), 'Trades started arriving, waiting till', exchange.iso8601(start));
await exchange.sleep(start - now);
console.log(exchange.iso8601(exchange.milliseconds()), 'Done warming up');
for (let i = 0;; i++) {
now = exchange.milliseconds();
let candle = Object.values(ohlcvs)[0];
console.log('');
console.log(exchange.iso8601(now), '------------------------------------------------------');
console.log('Datetime ', 'Timestamp ', ...['Open', 'High', 'Low', 'Close', market['base'], market['quote']].map((x) => x.toString().padEnd(10, ' ')));
for (let j = start; j < now; j += duration) {
if (!(j in ohlcvs)) {
ohlcvs[j] = [j, candle[4], candle[4], candle[4], candle[4], 0, 0];
}
candle = exchange.safeValue(ohlcvs, j);
console.log(exchange.iso8601(j), ...candle.map(x => x.toString().padEnd(10, ' ')));
}
ohlcvs = exchange.indexBy(Object.values(ohlcvs).slice(-1000), 0);
await exchange.sleep(1000);
}
}
async function watch(exchange, symbol, timeframe) {
console.log('Starting', exchange.id, symbol);
const duration = exchange.parseTimeframe(timeframe) * 1000;
while (true) {
try {
const trades = await exchange.watchTrades(symbol);
for (const trade of trades) { // eslint-disable-line
const timestamp = Math.floor(trade['timestamp'] / duration) * duration;
let candle = exchange.safeValue(ohlcvs, timestamp);
if (candle) {
candle[2] = Math.max(trade['price'], candle[2]);
candle[3] = Math.min(trade['price'], candle[3]);
candle[4] = trade['price'];
candle[5] = exchange.parseNumber(exchange.amountToPrecision(symbol, trade['amount'] + candle[5]));
candle[6] = exchange.parseNumber(exchange.costToPrecision(symbol, trade['cost'] + candle[6]));
}
else {
candle = [
timestamp,
trade['price'],
trade['price'],
trade['price'],
trade['price'],
exchange.parseNumber(exchange.amountToPrecision(symbol, trade['amount'])),
exchange.parseNumber(exchange.costToPrecision(symbol, trade['cost'])),
];
}
ohlcvs[timestamp] = candle;
}
}
catch (e) {
console.log(e.constructor.name, e.message);
}
}
}
async function main() {
const symbol = 'BTC/USDT';
const exchange = new ccxt.pro.binance({ 'newUpdates': true });
await exchange.loadMarkets();
const timeframe = '1m';
await Promise.all([watch(exchange, symbol, timeframe), log(exchange, symbol, timeframe)]);
await exchange.close();
}
main();(If the page is not being rendered for you, you can refer to the mirror at https://docs.ccxt.com/)
- Install
- Examples
- Manual
- CCXT Pro
- Contributing
- Supported Exchanges
- Exchanges By Country
- API Spec By Method
- FAQ
- Changelog
- Awesome
- API Spec by Exchange
- fetchCurrencies
- alpaca
- apex
- ascendex
- aster
- backpack
- bigone
- binance
- bingx
- bit2c
- bitbank
- bitbns
- bitfinex
- bitflyer
- bitget
- bithumb
- bitmart
- bitmex
- bitopro
- bitrue
- bitso
- bitstamp
- bitteam
- bittrade
- bitvavo
- blockchaincom
- blofin
- btcbox
- btcmarkets
- btcturk
- bullish
- bybit
- bydfi
- cex
- coinbase
- coinbaseexchange
- coinbaseinternational
- coincheck
- coinex
- coinmate
- coinmetro
- coinone
- coinsph
- coinspot
- cryptocom
- cryptomus
- deepcoin
- delta
- deribit
- derive
- digifinex
- dydx
- exmo
- extended
- foxbit
- gate
- gemini
- grvt
- hashkey
- hibachi
- hitbtc
- hollaex
- htx
- hyperliquid
- independentreserve
- indodax
- kraken
- krakenfutures
- kucoin
- fetchBidsAsks
- latoken
- lbank
- lighter
- luno
- mercado
- mexc
- modetrade
- ndax
- novadax
- okx
- onetrading
- p2b
- pacifica
- paradex
- paymium
- phemex
- poloniex
- tokocrypto
- toobit
- upbit
- weex
- whitebit
- woo
- woofipro
- xt
- zaif
- fetchStatus