alpacahq / alpaca-ts

A TypeScript Node.js library for the https://alpaca.markets REST API and WebSocket streams.
ISC License
154 stars 42 forks source link

A more streamlined API. #5

Closed aqilc closed 4 years ago

aqilc commented 4 years ago

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.

aqilc commented 4 years ago

Also, camelCase is super outdated xd

117 commented 4 years ago

Also, camelCase is super outdated xd

I'm ex-Java it's second nature to me.. :(

aqilc commented 4 years ago

Ohhh, I have a few friends who also retain Java practices when starting javascript. I am telling you, it will make you end up in a deep, unescapable hole of regret in many projects as these two languages are extremely different and you need a fresh mind for both.

aqilc commented 4 years ago

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

117 commented 4 years ago

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

Java is for dinosaurs.

117 commented 4 years ago

Also adding on to your initial suggestion... could you explain a bit more what you mean by assets cache? Because assets and some of their fields may change minute by minute, a cache of them wouldn't be all that helpful in that sense.

aqilc commented 4 years ago

Ohhh, I am a bit new at the stock market and some people just asked me to try making a bot. I found out Alpaca like 2 days ago and now I'm here xd

117 commented 4 years ago

Welcome to the Alpaca community! :) You ok with me closing this issue or is there more you'd like to add for streamlining the package?

aqilc commented 4 years ago

Oh yeah, two things:

aqilc commented 4 years ago

Also, I wanted to push something else but keep getting this error(I didn't feel like opening an issue because this is probably me being extremely dumb): image

aqilc commented 4 years ago

I usually only have private repos on personal projects so I'm not used to pushing much, and would love some help xd.

aqilc commented 4 years ago

If there's some permission problem, I can just send the file over.

117 commented 4 years ago

Also, I wanted to push something else but keep getting this error(I didn't feel like opening an issue because this is probably me being extremely dumb): image

No one can push directly to repo except for owners or collaborators. If you wish to make changes you must first fork the repo to your own account, commit changes there, then submit a pull request.

aqilc commented 4 years ago

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. 😁

aqilc commented 4 years ago

Oh you should add a some of the returns of the functions at the end. The previous code was so loosely written that it wouldn't have helped much anyways until a more thorough documentation of the returned classes comes out.

117 commented 4 years ago

That readme looks great!

aqilc commented 4 years ago

Thanks!! :D

aqilc commented 4 years ago

Uh can you extract the raw code from it? I don't know if you completely can... I can properly push if you are having problems xd or do a hackaround consisting of changing the file type to .log and attaching it

aqilc commented 4 years ago

Also, I know you are a Java person so you used IntelliJ iDEA a lot before, but Visual Studio Code is a lot better suited for Javascript and Node.js in general for me. Maybe try it out someday :D I can recommend a large suite of extensions too if you need some good ones(I've spent literal days looking for them).

aqilc commented 4 years ago

Also, can you link the direct website resources and API responses from the official Alpaca website? It would be helpful.

aqilc commented 4 years ago

Also, uh you don't have any descriptions filled out in the script files themselves, which are also sort of needed if you really want this to be complete. I'm imagining a giant "docs" phase of development now xd.

aqilc commented 4 years ago

I usually just make the docs as I go in my projects.

117 commented 4 years ago

Also, can you link the direct website resources and API responses from the official Alpaca website? It would be helpful.

https://alpaca.markets/docs/api-documentation/api-v2/account/

aqilc commented 4 years ago

No, I meant in the docs itself lmao.

aqilc commented 4 years ago

Actually, I guess it would be super unnecessary to implement it for every class. I think you should just finally add returned types and classes and it should be awesome.

117 commented 4 years ago

No, I meant in the docs itself lmao.

Ah yeah thats a good idea.

aqilc commented 4 years ago

ok so i commited a lot of things in a new pull request

aqilc commented 4 years ago

uhhh is it possible for you to accept my friend request and talk on Discord? I sent one a bit ago but you never responded...

aqilc commented 4 years ago

I want to discuss further plans there, since GitHub isn't the best place for it.