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.
* @see https://alpaca.markets/docs/api-references/trading-api/positions/#properties
*/
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;
interface 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;
}
/**
@ -40,15 +40,17 @@ export class AlpacaPortfolioProvider implements PortfolioProvider {
*/
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)
);
}));
return {
positions: positions.map((position) => {
return {
symbol: position.symbol,
quantity: parseInt(position.qty, 10),
marketValue: 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.
*/
export class Position {
export interface Position {
/**
* The symbol name of the asset
*/
@ -26,43 +26,16 @@ export class Position {
* The current asset price per share
*/
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.
*/
export class Portfolio {
export interface Portfolio {
/**
* An array of positions held in the portfolio.
*/
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;
}
}
/**