Begin implementing action fetching for Alpaca
This commit is contained in:
parent
ab23fb7145
commit
5f1cd195af
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