Added documentation for Alpaca

This commit is contained in:
Carter Bertolini 2023-10-20 17:28:06 -04:00
parent 7ceb69e9d5
commit ad27d7280b
3 changed files with 47 additions and 0 deletions

View File

@ -5,7 +5,13 @@ import { Exchange } from '../interface/exchange'
import { PortfolioProvider } from '../interface/portfolio'
import { QuoteProvider } from '../interface/quote'
/**
* Exchange implementation for Alpaca.
*/
export class AlpacaExchange implements Exchange {
/**
* The Alpaca API client.
*/
readonly alpaca: Alpaca;
/**
@ -23,6 +29,13 @@ export class AlpacaExchange implements Exchange {
*/
readonly name: string;
/**
* Creates an instance of the Alpaca exchange.
* @constructor
* @param {string} keyId - The API key ID for the Alpaca exchange.
* @param {string} secretKey - The secret key for the Alpaca exchange.
* @param {boolean} paper - Whether to use the paper trading environment or not.
*/
constructor(keyId: string, secretKey: string, paper: boolean) {
this.alpaca = new Alpaca({
keyId: keyId,

View File

@ -1,6 +1,10 @@
import Alpaca from '@alpacahq/alpaca-trade-api'
import { PortfolioProvider, Portfolio, Position } from '../interface/portfolio';
/**
* The position object returned by Alpaca.
* @see https://alpaca.markets/docs/api-references/trading-api/positions/#properties
*/
class AlpacaPosition {
asset_id!: string;
symbol!: string;
@ -22,9 +26,18 @@ class AlpacaPosition {
asset_marginable!: string;
}
/**
* Provides a portfolio using the Alpaca API client.
*/
export class AlpacaPortfolioProvider implements PortfolioProvider {
/**
* The Alpaca API client.
*/
readonly alpaca: Alpaca;
/**
* Fetches the portfolio.
*/
readonly fetchPortfolio = (): Promise<Portfolio> => {
return (this.alpaca.getPositions() as Promise<AlpacaPosition[]>).then((positions) => {
return new Portfolio(positions.map((position) => {
@ -39,6 +52,11 @@ export class AlpacaPortfolioProvider implements PortfolioProvider {
});
}
/**
* Creates a new AlpacaPortfolioProvider instance.
* @constructor
* @param {Alpaca} alpaca - The Alpaca API client.
*/
constructor(alpaca: Alpaca) {
this.alpaca = alpaca;
}

View File

@ -1,9 +1,20 @@
import Alpaca from '@alpacahq/alpaca-trade-api'
import { QuoteProvider, Quote } from '../interface/quote';
/**
* Provides quotes using the Alpaca API.
*/
export class AlpacaQuoteProvider implements QuoteProvider {
/**
* The Alpaca API client.
*/
readonly alpaca: Alpaca;
/**
* Fetches a quote for the given stock symbol.
* @param symbol The stock symbol to fetch the quote for.
* @returns A Promise that resolves to a Quote object.
*/
readonly fetchQuote = (symbol: string): Promise<Quote> => {
return this.alpaca.getLatestQuote(symbol).then((quote) => {
return new Quote(
@ -19,6 +30,11 @@ export class AlpacaQuoteProvider implements QuoteProvider {
})
}
/**
* Creates a new AlpacaQuoteProvider instance.
* @constructor
* @param {Alpaca} alpaca - The Alpaca API client.
*/
constructor(alpaca: Alpaca) {
this.alpaca = alpaca;
}