Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: More typescript type definitions #284

Open
2 tasks done
parris opened this issue Jan 25, 2025 · 0 comments
Open
2 tasks done

[Bug]: More typescript type definitions #284

parris opened this issue Jan 25, 2025 · 0 comments

Comments

@parris
Copy link

parris commented Jan 25, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

I like that we have the names of some parameters, but I still need to reference the API frequently to figure out the types.

Expected Behavior

I should be able to see both return types and input types of every function call.

I've ended up patching this in my own code like this:

interface AlpacaAccount {
  id: string;
  cash: string;
  equity: string;
  status: string;
  portfolio_value: string;
  crypto_status: string;
}

interface AlpacaOrder {
  id: string;
  symbol: string;
  qty: string;
  side: "buy" | "sell";
  type: "market" | "limit" | "stop" | "stop_limit" | "trailing_stop";
  time_in_force: "day" | "gtc" | "opg" | "ioc" | "fok" | "cls";
  status: string;
  filled_qty: string;
}

interface AlpacaPosition {
  symbol: string;
  qty: number;
  avg_entry_price: string;
  side: "long" | "short";
  market_value: string;
}

interface CreateOrderParams {
  symbol: string;
  qty: string;
  side: "buy" | "sell";
  type: "market" | "limit" | "stop" | "stop_limit" | "trailing_stop";
  time_in_force: "day" | "gtc" | "opg" | "ioc" | "fok" | "cls";
  limit_price?: string;
  stop_price?: number;
  trail_price?: number;
  trail_percent?: number;
}

interface GetOrdersParams {
  status: "open" | "closed" | "all";
  symbols?: string[];
  limit?: number;
  until?: string | null;
  after?: string | null;
  direction?: "asc" | "desc" | null;
  nested?: boolean;
}

// Interface for the Alpaca API
interface TypedAlpaca {
  getAccount(): Promise<AlpacaAccount>;
  createOrder(params: CreateOrderParams): Promise<AlpacaOrder>;
  getPosition(symbol: string): Promise<AlpacaPosition>;
  getOrders(params: GetOrdersParams): Promise<AlpacaOrder[]>;
  cancelOrder(orderId: string): Promise<void>;
  closePosition(symbol: string): Promise<AlpacaOrder>;
  getMultiBarsV2(
    symbols: string[],
    options?: {
      start?: string;
      end?: string;
      limit?: number;
      timeframe?:
        | `${number}Min`
        | `${number}Hour`
        | `${number}Day`
        | `${number}Week`
        | `${number}Month`;
      feed?: "iex" | "sip" | "otc";
    }
  ): Promise<Map<string, AlpacaBar[]>>;
  getLatestBars(
    symbols: string[],
    options?: {
      timeframe?:
        | `${number}Min`
        | `${number}Hour`
        | `${number}Day`
        | `${number}Week`
        | `${number}Month`;
      feed?: "iex" | "sip" | "otc";
    }
  ): Promise<Map<string, AlpacaBar>>;
  getCryptoBars(
    symbols: string[],
    options?: {
      start?: string;
      end?: string;
      limit?: number;
      timeframe?:
        | `${number}Min`
        | `${number}Hour`
        | `${number}Day`
        | `${number}Week`
        | `${number}Month`;
    }
  ): Promise<Map<string, CryptoBar[]>>;
  getLatestCryptoBars(
    symbols: string[],
    options?: {
      timeframe?:
        | `${number}Min`
        | `${number}Hour`
        | `${number}Day`
        | `${number}Week`
        | `${number}Month`;
    }
  ): Promise<Map<string, CryptoBar>>;
  getPortfolioHistory(options: {
    date_start?: string;
    date_end?: string;
    period?: string;
    timeframe?: "1Min" | "5Min" | "15Min" | "1H" | "1D" | "1W" | "1M";
    extended_hours?: string;
  }): Promise<{
    base_value: number;
    base_value_asof: string;
    equity: number[];
    profit_loss: number[];
    profit_loss_pct: number[];
    timeframe: string;
    timestamp: number[];
  }>;
  getCalendar(options?: { start?: string; end?: string }): Promise<
    {
      date: string;
      open: string;
      close: string;
      session_open: string;
      session_close: string;
      settlement_date: string;
    }[]
  >;
  getLatestQuote(
    symbol: string,
    config?: { feed?: "iex" | "sip" | "otc" }
  ): Promise<AlpacaQuote>;
}


export const alpaca = new Alpaca({
  keyId: process.env.ALPACA_API_KEY_LIVE,
  secretKey: process.env.ALPACA_API_SECRET_LIVE,
  paper: false,
  feed: "sip",
}) as TypedAlpaca;

Some of the types above might even be wrong, i know

SDK Version I encountered this issue in

"@alpacahq/alpaca-trade-api": "^3.1.3",

Steps To Reproduce

1. Install alpaca
2. Using vscode try to get some intellisense to auto complete types, notice that many of them are <any>

Filled out the Steps to Reproduce section?

  • I have entered valid steps to reproduce my issue or have attached a minimally reproducible case in code that shows my issue happening; and understand that without this my issue will be flagged as invalid and closed after 30 days.

Anything else?

Sorry for posting a bunch of issues at once - figured i drop in all the notes while i was here :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant