diff --git a/src/alpaca/actions.ts b/src/alpaca/actions.ts new file mode 100644 index 0000000..bf9eb8c --- /dev/null +++ b/src/alpaca/actions.ts @@ -0,0 +1,50 @@ +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 => { + return (this.alpaca.getAccountActivities({ + activityTypes: "FILL", + until: undefined as any, + after: undefined as any, + direction: "desc", + date: undefined as any, + pageSize: undefined as any, + pageToken: undefined as any, + }) as Promise).then((activities) => { + return new ActionFetchResponse( + activities.map((activity) => { + return new Action( + activity.symbol, + parseInt(activity.qty, 10), + activity.side === "buy" ? ActionSide.Buy : ActionSide.Sell, + parseFloat(activity.price) + ); + }) + ); + }).catch((err) => { + return err; + }); + } + + constructor(alpaca: Alpaca) { + this.alpaca = alpaca; + } +} \ No newline at end of file