Replace class with interface in portfolio.

This commit is contained in:
Carter Bertolini 2023-11-10 16:51:14 -05:00
parent 65d6756582
commit 97d808bca2
2 changed files with 32 additions and 57 deletions

View File

@ -5,25 +5,25 @@ import { PortfolioProvider, Portfolio, Position } from '../interface/portfolio';
* The position object returned by Alpaca. * The position object returned by Alpaca.
* @see https://alpaca.markets/docs/api-references/trading-api/positions/#properties * @see https://alpaca.markets/docs/api-references/trading-api/positions/#properties
*/ */
class AlpacaPosition { interface AlpacaPosition {
asset_id!: string; asset_id: string;
symbol!: string; symbol: string;
exchange!: string; exchange: string;
asset_class!: string; asset_class: string;
avg_entry_price!: string; avg_entry_price: string;
qty!: string; qty: string;
qty_available!: string; qty_available: string;
side!: string; side: string;
market_value!: string; market_value: string;
cost_basis!: string; cost_basis: string;
unrealized_pl!: string; unrealized_pl: string;
unrealized_plpc!: string; unrealized_plpc: string;
unrealized_intraday_pl!: string; unrealized_intraday_pl: string;
unrealized_intraday_plpc!: string; unrealized_intraday_plpc: string;
current_price!: string; current_price: string;
lastday_price!: string; lastday_price: string;
change_today!: string; change_today: string;
asset_marginable!: string; asset_marginable: string;
} }
/** /**
@ -40,15 +40,17 @@ export class AlpacaPortfolioProvider implements PortfolioProvider {
*/ */
readonly fetchPortfolio = (): Promise<Portfolio> => { readonly fetchPortfolio = (): Promise<Portfolio> => {
return (this.alpaca.getPositions() as Promise<AlpacaPosition[]>).then((positions) => { return (this.alpaca.getPositions() as Promise<AlpacaPosition[]>).then((positions) => {
return new Portfolio(positions.map((position) => { return {
return new Position( positions: positions.map((position) => {
position.symbol, return {
parseInt(position.qty, 10), symbol: position.symbol,
parseFloat(position.market_value), quantity: parseInt(position.qty, 10),
parseFloat(position.cost_basis), marketValue: parseFloat(position.market_value),
parseFloat(position.market_value) costBasis: parseFloat(position.cost_basis),
); pricePerShare: parseFloat(position.avg_entry_price)
})); }
})
};
}); });
}; };

View File

@ -1,7 +1,7 @@
/** /**
* Represents a financial position in a portfolio. * Represents a financial position in a portfolio.
*/ */
export class Position { export interface Position {
/** /**
* The symbol name of the asset * The symbol name of the asset
*/ */
@ -26,43 +26,16 @@ export class Position {
* The current asset price per share * The current asset price per share
*/ */
readonly pricePerShare: number; 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.quantity = quantity;
this.marketValue = marketValue;
this.costBasis = costBasis;
this.pricePerShare = pricePerShare;
}
} }
/** /**
* Represents a portfolio of financial positions. * Represents a portfolio of financial positions.
*/ */
export class Portfolio { export interface Portfolio {
/** /**
* An array of positions held in the portfolio. * An array of positions held in the portfolio.
*/ */
readonly positions: Position[]; readonly positions: Position[];
/**
* Creates a new Portfolio instance.
* @constructor
* @param {Position[]} positions - An array of Position objects representing the positions in the portfolio.
*/
constructor(positions: Position[]) {
this.positions = positions;
}
} }
/** /**