Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

v7 #113

Closed
wants to merge 55 commits into from
Closed

v7 #113

wants to merge 55 commits into from

Conversation

117
Copy link
Contributor

@117 117 commented Apr 6, 2023

🦙 Alpaca Trading SDK v7 (work in progress)

Note

Although this is a breaking change, the deprecated v6 will remain accessible through NPM if you are unable to make the necessary modifications to your code at this time.

Why v7?

I'm updating the Alpaca Trading SDK to include the latest endpoints for both crypto and stocks. To simplify future modifications, types have been consolidated alongside a reduction in the total number of files, lowering the overall code complexity.

Example

import { Client } from "@master-chief/alpaca-ts";

const client = new Client({
  paper: true,
  credentials: {
    key: "PK4Z802QBVAC6V4YMUV7",
    secret: "WEK6HzUjrtjTdVhvKhoQdp6DSn0ZujFOVhgUZkzZ",
  },
});

client.v2.getAccount().then((account) => {
  console.log(account);
});

Contributing

I hope that these updates will improve your overall experience with the Alpaca Trading SDK. If you have any feedback or suggestions, please let me know! Feel free to PR v7 branch.

@117 117 self-assigned this Apr 6, 2023
@117 117 added the enhancement New feature or request label Apr 6, 2023
@117 117 linked an issue Apr 6, 2023 that may be closed by this pull request
@117 117 linked an issue Apr 6, 2023 that may be closed by this pull request
@117 117 linked an issue Apr 6, 2023 that may be closed by this pull request
@117 117 mentioned this pull request Apr 6, 2023
@117 117 linked an issue Apr 6, 2023 that may be closed by this pull request
@117 117 mentioned this pull request Apr 6, 2023
@117 117 linked an issue Apr 6, 2023 that may be closed by this pull request
@iukea1
Copy link

iukea1 commented May 9, 2023

I am excited

@117
Copy link
Contributor Author

117 commented May 9, 2023

@iukea1 the endpoints are done i just need to do all the typings

@iukea1
Copy link

iukea1 commented May 9, 2023

@iukea1 the endpoints are done i just need to do all the typings

You are the man!

I am working on a NextJS project and I am excited to use the typescript equivalent of the Alpaca API

@iukea1
Copy link

iukea1 commented May 27, 2023

@iukea1 the endpoints are done i just need to do all the typings

By any chance did you get to work on this? I am just trying to figure out if I should use the other alpaca library for js.

Just I am trying to be a good boy and only use typescript (I am a noob when it comes to JS and typescript)

@117
Copy link
Contributor Author

117 commented May 30, 2023

@iukea1 I wish I had the time, I keep trying but the days slip by. 😵‍💫 If you use v6 for now it's a small lift to switch over to v7. All of the types will have the same fields.

@DaviDevMod
Copy link

@117 Love what you are doing here.

Is there any chance to get the current state of v7 as a pre-alpha release so that I can paper trade cryptos while waiting for the work to progress?

@117
Copy link
Contributor Author

117 commented Jul 24, 2023

@DaviDevMod good idea! ill put in work on this today. pre-alpha would make this release easier

@117
Copy link
Contributor Author

117 commented Aug 8, 2023

@bennycode oops. added in 7.0.12-alpha. for all of these calls they will be nested in the version in the docs

client.v2
  .getStocksBars({ symbols: "SPY", timeframe: "1Min" })
  .then(({ bars }) => {
    console.log(bars);
  });

// {
//   SPY: [
//     {
//       t: '2023-08-08T08:00:00Z',
//       o: 449.85,
//       h: 449.96,
//       l: 449.82,
//       c: 449.83,
//       v: 4226,
//       n: 30,
//       vw: 449.834134
//     },
//     ...

@bennycode
Copy link

Great, one step further! 💪

I noticed that client.v2.getAllOpenPositions() is broken with 7.0.12-alpha:

{
  "body": {
    "message": "endpoint not found."
  },
  "request": {
    "method": "GET",
    "url": "/v2/positions"
  },
  "status": 404,
  "statusText": "Not Found",
  "url": "https://data.alpaca.markets/v2/positions"
}

I guess the base URL here has to be api.alpaca.markets (or paper-api.alpaca.markets) instead of data.alpaca.markets? Is there a trick (quickfix), how I can change the base URL myself in case I discover more of these hiccups?

chore(package.json): update version to 7.0.12-alpha

chore(package-lock.json): update version to 7.0.12-alpha

refactor(positions): rename getAllOpenPositions to getPositions

feat(stocks): add temporary hack for customBase
@117
Copy link
Contributor Author

117 commented Aug 8, 2023

@bennycode what was happening is the BASE was being set on the persistent baseHttpRequest. so if you went from calling a data endpoint to a non-data endpoint, the base url was not changing.

fixed that with a temporary hack, ill revisit a more robust solution for handling the multiple possible base urls. the following is working for me:

7.0.13-alpha

client.v2
  .getStocksBars({ symbols: "SPY", timeframe: "1Min" })
  .then(({ bars }) => {
    console.log(bars);
  });

client.v2.getPositions().then((positions) => {
  console.log(positions);
});

@bennycode
Copy link

I changed from client.v2.getAllOpenPositions to client.v2.getPositions with 7.0.13-alpha and it works. 👍

@117
Copy link
Contributor Author

117 commented Aug 9, 2023 via email

@bennycode
Copy link

bennycode commented Aug 11, 2023

I appreciate your help very much! 👍 I am the creator of a Coinbase API for Node.js and know how much work it is to maintain a stable and current SDK.

I just tested client.v1beta3.getCryptoBarsLatest and noticed that it wants me to provide an exchange ("ERSX" | "CBSE" | "FTXU"). Unfortunately, the exchange param is not defined here: https://docs.alpaca.markets/reference/cryptolatestbars - Is it something new?

Update: Actually the client.v1beta3.getCryptoBarsLatest method is crashing (404 Not Found) as it tries to query /v1beta3/crypto/us/bars/latest but the actual URL is /v1beta3/crypto/{loc}/latest/bars (bars/latest vs. latest/bars).

@117
Copy link
Contributor Author

117 commented Aug 14, 2023

Back to this today. Will fix that.

fix: Import changes in BaseHttpRequest.ts
fix: URL changes in crypto.ts
fix: Version update in package.json
feat: Added prewrap function in prewrap.ts
fix: Import changes in request.ts
@117
Copy link
Contributor Author

117 commented Aug 14, 2023

I refactored methods like getStocksBars to getStockBars. Similar to getCrypto you can infer getStock to be plural or singular. So you may need to change those methods if you are using them right now.

@117
Copy link
Contributor Author

117 commented Aug 14, 2023

latest published 7.0.15-alpha

fix: Update import path

bump: Update package version
@bennycode
Copy link

Just tested client.v2.getStockBarsLatest & client.v1beta3.getCryptoBarsLatest. They are working well. Thanks. 👍

@bennycode
Copy link

bennycode commented Aug 20, 2023

Today I have been using client.v2.getAssets which returns an Asset. I noticed that Asset is not fully typed. When you retrieve a crypto asset, then it has properties like min_order_size or min_trade_increment. These props are not part of Asset.

It would be great if there was a union type for Asset and CryptoAsset, so that I could narrow down the properties with a descriminator like class:

if (asset.class === 'crypto') {
  console.log(asset.min_order_size);
}

Crypto Example

{
  "attributes": [],
  "class": "crypto",
  "easy_to_borrow": false,
  "exchange": "CRYPTO",
  "fractionable": true,
  "id": "19c466e7-e8f7-43e0-a8c5-f17ec9f5de8d",
  "maintenance_margin_requirement": 100,
  "marginable": false,
  "min_order_size": "0.015559358",
  "min_trade_increment": "0.000000001",
  "name": "Litecoin / US Dollar",
  "price_increment": "0.005",
  "shortable": false,
  "status": "active",
  "symbol": "LTC/USD",
  "tradable": true
}

Stock Example

{
  "attributes": [],
  "class": "us_equity",
  "easy_to_borrow": true,
  "exchange": "NYSE",
  "fractionable": true,
  "id": "78856c3d-67c8-43bc-8cd4-e95b686cf741",
  "maintenance_margin_requirement": 30,
  "marginable": true,
  "name": "Shopify Inc. Class A subordinate voting shares",
  "shortable": true,
  "status": "active",
  "symbol": "SHOP",
  "tradable": true
}

@bennycode
Copy link

How can client.v2.postOrder be used? It only supports a requestBody of type string:

image

@117
Copy link
Contributor Author

117 commented Aug 23, 2023

busy ):

@UchihaVeha
Copy link

Hi, is it abandoned? I need to remove raw functions from entities for memory optimization. Can I upgrade to this version from v6, or is it unfinished for production?

@aqilc
Copy link
Contributor

aqilc commented Mar 19, 2024

Damn you actually became active for once? If you need help I can take over.

@bennycode
Copy link

I'm using a combination of SDK v6 and SDK v7 to overcome any limitations. This method involves including both references in the dependencies list of the package.json file:

"dependencies": {
  "@master-chief/alpaca-ts": "^7.0.15-alpha",
  "alpaca-legacy": "npm:@master-chief/alpaca@^6.3.20"
}

Afterwards, both SDKs can be used in parallel like this:

import { Client } from "@master-chief/alpaca-ts";
import {
  AlpacaClient as LegacyClient
  // @ts-ignore:next-line
} from "alpaca-legacy";

const client = new Client(...);
const legacyClient = new LegacyClient(...);

@117
Copy link
Contributor Author

117 commented Mar 19, 2024

I'm using a combination of SDK v6 and SDK v7 to overcome any limitations. This method involves including both references in the dependencies list of the package.json file:

"dependencies": {
  "@master-chief/alpaca-ts": "^7.0.15-alpha",
  "alpaca-legacy": "npm:@master-chief/alpaca@^6.3.20"
}

Afterwards, both SDKs can be used in parallel like this:

import { Client } from "@master-chief/alpaca-ts";
import {
  AlpacaClient as LegacyClient
  // @ts-ignore:next-line
} from "alpaca-legacy";

const client = new Client(...);
const legacyClient = new LegacyClient(...);

brutal lol

@117
Copy link
Contributor Author

117 commented Mar 19, 2024

@bennycode do you like the approach/pattern of v7?

@bennycode
Copy link

@bennycode do you like the approach/pattern of v7?

Yes, I am very happy with v7! My application is quite sophisticated. If you'd like, I can provide feedback to one of your product managers.

I am using the legacy client only for placing orders and for connecting to AlpacaStream.

@117
Copy link
Contributor Author

117 commented Mar 20, 2024

@bennycode i have a new repo for this underway, spent the last 8 hours going through the docs line by line lol. this one is handwritten not codegen using api spec. will post here after i clean it up

everything other than broker api is included. i copied ALL of the endpoints over, market data, crypto included. no-lifing this for the next 48 hrs. just needs to be done once and for all

@117
Copy link
Contributor Author

117 commented Mar 22, 2024

@bennycode https://github.com/alpacahq/typescript-sdk almost done. v7 can be considered abandoned. The new repo is comprehensive, covers all endpoints (minus broker api), and all streams.

@117 117 closed this Mar 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
7 participants