Search This Blog

Friday, August 30, 2019

Python Code to pull NSE Option Derivative HTML to JSON on Every N minutes

1. Install Python, PIP
2. type the below command from your python sample folder [ by pip or pip3 cmd ].
     pip install beautifulsoup
     OR
     pip install BeautifulSoup4
     pip install requests
     pip install schedule
     python nse_options.py

-- ENJOY

nse_options.py

# author - dr. vijay
# email - drvijayy2k2@gmail.com
# date - 30-08-2019 7:30 PM

# install plugin
# pip install BeautifulSoup

import sys
import json
import requests
# Schedule Library imported
import schedule
import time
import datetime

from bs4 import BeautifulSoup

base_url = 'https://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbol=';


def doScheduler ():
    print ( "Schedule starts @ ", datetime.datetime.now() );
    result = parseNseOptions ( 'NIFTY' );
    # result = parseNseOptions ( 'INFY' );
    f = open( 'D:/nseOptionst.txt', 'a' )
    f.write( str(result) + "\n" );
    print ( "Done !" );
 

def parseNseOptions ( scrip='NIFTY' ):
    url = base_url + scrip;
    response = response = requests.get( url ) 
    if( response != None and response.ok ):
        optionScripListHtml = response.content
        # print ( optionScripListHtml );
        soup = BeautifulSoup( optionScripListHtml, "html.parser" )
        tableHtml = soup.find( "table", id="octable" )
        f1 = tableHtml.findAll( "thead" )[0].findAll( 'tr' )
     
        callDataColumnEndsAt = 11
        optionStrikePriceColumnAt = ( 12 - 1 )
        putDataColumnEndsAt = 23
        headers = {}
        headersOnIndex = {}
        h1 = f1[1].findAll( "th" )
        for index, h in enumerate( h1, start=0 ):
            if ( index < callDataColumnEndsAt ):
                headers["call-" + h.text] = h.get( "title", "" )
                headersOnIndex[index] = "call-" + h.text 
            elif ( index == optionStrikePriceColumnAt ):
                 headers[h.text] = h.get( "title", "" )
                 headersOnIndex[index] = h.text 
            elif ( index < putDataColumnEndsAt ):
                headers["put-" + h.text] = h.get( "title", "" )
                headersOnIndex[index] = "put-" + h.text 
        # print(headersOnIndex)
     
        fnoDataRows = tableHtml.findAll( "tr" )
        tempMap = {}
        totalOptionPCOI = {}
        tempMapWithStrikePriceAsKey = {}
        startingRow = 3
        endingRow = len( fnoDataRows )
        callOIChangeCol = 2
        callTotalChangeInOIValue = 0
        putOIChangeCol = 20
        putTotalChangeInOIValue = 0
        index = 1
        colIndex = 1
        recordFound = False
     
        for rowIndex, x in enumerate( fnoDataRows, start=0 ):
            if ( rowIndex == endingRow - 1 ):
                xx = x.findAll( "td" )
                totalOptionPCOI = {"callTotalOI": xx[1].text, "callTotalChangeInOI": xx[2].text, "callTotalVolume": xx[3].text, "putTotalVolume": xx[5].text, "putTotalChangeInOI": xx[6].text, "putTotalOI": xx[7].text }
                # break the entire loop as we got all the information
                break;
            for colIndex, c in enumerate( x.findAll( "td" ), start=0 ):
                value = c.text.strip()
                if value == "" or value == "-":
                    value = "0"
                else:
                    value = value.replace( ",", "" )
             
                if ( colIndex == callOIChangeCol ):
                    callTotalChangeInOIValue = callTotalChangeInOIValue + int( value )
                if ( colIndex == putOIChangeCol ):
                    putTotalChangeInOIValue = putTotalChangeInOIValue + int( value )
                     
                tempMap[ headersOnIndex[colIndex]] = value
                recordFound = True
             
            if recordFound == True:
                tempMapWithStrikePriceAsKey[tempMap[headersOnIndex[optionStrikePriceColumnAt]]] = tempMap.copy()
                strikePrice = tempMap["Strike Price"]
                tempMap.clear()   

            recordFound = False
     
        return dict( { "resultFetchTime": datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "totalOptionPCOI":totalOptionPCOI, "mapWithStrikePriceAsKey":tempMapWithStrikePriceAsKey} )


if __name__ == "__main__":
 
    schedule.every( 3 ).minutes.do( doScheduler )
    while True:
        schedule.run_pending()
        time.sleep( 1 )


OUTPUT

{
'mapWithStrikePriceAsKey': {
//..... other strike prices
'10850.00': {
'call-Chart': '0',
'call-OI': '49950',
'call-Chng in OI': '26475',
'call-Volume': '10689',
'call-IV': '14.71',
'call-LTP': '210.75',
'call-Net Chng': '29.00',
'call-BidQty': '300',
'call-BidPrice': '208.85',
'call-AskPrice': '215.95',
'call-AskQty': '75',
'Strike Price': '10850.00',
'put-BidQty': '300',
'put-BidPrice': '19.50',
'put-AskPrice': '20.45',
'put-AskQty': '600',
'put-Net Chng': '-25.55',
'put-LTP': '20.45',
'put-IV': '14.92',
'put-Volume': '134223',
'put-Chng in OI': '261600',
'put-OI': '399750',
'put-Chart': '0'
},
'10900.00': {
'call-Chart': '0',
'call-OI': '574650',
'call-Chng in OI': '198600',
'call-Volume': '157457',
'call-IV': '14.15',
'call-LTP': '169.05',
'call-Net Chng': '26.85',
'call-BidQty': '4350',
'call-BidPrice': '169.05',
'call-AskPrice': '170.85',
'call-AskQty': '75',
'Strike Price': '10900.00',
'put-BidQty': '150',
'put-BidPrice': '27.45',
'put-AskPrice': '28.20',
'put-AskQty': '525',
'put-Net Chng': '-33.60',
'put-LTP': '28.20',
'put-IV': '14.22',
'put-Volume': '396350',
'put-Chng in OI': '894450',
'put-OI': '1796100',
'put-Chart': '0'
}
  //..... other strike prices
},
'totalOptionPCOI': {
'callTotalOI': ' 12,057,225',
'callTotalChangeInOI': '',
'callTotalVolume': ' 2,231,841',
'putTotalVolume': ' 1,883,484',
'putTotalChangeInOI': '',
'putTotalOI': ' 12,479,775'
}

}

No comments:

Hit Counter


View My Stats