alpacahq / alpaca-trade-api-js

Node.js library for Alpaca Trade API.
https://www.npmjs.com/package/@alpacahq/alpaca-trade-api
Apache License 2.0
519 stars 136 forks source link

Can't connect to trade_ws websocket #103

Closed rrozenv closed 2 months ago

rrozenv commented 3 years ago

I am successfully able to connect to the data websocket (data_ws), but cannot connect to the trade websocket (trade_ws). I have tried on both my paper trading account as well as a live funded one.

The connection attempt to trade_ws is an endless loop of attempting to connect and eventually disconnecting. The logs are as follows:

State changed to connecting
State changed to disconnected
Disconnected
State changed to waiting to reconnect
State changed to connecting
State changed to disconnected
Disconnected
... 

I am using node version: v14.3.0 and alpaca-trade-api version: ^1.4.2. I have essentially copied the code listed in examples:

//
require('dotenv').config();
const Alpaca = require('@alpacahq/alpaca-trade-api');

// 
class WebsocketSubscriber {

    // 
    constructor({usePolygon, paper = true}) {
        // 
        this.alpaca = new Alpaca({
            keyId: paper ? process.env.APCA_PAPER_API_KEY_ID : process.env.APCA_LIVE_API_KEY_ID,
            secretKey: paper ? process.env.APCA_PAPER_API_SECRET_KEY : process.env.APCA_LIVE_API_SECRET_KEY,
            paper: paper,
            usePolygon: usePolygon
        }); 

        // 
        const data_client = this.alpaca.data_ws;

        // 
        data_client.onConnect(() => {
            console.log("Connected")
            const keys = usePolygon ? ['T.FB', 'Q.AAPL', 'A.FB', 'AM.AAPL'] :
                ['alpacadatav1/T.FB', 'alpacadatav1/Q.AAPL', 'alpacadatav1/AM.AAPL', 'alpacadatav1/AM.FB']
            data_client.subscribe(keys);
        });

        // 
        data_client.onDisconnect(() => {
            console.log("Disconnected")
        });

        // 
        data_client.onStateChange(newState => {
            console.log(`State changed to ${newState}`)
        });

        //
        data_client.onStockTrades((subject, data) => {
            console.log(`Stock trades: ${subject}, price: ${data.price}`)
        });

        // 
        data_client.onStockQuotes((subject, data) => {
            console.log(`Stock quotes: ${subject}, bid: ${data.bidprice}, ask: ${data.askprice}`)
        });

        // 
        data_client.onStockAggSec((subject, data) => {
            console.log(`Stock agg sec: ${subject}, ${data}`)
        });

        // 
        data_client.onStockAggMin((subject, data) => {
            console.log(`Stock agg min: ${subject}, ${data}`)
        });

        // 
        data_client.connect()

        // MARK: - 
        const updates_client = this.alpaca.trade_ws;

        // 
        updates_client.onConnect(() => {
            console.log("Connected")
            const trade_keys = ['trade_updates', 'account_updates']
            updates_client.subscribe(trade_keys);
        });

        // 
        updates_client.onDisconnect(() => {
            console.log("Disconnected")
        });

        // 
        updates_client.onStateChange(newState => {
            console.log(`State changed to ${newState}`)
        });

        // 
        updates_client.onOrderUpdate(data => {
            console.log(`Order updates: ${JSON.stringify(data)}`)
        });

        // 
        updates_client.onAccountUpdate(data => {
            console.log(`Account updates: ${JSON.stringify(data)}`)
        });

        // 
        updates_client.connect()
    }
}

module.exports = WebsocketSubscriber;

Is there anything else that I am missing?

Thank you!

shlomiku commented 3 years ago

Hi, the trade_ws is for trades you make. I don't see any trades being made in this code sample. general trade updates subscription is made like this: alpacadatav1/T.FB (which you did). but you get the updates through the data_ws

rrozenv commented 3 years ago

Hi @shlomikushchi , thank you for the quick reply. My point is that I can't even connect to the web socket in order to get trade updates. I know no trades are being made in this example, but I am executing orders and trades are being made.

For example:

    updates_client.onConnect(() => {
            // This never gets called because it cannot connect.
            console.log("Connected")
            const trade_keys = ['trade_updates', 'account_updates']
            updates_client.subscribe(trade_keys);
    });
shlomiku commented 3 years ago

this is the log I get running the websocket_listener.js example: image

changed to Connected2 to see 2 different connected (data_ws + updates_ws)