Make quote and portfolio interfaces.

This commit is contained in:
Carter Bertolini 2023-11-10 17:23:30 -05:00
parent 97d808bca2
commit 986a0ab569
5 changed files with 46 additions and 25 deletions

View File

@ -1,6 +1,10 @@
import Alpaca from '@alpacahq/alpaca-trade-api'; import Alpaca from '@alpacahq/alpaca-trade-api';
import { ActionSide, ActionFetchOptions, ActionFetchResponse, ActionDateType } from '../interface/actions'; import { ActionSide, ActionFetchOptions, ActionFetchResponse, ActionDateType } from '../interface/actions';
/**
* The activity object returned by Alpaca.
* @see https://docs.alpaca.markets/reference/getaccountactivitiesbyactivitytype
*/
interface AlpacaActivity { interface AlpacaActivity {
id: string; id: string;
activity_type: string; activity_type: string;

View File

@ -1,5 +1,5 @@
import Alpaca from '@alpacahq/alpaca-trade-api'; import Alpaca from '@alpacahq/alpaca-trade-api';
import { PortfolioProvider, Portfolio, Position } from '../interface/portfolio'; import { PortfolioProvider, Portfolio } from '../interface/portfolio';
/** /**
* The position object returned by Alpaca. * The position object returned by Alpaca.
@ -48,7 +48,7 @@ export class AlpacaPortfolioProvider implements PortfolioProvider {
marketValue: parseFloat(position.market_value), marketValue: parseFloat(position.market_value),
costBasis: parseFloat(position.cost_basis), costBasis: parseFloat(position.cost_basis),
pricePerShare: parseFloat(position.avg_entry_price) pricePerShare: parseFloat(position.avg_entry_price)
} };
}) })
}; };
}); });

View File

@ -17,14 +17,14 @@ export class AlpacaQuoteProvider implements QuoteProvider {
*/ */
readonly fetchQuote = (symbol: string): Promise<Quote> => { readonly fetchQuote = (symbol: string): Promise<Quote> => {
return this.alpaca.getLatestQuote(symbol).then((quote) => { return this.alpaca.getLatestQuote(symbol).then((quote) => {
return new Quote( return {
quote.Symbol, symbol: quote.Symbol,
quote.AskPrice, askPrice: quote.AskPrice,
quote.AskSize, askSize: quote.AskSize,
quote.BidPrice, bidPrice: quote.BidPrice,
quote.BidSize, bidSize: quote.BidSize,
new Date(Date.parse(quote.Timestamp)) timestamp: new Date(Date.parse(quote.Timestamp))
); };
}).catch((err) => { }).catch((err) => {
return err; return err;
}); });

View File

@ -2,6 +2,7 @@
* Represents a financial position in a portfolio. * Represents a financial position in a portfolio.
*/ */
export interface Position { export interface Position {
/** /**
* The symbol name of the asset * The symbol name of the asset
*/ */
@ -32,6 +33,7 @@ export interface Position {
* Represents a portfolio of financial positions. * Represents a portfolio of financial positions.
*/ */
export interface Portfolio { export interface Portfolio {
/** /**
* An array of positions held in the portfolio. * An array of positions held in the portfolio.
*/ */

View File

@ -1,22 +1,37 @@
/** /**
* Represents a stock quote. * Represents a stock quote.
*/ */
export class Quote { export interface Quote {
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) { /**
this.symbol = symbol; * The symbol of the asset
this.askPrice = askPrice; */
this.askSize = askSize; readonly symbol: string;
this.bidPrice = bidPrice;
this.bidSize = bidSize; /**
this.timeStamp = timeStamp; * The ask price of the asset
} */
readonly askPrice: number;
/**
* The ask size of the asset
*/
readonly askSize: number;
/**
* The bid price of the asset
*/
readonly bidPrice: number;
/**
* The bid size of the asset
*/
readonly bidSize: number;
/**
* The timestamp of the quote
*/
readonly timeStamp: Date;
} }
/** /**