@adaptic/utils 0.1.49 → 0.1.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +58226 -229
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +58193 -229
- package/dist/index.mjs.map +1 -1
- package/dist/types/alpaca/client.d.ts +105 -0
- package/dist/types/alpaca/client.d.ts.map +1 -0
- package/dist/types/alpaca/crypto/data.d.ts +281 -0
- package/dist/types/alpaca/crypto/data.d.ts.map +1 -0
- package/dist/types/alpaca/crypto/index.d.ts +75 -0
- package/dist/types/alpaca/crypto/index.d.ts.map +1 -0
- package/dist/types/alpaca/crypto/orders.d.ts +221 -0
- package/dist/types/alpaca/crypto/orders.d.ts.map +1 -0
- package/dist/types/alpaca/index.d.ts +204 -0
- package/dist/types/alpaca/index.d.ts.map +1 -0
- package/dist/types/alpaca/market-data/bars.d.ts +142 -0
- package/dist/types/alpaca/market-data/bars.d.ts.map +1 -0
- package/dist/types/alpaca/market-data/index.d.ts +13 -0
- package/dist/types/alpaca/market-data/index.d.ts.map +1 -0
- package/dist/types/alpaca/market-data/news.d.ts +87 -0
- package/dist/types/alpaca/market-data/news.d.ts.map +1 -0
- package/dist/types/alpaca/market-data/quotes.d.ts +85 -0
- package/dist/types/alpaca/market-data/quotes.d.ts.map +1 -0
- package/dist/types/alpaca/market-data/trades.d.ts +98 -0
- package/dist/types/alpaca/market-data/trades.d.ts.map +1 -0
- package/dist/types/alpaca/options/contracts.d.ts +279 -0
- package/dist/types/alpaca/options/contracts.d.ts.map +1 -0
- package/dist/types/alpaca/options/data.d.ts +126 -0
- package/dist/types/alpaca/options/data.d.ts.map +1 -0
- package/dist/types/alpaca/options/index.d.ts +17 -0
- package/dist/types/alpaca/options/index.d.ts.map +1 -0
- package/dist/types/alpaca/options/orders.d.ts +366 -0
- package/dist/types/alpaca/options/orders.d.ts.map +1 -0
- package/dist/types/alpaca/options/strategies.d.ts +224 -0
- package/dist/types/alpaca/options/strategies.d.ts.map +1 -0
- package/dist/types/alpaca/streams/base-stream.d.ts +143 -0
- package/dist/types/alpaca/streams/base-stream.d.ts.map +1 -0
- package/dist/types/alpaca/streams/crypto-stream.d.ts +173 -0
- package/dist/types/alpaca/streams/crypto-stream.d.ts.map +1 -0
- package/dist/types/alpaca/streams/index.d.ts +54 -0
- package/dist/types/alpaca/streams/index.d.ts.map +1 -0
- package/dist/types/alpaca/streams/option-stream.d.ts +167 -0
- package/dist/types/alpaca/streams/option-stream.d.ts.map +1 -0
- package/dist/types/alpaca/streams/stock-stream.d.ts +176 -0
- package/dist/types/alpaca/streams/stock-stream.d.ts.map +1 -0
- package/dist/types/alpaca/streams/stream-manager.d.ts +277 -0
- package/dist/types/alpaca/streams/stream-manager.d.ts.map +1 -0
- package/dist/types/alpaca/streams/trading-stream.d.ts +186 -0
- package/dist/types/alpaca/streams/trading-stream.d.ts.map +1 -0
- package/dist/types/alpaca/trading/account.d.ts +198 -0
- package/dist/types/alpaca/trading/account.d.ts.map +1 -0
- package/dist/types/alpaca/trading/bracket-orders.d.ts +162 -0
- package/dist/types/alpaca/trading/bracket-orders.d.ts.map +1 -0
- package/dist/types/alpaca/trading/clock.d.ts +99 -0
- package/dist/types/alpaca/trading/clock.d.ts.map +1 -0
- package/dist/types/alpaca/trading/index.d.ts +15 -0
- package/dist/types/alpaca/trading/index.d.ts.map +1 -0
- package/dist/types/alpaca/trading/oco-orders.d.ts +203 -0
- package/dist/types/alpaca/trading/oco-orders.d.ts.map +1 -0
- package/dist/types/alpaca/trading/order-utils.d.ts +404 -0
- package/dist/types/alpaca/trading/order-utils.d.ts.map +1 -0
- package/dist/types/alpaca/trading/orders.d.ts +199 -0
- package/dist/types/alpaca/trading/orders.d.ts.map +1 -0
- package/dist/types/alpaca/trading/oto-orders.d.ts +282 -0
- package/dist/types/alpaca/trading/oto-orders.d.ts.map +1 -0
- package/dist/types/alpaca/trading/positions.d.ts +389 -0
- package/dist/types/alpaca/trading/positions.d.ts.map +1 -0
- package/dist/types/alpaca/trading/smart-orders.d.ts +301 -0
- package/dist/types/alpaca/trading/smart-orders.d.ts.map +1 -0
- package/dist/types/alpaca/trading/trailing-stops.d.ts +247 -0
- package/dist/types/alpaca/trading/trailing-stops.d.ts.map +1 -0
- package/dist/types/config/api-endpoints.d.ts +94 -0
- package/dist/types/config/api-endpoints.d.ts.map +1 -0
- package/dist/types/errors/index.d.ts +130 -0
- package/dist/types/errors/index.d.ts.map +1 -0
- package/dist/types/examples/rate-limiter-example.d.ts +7 -0
- package/dist/types/examples/rate-limiter-example.d.ts.map +1 -0
- package/dist/types/http-timeout.d.ts +37 -0
- package/dist/types/http-timeout.d.ts.map +1 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/logger.d.ts +68 -0
- package/dist/types/logger.d.ts.map +1 -0
- package/dist/types/massive.d.ts +167 -0
- package/dist/types/massive.d.ts.map +1 -0
- package/dist/types/rate-limiter.d.ts +173 -0
- package/dist/types/rate-limiter.d.ts.map +1 -0
- package/dist/types/risk-free-rate.d.ts +153 -0
- package/dist/types/risk-free-rate.d.ts.map +1 -0
- package/dist/types/schemas/alpaca-schemas.d.ts +779 -0
- package/dist/types/schemas/alpaca-schemas.d.ts.map +1 -0
- package/dist/types/schemas/alphavantage-schemas.d.ts +255 -0
- package/dist/types/schemas/alphavantage-schemas.d.ts.map +1 -0
- package/dist/types/schemas/index.d.ts +21 -0
- package/dist/types/schemas/index.d.ts.map +1 -0
- package/dist/types/schemas/massive-schemas.d.ts +561 -0
- package/dist/types/schemas/massive-schemas.d.ts.map +1 -0
- package/dist/types/schemas/validate-response.d.ts +88 -0
- package/dist/types/schemas/validate-response.d.ts.map +1 -0
- package/dist/types/trading-policy/defaults/default-trading-policy.d.ts +29 -0
- package/dist/types/trading-policy/defaults/default-trading-policy.d.ts.map +1 -0
- package/dist/types/trading-policy/enums.d.ts +85 -0
- package/dist/types/trading-policy/enums.d.ts.map +1 -0
- package/dist/types/trading-policy/index.d.ts +13 -2
- package/dist/types/trading-policy/index.d.ts.map +1 -1
- package/dist/types/trading-policy/schemas/asset-universe-prefs.schema.d.ts +182 -0
- package/dist/types/trading-policy/schemas/asset-universe-prefs.schema.d.ts.map +1 -0
- package/dist/types/trading-policy/schemas/audit-notification-prefs.schema.d.ts +112 -0
- package/dist/types/trading-policy/schemas/audit-notification-prefs.schema.d.ts.map +1 -0
- package/dist/types/trading-policy/schemas/autonomy-prefs.schema.d.ts +369 -0
- package/dist/types/trading-policy/schemas/autonomy-prefs.schema.d.ts.map +1 -0
- package/dist/types/trading-policy/schemas/effective-policy.schema.d.ts +1703 -0
- package/dist/types/trading-policy/schemas/effective-policy.schema.d.ts.map +1 -0
- package/dist/types/trading-policy/schemas/execution-prefs.schema.d.ts +122 -0
- package/dist/types/trading-policy/schemas/execution-prefs.schema.d.ts.map +1 -0
- package/dist/types/trading-policy/schemas/index.d.ts +13 -0
- package/dist/types/trading-policy/schemas/index.d.ts.map +1 -0
- package/dist/types/trading-policy/schemas/model-prefs.schema.d.ts +249 -0
- package/dist/types/trading-policy/schemas/model-prefs.schema.d.ts.map +1 -0
- package/dist/types/trading-policy/schemas/overlay-response-prefs.schema.d.ts +217 -0
- package/dist/types/trading-policy/schemas/overlay-response-prefs.schema.d.ts.map +1 -0
- package/dist/types/trading-policy/schemas/policy-mutation.schema.d.ts +3031 -0
- package/dist/types/trading-policy/schemas/policy-mutation.schema.d.ts.map +1 -0
- package/dist/types/trading-policy/schemas/portfolio-construction-prefs.schema.d.ts +258 -0
- package/dist/types/trading-policy/schemas/portfolio-construction-prefs.schema.d.ts.map +1 -0
- package/dist/types/trading-policy/schemas/position-management-prefs.schema.d.ts +218 -0
- package/dist/types/trading-policy/schemas/position-management-prefs.schema.d.ts.map +1 -0
- package/dist/types/trading-policy/schemas/risk-budget-prefs.schema.d.ts +188 -0
- package/dist/types/trading-policy/schemas/risk-budget-prefs.schema.d.ts.map +1 -0
- package/dist/types/trading-policy/schemas/signal-consumption-prefs.schema.d.ts +274 -0
- package/dist/types/trading-policy/schemas/signal-consumption-prefs.schema.d.ts.map +1 -0
- package/dist/types/types/alpaca-types.d.ts +108 -17
- package/dist/types/types/alpaca-types.d.ts.map +1 -1
- package/dist/types/utils/paginator.d.ts +154 -0
- package/dist/types/utils/paginator.d.ts.map +1 -0
- package/dist/types/utils/retry.d.ts +83 -0
- package/dist/types/utils/retry.d.ts.map +1 -0
- package/package.json +5 -1
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Trading Stream Module
|
|
3
|
+
* WebSocket stream for real-time trading updates (order fills, cancellations, etc.)
|
|
4
|
+
*
|
|
5
|
+
* Features:
|
|
6
|
+
* - Real-time order status updates
|
|
7
|
+
* - Automatic reconnection with exponential backoff
|
|
8
|
+
* - Type-safe event handling
|
|
9
|
+
* - Comprehensive logging
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const client = createAlpacaClient(config);
|
|
14
|
+
* const stream = createTradingStream(client);
|
|
15
|
+
*
|
|
16
|
+
* stream.on('fill', (update) => {
|
|
17
|
+
* console.log(`Order filled: ${update.order.symbol} @ ${update.price}`);
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* stream.on('trade_update', (update) => {
|
|
21
|
+
* console.log(`Trade update: ${update.event}`);
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* await stream.connect();
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
import { AlpacaClient } from "../client";
|
|
28
|
+
import { BaseStream, StreamConfig } from "./base-stream";
|
|
29
|
+
import { TradeUpdate } from "../../types/alpaca-types";
|
|
30
|
+
/**
|
|
31
|
+
* Trading stream event names representing all possible order status changes.
|
|
32
|
+
* These events are emitted when order state changes occur.
|
|
33
|
+
*/
|
|
34
|
+
export type TradingStreamEvent = "new" | "fill" | "partial_fill" | "canceled" | "expired" | "done_for_day" | "replaced" | "rejected" | "pending_new" | "pending_cancel" | "pending_replace" | "calculated" | "suspended" | "order_cancel_rejected" | "order_replace_rejected" | "stopped" | "accepted" | "accepted_for_bidding";
|
|
35
|
+
/**
|
|
36
|
+
* Trading stream event map for type-safe event handling.
|
|
37
|
+
* Maps event names to their payload types.
|
|
38
|
+
*/
|
|
39
|
+
export interface TradingStreamEventMap {
|
|
40
|
+
/** Generic trade update event - emitted for all order changes */
|
|
41
|
+
trade_update: TradeUpdate;
|
|
42
|
+
/** Emitted when stream is authenticated */
|
|
43
|
+
authenticated: void;
|
|
44
|
+
/** Emitted when stream is connected */
|
|
45
|
+
connected: void;
|
|
46
|
+
/** Emitted when stream is disconnected */
|
|
47
|
+
disconnected: {
|
|
48
|
+
code: number;
|
|
49
|
+
reason: string;
|
|
50
|
+
};
|
|
51
|
+
/** Emitted on stream errors */
|
|
52
|
+
error: Error;
|
|
53
|
+
/** Emitted when max reconnection attempts reached */
|
|
54
|
+
max_reconnects: void;
|
|
55
|
+
new: TradeUpdate;
|
|
56
|
+
fill: TradeUpdate;
|
|
57
|
+
partial_fill: TradeUpdate;
|
|
58
|
+
canceled: TradeUpdate;
|
|
59
|
+
expired: TradeUpdate;
|
|
60
|
+
done_for_day: TradeUpdate;
|
|
61
|
+
replaced: TradeUpdate;
|
|
62
|
+
rejected: TradeUpdate;
|
|
63
|
+
pending_new: TradeUpdate;
|
|
64
|
+
pending_cancel: TradeUpdate;
|
|
65
|
+
pending_replace: TradeUpdate;
|
|
66
|
+
calculated: TradeUpdate;
|
|
67
|
+
suspended: TradeUpdate;
|
|
68
|
+
order_cancel_rejected: TradeUpdate;
|
|
69
|
+
order_replace_rejected: TradeUpdate;
|
|
70
|
+
stopped: TradeUpdate;
|
|
71
|
+
accepted: TradeUpdate;
|
|
72
|
+
accepted_for_bidding: TradeUpdate;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Trading Stream class for receiving real-time order updates.
|
|
76
|
+
*
|
|
77
|
+
* Connects to Alpaca's trading WebSocket and provides real-time
|
|
78
|
+
* updates on order status changes including fills, cancellations,
|
|
79
|
+
* rejections, and more.
|
|
80
|
+
*
|
|
81
|
+
* @extends BaseStream
|
|
82
|
+
*/
|
|
83
|
+
export declare class TradingStream extends BaseStream {
|
|
84
|
+
protected readonly streamName = "TradingStream";
|
|
85
|
+
private tradeUpdateCallback;
|
|
86
|
+
private orderCallbacks;
|
|
87
|
+
constructor(client: AlpacaClient, config?: Partial<StreamConfig>);
|
|
88
|
+
/**
|
|
89
|
+
* Get the WebSocket URL for trading stream
|
|
90
|
+
*/
|
|
91
|
+
protected getStreamUrl(): string;
|
|
92
|
+
/**
|
|
93
|
+
* Override authenticate to use trading stream format
|
|
94
|
+
*/
|
|
95
|
+
protected authenticate(): Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* Subscribe to trade updates after authentication
|
|
98
|
+
*/
|
|
99
|
+
private subscribeToTradeUpdates;
|
|
100
|
+
/**
|
|
101
|
+
* Process incoming messages
|
|
102
|
+
*/
|
|
103
|
+
protected processMessage(message: Record<string, unknown>): void;
|
|
104
|
+
/**
|
|
105
|
+
* Handle listening confirmation message
|
|
106
|
+
*/
|
|
107
|
+
private handleListeningMessage;
|
|
108
|
+
/**
|
|
109
|
+
* Handle trade update message
|
|
110
|
+
*/
|
|
111
|
+
private handleTradeUpdate;
|
|
112
|
+
/**
|
|
113
|
+
* Check if an event represents a terminal order state
|
|
114
|
+
*/
|
|
115
|
+
private isTerminalState;
|
|
116
|
+
/**
|
|
117
|
+
* Register a callback for trade updates
|
|
118
|
+
* @param callback Function to call when trade updates are received
|
|
119
|
+
*/
|
|
120
|
+
onTradeUpdate(callback: (update: TradeUpdate) => void): void;
|
|
121
|
+
/**
|
|
122
|
+
* Remove the trade update callback
|
|
123
|
+
*/
|
|
124
|
+
removeTradeUpdateCallback(): void;
|
|
125
|
+
/**
|
|
126
|
+
* Register a callback for a specific order's updates.
|
|
127
|
+
* The callback is automatically removed when the order reaches a terminal state.
|
|
128
|
+
*
|
|
129
|
+
* @param orderId The order ID to watch
|
|
130
|
+
* @param callback Function to call when updates for this order are received
|
|
131
|
+
*/
|
|
132
|
+
watchOrder(orderId: string, callback: (update: TradeUpdate) => void): void;
|
|
133
|
+
/**
|
|
134
|
+
* Stop watching a specific order
|
|
135
|
+
*
|
|
136
|
+
* @param orderId The order ID to stop watching
|
|
137
|
+
*/
|
|
138
|
+
unwatchOrder(orderId: string): void;
|
|
139
|
+
/**
|
|
140
|
+
* Get all currently watched order IDs
|
|
141
|
+
*/
|
|
142
|
+
getWatchedOrders(): string[];
|
|
143
|
+
/**
|
|
144
|
+
* Wait for an order to reach a terminal state.
|
|
145
|
+
* Returns a promise that resolves with the final trade update.
|
|
146
|
+
*
|
|
147
|
+
* @param orderId The order ID to wait for
|
|
148
|
+
* @param timeoutMs Optional timeout in milliseconds (default: 30000)
|
|
149
|
+
* @returns Promise resolving to the final TradeUpdate
|
|
150
|
+
* @throws Error if timeout is reached
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```typescript
|
|
154
|
+
* const finalUpdate = await stream.waitForOrderCompletion(order.id, 60000);
|
|
155
|
+
* console.log(`Order ${finalUpdate.event}: ${finalUpdate.order.filled_qty} filled`);
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
waitForOrderCompletion(orderId: string, timeoutMs?: number): Promise<TradeUpdate>;
|
|
159
|
+
/**
|
|
160
|
+
* Schedule a reconnection attempt with exponential backoff.
|
|
161
|
+
* Emits 'max_reconnects' event when maximum attempts are reached.
|
|
162
|
+
*/
|
|
163
|
+
protected scheduleReconnect(): void;
|
|
164
|
+
/**
|
|
165
|
+
* Type-safe event listener registration
|
|
166
|
+
*/
|
|
167
|
+
on<K extends keyof TradingStreamEventMap>(event: K, listener: (data: TradingStreamEventMap[K]) => void): this;
|
|
168
|
+
/**
|
|
169
|
+
* Type-safe once event listener registration
|
|
170
|
+
*/
|
|
171
|
+
once<K extends keyof TradingStreamEventMap>(event: K, listener: (data: TradingStreamEventMap[K]) => void): this;
|
|
172
|
+
/**
|
|
173
|
+
* Type-safe event emitter
|
|
174
|
+
*/
|
|
175
|
+
emit<K extends keyof TradingStreamEventMap>(event: K, data?: TradingStreamEventMap[K]): boolean;
|
|
176
|
+
/**
|
|
177
|
+
* Type-safe remove listener
|
|
178
|
+
*/
|
|
179
|
+
off<K extends keyof TradingStreamEventMap>(event: K, listener: (data: TradingStreamEventMap[K]) => void): this;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Create a trading stream for a client
|
|
183
|
+
*/
|
|
184
|
+
export declare function createTradingStream(client: AlpacaClient, config?: Partial<StreamConfig>): TradingStream;
|
|
185
|
+
export default TradingStream;
|
|
186
|
+
//# sourceMappingURL=trading-stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trading-stream.d.ts","sourceRoot":"","sources":["../../../../src/alpaca/streams/trading-stream.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAGvD;;;GAGG;AACH,MAAM,MAAM,kBAAkB,GAC1B,KAAK,GACL,MAAM,GACN,cAAc,GACd,UAAU,GACV,SAAS,GACT,cAAc,GACd,UAAU,GACV,UAAU,GACV,aAAa,GACb,gBAAgB,GAChB,iBAAiB,GACjB,YAAY,GACZ,WAAW,GACX,uBAAuB,GACvB,wBAAwB,GACxB,SAAS,GACT,UAAU,GACV,sBAAsB,CAAC;AAE3B;;;GAGG;AACH,MAAM,WAAW,qBAAqB;IACpC,iEAAiE;IACjE,YAAY,EAAE,WAAW,CAAC;IAC1B,2CAA2C;IAC3C,aAAa,EAAE,IAAI,CAAC;IACpB,uCAAuC;IACvC,SAAS,EAA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,KAAK,EAAE,CAAC,EACR,QAAQ,EAAE,CAAC,IAAI,EAAE,qBAAqB,CAAC,CAAC,CAAC,KAAK,IAAI,GACjD,IAAI;CAGR;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,YAAY,EACpB,MAAM,GAAE,OAAO,CAAC,YAAY,CAAM,GACjC,aAAa,CAEf;AAED,eAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Account Management Module
|
|
3
|
+
* Handles account details, configuration, and portfolio history
|
|
4
|
+
*/
|
|
5
|
+
import { AlpacaClient } from "../client";
|
|
6
|
+
import { AlpacaAccountDetails, AccountConfiguration, PortfolioHistoryParams, PortfolioHistoryResponse } from "../../types/alpaca-types";
|
|
7
|
+
/**
|
|
8
|
+
* Trading eligibility status for an account
|
|
9
|
+
*/
|
|
10
|
+
export interface TradingEligibility {
|
|
11
|
+
canTrade: boolean;
|
|
12
|
+
reasons: string[];
|
|
13
|
+
buyingPower: number;
|
|
14
|
+
dayTradeCount: number;
|
|
15
|
+
isPatternDayTrader: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Buying power breakdown across different asset classes
|
|
19
|
+
*/
|
|
20
|
+
export interface BuyingPowerBreakdown {
|
|
21
|
+
cash: number;
|
|
22
|
+
equity: number;
|
|
23
|
+
dayTradingBuyingPower: number;
|
|
24
|
+
regtBuyingPower: number;
|
|
25
|
+
optionsBuyingPower: number;
|
|
26
|
+
cryptoBuyingPower: number;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Pattern Day Trader (PDT) status information
|
|
30
|
+
*/
|
|
31
|
+
export interface PDTStatus {
|
|
32
|
+
isPatternDayTrader: boolean;
|
|
33
|
+
dayTradeCount: number;
|
|
34
|
+
dayTradesRemaining: number;
|
|
35
|
+
canDayTrade: boolean;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Daily return information
|
|
39
|
+
*/
|
|
40
|
+
export interface DailyReturn {
|
|
41
|
+
date: Date;
|
|
42
|
+
equity: number;
|
|
43
|
+
profitLoss: number;
|
|
44
|
+
profitLossPct: number;
|
|
45
|
+
dailyReturn: number;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Period performance metrics
|
|
49
|
+
*/
|
|
50
|
+
export interface PeriodPerformance {
|
|
51
|
+
startDate: Date;
|
|
52
|
+
endDate: Date;
|
|
53
|
+
startingEquity: number;
|
|
54
|
+
endingEquity: number;
|
|
55
|
+
totalReturn: number;
|
|
56
|
+
totalReturnPct: number;
|
|
57
|
+
averageDailyReturn: number;
|
|
58
|
+
maxDrawdown: number;
|
|
59
|
+
maxDrawdownPct: number;
|
|
60
|
+
sharpeRatio: number | null;
|
|
61
|
+
volatility: number;
|
|
62
|
+
winningDays: number;
|
|
63
|
+
losingDays: number;
|
|
64
|
+
winRate: number;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Equity curve data point
|
|
68
|
+
*/
|
|
69
|
+
export interface EquityCurvePoint {
|
|
70
|
+
timestamp: Date;
|
|
71
|
+
equity: number;
|
|
72
|
+
profitLoss: number;
|
|
73
|
+
profitLossPct: number;
|
|
74
|
+
cumulativeReturn: number;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Get account details from Alpaca
|
|
78
|
+
* @param client - AlpacaClient instance
|
|
79
|
+
* @returns Promise resolving to account details
|
|
80
|
+
*/
|
|
81
|
+
export declare function getAccountDetails(client: AlpacaClient): Promise<AlpacaAccountDetails>;
|
|
82
|
+
/**
|
|
83
|
+
* Get account configuration from Alpaca
|
|
84
|
+
* @param client - AlpacaClient instance
|
|
85
|
+
* @returns Promise resolving to account configuration
|
|
86
|
+
*/
|
|
87
|
+
export declare function getAccountConfiguration(client: AlpacaClient): Promise<AccountConfiguration>;
|
|
88
|
+
/**
|
|
89
|
+
* Update account configuration on Alpaca
|
|
90
|
+
* @param client - AlpacaClient instance
|
|
91
|
+
* @param config - Partial configuration to update
|
|
92
|
+
* @returns Promise resolving to updated account configuration
|
|
93
|
+
*/
|
|
94
|
+
export declare function updateAccountConfiguration(client: AlpacaClient, config: Partial<AccountConfiguration>): Promise<AccountConfiguration>;
|
|
95
|
+
/**
|
|
96
|
+
* Get portfolio history from Alpaca
|
|
97
|
+
* @param client - AlpacaClient instance
|
|
98
|
+
* @param params - Parameters for portfolio history request
|
|
99
|
+
* @returns Promise resolving to portfolio history response
|
|
100
|
+
*/
|
|
101
|
+
export declare function getPortfolioHistory(client: AlpacaClient, params: PortfolioHistoryParams): Promise<PortfolioHistoryResponse>;
|
|
102
|
+
/**
|
|
103
|
+
* Check if account is eligible for trading
|
|
104
|
+
* @param client - AlpacaClient instance
|
|
105
|
+
* @returns Promise resolving to trading eligibility status
|
|
106
|
+
*/
|
|
107
|
+
export declare function checkTradingEligibility(client: AlpacaClient): Promise<TradingEligibility>;
|
|
108
|
+
/**
|
|
109
|
+
* Get buying power breakdown for different asset classes
|
|
110
|
+
* @param client - AlpacaClient instance
|
|
111
|
+
* @returns Promise resolving to buying power breakdown
|
|
112
|
+
*/
|
|
113
|
+
export declare function getBuyingPower(client: AlpacaClient): Promise<BuyingPowerBreakdown>;
|
|
114
|
+
/**
|
|
115
|
+
* Get options trading level for the account
|
|
116
|
+
* @param client - AlpacaClient instance
|
|
117
|
+
* @returns Promise resolving to options trading level (0-3)
|
|
118
|
+
*/
|
|
119
|
+
export declare function getOptionsTradingLevel(client: AlpacaClient): Promise<0 | 1 | 2 | 3>;
|
|
120
|
+
/**
|
|
121
|
+
* Get Pattern Day Trader (PDT) status
|
|
122
|
+
* @param client - AlpacaClient instance
|
|
123
|
+
* @returns Promise resolving to PDT status
|
|
124
|
+
*/
|
|
125
|
+
export declare function getPDTStatus(client: AlpacaClient): Promise<PDTStatus>;
|
|
126
|
+
/**
|
|
127
|
+
* Get daily returns from portfolio history
|
|
128
|
+
* @param client - AlpacaClient instance
|
|
129
|
+
* @param period - Period to fetch (e.g., '1W', '1M', '3M', '1A')
|
|
130
|
+
* @returns Promise resolving to array of daily returns
|
|
131
|
+
*/
|
|
132
|
+
export declare function getDailyReturns(client: AlpacaClient, period?: string): Promise<DailyReturn[]>;
|
|
133
|
+
/**
|
|
134
|
+
* Calculate period performance metrics
|
|
135
|
+
* @param client - AlpacaClient instance
|
|
136
|
+
* @param period - Period to analyze (e.g., '1W', '1M', '3M', '1A')
|
|
137
|
+
* @returns Promise resolving to period performance metrics
|
|
138
|
+
*/
|
|
139
|
+
export declare function calculatePeriodPerformance(client: AlpacaClient, period?: string): Promise<PeriodPerformance>;
|
|
140
|
+
/**
|
|
141
|
+
* Get equity curve data
|
|
142
|
+
* @param client - AlpacaClient instance
|
|
143
|
+
* @param params - Portfolio history parameters
|
|
144
|
+
* @returns Promise resolving to equity curve data points
|
|
145
|
+
*/
|
|
146
|
+
export declare function getEquityCurve(client: AlpacaClient, params?: PortfolioHistoryParams): Promise<EquityCurvePoint[]>;
|
|
147
|
+
/**
|
|
148
|
+
* Get account equity and cash summary
|
|
149
|
+
* @param client - AlpacaClient instance
|
|
150
|
+
* @returns Promise resolving to account summary
|
|
151
|
+
*/
|
|
152
|
+
export declare function getAccountSummary(client: AlpacaClient): Promise<{
|
|
153
|
+
equity: number;
|
|
154
|
+
cash: number;
|
|
155
|
+
portfolioValue: number;
|
|
156
|
+
longMarketValue: number;
|
|
157
|
+
shortMarketValue: number;
|
|
158
|
+
lastEquity: number;
|
|
159
|
+
todayProfitLoss: number;
|
|
160
|
+
todayProfitLossPct: number;
|
|
161
|
+
}>;
|
|
162
|
+
/**
|
|
163
|
+
* Check if account is a margin account
|
|
164
|
+
* @param client - AlpacaClient instance
|
|
165
|
+
* @returns Promise resolving to boolean indicating margin status
|
|
166
|
+
*/
|
|
167
|
+
export declare function isMarginAccount(client: AlpacaClient): Promise<boolean>;
|
|
168
|
+
/**
|
|
169
|
+
* Get margin information for the account
|
|
170
|
+
* @param client - AlpacaClient instance
|
|
171
|
+
* @returns Promise resolving to margin information
|
|
172
|
+
*/
|
|
173
|
+
export declare function getMarginInfo(client: AlpacaClient): Promise<{
|
|
174
|
+
multiplier: string;
|
|
175
|
+
initialMargin: number;
|
|
176
|
+
maintenanceMargin: number;
|
|
177
|
+
lastMaintenanceMargin: number;
|
|
178
|
+
sma: number;
|
|
179
|
+
marginCallAmount: number;
|
|
180
|
+
}>;
|
|
181
|
+
declare const _default: {
|
|
182
|
+
getAccountDetails: typeof getAccountDetails;
|
|
183
|
+
getAccountConfiguration: typeof getAccountConfiguration;
|
|
184
|
+
updateAccountConfiguration: typeof updateAccountConfiguration;
|
|
185
|
+
getPortfolioHistory: typeof getPortfolioHistory;
|
|
186
|
+
checkTradingEligibility: typeof checkTradingEligibility;
|
|
187
|
+
getBuyingPower: typeof getBuyingPower;
|
|
188
|
+
getOptionsTradingLevel: typeof getOptionsTradingLevel;
|
|
189
|
+
getPDTStatus: typeof getPDTStatus;
|
|
190
|
+
getDailyReturns: typeof getDailyReturns;
|
|
191
|
+
calculatePeriodPerformance: typeof calculatePeriodPerformance;
|
|
192
|
+
getEquityCurve: typeof getEquityCurve;
|
|
193
|
+
getAccountSummary: typeof getAccountSummary;
|
|
194
|
+
isMarginAccount: typeof isMarginAccount;
|
|
195
|
+
getMarginInfo: typeof getMarginInfo;
|
|
196
|
+
};
|
|
197
|
+
export default _default;
|
|
198
|
+
//# sourceMappingURL=account.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account.d.ts","sourceRoot":"","sources":["../../../../src/alpaca/trading/account.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEzC,OAAO,EACL,oBAAoB,EAEpB,oBAAoB,EACpB,sBAAsB,EACtB,wBAAwB,EACzB,MAAM,0BAA0B,CAAC;AASlC;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,OAAO,CAAC;IAClB,OAAO,EAA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,CAC9C,MAAM,EAAE,YAAY,EACpB,MAAM,GAAE,MAAa,GACpB,OAAO,CAAC,iBAAiB,CAAC,CAgG5B;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,MAAM,EAAE,YAAY,EACpB,MAAM,GAAE,sBAA0D,GACjE,OAAO,CAAC,gBAAgB,EAAE,CAAC,CA8C7B;AAED;;;;GAIG;AACH,wBAAsB,iBAAiB,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC;IACrE,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,cAAc,EAAE,MAAM,CAAC;IACvB,eAAe,EAAE,MAAM,CAAC;IACxB,gBAAgB,EAAE,MAAM,CAAC;IACzB,UAAU,EAAE,MAAM,CAAC;IACnB,eAAe,EAAE,MAAM,CAAC;IACxB,kBAAkB,EAAE,MAAM,CAAC;CAC5B,CAAC,CAgCD;AAED;;;;GAIG;AACH,wBAAsB,eAAe,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,CAgB5E;AAED;;;;GAIG;AACH,wBAAsB,aAAa,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC;IACjE,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,EAAE,MAAM,CAAC;IACtB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,qBAAqB,EAAE,MAAM,CAAC;IAC9B,GAAG,EAAE,MAAM,CAAC;IACZ,gBAAgB,EAAE,MAAM,CAAC;CAC1B,CAAC,CA+BD;;;;;;;;;;;;;;;;;AAED,wBAeE"}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { AlpacaOrder, OrderSide, TimeInForce, CreateOrderParams } from "../../types/alpaca-types";
|
|
2
|
+
/**
|
|
3
|
+
* Interface for the Alpaca client/API that can execute bracket orders.
|
|
4
|
+
* This can be implemented by AlpacaTradingAPI or a future AlpacaClient class.
|
|
5
|
+
*/
|
|
6
|
+
export interface BracketOrderExecutor {
|
|
7
|
+
/** Execute an order request and return the created order */
|
|
8
|
+
createOrder(params: CreateOrderParams): Promise<AlpacaOrder>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Parameters for creating a bracket order.
|
|
12
|
+
*/
|
|
13
|
+
export interface BracketOrderParams {
|
|
14
|
+
/** Stock symbol */
|
|
15
|
+
symbol: string;
|
|
16
|
+
/** Number of shares */
|
|
17
|
+
qty: number;
|
|
18
|
+
/** Order side: buy or sell */
|
|
19
|
+
side: OrderSide;
|
|
20
|
+
/** Entry order type: market for immediate, limit for price-specific */
|
|
21
|
+
type: "market" | "limit";
|
|
22
|
+
/** Limit price for entry (required if type is 'limit') */
|
|
23
|
+
limitPrice?: number;
|
|
24
|
+
/** Take profit configuration */
|
|
25
|
+
takeProfit: {
|
|
26
|
+
/** Price at which to take profit */
|
|
27
|
+
limitPrice: number;
|
|
28
|
+
};
|
|
29
|
+
/** Stop loss configuration */
|
|
30
|
+
stopLoss: {
|
|
31
|
+
/** Price at which to trigger stop */
|
|
32
|
+
stopPrice: number;
|
|
33
|
+
/** Optional limit price for stop-limit (if omitted, uses market stop) */
|
|
34
|
+
limitPrice?: number;
|
|
35
|
+
};
|
|
36
|
+
/** Time in force: 'day' expires at market close, 'gtc' good till canceled */
|
|
37
|
+
timeInForce?: TimeInForce;
|
|
38
|
+
/** Enable extended hours trading */
|
|
39
|
+
extendedHours?: boolean;
|
|
40
|
+
/** Client-specified order ID for tracking */
|
|
41
|
+
clientOrderId?: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Result of a bracket order submission.
|
|
45
|
+
*/
|
|
46
|
+
export interface BracketOrderResult {
|
|
47
|
+
/** The parent/entry order */
|
|
48
|
+
entryOrder: AlpacaOrder;
|
|
49
|
+
/** The take profit leg */
|
|
50
|
+
takeProfitLeg: AlpacaOrder | null;
|
|
51
|
+
/** The stop loss leg */
|
|
52
|
+
stopLossLeg: AlpacaOrder | null;
|
|
53
|
+
/** All legs combined */
|
|
54
|
+
allOrders: AlpacaOrder[];
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Create a bracket order (entry + take profit + stop loss).
|
|
58
|
+
*
|
|
59
|
+
* A bracket order ensures that when the entry fills:
|
|
60
|
+
* - A take profit limit order is placed
|
|
61
|
+
* - A stop loss order is placed
|
|
62
|
+
* - If either TP or SL fills, the other is automatically canceled (OCO behavior)
|
|
63
|
+
*
|
|
64
|
+
* @param executor - The order executor (AlpacaTradingAPI or AlpacaClient)
|
|
65
|
+
* @param params - The bracket order parameters
|
|
66
|
+
* @returns The bracket order result containing entry and leg orders
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* const api = AlpacaTradingAPI.new(credentials);
|
|
71
|
+
* const result = await createBracketOrder(
|
|
72
|
+
* { createOrder: (p) => api.makeRequest('/orders', 'POST', p) },
|
|
73
|
+
* {
|
|
74
|
+
* symbol: 'AAPL',
|
|
75
|
+
* qty: 100,
|
|
76
|
+
* side: 'buy',
|
|
77
|
+
* type: 'limit',
|
|
78
|
+
* limitPrice: 150.00,
|
|
79
|
+
* takeProfit: { limitPrice: 160.00 },
|
|
80
|
+
* stopLoss: { stopPrice: 145.00 },
|
|
81
|
+
* timeInForce: 'gtc',
|
|
82
|
+
* }
|
|
83
|
+
* );
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export declare function createBracketOrder(executor: BracketOrderExecutor, params: BracketOrderParams): Promise<BracketOrderResult>;
|
|
87
|
+
/**
|
|
88
|
+
* Parameters for creating a protective bracket on an existing position.
|
|
89
|
+
*/
|
|
90
|
+
export interface ProtectiveBracketParams {
|
|
91
|
+
/** Stock symbol */
|
|
92
|
+
symbol: string;
|
|
93
|
+
/** Number of shares to protect */
|
|
94
|
+
qty: number;
|
|
95
|
+
/** Side is always 'sell' to close a long position */
|
|
96
|
+
side: "sell";
|
|
97
|
+
/** Take profit configuration */
|
|
98
|
+
takeProfit: {
|
|
99
|
+
/** Price at which to take profit */
|
|
100
|
+
limitPrice: number;
|
|
101
|
+
};
|
|
102
|
+
/** Stop loss configuration */
|
|
103
|
+
stopLoss: {
|
|
104
|
+
/** Price at which to trigger stop */
|
|
105
|
+
stopPrice: number;
|
|
106
|
+
/** Optional limit price for stop-limit */
|
|
107
|
+
limitPrice?: number;
|
|
108
|
+
};
|
|
109
|
+
/** Time in force: 'day' expires at market close, 'gtc' good till canceled */
|
|
110
|
+
timeInForce?: TimeInForce;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Create a bracket order for an existing position (protective bracket).
|
|
114
|
+
* Useful for adding stop loss and take profit to a position that was entered without them.
|
|
115
|
+
*
|
|
116
|
+
* Note: This creates an OCO (One-Cancels-Other) order rather than a full bracket
|
|
117
|
+
* because we already have the position - we just need the exit legs.
|
|
118
|
+
*
|
|
119
|
+
* @param executor - The order executor (AlpacaTradingAPI or AlpacaClient)
|
|
120
|
+
* @param params - The protective bracket parameters
|
|
121
|
+
* @returns The bracket order result
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```typescript
|
|
125
|
+
* // Add protection to an existing long position
|
|
126
|
+
* const result = await createProtectiveBracket(
|
|
127
|
+
* executor,
|
|
128
|
+
* {
|
|
129
|
+
* symbol: 'AAPL',
|
|
130
|
+
* qty: 100,
|
|
131
|
+
* side: 'sell',
|
|
132
|
+
* takeProfit: { limitPrice: 160.00 },
|
|
133
|
+
* stopLoss: { stopPrice: 145.00 },
|
|
134
|
+
* timeInForce: 'gtc',
|
|
135
|
+
* }
|
|
136
|
+
* );
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
export declare function createProtectiveBracket(executor: BracketOrderExecutor, params: ProtectiveBracketParams): Promise<BracketOrderResult>;
|
|
140
|
+
/**
|
|
141
|
+
* Creates an adapter to use AlpacaTradingAPI as a BracketOrderExecutor.
|
|
142
|
+
*
|
|
143
|
+
* @param api - Instance of AlpacaTradingAPI
|
|
144
|
+
* @returns BracketOrderExecutor compatible with bracket order functions
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```typescript
|
|
148
|
+
* const api = AlpacaTradingAPI.new(credentials);
|
|
149
|
+
* const executor = createExecutorFromTradingAPI(api);
|
|
150
|
+
* const result = await createBracketOrder(executor, params);
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
export declare function createExecutorFromTradingAPI(api: {
|
|
154
|
+
makeRequest: (endpoint: string, method: string, body?: unknown) => Promise<AlpacaOrder>;
|
|
155
|
+
}): BracketOrderExecutor;
|
|
156
|
+
declare const _default: {
|
|
157
|
+
createBracketOrder: typeof createBracketOrder;
|
|
158
|
+
createProtectiveBracket: typeof createProtectiveBracket;
|
|
159
|
+
createExecutorFromTradingAPI: typeof createExecutorFromTradingAPI;
|
|
160
|
+
};
|
|
161
|
+
export default _default;
|
|
162
|
+
//# sourceMappingURL=bracket-orders.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bracket-orders.d.ts","sourceRoot":"","sources":["../../../../src/alpaca/trading/bracket-orders.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,WAAW,EACX,SAAS,EACT,WAAW,EACX,iBAAiB,EAClB,MAAM,0BAA0B,CAAC;AAYlC;;;GAGG;AACH,MAAM,WAAW,oBAAoB;IACnC,4DAA4D;IAC5D,WAAW,CAAC,MAAM,EAAE,iBAAiB,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;CAC9D;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,mBAAmB;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,uBAAuB;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,8BAA8B;IAC9B,IAAI,EAAE,SAAS,CAAC;IAChB,uEAAuE;IACvE,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC;IACzB,0DAA0D;IAC1D,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,gCAAgC;IAChC,UAAU,EA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;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAsB,uBAAuB,CAC3C,QAAQ,EAAE,oBAAoB,EAC9B,MAAM,EAAE,uBAAuB,GAC9B,OAAO,CAAC,kBAAkB,CAAC,CAsF7B;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,4BAA4B,CAAC,GAAG,EAAE;IAChD,WAAW,EAAE,CACX,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,IAAI,CAAC,EAAE,OAAO,KACX,OAAO,CAAC,WAAW,CAAC,CAAC;CAC3B,GAAG,oBAAoB,CAMvB;;;;;;AAED,wBAIE"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Market Clock and Calendar Module
|
|
3
|
+
* Provides access to market hours (clock) and trading calendar via the Alpaca API
|
|
4
|
+
*/
|
|
5
|
+
import { AlpacaClient } from "../client";
|
|
6
|
+
/**
|
|
7
|
+
* Alpaca market clock representing the current market status and next session times.
|
|
8
|
+
* Corresponds to the response from GET /v2/clock.
|
|
9
|
+
*/
|
|
10
|
+
export interface AlpacaClock {
|
|
11
|
+
/** ISO 8601 timestamp of the current server time */
|
|
12
|
+
timestamp: string;
|
|
13
|
+
/** Whether the market is currently open for trading */
|
|
14
|
+
is_open: boolean;
|
|
15
|
+
/** ISO 8601 timestamp of the next market open */
|
|
16
|
+
next_open: string;
|
|
17
|
+
/** ISO 8601 timestamp of the next market close */
|
|
18
|
+
next_close: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A single trading day entry from the Alpaca market calendar.
|
|
22
|
+
* Corresponds to one element in the response from GET /v2/calendar.
|
|
23
|
+
*/
|
|
24
|
+
export interface AlpacaCalendarDay {
|
|
25
|
+
/** The date in YYYY-MM-DD format */
|
|
26
|
+
date: string;
|
|
27
|
+
/** The market open time in HH:MM format (US Eastern) */
|
|
28
|
+
open: string;
|
|
29
|
+
/** The market close time in HH:MM format (US Eastern) */
|
|
30
|
+
close: string;
|
|
31
|
+
/** The settlement date in YYYY-MM-DD format */
|
|
32
|
+
settlement_date: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Options for fetching the Alpaca market calendar.
|
|
36
|
+
*/
|
|
37
|
+
export interface GetAlpacaCalendarOptions {
|
|
38
|
+
/** Start date for the calendar range (inclusive). Defaults to today if omitted. */
|
|
39
|
+
start?: Date;
|
|
40
|
+
/** End date for the calendar range (inclusive). Defaults to today if omitted. */
|
|
41
|
+
end?: Date;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Fetches the current market clock from Alpaca (GET /v2/clock).
|
|
45
|
+
*
|
|
46
|
+
* Returns the current server time, whether the market is open, and the next
|
|
47
|
+
* open/close timestamps. Useful for determining whether trading is currently
|
|
48
|
+
* possible before placing orders.
|
|
49
|
+
*
|
|
50
|
+
* @param client - An initialized AlpacaClient instance
|
|
51
|
+
* @returns Promise resolving to the current market clock status
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const clock = await getAlpacaClock(client);
|
|
56
|
+
* if (clock.is_open) {
|
|
57
|
+
* console.log('Market is open, next close:', clock.next_close);
|
|
58
|
+
* } else {
|
|
59
|
+
* console.log('Market closed, next open:', clock.next_open);
|
|
60
|
+
* }
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @throws {Error} If the Alpaca API request fails
|
|
64
|
+
*/
|
|
65
|
+
export declare function getAlpacaClock(client: AlpacaClient): Promise<AlpacaClock>;
|
|
66
|
+
/**
|
|
67
|
+
* Fetches the market trading calendar from Alpaca (GET /v2/calendar).
|
|
68
|
+
*
|
|
69
|
+
* Returns the scheduled open and close times for each trading day within the
|
|
70
|
+
* requested date range. The calendar accounts for market holidays and early
|
|
71
|
+
* closures. Dates default to today when omitted.
|
|
72
|
+
*
|
|
73
|
+
* @param client - An initialized AlpacaClient instance
|
|
74
|
+
* @param options - Optional start/end dates for the calendar range
|
|
75
|
+
* @returns Promise resolving to an array of trading calendar days
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* // Fetch the next 5 trading days
|
|
80
|
+
* const today = new Date();
|
|
81
|
+
* const nextWeek = new Date(today);
|
|
82
|
+
* nextWeek.setDate(today.getDate() + 7);
|
|
83
|
+
*
|
|
84
|
+
* const calendar = await getAlpacaCalendar(client, {
|
|
85
|
+
* start: today,
|
|
86
|
+
* end: nextWeek,
|
|
87
|
+
* });
|
|
88
|
+
* console.log('Trading days this week:', calendar.length);
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @throws {Error} If the Alpaca API request fails
|
|
92
|
+
*/
|
|
93
|
+
export declare function getAlpacaCalendar(client: AlpacaClient, options?: GetAlpacaCalendarOptions): Promise<AlpacaCalendarDay[]>;
|
|
94
|
+
declare const _default: {
|
|
95
|
+
getAlpacaClock: typeof getAlpacaClock;
|
|
96
|
+
getAlpacaCalendar: typeof getAlpacaCalendar;
|
|
97
|
+
};
|
|
98
|
+
export default _default;
|
|
99
|
+
//# sourceMappingURL=clock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clock.d.ts","sourceRoot":"","sources":["../../../../src/alpaca/trading/clock.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAQzC;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,oDAAoD;IACpD,SAAS,EAAE,MAAM,CAAC;IAClB,uDAAuD;IACvD,OAAO,EAAE,OAAO,CAAC;IACjB,iDAAiD;IACjD,SAAS,EAAE,MAAM,CAAC;IAClB,kDAAkD;IAClD,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;;GAGG;AACH,MAAM,WAAW,iBAAiB;IAChC,oCAAoC;IACpC,IAAI,EAAE,MAAM,CAAC;IACb,wDAAwD;IACxD,IAAI,EAAE,MAAM,CAAC;IACb,yDAAyD;IACzD,KAAK,EAAE,MAAM,CAAC;IACd,+CAA+C;IAC/C,eAAe,EAAE,MAAM,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,mFAAmF;IACnF,KAAK,CAAC,EAAE,IAAI,CAAC;IACb,iFAAiF;IACjF,GAAG,CAAC,EAAE,IAAI,CAAC;CACZ;AAeD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAsB,cAAc,CAClC,MAAM,EAAE,YAAY,GACnB,OAAO,CAAC,WAAW,CAAC,CAkBtB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAsB,iBAAiB,CACrC,MAAM,EAAE,YAAY,EACpB,OAAO,CAAC,EAAE,wBAAwB,GACjC,OAAO,CAAC,iBAAiB,EAAE,CAAC,CA4B9B;;;;;AAED,wBAGE"}
|