npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

tradestation-api-ts

v1.2.1

Published

A comprehensive TypeScript wrapper for TradeStation WebAPI v3

Readme

TradeStation API TypeScript Wrapper

A comprehensive TypeScript wrapper for TradeStation WebAPI v3, providing type-safe access to TradeStation's brokerage and market data services.

License: MIT

Overview

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.

Installation

npm install tradestation-api-ts

Usage

Basic Setup

import { TradeStationClient } from 'tradestation-api-ts';

// Initialize with environment variables
// Automatically reads CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, and ENVIRONMENT from .env
const client = new TradeStationClient();

// Or with explicit configuration
const client = new TradeStationClient({
    refresh_token: 'your_refresh_token',
    environment: 'Simulation'  // or 'Live'
});

Market Data

// 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
});

Order Execution

// 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' }
        }
    ]
});

Brokerage

// 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);
});

Error Handling

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);
    }
}

Resource Cleanup

// Close specific stream
stream.emit('close');

// Close all active streams
client.closeAllStreams();

Quick Start

  1. Create a .env file with your TradeStation API credentials:
CLIENT_ID=your_client_id
CLIENT_SECRET=your_client_secret
REFRESH_TOKEN=your_refresh_token
ENVIRONMENT=Simulation  # or 'Live'
  1. 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);
});

Features

1. Request Client

  • Automatic token management
  • Configurable retry logic
  • Error handling
  • Request/response interceptors

2. Authentication

  • OAuth 2.0 implementation
  • Automatic token refresh
  • Simulation/Live environment support

3. Rate Limiter

  • Automatic rate limiting
  • Configurable limits
  • Queue management
  • Headers-based rate tracking

4. Market Data APIs

  • Real-time quotes
  • Historical bars
  • Option chains
  • Market depth
  • Symbol information

5. Brokerage APIs

  • Account management
  • Position tracking
  • Balance information
  • Order history
  • Activity tracking

6. Order Execution APIs

  • Order placement
  • Order modification
  • Order cancellation
  • Complex order types
  • OCO and bracket orders

Local Development

Prerequisites

  • Node.js (>=14.0.0)
  • npm or yarn
  • Git

Clone and Setup

# 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.sample .env
# Edit .env with your TradeStation API credentials

Build

# Build the library
npm run build

# Build examples
npm run build:examples

# Build both library and examples
npm run build:all

Test

# Run all tests
npm test

# Run tests with coverage
npm run test:coverage

# Run tests in watch mode
npm run test:watch

Run Examples

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

Development Workflow

  1. Make your changes in the src directory
  2. Add or update tests in src/**/__tests__
  3. Run tests to ensure everything works
  4. Build the project to check for compilation errors
  5. Try your changes using the examples
  6. Submit a pull request with your changes

Documentation

Contributing

We welcome contributions! Please see our Contributing Guide for details on:

  • Code of Conduct
  • Development process
  • Pull request process
  • Coding standards
  • Testing requirements

Building from Source

# Install dependencies
npm install

# Build the library
npm run build

# Build examples
npm run build:examples

# Run tests
npm test

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • TradeStation for providing the WebAPI
  • All contributors to this project
  • The TypeScript team for the amazing language and tools

Support


Disclaimer: This is an unofficial wrapper for the TradeStation WebAPI. It is not affiliated with, maintained, authorized, or endorsed by TradeStation.