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)
rateLimitRateLimiterOptions-Rate limiting (prevent rate limit errors)
rotateUserAgentbooleanfalseEnable UA rotation (Node.js only)
circuitBreakerCircuitBreakerOptions-Circuit breaker (pause on consecutive failures)
providerPoliciesPartial<Record<ProviderName, ProviderRequestPolicy>>-Override timeout, retry, rate limit, circuit breaker, and headers per provider

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 (RateLimiterOptions)

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

Recommendation

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

Provider Overrides (ProviderRequestPolicy)

Global timeout / retry / rateLimit / circuitBreaker settings still work as the default policy.
providerPolicies only overrides those defaults for a specific provider, so existing initialization code remains compatible.

typescript
interface ProviderRequestPolicy {
  timeout?: number;
  retry?: RetryOptions;
  headers?: Record<string, string>;
  userAgent?: string;
  rateLimit?: RateLimiterOptions;
  rotateUserAgent?: boolean;
  circuitBreaker?: CircuitBreakerOptions;
}

Built-in provider names:

  • tencent
  • eastmoney
  • sina
  • linkdiary
  • unknown

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: 'my-stock-app/1.0',
  
  // 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}`);
    }
  },

  // Override strategy for a specific provider
  providerPolicies: {
    eastmoney: {
      timeout: 12000,
      retry: {
        maxRetries: 5,
        baseDelay: 800,
      },
      rateLimit: {
        requestsPerSecond: 3,
        maxBurst: 3,
      },
      circuitBreaker: {
        failureThreshold: 3,
        resetTimeout: 30000,
      }
    }
  }
});

See Error Handling & Retry for details.

Real-time Quotes

K-Line Data

Technical Indicators

Industry Sectors

Concept Sectors

Batch & Extended

Fund Flow (Deep)

Limit-Up & Dragon-Tiger

Other Data

Released under the ISC License.