aurbano / robinhood-node

:chart_with_upwards_trend: NodeJS client for Robinhood Trading :fire:
https://aurbano.github.io/robinhood-node
MIT License
694 stars 185 forks source link

How to list all the instruments #117

Open liuhongbo opened 3 years ago

liuhongbo commented 3 years ago

Code being executed:

rh.instruments('', (err, response, body) => {

            if (err){
                console.error(err);
            }
            else{

                if (body.next){
                   //which means has more pages , but how to retrieve the next page? it needs to pass a parameter cursor into the url 
                }
            }
        })

the instruments only get the first page of the instruments with the body.next has the url for next page, but seems there is no way to call the instruments method with the cursor parameter

mafischer commented 3 years ago

@liuhongbo here is a snippet of code that worked for me when using the next param to fetch more orders in the transaction history. Hopefully this helps you!

let orders = [];
let cursor = null;
let next = null;
do {
  try {
    const options = {};
    if (cursor) {
      options.cursor = cursor;
    }
    const { body } = await promisify(robinhood.orders)(options);
    orders = [...orders, ...body.results];
    if (body.next) {
      next = new URL(body.next);
      cursor = next.searchParams.get('cursor');
    } else {
      next = null;
    }
  } catch (err) {
    internal.log(err);
    cursor = null;
  }
} while (next !== null);
mafischer commented 3 years ago

also, see url in documentation