This guide explains how to use the streaming functionality in the TradeStation API TypeScript wrapper.
The wrapper provides real-time streaming capabilities for:
- Market Data (quotes, bars, options)
- Order Updates
- Position Updates
- Market Depth
All streams use Node.js EventEmitter pattern and handle:
- Automatic reconnection
- Rate limiting
- Error handling
- Resource cleanup
- Quote Streaming
const stream = await client.marketData.streamQuotes(['MSFT', 'AAPL']);
stream.on('data', (quote) => {
if ('Ask' in quote) {
console.log('Quote:', quote);
} else if ('Heartbeat' in quote) {
console.log('Heartbeat:', quote);
}
});- Bar Streaming
const stream = await client.marketData.streamBars('MSFT', {
interval: '1',
unit: 'Minute'
});
stream.on('data', (bar) => {
if ('Close' in bar) {
console.log('Bar:', bar);
}
});- Option Chain Streaming
const stream = await client.marketData.streamOptionChain('MSFT');
stream.on('data', (option) => {
console.log('Option:', option);
});- Order Updates
const stream = await client.brokerage.streamOrders('account_id');
stream.on('data', (order) => {
if ('OrderID' in order) {
console.log('Order Update:', order);
}
});- Specific Order Updates
const stream = await client.brokerage.streamOrdersByOrderID(
'account_id',
'order_id1,order_id2'
);
stream.on('data', (order) => {
console.log('Order Update:', order);
});const stream = await client.brokerage.streamPositions('account_id');
stream.on('data', (position) => {
console.log('Position Update:', position);
});// Single symbol
const stream1 = await client.marketData.streamQuotes(['MSFT']);
// Multiple symbols
const stream2 = await client.marketData.streamQuotes(['MSFT', 'AAPL', 'GOOGL']);
// With parameters
const stream3 = await client.marketData.streamBars('MSFT', {
interval: '5',
unit: 'Minute',
sessiontemplate: 'USEQPreAndPost'
});All streams emit these events:
stream.on('data', (data) => {
// Handle data updates
});
stream.on('error', (error) => {
// Handle errors
});
stream.on('end', () => {
// Handle stream end
});
stream.on('close', () => {
// Handle stream closure
});// Close a specific stream
stream.emit('close');
// Close all active streams
client.closeAllStreams();
// Get list of active streams
const activeStreams = client.getActiveStreams();- Resource Management
// Create stream
const stream = await client.marketData.streamQuotes(['MSFT']);
// Use try-finally for cleanup
try {
// Use the stream
} finally {
stream.emit('close');
}- Error Handling
stream.on('error', (error) => {
if (error.name === 'RateLimitError') {
// Handle rate limiting
} else if (error.name === 'NetworkError') {
// Handle network issues
} else {
// Handle other errors
}
});- Heartbeat Monitoring
stream.on('data', (data) => {
if ('Heartbeat' in data) {
// Update last heartbeat timestamp
lastHeartbeat = Date.now();
}
});Streams are subject to rate limits:
- Maximum concurrent streams per client
- Maximum symbols per stream
- Maximum data frequency
// Configure max concurrent streams
const client = new TradeStationClient({
maxConcurrentStreams: 10
});Common streaming errors:
- Rate limit exceeded
- Invalid symbols
- Network disconnection
- Authentication failures
Example error handling:
stream.on('error', (error) => {
switch (error.name) {
case 'RateLimitError':
console.error('Rate limit exceeded:', error.message);
// Implement backoff strategy
break;
case 'NetworkError':
console.error('Network issue:', error.message);
// Attempt reconnection
break;
case 'AuthenticationError':
console.error('Auth error:', error.message);
// Re-authenticate
break;
default:
console.error('Unknown error:', error.message);
}
});- Symbol Batching
// Instead of multiple single-symbol streams
const stream = await client.marketData.streamQuotes([
'MSFT', 'AAPL', 'GOOGL'
]);- Data Processing
// Use efficient data handling
stream.on('data', (data) => {
// Process only needed fields
const { Symbol, Last, Volume } = data;
// Update your application state
});- Resource Cleanup
// Monitor memory usage
const usage = process.memoryUsage();
if (usage.heapUsed > threshold) {
// Clean up unused streams
}Enable debug mode for detailed logging:
const client = new TradeStationClient({
debug: true
});Monitor stream status:
setInterval(() => {
const streams = client.getActiveStreams();
console.log('Active streams:', streams.length);
}, 60000);