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

Bar interface does not match Alpaca Bar #119

Open
mozeryansky opened this issue Oct 11, 2023 · 10 comments
Open

Bar interface does not match Alpaca Bar #119

mozeryansky opened this issue Oct 11, 2023 · 10 comments
Assignees
Labels
bug Something isn't working

Comments

@mozeryansky
Copy link

mozeryansky commented Oct 11, 2023

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'.
@mozeryansky mozeryansky added the bug Something isn't working label Oct 11, 2023
@mozeryansky mozeryansky changed the title Bar interface does not match how Alpaca Bar Bar interface does not match Alpaca Bar Oct 11, 2023
@117
Copy link
Contributor

117 commented Oct 11, 2023

👀

@117
Copy link
Contributor

117 commented Oct 11, 2023

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

@mozeryansky
Copy link
Author

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

@117
Copy link
Contributor

117 commented Oct 11, 2023

im struggling to find time to finish v7 😢

@mozeryansky
Copy link
Author

mozeryansky commented Oct 11, 2023

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
Copy link
Contributor

117 commented Oct 11, 2023

code quality and extensibility

@117
Copy link
Contributor

117 commented Oct 11, 2023

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

@mozeryansky
Copy link
Author

Ok, I can try to use it from now on, but when I tried installing it using npm i [email protected]: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
Copy link
Author

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
Copy link
Author

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!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants