Setup Alpaca API implementation

This commit is contained in:
Carter Bertolini 2023-10-20 16:32:41 -04:00
parent 4b0a2a284a
commit 6dc8ecaf26
7 changed files with 138 additions and 101 deletions

View File

@ -1,9 +1,26 @@
import Alpaca from '@alpacahq/alpaca-trade-api' import Alpaca from '@alpacahq/alpaca-trade-api'
import { AlpacaPortfolioProvider } from './portfolio'
import { AlpacaQuoteProvider } from './quote'
import { Exchange } from '../interface/exchange' import { Exchange } from '../interface/exchange'
import { PortfolioProvider } from '../interface/portfolio'
import { QuoteProvider } from '../interface/quote'
export class AlpacaExchange implements Exchange { export class AlpacaExchange implements Exchange {
readonly alpaca: Alpaca; readonly alpaca: Alpaca;
/**
* The portfolio provider for the exchange.
*/
readonly portfolioProvider: PortfolioProvider;
/**
* The quote provider for the exchange.
*/
readonly quoteProvider: QuoteProvider;
/**
* The name of the exchange.
*/
readonly name: string; readonly name: string;
constructor(keyId: string, secretKey: string, paper: boolean) { constructor(keyId: string, secretKey: string, paper: boolean) {
@ -13,6 +30,9 @@ export class AlpacaExchange implements Exchange {
paper: paper paper: paper
}); });
this.portfolioProvider = new AlpacaPortfolioProvider(this.alpaca);
this.quoteProvider = new AlpacaQuoteProvider(this.alpaca);
this.name = 'Alpaca'; this.name = 'Alpaca';
} }
} }

45
src/alpaca/portfolio.ts Normal file
View File

@ -0,0 +1,45 @@
import Alpaca from '@alpacahq/alpaca-trade-api'
import { PortfolioProvider, Portfolio, Position } from '../interface/portfolio';
class AlpacaPosition {
asset_id!: string;
symbol!: string;
exchange!: string;
asset_class!: string;
avg_entry_price!: string;
qty!: string;
qty_available!: string;
side!: string;
market_value!: string;
cost_basis!: string;
unrealized_pl!: string;
unrealized_plpc!: string;
unrealized_intraday_pl!: string;
unrealized_intraday_plpc!: string;
current_price!: string;
lastday_price!: string;
change_today!: string;
asset_marginable!: string;
}
export class AlpacaPortfolioProvider implements PortfolioProvider {
readonly alpaca: Alpaca;
readonly fetchPortfolio = (): Promise<Portfolio> => {
return (this.alpaca.getPositions() as Promise<AlpacaPosition[]>).then((positions) => {
return new Portfolio(positions.map((position) => {
return new Position(
position.symbol,
parseInt(position.qty, 10),
parseFloat(position.market_value),
parseFloat(position.cost_basis),
parseFloat(position.market_value)
);
}));
});
}
constructor(alpaca: Alpaca) {
this.alpaca = alpaca;
}
}

25
src/alpaca/quote.ts Normal file
View File

@ -0,0 +1,25 @@
import Alpaca from '@alpacahq/alpaca-trade-api'
import { QuoteProvider, Quote } from '../interface/quote';
export class AlpacaQuoteProvider implements QuoteProvider {
readonly alpaca: Alpaca;
readonly fetchQuote = (symbol: string): Promise<Quote> => {
return this.alpaca.getLatestQuote(symbol).then((quote) => {
return new Quote(
quote.Symbol,
quote.AskPrice,
quote.AskSize,
quote.BidPrice,
quote.BidSize,
new Date(Date.parse(quote.Timestamp))
);
}).catch((err) => {
return err;
})
}
constructor(alpaca: Alpaca) {
this.alpaca = alpaca;
}
}

View File

@ -9,4 +9,4 @@ export {
PortfolioProvider, PortfolioProvider,
Quote, Quote,
QuoteProvider QuoteProvider
} }

View File

@ -20,4 +20,4 @@ export interface Exchange {
* The name of the exchange. * The name of the exchange.
*/ */
readonly name: string; readonly name: string;
} }

View File

@ -3,67 +3,46 @@
*/ */
export class Position { export class Position {
/** /**
* The price of the last trade made for this position. * The symbol name of the asset
*/
readonly lastTrade: number;
/**
* The date and time of the last trade made for this position.
*/
readonly lastTradeTime: Date;
/**
* The change in the position value.
*/
readonly change: number;
/**
* The percentage change in the position value.
*/
readonly changePercent: number;
/**
* The earnings per share of the position.
*/
readonly earningsPerShare: number;
/**
* The market capitalization of the position.
*/
readonly marketCap: number;
/**
* The symbol of the position.
*/ */
readonly symbol: string; readonly symbol: string;
/** /**
* Represents a position in a portfolio. * The total number of shares, not including open orders
* @constructor
* @param {number} lastTrade - The price of the last trade made for this position.
* @param {Date} lastTradeTime - The date and time of the last trade made for this position.
* @param {number} change - The change in the position value.
* @param {number} changePercent - The percentage change in the position value.
* @param {number} earningsPerShare - The earnings per share of the position.
* @param {number} marketCap - The market capitalization of the position.
* @param {string} symbol - The symbol of the position.
*/ */
constructor( readonly quantity: number;
lastTrade: number,
lastTradeTime: Date, /**
change: number, * The total dollar amount of the position
changePercent: number, */
earningsPerShare: number, readonly marketValue: number;
marketCap: number,
symbol: string /**
) { * The total cost basis
this.lastTrade = lastTrade; */
this.lastTradeTime = lastTradeTime; readonly costBasis: number;
this.change = change;
this.changePercent = changePercent; /**
this.earningsPerShare = earningsPerShare; * The current asset price per share
this.marketCap = marketCap; */
readonly pricePerShare: number;
/**
* Creates a new Portfolio object.
* @constructor
* @param {string} symbol - The symbol of the asset in the portfolio.
* @param {number} quantity - The quantity of the asset in the portfolio.
* @param {number} marketValue - The market value of the asset in the portfolio.
* @param {number} costBasis - The cost basis of the asset in the portfolio.
* @param {number} pricePerShare - The price per share of the asset in the portfolio.
*/
constructor(symbol: string, quantity: number, marketValue: number, costBasis: number, pricePerShare: number) {
this.symbol = symbol; this.symbol = symbol;
this.quantity = quantity;
this.marketValue = marketValue;
this.costBasis = costBasis;
this.pricePerShare = pricePerShare;
} }
} }

View File

@ -2,52 +2,20 @@
* Represents a stock quote. * Represents a stock quote.
*/ */
export class Quote { export class Quote {
/**
* The name of the company associated with this quote.
*/
readonly companyName: string;
/**
* The earnings per share of a company.
*/
readonly earningsPerShare: number;
/**
* The estimated earnings for a stock.
*/
readonly estimatedEarnings: number;
/**
* The price of the last trade for the security.
*/
readonly lastTrade: number;
/**
* The symbol of the financial instrument being quoted.
*/
readonly symbol: string; readonly symbol: string;
readonly askPrice: number;
readonly askSize: number;
readonly bidPrice: number;
readonly bidSize: number;
readonly timeStamp: Date;
/** constructor(symbol: string, askPrice: number, askSize: number, bidPrice: number, bidSize: number, timeStamp: Date) {
* Represents a quote for a particular stock.
* @constructor
* @param {string} companyName - The name of the company associated with the stock.
* @param {number} earningsPerShare - The earnings per share for the stock.
* @param {number} estimatedEarnings - The estimated earnings for the stock.
* @param {number} lastTrade - The last trade price for the stock.
* @param {string} symbol - The symbol for the stock.
*/
constructor(
companyName: string,
earningsPerShare: number,
estimatedEarnings: number,
lastTrade: number,
symbol: string
) {
this.companyName = companyName;
this.earningsPerShare = earningsPerShare;
this.estimatedEarnings = estimatedEarnings;
this.lastTrade = lastTrade;
this.symbol = symbol; this.symbol = symbol;
this.askPrice = askPrice;
this.askSize = askSize;
this.bidPrice = bidPrice;
this.bidSize = bidSize;
this.timeStamp = timeStamp;
} }
} }