alpaca
![Prettier(idk)](https://img.shields.io/static/v1?label=code%20style&message=prettier&color=ff51bc&style=flat-square)
A TypeScript Node.js library for the https://alpaca.markets REST API and
WebSocket streams.
Contents
Installation
> npm i @master-chief/alpaca
Client
A client for handling all account based requests.
Instance
The standard way to initialize the client.
import * as alpaca from 'alpaca-trade-api-ts'
const client = new alpaca.Client({
credentials: {
key: 'mykey',
secret: 'mysecret',
},
paper: true,
rate_limit: true,
})
You can also use environment variables which will be applied to every new
client.
> set APCA_API_KEY_ID=yourKeyGoesHere
> set APCA_API_SECRET_KEY=yourKeyGoesHere
> set APCA_PAPER=true
Methods
All Client instance methods.
isAuthenticated
Checks if the client is authenticated.
await client.isAuthenticated()
getAccount
Connects to an Alpaca account.
await client.getAccount()
getOrder
Gets a specific order.
await client.getOrder({
order_id: '6187635d-04e5-485b-8a94-7ce398b2b81c',
})
getOrders
Gets all orders made by the client.
await client.getOrders({
limit: 25,
status: 'all',
})
placeOrder
Places an order using your account.
await client.placeOrder({
symbol: 'SPY',
qty: 1,
side: 'buy',
type: 'market',
time_in_force: 'day',
})
replaceOrder
Re-places an order(to change some details maybe).
await client.replaceOrder({
order_id: '69a3db8b-cc63-44da-a26a-e3cca9490308',
limit_price: 9.74,
})
cancelOrder
Cancels an order.
await client.cancelOrder({
order_id: '69a3db8b-cc63-44da-a26a-e3cca9490308',
})
cancelOrders
Cancels every single order(be sure to not make a typo here!)
await client.cancelOrders()
More examples are coming soon... give me some time or feel free to contribute.
Stream
An Alpaca websocket API for streamlining the exchange of requests and data to
and from the Alpaca servers.
Instance
An API key is allowed 1 simultaneous connection to each server. Connecting to
them is easy:
import * as alpaca from 'alpaca-trade-api-ts'
const stream = new alpaca.Stream({
credentials: {
key: 'mykey',
secret: 'mysecret',
},
host: alpaca.URL.WSS_MARKET_DATA,
})
stream.on('authenticated', () => stream.subscribe(['AM.SPY']))
stream.on('aggregate_minute', (aggregate) => {
console.log(aggregate)
})
Contribute
Pull requests are encouraged. 😁