XTS codebase

XTS webscokets

Drive for Python setup installers https://drive.google.com/drive/folders/1xHRH3BCApXMxoQJo0_fkoH2k_c767_qQ?usp=sharing

Use below config file for login in IIFL XTS.

config.ini

Use below file in program Directory for login in XTS.

Exception.py

Connect.py

Before moving forward, download above mentioned files

from XTS_TradeHull import Tradehull
import pdb

# creates tradehull object
# tsl = tradehull.Tradehull(interactive key,interactive secret,Market key,Market secret, clientID,'both')

tsl = Tradehull('','','','','','both')

# -------------for interactive login only----------------
# TH_XTS = tradehull.Tradehull(interactive key,interactive secret,None,None, clientID,'interactive')
tsl  						= Tradehull('','','None','None','','interactive')
xts_interactive 			= tsl.return_object('interactive')

# ---------------for market login only----------------
# TH_XTS = tradehull.Tradehull(None,None,Market key,Market secret, clientID,'market')

tsl 						= Tradehull('None','None','','','','market')
tsl_market 					= tsl.return_object('market')

# get Interactive object
XTS_Interactive = tsl.xts1

# get Market object
XYS_Market = tsl.xts2

# get instrument dataframe
instrument = tsl.instrument_df

# -----------------for historical data-------------

# TO GET THE HISTORICAL DATA

#Function:
#    get_historical_data(name: str, timeframe: str, interval: int) -> pd.DataFrame

#Description:
#    Fetches historical OHLC data for a given trading symbol.

#Parameters:
#    - name (str): Trading symbol, e.g., 'ACC-EQ'
#    - timeframe (str): Timeframe for the data, e.g., '15minute'
#    - interval (int): Number of intervals to fetch, e.g., 900 for 15-minute interval

#Supported Timeframes:
#    - 'minute'
#    - '2minute'
#    - '3minute'
#    - '5minute'
#    - '10minute'
#    - '15minute'
#    - '30minute'
#    - '60minute'
#    - 'day'

# Example Usage:
acc_df = tsl.get_historical_data('ACC-EQ', '15minute', tsl.intervals_dict['15minute'])
print(acc_df)

# ---------------- for script data of 800 scripts at a time ---------

# TO GET LTP, OPEN, HIGH, LOW, CLOSE VALUES FOR 800 SCRIPTS IN A SINGLE CALL
# Function:
#     get_stock_data(names: list) -> dict
# Description:
#     Fetches LTP, open, high, low, and close values for multiple trading symbols.
# Parameters:
#     - names (list): List of trading symbols, e.g., ['ACC-EQ', 'DMART-EQ']
# Example Usage:
#     script_data = tsl.get_stock_data(['ACC-EQ', 'DMART-EQ'])
#     acc = script_data['ACC-EQ']

script_data = tsl.get_stock_data(['ACC-EQ', 'DMART-EQ'])
acc = script_data['ACC-EQ']
print(acc)

#to get data for a single script
# ltp = tsl.get_ltp(Stock/Index/Option)
ltp = tsl.get_ltp('ACC-EQ')
Open, high, low, close = tsl.get_ohlc_data('ACC-EQ') 
quote = tsl.get_quote(['ACC-EQ', 'DMART-EQ'])

# --------- ATM/ITM/OTM strike price ---------
'''
TO GET ATM/ITM/OTM STRIKE PRICE

Function:
    get_atm_itm_otm_strike(ltp, underlying, multiplier, script_type, expiry)

Description:
    Calculates the ATM/ITM/OTM strike price based on the underlying asset and other parameters.

Parameters:
    - ltp: Last traded price
    - underlying: Trading symbol, e.g., 'ACC-EQ'
    - multiplier: Determines ITM/OTM level (0

    # - multiplier: Determines ITM/OTM level (0 for ATM, positive for OTM, negative for ITM)
    - script_type: 'CE' for call options, 'PE' for put options
    - expiry: The expiry index (0 for nearest expiry)

Example Usage:
    itm_strike = tsl.get_atm_itm_otm_strike(underlying_ltp,Underlying, strike no, Option type, expiry)
    print(f'ITM STRIKE : {itm_strike}')
'''
itm_strike = tsl.get_atm_itm_otm_strike(Underlying ltp, Underlying, -1, 'CE', 0)
print(f'ITM STRIKE : {itm_strike}')

# -------------------calling delta and other values--------------------
'''
TO GET OPTION GREEK VALUES

Function:
    get_option_greek(strike: int, expiry_date: str, asset: str, interest_rate: float, flag: str, scrip_type: str)

Description:
    Calculates option Greeks for a given strike, expiry date, and asset.

Parameters:
    - strike (int): Strike price of the option
    - expiry_date (str): Expiry date of the option in 'YYYY-MM-DDTHH:MM:SS' format
    - asset (str): Name of the underlying asset, e.g., 'NIFTY'
    - interest_rate (float): Interest rate, e.g., 2
    - flag (str): Greek value to fetch ('price', 'delta', 'delta2', 'theta', 'rho', 'vega', 'gamma', 'all_val')
    - scrip_type (str): 'CE' for call options, 'PE' for put options

Example Usage:
    delta = tsl.get_option_greek(22100, '2024-07-04T14:30:00', 'NIFTY', 2, 'delta', 'CE')
    print(f'NIFTY CURRENT EXPIRY 22100 DELTA: {delta}')
'''
delta = tsl.get_option_greek(22100, '2024-07-04T14:30:00', 'NIFTY', 2, 'delta', 'CE')
print(f'NIFTY CURRENT EXPIRY 22100 DELTA: {delta}')

# ----------------------placing order-------------------------
'''
TO PLACE AN ORDER

Function:
    order_placement(tradingsymbol: str, quantity: int, price: int, trigger_price: int, order_type: str, transaction_type: str, trade_type: str) -> str

Description:
    Places an order for a given trading symbol with specified parameters.

Parameters:
    - tradingsymbol (str): Trading symbol, e.g., 'ACC-EQ'
    - quantity (int): Order quantity
    - price (int): Limit price for the order
    - trigger_price (int): Trigger price for stop orders
    - order_type (str): Type of order ('LIMIT', 'MARKET', 'STOPLIMIT', 'STOPMARKET')
    - transaction_type (str): Transaction type ('BUY', 'SELL')
    - trade_type (str): Trade type ('MIS', 'NRML')

ORDER TYPES:
- 'LIMIT' - ('ACC-EQ', 1, 2700, 0, 'LIMIT', 'BUY', 'MIS')
- 'MARKET' - ('ACC-EQ', 1, 0, 0, 'MARKET', 'BUY', 'MIS')
- 'STOPLIMIT' - ('ACC-EQ', 1, 2704, 2700, 'STOPLIMIT', 'BUY', 'MIS')
- 'STOPMARKET' - ('ACC-EQ', 1, 0, 2700, 'STOPMARKET', 'BUY', 'MIS')

TRANSACTION TYPES:
- 'BUY' - ('ACC-EQ', 1, 0, 0, 'MARKET', 'BUY', 'MIS')
- 'SELL' - ('ACC-EQ', 1, 0, 0, 'MARKET', 'SELL', 'MIS')

TRADE TYPES:
- 'MIS' - ('ACC-EQ', 1, 0, 0, 'MARKET', 'BUY', 'MIS')
- 'NRML' - ('ACC-EQ', 1, 0, 0, 'MARKET', 'BUY', 'NRML')

Example Usage:
    orderid = tsl.order_placement('ACC-EQ', 1, 0, 0, 'MARKET', 'BUY', 'MIS')
    print(orderid)
'''
orderid = tsl.order_placement('ACC-EQ', 1, 0, 0, 'MARKET', 'BUY', 'MIS')
print(orderid)

# Get order status by passing orderID
# status = tsl.get_order_status(orderID)
status = tsl.get_orderhistory('515561515165')

# Get average executed price of any order
# price = tsl.get_executed_price(orderID)
price = tsl.get_executed_price('15465456541')

# ----------------------Get Order Book-------------------------
'''
# TO GET ALL ORDER STATUS AND PRICE DETAILS

# Function:
#     order_report() -> Tuple[Dict, Dict]

# Description:
#     Provides a report of all orders with their status and execution prices.

# Example Usage:
#     order_status, order_price = tsl.order_report()
#     print(order_status)
#     print(order_price)
order_status, order_price = tsl.order_report()

# ----------------------MODIFY ORDER-------------------------
'''
# TO MODIFY AN ORDER

# Function:
#     modify_order(appOrderID: str, modifiedOrderType: str, modifiedOrderQuantity: int, modifiedLimitPrice: int, modifiedStopPrice: int, trade_type: str) -> str

# Description:
#     Modifies an existing order with new parameters.

# Parameters:
#     - appOrderID (str): Order ID of the placed order
#     - modifiedOrderType (str): New order type ('LIMIT', 'STOPLIMIT', 'STOPMARKET')
#     - modifiedOrderQuantity (int): New quantity for the order
#     - modifiedLimitPrice (int): New limit price for the order
#     - modifiedStopPrice (int): New stop price for the order
#     - trade_type (str): New trade type ('MIS', 'NRML')

# ORDER TYPES:
# - 'LIMIT' - ('436782', 1, 2750, 0, 'LIMIT', 'BUY', 'MIS')
# - 'STOPLIMIT' - ('436782', 1, 2707, 2704, 'STOPLIMIT', 'BUY', 'MIS')
# - 'STOPMARKET' - ('436782', 1, 0, 2704, 'STOPMARKET', 'BUY', 'MIS')

# TRADE TYPES:
# - 'MIS' - ('436782', 1, 0, 0, 'MARKET', 'BUY', 'MIS')
# - 'NRML' - ('436782', 1, 0, 0, 'MARKET', 'BUY', 'NRML')

# Example Usage:
#     orderid = tsl.modify_order(orderid, 'LIMIT', 1, 2705, 0, 'MIS')
#     print(orderid)
'''
orderid = tsl.modify_order(orderid, 'LIMIT', 1, 2705, 0, 'MIS')

# ----------------------CANCEL ORDER-------------------------
'''
# TO CANCEL AN ORDER

# Function:
#     cancel_order(OrderID: str) -> None

# Description:
#     Cancels a specific order.

# Parameters:
#     - OrderID (str): Order ID of the placed order

# Example Usage:
#     tsl.cancel_order(orderid)
'''
tsl.cancel_order(orderid)

# ----------------------CANCEL ALL ORDERS-------------------------
'''
# TO CANCEL ALL ORDERS

# Function:
    # cancel_all_orders() -> dict

# Description:
    # Cancels all open orders and returns a report of the canceled orders.

# Example Usage:
    # order_details = tsl.cancel_all_orders()
    # print(order_details)
'''
order_details = tsl.cancel_all_orders()

# ----------------------GET EXPIRY DATES-------------------------
'''
# TO GET A EXPIRY FOR A SPECIFIC SPOT

# Function:
#     get_expiry(name) -> list

# Parameters:
#     -name (str): spot name to get the expiry

# Spots:
#     - MIDCPNIFTY
#     - SENSEX
#     - BANKEX
#     - NIFTY
#     - BANKNIFTY
#     - FINNIFTY

# Description:
#     Get the all the expires of a specific spot and return all the dates.

# Example Usage:
#     expiry_dates = tsl.get_expiry('NIFTY')
#     print(expiry_dates)
expiry_dates = tsl.get_expiry('BANKNIFTY')

# ----------------------CHECK VALID INNSTRUMENT-------------------------
'''
# TO CHECK THE GIVEN INSTRUMENT/SCRIPT VALID OR NOT

# Function:
#     check_valid_instrument(name) -> str

# Parameters:
#     -name (str): instrument name to check valid or not

# Description:
#     TO check the given instrument valid or not and return the string 

# Example Usage:
#     isvalid = tsl.check_valid_instrument('NIFTY')
#     print(isvalid)
'''
isvalid = tsl.check_valid_instrument('BANKNIFTY')
print(isvalid)

<aside> 💡 To get more details regarding function calls. let's check their outputs

</aside>