tradestation-api-ts
Version:
A comprehensive TypeScript wrapper for TradeStation WebAPI v3
68 lines (67 loc) • 2.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TradeStationClient = void 0;
const HttpClient_1 = require("./HttpClient");
const StreamManager_1 = require("../streaming/StreamManager");
const MarketDataService_1 = require("../services/MarketDataService");
const OrderExecutionService_1 = require("../services/OrderExecutionService");
const BrokerageService_1 = require("../services/BrokerageService");
class TradeStationClient {
/**
* Creates a new TradeStationClient instance
* @param config Optional configuration object. If not provided, values will be read from environment variables
* @example
* // Using environment variables (CLIENT_ID and CLIENT_SECRET must be set)
* const client = new TradeStationClient({
* refresh_token: 'your_refresh_token',
* environment: 'Simulation' // or 'Live'
* });
*
* // Using explicit configuration
* const client = new TradeStationClient({
* clientId: 'your_client_id',
* clientSecret: 'your_client_secret',
* refresh_token: 'your_refresh_token',
* environment: 'Simulation' // or 'Live'
* });
*/
constructor(config) {
// Get environment from config or env var
let environment = config?.environment || process.env.ENVIRONMENT;
// Normalize environment to proper case
if (environment) {
environment = environment.toLowerCase() === 'simulation' ? 'Simulation' : 'Live';
}
else {
throw new Error('Environment must be specified either in config or ENVIRONMENT env var');
}
// Get refresh token from config or env var
const refresh_token = config?.refresh_token || process.env.REFRESH_TOKEN;
// Create final config with normalized environment and refresh token
const finalConfig = {
...config,
environment,
refresh_token
};
this.httpClient = new HttpClient_1.HttpClient(finalConfig);
this.streamManager = new StreamManager_1.StreamManager(this.httpClient, finalConfig);
// Initialize services
this.marketData = new MarketDataService_1.MarketDataService(this.httpClient, this.streamManager);
this.orderExecution = new OrderExecutionService_1.OrderExecutionService(this.httpClient, this.streamManager);
this.brokerage = new BrokerageService_1.BrokerageService(this.httpClient, this.streamManager);
}
/**
* Gets the current refresh token
* @returns The current refresh token or null if none is available
*/
getRefreshToken() {
return this.httpClient.getRefreshToken();
}
/**
* Closes all active streams
*/
closeAllStreams() {
this.streamManager.closeAllStreams();
}
}
exports.TradeStationClient = TradeStationClient;