Skip to content

A TypeScript Node.js library for the Alpaca.markets API.

License

Notifications You must be signed in to change notification settings

shaunhardy/alpaca-trade-api-ts

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

alpaca-trade-api-ts

A TypeScript Node.js library for the Alpaca.markets API.

Installation

$ npm install 117/alpaca-trade-api-ts

Client

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

Stream

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)
})

Methods

These are all the methods supported by the package.

getAccount

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))

getOrder

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))

getOrders

client
  .getOrders({
    limit: 25,
    status: 'all',
  })
  .then((orders) => console.log(`Found ${orders.length} order(s).`))
  .catch((error) => console.log(error))

placeOrder

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.

Contribute

Pull requests are encouraged. 😁

About

A TypeScript Node.js library for the Alpaca.markets API.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 100.0%