Coder Social home page Coder Social logo

Comments (10)

nodesocket avatar nodesocket commented on September 3, 2024

I am getting a strange error using a custom written wrapper class:

'use strict';

const Log = require('./Log.js');

module.exports = class {
    constructor(alpaca, subscriptions) {
        this.client = alpaca.websocket;

        this.client.onStateChange((newState) => {
            Log.debug(`websocket state changed to ${newState}`);
        });

        this.client.onConnect(() => {
            Log.info('connected to websocket');

            this.client.subscribe([
                'trade_updates',
                'account_updates',
                'T.FB',
                'Q.AAPL',
                'A.FB',
                'AM.AAPL'
            ]);
        });

        this.client.onOrderUpdate(data => {
            Log.info(`order updates: ${JSON.stringify(data)}`);
        });

        this.client.onAccountUpdate(data => {
            Log.info(`account updates: ${JSON.stringify(data)}`);
        });

        this.client.onStockTrades((subject, data) => {
            Log.info(`stock trades: ${subject}, ${data}`);
        });

        this.client.onStockQuotes((subject, data) => {
            Log.info(`stock quotes: ${subject}, ${data}`);
        });

        this.client.onStockAggSec((subject, data) => {
            Log.info(`stock aggregate seconds: ${subject}, ${data}`);
        });

        this.client.onStockAggMin((subject, data) => {
            Log.info(`stock aggregate minutes: ${subject}, ${data}`);
        });

        this.client.onError((error) => {
            Log.error(error);
        });

        this.client.onDisconnect(() => {
            Log.info('disconnected from websocket');
        });
    }

    connect() {
        this.client.connect();
    }
};
MacBook-Pro ➜  alpaca-trade node index.js
2018-12-06T04:36:50.649Z [debug] websocket state changed to connecting
2018-12-06T04:36:50.649Z [debug] websocket state changed to authenticating
2018-12-06T04:36:50.649Z [debug] websocket state changed to connected
2018-12-06T04:36:50.649Z [info] connected to websocket
{ NatsError: 'Authorization Violation'
    at Client.processErr (/Users/justin/Sites/alpaca-trade/node_modules/nats/lib/nats.js:1114:28)
    at Client.processInbound (/Users/justin/Sites/alpaca-trade/node_modules/nats/lib/nats.js:859:28)
    at Socket.<anonymous> (/Users/justin/Sites/alpaca-trade/node_modules/nats/lib/nats.js:529:16)
    at Socket.emit (events.js:182:13)
    at addChunk (_stream_readable.js:283:12)
    at readableAddChunk (_stream_readable.js:264:11)
    at Socket.Readable.push (_stream_readable.js:219:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
  name: 'NatsError',
  message: '\'Authorization Violation\'',
  code: 'NATS_PROTOCOL_ERR',
  chainedError: undefined }

I verified that I am passing a correct AlpacakeyId and secretKey.

from alpaca-trade-api-js.

faridelnasire avatar faridelnasire commented on September 3, 2024

Ran into the exact same error yesterday. It worked a couple minutes before, then without me changing my code it just started returning those errors.

from alpaca-trade-api-js.

suddjian avatar suddjian commented on September 3, 2024

Currently to access polygon data it is required that you have opened a brokerage account. I ran the code above with my own credentials and it worked. If you have a brokerage account and are seeing this issue then it is indeed a problem.

@faridelnasire that is very strange that it was working at all... It could also be an issue with Polygon.

We are working on adding trade and quote streaming to the Alpaca data api, so you should be able to stream that data soon without requiring a brokerage account.

The streaming endpoints are definitely not well documented right now (not documented at all really) and we are planning on improving this.

from alpaca-trade-api-js.

nodesocket avatar nodesocket commented on September 3, 2024

@ChemicalRocketeer thanks for the reply. So, is there anyway without streaming, to get real-time quote prices/volume?

from alpaca-trade-api-js.

suddjian avatar suddjian commented on September 3, 2024

Unfortunately not, until more data api features are available. Creating a brokerage account with Alpaca should be a pretty easy fix in the meantime though, and should make the above code start working.

from alpaca-trade-api-js.

suddjian avatar suddjian commented on September 3, 2024

You might be able to get what you want using the getBars() function also.

from alpaca-trade-api-js.

sopko16 avatar sopko16 commented on September 3, 2024

Hey @suddjian , does this still not work? I tested this before, and I do not see it working so I wanted to ask:

image

I'm trying to get some kind of streaming data.

I've been getting it with this, and hit the limits often when I fetch any more rapidly than ~1000ms:
image

If this does not work, definitely no worries - but I just wanted to check that I'm not missing something else that is also available.

from alpaca-trade-api-js.

suddjian avatar suddjian commented on September 3, 2024

I'm afraid I can't answer that, I'm not involved with this project anymore. @umitanuki?

from alpaca-trade-api-js.

JoeOsterfeld avatar JoeOsterfeld commented on September 3, 2024

For those using node, you simply have to call client.connect(). I had the same issue, then after calling this function, the socket is working as expected.

from alpaca-trade-api-js.

smplman avatar smplman commented on September 3, 2024

This seems to be working now. I'm using a paper account and polygon: false and I am getting minuet bar data. I tried with aggregate second data and it does not work which matches what the docs say about being Polygon only.

websocket.onStockAggSec(function(data)): Register callback function for the channel 'A.<SYMBOL>'. (Polygon only)

this._dataSocket.subscribe(['alpacadatav1/AM.FB', 'alpacadatav1/AM.AAPL', 'alpacadatav1/AM.GOOG']);

[10:38:57 AM] [6/10/2020] [log] State changed to connecting
[10:38:57 AM] [6/10/2020] [log] Server started on port: 4000
[10:38:57 AM] [6/10/2020] [log] State changed to authenticating
[10:38:57 AM] [6/10/2020] [log] State changed to connected
[10:38:57 AM] [6/10/2020] [log] Connected to data socket
[10:39:05 AM] [6/10/2020] [log] Stock agg min: AM.AAPL {"ev":"AM","symbol":"AAPL","volume":77794,"accumulatedVolume":803668,"officialOpenPrice":347.98,"vwap":349.5151,"openPrice":349.93,"closePrice":350.03,"highPrice":350.03,"lowPrice":349.85,"averagePrice":349.4641,"startEpochTime":1591799880000,"endEpochTime":1591799940000}
[10:39:05 AM] [6/10/2020] [log] Stock agg min: AM.FB {"ev":"AM","symbol":"FB","volume":46717,"accumulatedVolume":314452,"officialOpenPrice":240.92,"vwap":236.1886,"openPrice":237.27,"closePrice":237.16,"highPrice":237.28,"lowPrice":237.14,"averagePrice":237.5336,"startEpochTime":1591799880000,"endEpochTime":1591799940000}

from alpaca-trade-api-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.