jstiqs
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

TIQS Trading API SDK

A TypeScript/JavaScript SDK for the TIQS Trading API, providing a simple interface to trade stocks, derivatives, and access market data.

Installation

npm install jstiqs

Getting Started

// Import the SDK
import { Tiqs } from 'jstiqs';

// Initialize the SDK
const tiqs = new Tiqs({
    appId: 'YOUR_APP_ID',
    debug: true // Optional, enables debug logging
});

Authentication

// Get login URL
const loginUrl = tiqs.loginUrl();
console.log('Please visit this URL to login:', loginUrl);
console.log('After login, you will get a request token in the url');

// After user authorization, generate session token
const requestToken = 'YOUR_REQUEST_TOKEN';
const appSecret = 'YOUR_APP_SECRET';
const session = await tiqs.generateSession(requestToken, appSecret);

// Session token is automatically set, but you can also set it manually
tiqs.setAccessToken('YOUR_ACCESS_TOKEN');

Market Data

WebSocket Market Data

// Connect to WebSocket
await tiqs.connectWebSocket();

// Subscribe to different modes of market data
tiqs.subscribe(MarketDataMode.LTPC, [26009, 26000]); // BANKNIFTY and NIFTY LTPC data
tiqs.subscribe(MarketDataMode.QUOTE, [26009]); // BANKNIFTY full quote
tiqs.subscribe(MarketDataMode.FULL, [26000]); // NIFTY full market depth

// Register callback for market data updates
tiqs.onMarketData((data) => {
    console.log('Received market data:', data);
});

// Unsubscribe when done
tiqs.unsubscribe(MarketDataMode.LTPC, [26009, 26000]);

REST Market Data

// Get quote for a single instrument
const quote = await tiqs.getQuote(26009, QuoteMode.FULL);

// Get quotes for multiple instruments
const quotes = await tiqs.getQuotes([26009, 26000], QuoteMode.FULL);

// Get historical data
const historicalData = await tiqs.getHistoricalData(
    'NSE',
    '26000',
    new Date('2023-05-01T00:00:00'),
    new Date('2023-06-29T20:00:00'),
    'month'
);

Order Management

Place Order

// Place a regular order
const orderResponse = await tiqs.placeOrder({
    exchange: "NSE",
    quantity: "15",
    disclosedQty: "0",
    product: "I",
    symbol: "ITC",
    transactionType: "B",
    order: "LMT",
    price: "200",
    validity: "DAY",
    tags: "ok,ok2",
    triggerPrice: "200"
});

// Place a cover order
const coverOrder = await tiqs.placeOrder({
    exchange: "NSE",
    quantity: "15",
    disclosedQty: "0",
    product: "I",
    symbol: "ITC",
    transactionType: "B",
    order: "LMT",
    price: "10",
    validity: "DAY",
    tags: "ok,ok2",
    amo: false,
    triggerPrice: "0",
    bookLossPrice: "10"
});

Order Operations

// Modify an order
const modifiedOrder = await tiqs.modifyOrder('regular', 'ORDER_ID', {
    exchange: "NSE",
    quantity: "15",
    disclosedQty: "0",
    product: "I",
    symbol: "ITC",
    transactionType: "B",
    order: "LMT",
    price: "200",
    validity: "DAY",
    tags: "ok,ok2",
    triggerPrice: "200"
});

// Cancel an order
const cancelResponse = await tiqs.cancelOrder('regular', 'ORDER_ID');

// Get order details
const orderDetails = await tiqs.getOrder('ORDER_ID');

// Get all orders
const orders = await tiqs.getOrders();

// Get all trades
const trades = await tiqs.getTrades();

Portfolio Management

// Get positions
const positions = await tiqs.getPositions();

// Get holdings
const holdings = await tiqs.getHoldings();

// Get user limits and margins
const limits = await tiqs.getUserLimits();

// Calculate available margin
const availableMargin = tiqs.calculateAvailableMargin(limits[0]);

Margin Calculations

// Calculate margin for a single order
const margin = await tiqs.getOrderMargin({
    symbol: 'ITC',
    exchange: 'NSE',
    quantity: '25',
    product: 'M',
    price: '905',
    transactionType: 'S',
    order: 'LMT'
});

// Calculate margin for basket orders
const basketMargin = await tiqs.getBasketMargin([
    {
        symbol: 'ITC',
        exchange: 'NSE',
        quantity: '900',
        price: '133.5',
        product: 'M',
        transactionType: 'B',
        order: 'LMT'
    },
    {
        symbol: 'ITC',
        exchange: 'NSE',
        quantity: '900',
        price: '132',
        product: 'M',
        transactionType: 'S',
        order: 'LMT'
    }
]);

Options Trading

// Get option chain symbols
const optionChainSymbols = await tiqs.getOptionChainSymbols();

// Get option chain
const optionChain = await tiqs.getOptionChain({
    token: '26009', // BANKNIFTY
    exchange: 'INDEX',
    count: '10',
    expiry: '27-FEB-2025'
});

// Get Greeks values
const greeks = await tiqs.getGreeks([43797, 43798]);

Utility Methods

// Get user profile
const profile = await tiqs.getUserDetails();

// Get exchange holidays
const holidays = await tiqs.getHolidays();

// Get index list
const indices = await tiqs.getIndexList();

// Get all instruments
const instruments = await tiqs.getInstruments();

Rate Limits

The SDK automatically handles rate limiting. Here are the default limits:

  • Quote API: 10 requests/second
  • Historical candle API: 10 requests/second
  • Order placement: 10 requests/second (max 200 orders/minute, 3,000 orders/day)
  • All other endpoints: 100 requests/second

Error Handling

The SDK throws standardized errors with proper error codes and messages:

try {
    const order = await tiqs.placeOrder({...});
} catch (error) {
    if (error.errorCode === 'TooManyRequests') {
        console.log('Rate limit exceeded');
    } else if (error.errorCode === 'UnAuthorized') {
        console.log('Session expired, please relogin');
    } else {
        console.log('Error:', error.message);
    }
}

WebSocket Reconnection

The WebSocket connection automatically handles disconnections and will attempt to reconnect. You can monitor the connection status through the debug logs:

const tiqs = new Tiqs({
    appId: 'YOUR_APP_ID',
    debug: true
});

Types

The SDK provides comprehensive TypeScript types for all requests and responses. Refer to the types.ts file for detailed type definitions.

Support

For any queries or support, please contact: apisupport@tiqs.in

Package Sidebar

Install

npm i jstiqs

Homepage

tiqs.in

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

47.5 kB

Total Files

10

Last publish

Collaborators

  • neeraj12300