Skip to content

API Overview

This page helps you quickly locate Stock SDK features and specific interfaces.

SDK Initialization

typescript
import { StockSDK } from 'stock-sdk';

const sdk = new StockSDK(options?);

Configuration Options

OptionTypeDefaultDescription
baseUrlstring'https://qt.gtimg.cn'Tencent API endpoint (can use proxy)
timeoutnumber30000Request timeout (ms)
retryRetryOptionsSee belowRetry configuration
headersRecord<string, string>-Custom request headers
userAgentstring-Custom User-Agent (may be ignored in browsers)
rateLimitRateLimitOptions-Rate limiting (prevent rate limit errors)
rotateUserAgentbooleanfalseEnable UA rotation (Node.js only)
circuitBreakerCircuitBreakerOptions-Circuit breaker (pause on consecutive failures)

Retry Options (RetryOptions)

OptionTypeDefaultDescription
maxRetriesnumber3Maximum retry attempts
baseDelaynumber1000Initial backoff delay (ms)
maxDelaynumber30000Maximum backoff delay (ms)
backoffMultipliernumber2Backoff multiplier
retryableStatusCodesnumber[][408, 429, 500, 502, 503, 504]HTTP status codes to retry
retryOnNetworkErrorbooleantrueRetry on network errors
retryOnTimeoutbooleantrueRetry on timeout
onRetryfunction-Retry callback (attempt, error, delay) => void

Rate Limit Options (RateLimitOptions)

OptionTypeDefaultDescription
requestsPerSecondnumber5Maximum requests per second
maxBurstnumber= requestsPerSecondToken bucket capacity (burst limit)

Recommendation

Configure requestsPerSecond: 3~5 to avoid triggering Eastmoney's rate limits.

Circuit Breaker Options (CircuitBreakerOptions)

Disabled by Default

The circuit breaker is disabled by default and must be explicitly configured. Recommended for production environments to prevent cascade failures.

OptionTypeDefaultDescription
failureThresholdnumber5Number of consecutive failures to trigger open state
resetTimeoutnumber30000Time in ms before transitioning to half-open
halfOpenRequestsnumber1Number of probe requests allowed in half-open state
onStateChangefunction-State change callback (from, to) => void

Circuit Breaker States:

  • CLOSED: Normal state, all requests allowed
  • OPEN: Circuit is open, all requests rejected with CircuitBreakerError
  • HALF_OPEN: Allows limited probe requests to test if service recovered

Full Configuration Example

typescript
const sdk = new StockSDK({
  timeout: 10000,
  headers: {
    'X-Request-Source': 'my-app',
  },
  userAgent: 'StockSDK/1.6',
  
  // Retry configuration
  retry: {
    maxRetries: 5,
    baseDelay: 500,
    onRetry: (attempt, error, delay) => {
      console.log(`Retry ${attempt}, waiting ${delay}ms`);
    }
  },
  
  // Rate limiting (recommended)
  rateLimit: {
    requestsPerSecond: 5,
    maxBurst: 10,
  },
  
  // UA rotation (Node.js only)
  rotateUserAgent: true,
  
  // Circuit breaker (optional, recommended for production)
  circuitBreaker: {
    failureThreshold: 5,
    resetTimeout: 30000,
    onStateChange: (from, to) => {
      console.log(`Circuit breaker: ${from} -> ${to}`);
    }
  }
});

See Error Handling & Retry for details.

Real-time Quotes

K-Line Data

Technical Indicators

Industry Sectors

Concept Sectors

Batch & Extended

Released under the ISC License.