question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A more streamlined API.

See original GitHub issue

Client.getAsset(), Client.getAssets(), Client.getWatchlist(), Client.createWatchlist(), etc. seems like a really outdated way to do a modern API. I would suggest an Client.assets property that has an assets cache and the required methods built-in like this:

Client.assets.get(parameters?: GetAssetsParameters): Promise<Asset|Asset[]> {
    return this.request(method.GET, BaseURL.Account, parameters.asset_id_or_symbol ? "assets/" + parameters.asset_id_or_symbol : "assets?" + qs.stringify(parameters));
}

which gets all assets without parameters and specific assets using given parameters otherwise. This can be done with watchlists, account configurations, clocks, calendars, and many other methods/objects.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:29 (21 by maintainers)

github_iconTop GitHub Comments

2reactions
AqilContcommented, Jul 5, 2020

I’m too lazy so can you just commit this yourself? Here is a completely redone README.md. You have the code, so you can revise and change whatever you want before committing too:

alpaca-trade-api-ts

Version Language Maintenance Prettier(idk)

A TypeScript Node.js library for the https://alpaca.markets REST API and WebSocket streams.

Table of Contents:

Installation

> npm i 117/alpaca-trade-api-ts

alpaca.Client

A client for handling all account based requests.

Client Initialization

The standard way to initialize

// Import the Client
import { Client } from 'alpaca-trade-api-ts'

// The actual initialization
const client = new Client({
  key: '...',
  secret: '...',
  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

Due to the asynchronous nature of the client we recommended you listen for interrupts.

// Allow pending promises to resolve before exiting the process.
process.on('SIGINT', async () => {

  // Properly closes the client.
  await alpacaClient.close()

  // Then exits the process
  process.exit(0)
})

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.

alpaca.Stream Initialization

An API key is allowed 1 simultaneous connection to each server. Connecting to them is easy:

// Imports the Alpaca websocket stream API
import { Stream, BaseURL } from 'alpaca-trade-api-ts';

// Creates a websocket stream
const stream = new Stream(client, {
  host: BaseURL.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)
});

BaseURL

Contains 2 properties used for securing a connection to an Alpaca websocket:

URL Enum
wss://api.alpaca.markets/stream BaseURL.AccountStream
wss://data.alpaca.markets/stream BaseURL.MarketDataStream

Contribute

Pull requests are encouraged. 😁

2reactions
AqilContcommented, Jul 5, 2020

Also, after learning JavaScript and mainly TypeScript practices, I can never look at Java code without frustration or disgust anymore.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use APIs to Streamline Processes and Increase ...
Here are a few examples of how businesses can use APIs to streamline processes. DreamFactory Hosted Trial Signup. Generate a full-featured, ...
Read more >
6 Ways APIs Can Streamline Your Business
One way businesses are using APIs is to open up new forms of monetization. There are multiple ways to do so. For instance,...
Read more >
What Is an API and How Can I Streamline Operations ...
Learn what an API is and how to streamline your operations by using one. ... The term API is an acronym for application...
Read more >
Streamlined API Management for Developers - Himasha Guruge
In an ideal world, API management should be more aligned with the usual developer and ops workflow of an organization.
Read more >
How APIs integrate and streamline your digital ecosystem
Adopting an Application Programming Interface (API) integration is a stress-free approach to streamlining an organization's digital ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found