Skip to content

phoobynet/alpaca-multiplexer

Repository files navigation

Alpaca Multiplexer

What does it do?

Alpaca only permits a single connection to a real-time endpoint.

What this does is provide a proxy to the specified endpoint (stock or crypto) that many clients can connect at the same time.

Should I use it?

I will not offer ANY guarantees for this library.

It was developed to help me develop personal work product built on top of Alpaca's API/WS services.

USE IT AT YOUR OWN RISK

Does it scale?

I have no idea. This is for personal use on a small number of assets.

Don't be a maniac and do something like this.

{
  "action": "subscribe",
  "trades": ["*"],
  "quotes": ["*"],
  "bars": ["*"]
}

Build

npm run build

Configuration

Environment Variables

Your Alpaca key and secret must be stored in your environment.

APCA_API_KEY_ID=<your key>
APCA_API_SECRET_KEY=<your secret>

CLI Arguments

  • alpacaURL - the Alpaca real-time url, e.g. wss://stream.data.alpaca.markets/v2/sip
  • port - whatever port you want, just not one already in use

Run

Crypto

node dist/index.js --port=3002 --alpacaURL=wss://stream.data.alpaca.markets/v1beta1/crypto

Stock

node dist/index.js --port=3003 --alpacaURL=wss://stream.data.alpaca.markets/v2/sip

Docker

Build

docker build -f docker/crypto/Dockerfile -t alpaca-multiplexer-crypto:1.0.0 .

docker build -f docker/stocks/Dockerfile -t alpaca-multiplexer-stocks:1.0.0 .

Run

docker run -d -it -p 8080:8080 -e=APCA_API_KEY_ID=<key> -e=APCA_API_SECRET_KEY=<secret> alpaca-multiplexer-crypto:1.0.0

docker run -d -it -p 8081:8081 -e=APCA_API_KEY_ID=<key> -e=APCA_API_SECRET_KEY=<secret> alpaca-multiplexer-stocks:1.0.0

Usage

Connect to the API and weight for the open signal.

Once open, you should then be able to subscribe.

The format of subscribe/unsubscribe events is the same as Alpaca's format.

Requests must be sent as a string, i.e. JSON.stringify is required.

import WebSocket, { RawData } from 'ws'

const client = new WebSocket('ws://0.0.0.0:8080')

client.on('open', () => {
  client.send(
    JSON.stringify({
      action: 'subscribe',
      trades: ['BTCUSD'],
    }),
  )
})

client.on('message', (data: RawData) => {
  console.log(JSON.parse(data.toString()))
})

setTimeout(() => {
  client.send(
    JSON.stringify({
      action: 'unsubscribe',
      trades: ['BTCUSD'],
    }),
  )
  client.close()
  process.exit(0)
}, 10_000)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published