Resolve "Implement action fetching for Alpaca" #31
50
src/alpaca/actions.ts
Normal file
50
src/alpaca/actions.ts
Normal file
@ -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<ActionFetchResponse> => {
|
||||
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<AlpacaActivity[]>).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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user