question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Best practice to watch huge number of tickers

See original GitHub issue

Is it good practice, in Node.js case, to create one infinite while cycle for each symbol like this:

// !!! Can contain 700+ symbols.
const symbols = [
    '1INCH-PERP',
    'ADA-PERP'
];
// Solution 1
const exchange = new ccxt.ftx();

function start() {
    for (let symbol of symbols) {
        watchTicker(symbol);
    }
}

// One infinite while for each symbol will be created, so 700+ 'while' cycles.
// All processed in async way.
async function watchTicker(symbol) {
    while (true) {
        try {
            let ticker = await exchange.watchTicker(symbol);

           // do magic with ticker ...
        }
        catch (e) {
            console.log(e);
        }
    }
}

start();

Currently, we process all tickets in one while cycle, but considering switching to above solution. Current solution:

// Solution 2
const exchange = new ccxt.ftx();

function subscribeAll() {
    for (let symbol of symbols) {
        subscribe(symbol);
    }
}

async function subscribe(symbol) {
    try {
        await exchange.watchTicker(symbol);
    }
    catch (e) {
        console.log(e);
    }
}

// Contains only one infinite while for all subscribed symbols,
// but each ticker is processed synchronously one by one.
async function process() {
    while(true) {
        for (let symbol in exchange.tickers) {
            let ticker = exchange.tickers[symbol];

            // do magic with ticker ...
        }

        await delay(1000);
    }
}

function delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

subscribeAll();
process();
  • OS: Debian (Docker)
  • Programming Language version: JavaScript (Node.js)
  • CCXT version: pro 0.9.35

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:18 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
kroitorcommented, Dec 10, 2021

btw, maybe we can even add that as another ‘method’ type variable in fetchticker/s ? (here and here) [Not sure atm, how the parameter should be set, but actually, may apps/use-cases might just want bid/ask data for ‘all-tickers’ and this method will be useful for many users ?

Yeah, we can think of how to better merge the two methods with user-controllable options.

1reaction
kroitorcommented, Dec 9, 2021

@kroitor can you mention also 2 words, whether ‘fetchTickers’ is/isn’t suitable in such cases? if latency is not high, in every 500ms they might be obtainable, while seems to be quite enough for OP. (as he makes ‘process’ function to run after every 1000 ms delay, and I don’t see the app is that much untold-critical dependant on 500ms diapason).

Some of the bots I’ve worked with were much closer to real-time, dealing with milliseconds and sub-ms time precision. However, if +/- 500ms is not critical for this specific use-case, then fetchTicker/fetchTickers should be ok. Mind, however, that some exchanges (especially, Binance, the biggest one) will impose higher rate limits on fetchTickers, so you won’t be able to call it with Binance once or twice a second – they will ban you temporarily to cool off.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Building An Effective Watchlist - Investopedia
It's a good idea to devote at least one screen entirely to tickers, with each entry displaying just two or three fields, including...
Read more >
How to Always Find the Top Stocks to Watch - YouTube
It's a great way to prep for the upcoming week every Sunday.) We're not looking to be first, and you shouldn't either.
Read more >
Opinion: Here's the best way to spot stock-market winners ...
Mark Mahaney says look for high-quality companies whose stock price has fallen 20% or more.
Read more >
Learn How to Create a Stock Watch List with MarketSmith
A good way to gauge those things is to look at the stocks rated highest by Relative Strength or Earnings Per Share (EPS)...
Read more >
7 Top Stocks to Watch in November 2022 - StocksToTrade
Building your watchlist every day is the best way to get the highest-potential trades in your sights. I also send out a NO-COST...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found