49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import Alpaca from '@alpacahq/alpaca-trade-api';
|
|
import { Action, ActionSide, ActionFetchOptions, ActionFetchResponse } from '../interface/actions';
|
|
|
|
class AlpacaActivity {
|
|
id!: string;
|
|
activity_type!: string;
|
|
transaction_time!: string;
|
|
type!: string;
|
|
price!: string;
|
|
qty!: string;
|
|
side!: string;
|
|
symbol!: string;
|
|
leaves_qty!: string;
|
|
order_id!: string;
|
|
cum_qty!: string;
|
|
order_status!: string;
|
|
}
|
|
|
|
export class AlpacaActionProvider {
|
|
readonly alpaca: Alpaca;
|
|
|
|
readonly fetchActions = (options: ActionFetchOptions): Promise<ActionFetchResponse> => {
|
|
return (this.alpaca.getAccountActivities({
|
|
activityTypes: "FILL",
|
|
direction: "desc",
|
|
}) as Promise<AlpacaActivity[]>).then((activities) => {
|
|
return new ActionFetchResponse(
|
|
activities
|
|
.filter((activity) => activity.order_status === "filled")
|
|
.map((activity) => {
|
|
return new Action(
|
|
activity.symbol,
|
|
parseInt(activity.qty, 10),
|
|
activity.side === "buy" ? ActionSide.Buy : ActionSide.Sell,
|
|
parseFloat(activity.price)
|
|
);
|
|
}),
|
|
undefined
|
|
);
|
|
}).catch((err) => {
|
|
return err;
|
|
});
|
|
};
|
|
|
|
constructor(alpaca: Alpaca) {
|
|
this.alpaca = alpaca;
|
|
}
|
|
}
|