alpacahq / alpaca-ts

A TypeScript Node.js library for the https://alpaca.markets REST API and WebSocket streams.
ISC License
156 stars 42 forks source link

Streaming using Paper Credentials #102

Closed dimitri-deychak closed 2 years ago

dimitri-deychak commented 2 years ago

Description For market data, it appears that streaming using paper account won't work, since this codebase always defaults to the following

    switch (params.type) {
      case 'account':
        this.host = params.credentials.paper
          ? urls.websocket.account.replace('api.', 'paper-api.')
          : urls.websocket.account
        break
      case 'market_data':
        this.host = urls.websocket.market_data(this.params.source)
        break
      default:
        this.host = 'unknown'
    }

export default {
  rest: {
    account: 'https://api.alpaca.markets/v2',
    market_data_v2: 'https://data.alpaca.markets/v2',
    market_data_v1: 'https://data.alpaca.markets/v1',
  },
  websocket: {
    account: 'wss://api.alpaca.markets/stream',
    market_data: (source: DataSource = 'iex') =>
      `wss://stream.data.alpaca.markets/v2/${source}`,
  },
}

Should the url be pointed to paper endpoint for streaming too? It does not seem to work for me using paper credentials as of now.
117 commented 2 years ago

I am able to subscribe w/ paper credentials.

const { AlpacaStream } = require("@master-chief/alpaca");

const stream = new AlpacaStream({
  credentials: {
    key: "mypaperkey",
    secret: "mypapersecret",
    paper: true,
  },
  type: "market_data", // or "account"
  source: "iex", // or "sip" depending on your subscription
});

stream.on("message", (message) => {
  console.log(message);
});

stream.once("authenticated", () =>
  stream.subscribe("bars", ["SPY", "AAPL", "TSLA"])
);
{ T: 'success', msg: 'connected' }
{ T: 'success', msg: 'authenticated' }
{
  T: 'subscription',
  trades: [],
  quotes: [],
  bars: [ 'SPY', 'AAPL', 'TSLA' ],
  updatedBars: [],
  dailyBars: [],
  statuses: [],
  lulds: [],
  corrections: [],
  cancelErrors: []
}
117 commented 2 years ago

What source are you using?

dimitri-deychak commented 2 years ago

Was incorrectly assigning the event callbacks within the callback of authenticated, resolving