alpacahq / alpaca-ts

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

Bar interface does not match Alpaca Bar #119

Open mozeryansky opened 9 months ago

mozeryansky commented 9 months ago

Description

I'm using getBars() which returns PageOfBars containing Bar[], and RawPageOfBars containing RawBar[].

Using typescript, I'm unable to access vw and n as it's not typed, and S is undefined.

The API returns:

{
    c: 221.72,
    h: 222.99,
    l: 206.22,
    n: 1157762,
    o: 208.3,
    t: '2022-10-13T04:00:00Z',
    v: 94549943,
    vw: 215.480385
},

However, Bar is:

export interface Bar {
  S: string;
  t: Date;
  o: number;
  h: number;
  l: number;
  c: number;
  v: number;
}

and RawBar:

export interface RawBar {
  S: string;
  t: string;
  o: number;
  h: number;
  l: number;
  c: number;
  v: number;
}

Expected

Type interface should match what the API returns. Also, I would expect raw to give an any or object type as that's truly raw. I don't see a purpose of raw otherwise, reference:

bars: (page.bars == null ? [] : page.bars).map((bar) => ({
    raw: () => bar,
    ...bar,
    t: new Date(bar.t),
})),

Reproduction

const pageOfBars = await.getBars({ ... })
const bars = pageOfBars.bars
bars.map(b => b.vm) // Property 'vm' does not exist on type 'Bar'.
117 commented 9 months ago

👀

117 commented 9 months ago

raw is not present in v7. do you mind just casting this for now?

mozeryansky commented 9 months ago

No problem, I'm not blocked, just wanted to report the bug.

117 commented 9 months ago

im struggling to find time to finish v7 😢

mozeryansky commented 9 months ago

Np. Do you know why alpaca-ts is necessary considering the alpaca-trade-api-js contains types definitions? But also why are the types mapped behind a non-typed function?

https://github.com/alpacahq/alpaca-trade-api-js/blob/master/lib/alpaca-trade-api.js#L148:

getBarsV2(symbol, options, config = this.configuration) { // <--- not typed
    return dataV2.getBars(symbol, options, config); // <-- typed, but not accessible...
}

VSCode tells me '@alpacahq/alpaca-trade-api' is 258.5k (gzipped: 79.8k), while '@master-chief/alpaca' is 111.3k (gzipped 28.8k). So I do prefer the slimmer version.

117 commented 9 months ago

code quality and extensibility

117 commented 9 months ago

v7 is pretty sweet. check out the src in #113

mozeryansky commented 9 months ago

Ok, I can try to use it from now on, but when I tried installing it using npm i git@github.com:alpacahq/alpaca-ts.git#v7, vscode says: "Cannot find module '@master-chief/alpaca-ts' or its corresponding type declarations."

My package.json shows:

"@master-chief/alpaca-ts": "github:alpacahq/alpaca-ts#v7",

I see the package and files in node_modules, and I'm importing like: import { AlpacaClient } from '@master-chief/alpaca-ts'

Did I install it correctly?

mozeryansky commented 9 months ago

Oh, specifying the path to index worked: import { Client } from '@master-chief/alpaca-ts/src/index'

However, I'm using OAuth, and it doesn't look like you support access_token auth yet. If you can add that, then I'll use it from now on.

mozeryansky commented 9 months ago

Last comment on this. I've edited the files myself:

In Client.ts I updated ClientOptions and the constructor:

interface ClientOptions {
  paper: boolean;
  credentials: {
    key: string;
    secret: string;
  } | {
    access_token: string;
  };
}

export class Client {
...
  constructor(
    options?: ClientOptions,
    HttpRequest: HttpRequestConstructor = AxiosHttpRequest
  ) {
    const { paper, credentials } = options ?? {};

    if (!credentials) {
      throw new Error("No!")
    }

    // base request object for all requests
    // changes based on paper/live mode and/or data endpoints
    this.baseHttpRequest = new HttpRequest({
      BASE:
        paper === true || paper === undefined
          ? "https://paper-api.alpaca.markets"
          : "https://api.alpaca.markets",
      HEADERS: ('access_token' in credentials)
        ? {
          "Authorization": `Bearer ${credentials.access_token}`
        } : {
          "APCA-API-KEY-ID": credentials.key,
          "APCA-API-SECRET-KEY": credentials.secret,
        },
    });
  }
...

And I was able to get the bars, including the vm property!