0

i am new to Interactive brokers API, wondering if there is any way to get quote of NIFTY50 index, as i can see examples of NIFTY FUTURES, etc but not on index price quote of NIFTY50 or BANKNIFTY,

need help with some example, thanks,

sample on on getting quote of NIFTY OPTIONS,

    from locale import currency
    from ibapi.client import EClient
    from ibapi.wrapper import EWrapper
    from ibapi.contract import Contract
    from ibapi.ticktype import TickTypeEnum
    from ibapi.common import *
    import threading
    import time

    def get_close_quote(symbol, secType='OPT', exchange='NSE', currency='INR'):
        class IBapi(EWrapper, EClient):
            def __init__(self):
                EClient.__init__(self, self)
            def historicalData(self, reqId, bar):
                print(f'Time: {bar.date} Close: {bar.close}')
            def tickPrice(self, reqId, tickType, price, attrib):
                if tickType == TickTypeEnum.LAST or tickType == TickTypeEnum.DELAYED_LAST:
                    self.last = price;
                    self.disconnect()

        def run_loop():
            app.run()

        app = IBapi()
        app.connect('127.0.0.1', 7496, 123)

        #Start the socket in a thread
        api_thread = threading.Thread(target=run_loop, daemon=True)
        api_thread.start()

        time.sleep(1) #Sleep interval to allow time for connection to server

        #Create contract object
        contract = Contract()
        contract.localSymbol = symbol
        contract.secType = secType
        contract.exchange = exchange
        contract.currency = currency

        app.reqMktData(100, contract, "", False, False, None)
        time.sleep(2) #sleep to allow enough time for data to be returned
        app.disconnect()
        return(app.last)
Maria628
  • 224
  • 3
  • 19

1 Answers1

0

I am not familiar with Interactive brokers API but from digging on their website I did find those useful commands. You are welcome to check more than provided in this answer.

If you are trying to get a different type of security, as per the documentation, you would change the contract.secType to one of the following:

The security's type: STK - stock (or ETF) OPT - option FUT - future IND - index FOP - futures option CASH - forex pair BAG - combo WAR - warrant BOND- bond CMDTY- commodity NEWS- news FUND- mutual fund.

As I understood, you also want to get the actual price as part of the request for example last price is tick id = 4 as seen on the documentation of different types, and in general only have to request for specific ticks.

In reqMktData the genericTickList (3rd parameter) can be used to specify extra ticks to monitor, like demonstrated in documentation:

# Requesting RTVolume (Time & Sales) and shortable generic ticks
self.reqMktData(1004, ContractSamples.USStockAtSmart(), "233,236", False, False, [])

Where "233, 236" are the specified ticks to get from the website.

Also when checking the contract documentation, you can acquire the Strike price from the contract object.

Warkaz
  • 845
  • 6
  • 18