@adaptic/utils 0.0.383 → 0.1.1
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 +48504 -341
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +48321 -341
- package/dist/index.mjs.map +1 -1
- package/dist/types/alpaca/client.d.ts +95 -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 +201 -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/streams.d.ts +88 -0
- package/dist/types/alpaca/streams.d.ts.map +1 -0
- package/dist/types/alpaca/test-imports.d.ts +7 -0
- package/dist/types/alpaca/test-imports.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/index.d.ts +13 -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 +240 -0
- package/dist/types/alpaca/trading/trailing-stops.d.ts.map +1 -0
- package/dist/types/index.d.ts +353 -6
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +4 -4
|
@@ -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;AAEvD;;;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,cAAc,EAAE,WAAW,CAAC;IAC5B,2CAA2C;IA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,OAAO;IAIV;;OAEG;IACH,GAAG,CAAC,CAAC,SAAS,MAAM,qBAAqB,EACvC,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,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Alpaca WebSocket Streams Module
|
|
3
|
+
* Real-time market data and trade update streams
|
|
4
|
+
*
|
|
5
|
+
* NOTE: This is a placeholder module. Full streaming support requires
|
|
6
|
+
* additional implementation for WebSocket connection management.
|
|
7
|
+
*
|
|
8
|
+
* @module @adaptic/utils/alpaca/streams
|
|
9
|
+
*/
|
|
10
|
+
import { AlpacaClient } from './client';
|
|
11
|
+
/**
|
|
12
|
+
* Stream manager interface for managing WebSocket connections
|
|
13
|
+
*/
|
|
14
|
+
export interface StreamManager {
|
|
15
|
+
/** Connect to trading updates stream */
|
|
16
|
+
connectTradingStream: () => Promise<void>;
|
|
17
|
+
/** Connect to market data stream */
|
|
18
|
+
connectDataStream: () => Promise<void>;
|
|
19
|
+
/** Disconnect all streams */
|
|
20
|
+
disconnect: () => void;
|
|
21
|
+
/** Subscribe to trade updates for specific symbols */
|
|
22
|
+
subscribeToTrades: (symbols: string[]) => void;
|
|
23
|
+
/** Subscribe to quote updates for specific symbols */
|
|
24
|
+
subscribeToQuotes: (symbols: string[]) => void;
|
|
25
|
+
/** Subscribe to bar updates for specific symbols */
|
|
26
|
+
subscribeToBars: (symbols: string[]) => void;
|
|
27
|
+
/** Unsubscribe from symbols */
|
|
28
|
+
unsubscribe: (symbols: string[]) => void;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Stream event types
|
|
32
|
+
*/
|
|
33
|
+
export type StreamEventType = 'trade' | 'quote' | 'bar' | 'trade_update' | 'error' | 'connected' | 'disconnected';
|
|
34
|
+
/**
|
|
35
|
+
* Stream event handler
|
|
36
|
+
*/
|
|
37
|
+
export type StreamEventHandler<T = unknown> = (event: T) => void;
|
|
38
|
+
/**
|
|
39
|
+
* Configuration for stream manager
|
|
40
|
+
*/
|
|
41
|
+
export interface StreamManagerConfig {
|
|
42
|
+
/** Client for authentication */
|
|
43
|
+
client: AlpacaClient;
|
|
44
|
+
/** Handler for trade events */
|
|
45
|
+
onTrade?: StreamEventHandler;
|
|
46
|
+
/** Handler for quote events */
|
|
47
|
+
onQuote?: StreamEventHandler;
|
|
48
|
+
/** Handler for bar events */
|
|
49
|
+
onBar?: StreamEventHandler;
|
|
50
|
+
/** Handler for trade update events (order fills, etc.) */
|
|
51
|
+
onTradeUpdate?: StreamEventHandler;
|
|
52
|
+
/** Handler for errors */
|
|
53
|
+
onError?: StreamEventHandler<Error>;
|
|
54
|
+
/** Handler for connection events */
|
|
55
|
+
onConnected?: StreamEventHandler<void>;
|
|
56
|
+
/** Handler for disconnection events */
|
|
57
|
+
onDisconnected?: StreamEventHandler<void>;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Create a stream manager for real-time data
|
|
61
|
+
*
|
|
62
|
+
* NOTE: This is a placeholder implementation. Full streaming support
|
|
63
|
+
* requires WebSocket connection management which is available through
|
|
64
|
+
* the Alpaca SDK's built-in streaming capabilities.
|
|
65
|
+
*
|
|
66
|
+
* @param config - Stream manager configuration
|
|
67
|
+
* @returns A stream manager instance
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* const client = createAlpacaClient({ ... });
|
|
72
|
+
* const streamManager = createStreamManager({
|
|
73
|
+
* client,
|
|
74
|
+
* onTrade: (trade) => console.log('Trade:', trade),
|
|
75
|
+
* onQuote: (quote) => console.log('Quote:', quote),
|
|
76
|
+
* onError: (error) => console.error('Stream error:', error),
|
|
77
|
+
* });
|
|
78
|
+
*
|
|
79
|
+
* await streamManager.connectDataStream();
|
|
80
|
+
* streamManager.subscribeToTrades(['AAPL', 'GOOGL']);
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export declare function createStreamManager(_config: StreamManagerConfig): StreamManager;
|
|
84
|
+
declare const _default: {
|
|
85
|
+
createStreamManager: typeof createStreamManager;
|
|
86
|
+
};
|
|
87
|
+
export default _default;
|
|
88
|
+
//# sourceMappingURL=streams.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streams.d.ts","sourceRoot":"","sources":["../../../src/alpaca/streams.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAExC;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,wCAAwC;IACxC,oBAAoB,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1C,oCAAoC;IACpC,iBAAiB,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IACvC,6BAA6B;IAC7B,UAAU,EAAE,MAAM,IAAI,CAAC;IACvB,sDAAsD;IACtD,iBAAiB,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,IAAI,CAAC;IAC/C,sDAAsD;IACtD,iBAAiB,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,IAAI,CAAC;IAC/C,oDAAoD;IACpD,eAAe,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,IAAI,CAAC;IAC7C,+BAA+B;IAC/B,WAAW,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,IAAI,CAAC;CAC1C;AAED;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,OAAO,GAAG,OAAO,GAAG,KAAK,GAAG,cAAc,GAAG,OAAO,GAAG,WAAW,GAAG,cAAc,CAAC;AAElH;;GAEG;AACH,MAAM,MAAM,kBAAkB,CAAC,CAAC,GAAG,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;AAEjE;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,gCAAgC;IAChC,MAAM,EAAE,YAAY,CAAC;IACrB,+BAA+B;IAC/B,OAAO,CAAC,EAAE,kBAAkB,CAAC;IAC7B,+BAA+B;IAC/B,OAAO,CAAC,EAAE,kBAAkB,CAAC;IAC7B,6BAA6B;IAC7B,KAAK,CAAC,EAAE,kBAAkB,CAAC;IAC3B,0DAA0D;IAC1D,aAAa,CAAC,EAAE,kBAAkB,CAAC;IACnC,yBAAyB;IACzB,OAAO,CAAC,EAAE,kBAAkB,CAAC,KAAK,CAAC,CAAC;IACpC,oCAAoC;IACpC,WAAW,CAAC,EAAE,kBAAkB,CAAC,IAAI,CAAC,CAAC;IACvC,uCAAuC;IACvC,cAAc,CAAC,EAAE,kBAAkB,CAAC,IAAI,CAAC,CAAC;CAC3C;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,mBAAmB,GAAG,aAAa,CA0B/E;;;;AAED,wBAEE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-imports.d.ts","sourceRoot":"","sources":["../../../src/alpaca/test-imports.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAqGH,QAAA,MAAM,SAAS,qBAuBd,CAAC;AAGF,OAAO,EAAE,SAAS,EAAE,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,EACpB,oBAAoB,EACpB,sBAAsB,EACtB,wBAAwB,EACzB,MAAM,0BAA0B,CAAC;AAMlC;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,OAAO,CAAC;IAClB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,MAAM,CAAC;IACtB,kBAAkB,EAAE,OAAO,CAAC;CAC7B;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,qBAAqB,EAAE,MAAM,CAAC;IAC9B,eAAe,EAAE,MAAM,CAAC;IACxB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,iBAAiB,EAAE,MAAM,CAAC;CAC3B;AAED;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,kBAAkB,EAAE,OAAO,CAAC;IAC5B,aAAa,EAAE,MAAM,CAAC;IACtB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,WAAW,EAAE,OAAO,CAAC;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IA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AED;;;;GAIG;AACH,wBAAsB,eAAe,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,CAa5E;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,CA4BD;;;;;;;;;;;;;;;;;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,EAAE,WAAW,EAAE,SAAS,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAYlG;;;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,EAAE;QACV,oCAAoC;QACpC,UAAU,EAAE,MAAM,CAAC;KACpB,CAAC;IACF,8BAA8B;IAC9B,QAAQ,EAAE;QACR,qCAAqC;QACrC,SAAS,EAAE,MAAM,CAAC;QAClB,yEAAyE;QACzE,UAAU,CAAC,EAAE,MAAM,CAAC;KACrB,CAAC;IACF,6EAA6E;IAC7E,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,oCAAoC;IACpC,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,6CAA6C;IAC7C,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,6BAA6B;IAC7B,UAAU,EAAE,WAAW,CAAC;IACxB,0BAA0B;IAC1B,aAAa,EAAE,WAAW,GAAG,IAAI,CAAC;IAClC,wBAAwB;IACxB,WAAW,EAAE,WAAW,GAAG,IAAI,CAAC;IACh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