alpacahq / alpaca-ts

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

Paper API trade_updates stream #24

Closed drixie closed 3 years ago

drixie commented 3 years ago

I noticed the Paper API websockets is not working. My live key authenticates to AlpacaStream, but I get the error below when I try to connect with my paper key:

Error: There was an error in authorizing your websocket connection. Object received: { "stream": "authorization", "data": { "action": "authenticate", "message": "access key verification failed", "status": "unauthorized" } }

I checked the urls.js in your code, the paper websockets endpoint is not there, so I figured either paper websockets trade_updates is probably not implemented or Alpaca made one of their confusing changes again. Paper and live accounts share the same wss endpoint for data (wss://data.alpaca.markets/stream), but the trade_updates for paper accounts is at this wss endpoint:

wss://paper-api.alpaca.markets/stream

I will appreciate if this can be integrated.

117 commented 3 years ago

Thank you for letting me know, I'll attempt to integrate this today. 😎

117 commented 3 years ago

I've gone ahead and pushed a new version which has support for paper. You can now specify whether or not the credentials are paper when creating the AlpacaStream like below:

let stream = new AlpacaStream({
  credentials: {
    key: '***',
    secret: '******',
  },
  paper: true,
  stream: 'account',
})

stream.on('authenticated', () =>
  stream.subscribe(['trade_updates'])
)

stream.on('trade_updates', (data) => console.log(data))

Hope this helps! Just make sure you are on version 2.2.4.

drixie commented 3 years ago

@117 Have you tested during a market session? Trade_updates on paper API is not working on my end. The data is just not coming through, although it authenticates successfully. Unfortunately, I cannot test trade_updates on a live account now. Market data is working fine though.

117 commented 3 years ago

I tested it outside of market hours and was able to receive a message when i canceled a paper limit order. Ill try again while the market is open.

117 commented 3 years ago

Just tested it again, here is the code and output is below.

// test.js
let stream = new AlpacaStream({
  credentials: {
    key: 'PKA4GT5XOZWI87******',
    secret: 'QV7VOZJq93PwmC9svN7xiqHKeY1z************',
  },
  paper: true,
  stream: 'account',
})

stream.on('authenticated', () =>
  stream.subscribe(['trade_updates'])
)

stream.on('trade_updates', (data) => console.log(data))

me@Air alpaca % node test.js 
Connected to the websocket.
{
  event: 'new',
  order: {
    asset_class: 'us_equity',
    asset_id: 'b28f4066-5c6d-479b-a2af-85dc1a8f16fb',
    canceled_at: null,
    client_order_id: '9f3af6b8-ee93-4760-b5bc-6c840b78d061',
    created_at: '2020-11-12T19:32:35.378212Z',
    expired_at: null,
    extended_hours: false,
    failed_at: null,
    filled_at: null,
    filled_avg_price: null,
    filled_qty: '0',
    hwm: null,
    id: '75406290-2201-4a37-8d6e-f53cb5d6d789',
    legs: null,
    limit_price: null,
    order_class: '',
    order_type: 'market',
    qty: '1',
    replaced_at: null,
    replaced_by: null,
    replaces: null,
    side: 'buy',
    status: 'new',
    stop_price: null,
    submitted_at: '2020-11-12T19:32:35.3684Z',
    symbol: 'SPY',
    time_in_force: 'day',
    trail_percent: null,
    trail_price: null,
    type: 'market',
    updated_at: '2020-11-12T19:32:35.449591Z'
  }
}
{
  event: 'fill',
  order: {
    asset_class: 'us_equity',
    asset_id: 'b28f4066-5c6d-479b-a2af-85dc1a8f16fb',
    canceled_at: null,
    client_order_id: '9f3af6b8-ee93-4760-b5bc-6c840b78d061',
    created_at: '2020-11-12T19:32:35.378212Z',
    expired_at: null,
    extended_hours: false,
    failed_at: null,
    filled_at: '2020-11-12T19:32:35.451194Z',
    filled_avg_price: '353.19',
    filled_qty: '1',
    hwm: null,
    id: '75406290-2201-4a37-8d6e-f53cb5d6d789',
    legs: null,
    limit_price: null,
    order_class: '',
    order_type: 'market',
    qty: '1',
    replaced_at: null,
    replaced_by: null,
    replaces: null,
    side: 'buy',
    status: 'filled',
    stop_price: null,
    submitted_at: '2020-11-12T19:32:35.3684Z',
    symbol: 'SPY',
    time_in_force: 'day',
    trail_percent: null,
    trail_price: null,
    type: 'market',
    updated_at: '2020-11-12T19:32:35.469497Z'
  },
  position_qty: '1',
  price: '353.19',
  qty: '1',
  timestamp: '2020-11-12T19:32:35.451194171Z'
}```
drixie commented 3 years ago

I didn't change anything in my code but it works now after a server refresh.

Thanks for your patience and fast responses!

drixie commented 3 years ago

.