A TypeScript Node.js library for the Alpaca.markets API.
$ npm install 117/alpaca-trade-api-ts
New feature! Built in rate-limiter, just pass
rate_limit: true
into client options.
import { Client } from 'alpaca-trade-api-ts'
const client = new Client({
key: 'yourKeyGoesHere', // optional
secret: 'yourKeyGoesHere', // optional
rate_limit: true,
})
You can also use environment variables which will be automatically applied to every new client.
$ APCA_API_KEY_ID=yourKeyGoesHere
$ APCA_API_SECRET_KEY=yourKeyGoesHere
$ APCA_PAPER=true
Your API key allows 1 simultaneous connection to each server.
Server | URL | Enum |
---|---|---|
AccountStream |
wss://api.alpaca.markets/stream |
URL.AccountStream |
MarketDataStream |
wss://data.alpaca.markets/stream |
URL.MarketDataStream |
Connecting to these servers is easy.
import { Stream, URL } from 'alpaca-trade-api-ts'
const stream = new Stream(client, {
host: URL.MarketDataStream,
})
// to see all stream messages use .onMessage
stream.subscribe(['T.SPY'])
// will get called on each new trade event for SPY
stream.onTrade((trade) => {
console.log(trade)
})
These are all the methods supported by the package.
client
.getAccount()
.then((account) => {
console.log(`You have $${account.cash} in cash.`)
console.log(`You have $${account.buying_power} in buying power.`)
})
.catch((error) => console.log(error))
client
.getOrder({
order_id: '6187635d-04e5-485b-8a94-7ce398b2b81c',
})
.then((order) => {
console.log(
`Order ${order.side} ${order.qty} ${order.symbol} @ $${order.filled_avg_price}.`
)
})
.catch((error) => console.log(error))
client
.getOrders({
limit: 25,
status: 'all',
})
.then((orders) => console.log(`Found ${orders.length} order(s).`))
.catch((error) => console.log(error))
client
.placeOrder({
symbol: 'SPY',
qty: 1,
side: 'buy',
type: 'market',
time_in_force: 'day',
})
.then((order) => console.log(`New order placed with ID ${order.id}.`))
.catch((error) => console.log(error))
More examples are coming soon... give me some time or feel free to contribute.
Pull requests are encouraged. 😁