Setup Alpaca API implementation
This commit is contained in:
45
src/alpaca/portfolio.ts
Normal file
45
src/alpaca/portfolio.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user