From 97d808bca2848d228ffe99a4d53d8c5fdb4768ae Mon Sep 17 00:00:00 2001 From: Carter Bertolini Date: Fri, 10 Nov 2023 16:51:14 -0500 Subject: [PATCH] Replace class with interface in portfolio. --- src/alpaca/portfolio.ts | 58 ++++++++++++++++++++------------------ src/interface/portfolio.ts | 31 ++------------------ 2 files changed, 32 insertions(+), 57 deletions(-) diff --git a/src/alpaca/portfolio.ts b/src/alpaca/portfolio.ts index 728513f..105d68a 100644 --- a/src/alpaca/portfolio.ts +++ b/src/alpaca/portfolio.ts @@ -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 => { return (this.alpaca.getPositions() as Promise).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) + } + }) + }; }); }; diff --git a/src/interface/portfolio.ts b/src/interface/portfolio.ts index 1b06baa..0d26058 100644 --- a/src/interface/portfolio.ts +++ b/src/interface/portfolio.ts @@ -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; - } } /**