alpaca-trade-api-js: alpaca.getBarsV2 is not a function

following the Documentation:

getBarsV2(
  symbol,
  
  {
    limit: number,
    start: date isoformat string yyyy-mm-ddThh:MM:ss-04:00,
    end: date isoformat string yyyy-mm-ddThh:MM:ss-04:00,
    timeframe: "1Min" | "1Hour" | "1Day"
  }
) => Promise<BarsObject>

Here is my code:

const alpaca = new Alpaca({
    keyId: process.env.ALPACA_API_KEY_ID,
    secretKey: process.env.ALPACA_API_SECRET_KEY,
    paper: true,
    usePolygon: false
  })

alpaca.getBarsV2('GME', {
    limit: 100,
    start: new Date(Date.now() - 30 * 60 * 60 * 24 * 1000).toISOString(), // 30 days ago
    end: new Date().toISOString(),
    timeframe: '1Day',
})

Expected to get Bars. Got alpaca.getBarsV2 is not a function

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (6 by maintainers)

Most upvoted comments

For the time being if you’re using node.js. Here is my current solution. I decided to do the call manually.

const getHistoricalTradeData = async (symbol) => {
    
    // This gets the date time from wherever the server is located
    const { startDate, endDate } = getDateForHistoricalData(30);
    console.log("Start: ", startDate)
    console.log("End: ", endDate);
    const url = `https://data.alpaca.markets/v2/stocks/${symbol}/trades`
    const options = {
        headers: {
           'APCA-API-KEY-ID': process.env.API_KEY,
           'APCA-API-SECRET-KEY': process.env.API_SECRET
        },
        params: {
            start: startDate,
            end: endDate,
            limit: 1000,
        }
    }
    return new Promise(async (resolve, reject) => {
        await axios.get(url, options)
        .then((response) => {
            // Map response to my data format
            const result = response.data.trades.map((asset, idx) => [Date.parse(asset.t), asset.p])
            resolve(result);
        })
        .catch((err) => {
            console.log("err: ", err);
            reject([]);
        });
    })
}

an example value for startDate would be ‘2021-05-23’