A comprehensive TypeScript wrapper for TradeStation WebAPI v3, providing type-safe access to TradeStation's brokerage and market data services.
This project provides a complete TypeScript implementation of the TradeStation WebAPI v3, offering:
- 🔒 Secure authentication and token management
- 🚦 Built-in rate limiting
- 📊 Real-time market data streaming
- 💼 Complete brokerage functionality
- 📈 Order execution capabilities
- 📘 Comprehensive TypeScript definitions
- ⚡ Streaming WebSocket support
- 🧪 Example implementations
Note: For the most up-to-date API specifications and documentation, please refer to the official TradeStation API documentation.
npm install tradestation-api-ts
import { TradeStationClient } from 'tradestation-api-ts';
// Initialize with environment variables
const client = new TradeStationClient();
// Or with explicit configuration
const client = new TradeStationClient({
clientId: 'your_client_id',
clientSecret: 'your_client_secret',
username: 'your_username',
password: 'your_password',
environment: 'Simulation' // or 'Live'
});
// Get quote snapshots
const quotes = await client.marketData.getQuoteSnapshots(['MSFT', 'AAPL']);
// Stream real-time quotes
const stream = await client.marketData.streamQuotes(['MSFT', 'AAPL']);
stream.on('data', (quote) => {
console.log('Quote update:', quote);
});
// Get historical bars
const bars = await client.marketData.getBarHistory('MSFT', {
interval: '1',
unit: 'Minute',
barsback: 100
});
// Place a market order
const order = await client.orderExecution.placeOrder({
AccountID: 'your_account_id',
Symbol: 'MSFT',
Quantity: '100',
OrderType: 'Market',
TradeAction: 'Buy',
TimeInForce: { Duration: 'DAY' },
Route: 'Intelligent'
});
// Place a bracket order
const bracketOrder = await client.orderExecution.placeGroupOrder({
Type: 'BRK',
Orders: [
{
AccountID: 'your_account_id',
Symbol: 'MSFT',
Quantity: '100',
OrderType: 'Market',
TradeAction: 'Buy',
TimeInForce: { Duration: 'DAY' }
},
{
AccountID: 'your_account_id',
Symbol: 'MSFT',
Quantity: '100',
OrderType: 'Limit',
LimitPrice: '160.00',
TradeAction: 'Sell',
TimeInForce: { Duration: 'GTC' }
},
{
AccountID: 'your_account_id',
Symbol: 'MSFT',
Quantity: '100',
OrderType: 'StopMarket',
StopPrice: '145.00',
TradeAction: 'Sell',
TimeInForce: { Duration: 'GTC' }
}
]
});
// Get accounts
const accounts = await client.brokerage.getAccounts();
// Get positions
const positions = await client.brokerage.getPositions('your_account_id');
// Stream position updates
const positionStream = await client.brokerage.streamPositions('your_account_id');
positionStream.on('data', (position) => {
console.log('Position update:', position);
});
try {
const quotes = await client.marketData.getQuoteSnapshots(['INVALID']);
} catch (error) {
if (error.name === 'ValidationError') {
console.error('Invalid input:', error.message);
} else if (error.name === 'ApiError') {
console.error('API error:', error.message);
} else {
console.error('Unknown error:', error);
}
}
// Close specific stream
stream.emit('close');
// Close all active streams
client.closeAllStreams();
- Create a
.env.local
file with your TradeStation API credentials:
CLIENT_ID=your_client_id
CLIENT_SECRET=your_client_secret
USERNAME=your_username
PASSWORD=your_password
ENVIRONMENT=Simulation # or 'Live'
- Initialize the client:
import { TradeStationClient } from 'tradestation-api-ts';
const client = new TradeStationClient();
// Get account balances
const balances = await client.brokerage.getBalances('your_account_id');
// Stream real-time quotes
const quoteStream = await client.marketData.streamQuotes(['MSFT', 'AAPL']);
quoteStream.on('data', (quote) => {
console.log('Quote update:', quote);
});
- Automatic token management
- Configurable retry logic
- Error handling
- Request/response interceptors
- OAuth 2.0 implementation
- Automatic token refresh
- Simulation/Live environment support
- Automatic rate limiting
- Configurable limits
- Queue management
- Headers-based rate tracking
- Real-time quotes
- Historical bars
- Option chains
- Market depth
- Symbol information
- Account management
- Position tracking
- Balance information
- Order history
- Activity tracking
- Order placement
- Order modification
- Order cancellation
- Complex order types
- OCO and bracket orders
- Node.js (>=14.0.0)
- npm or yarn
- Git
# Clone the repository
git clone https://github.com/mxcoppell/tradestation-api-ts.git
cd tradestation-api-ts
# Install dependencies
npm install
# Create local environment file
cp .env.local.sample .env.local
# Edit .env.local with your TradeStation API credentials
# Build the library
npm run build
# Build examples
npm run build:examples
# Build both library and examples
npm run build:all
# Run all tests
npm test
# Run tests with coverage
npm run test:coverage
# Run tests in watch mode
npm run test:watch
The project includes a helper script to run examples:
# List all available examples
./run-example.sh --list
# Run a specific example
./run-example.sh QuickStart/getStarted
# Run multiple examples
./run-example.sh MarketData/getBars MarketData/getQuotes
- Make your changes in the
src
directory - Add or update tests in
src/**/__tests__
- Run tests to ensure everything works
- Build the project to check for compilation errors
- Try your changes using the examples
- Submit a pull request with your changes
- Detailed API Documentation
- Authentication Guide
- Streaming Guide
- Rate Limiting
- Examples Guide
- Contributing Guide
- Changelog
We welcome contributions! Please see our Contributing Guide for details on:
- Code of Conduct
- Development process
- Pull request process
- Coding standards
- Testing requirements
# Install dependencies
npm install
# Build the library
npm run build
# Build examples
npm run build:examples
# Run tests
npm test
This project is licensed under the MIT License - see the LICENSE file for details.
- TradeStation for providing the WebAPI
- All contributors to this project
- The TypeScript team for the amazing language and tools
- For API-specific questions, refer to TradeStation API Documentation
- For issues with this wrapper, open an issue
- For general TradeStation questions, contact TradeStation Client Services
Disclaimer: This is an unofficial wrapper for the TradeStation WebAPI. It is not affiliated with, maintained, authorized, or endorsed by TradeStation.