Package Exports
- @master-chief/alpaca
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@master-chief/alpaca) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
alpaca
A TypeScript Node.js library for the https://alpaca.markets REST API and WebSocket streams.
Contents
Installation
> npm i @master-chief/alpacaClient
A client for handling all account based requests.
Instance
The standard way to initialize the client.
import * as alpaca from '@master-chief/alpaca'
// create the client
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=trueMethods
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 '@master-chief/alpaca'
// create the stream
const stream = new alpaca.Stream({
credentials: {
key: 'mykey',
secret: 'mysecret',
},
host: alpaca.URL.WSS_MARKET_DATA,
})
// subscribe once authenticated with the server
stream.on('authenticated', () => stream.subscribe(['AM.SPY']))
// listen for new aggregates
stream.on('aggregate_minute', (aggregate) => {
console.log(aggregate)
})Contribute
Pull requests are encouraged. 😁