top of page
My photo

TechBobbles
@Siddartha

  • Writer's pictureAdmin

How to get Open Interest data from NSE site using PowerShell



Getting Open Interest data from the National Stock Exchange (NSE) website can be done using PowerShell. PowerShell is a task automation and configuration management framework developed by Microsoft, and it can be used to automate tasks on Windows and other operating systems. In this blog post, we will look at how to get Open Interest data from the NSE website using PowerShell.

Before we proceed with the script, let's understand what Open Interest data is and why it is important for traders and investors.

Open Interest is the total number of outstanding contracts that are held by market participants at any given point in time. It is an important parameter used by traders and investors to gauge market sentiment and identify potential trends in the market.



This script will help to fetch the total sum of calls and the total sum of the puts. If the current strike price is 40000 , then it will pick the starting strike at 39000 and end strike price 41000 and collect the respective OI(open interest) and calculate the total value.





There may be multiple ways to extract data, I use selenium to fetch data, I tried with the "invoke-webrequest" method but the command was getting stuck multiple times.


To run the commands first requirement is to download the latest version of the chrome browser and then the latest version of the chrome web driver. Then install the selenium module using PowerShell administrative user.


Once you install the selenium, if the chrome browser and chrome webdriver version mismatch happens the script will fail. Better download the latest web driver from the web and paste it into the right location.


Default location

C:\Program Files (x86)\WindowsPowerShell\Modules\Selenium\3.0.1\assemblies


Check location command:



Commands below :

# install selenium

Install-Module selenium


Import-Module selenium

$opt = New-Object OpenQA.Selenium.Chrome.ChromeOptions

$opt.addArguments("disable-infobars");

$opt.addArguments("--start-maximized");

$opt.addArguments("--disable-extensions");

#$opt.addArguments("headless");

$driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($opt)

($driver.Navigate().GoToURL('https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY'))

$totalData = ($driver.FindElementByXPath('/html/body/pre')).Text | ConvertFrom-Json

$driver.Quit()


function get-OpenIntrest($dataBank)

{

#$dataBank = Invoke-RestMethod -Uri "https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY" -Method Get

$bankNiftyCurrentValue = [math]::Round($dataBank.records.underlyingValue)

for ($i=$bankNiftyCurrentValue; $i -lt ($bankNiftyCurrentValue + 200); $i++)

{


$remider = $i % 100

if($remider -eq 0)

{

$currentStrikePrice = $i

break

}

}

$StartRange = $currentStrikePrice - 1000

$EndRange = $currentStrikePrice + 1000


$totalOICE = 0

$totalOIPE = 0


for($j=$StartRange;$j -le $EndRange;$j =$j+100)

{


[int]$OI = ($dataBank.records.data.ce | where {$_.expirydate -like "*23-Mar-2023*"} | where {$_.strikeprice -eq $j}).changeinOpenInterest

[int]$totalOICE = [int]$OI + [int]$totalOICE



[int]$OI = ($dataBank.records.data.pe | where {$_.expirydate -like "*23-Mar-2023*"} | where {$_.strikeprice -eq $j}).changeinOpenInterest

[int]$totalOIPE = [int]$OI + [int]$totalOIPE


}

return @($totalOICE,$totalOIPE)

}

get-OpenIntrest $totalData




Thanks,

Siddartha Kumar Das



Comments


Thanks for visiting 

Bangalore,

Siddartha1192@gmail.com

+919148014455

  • Linkedin
  • Wix Facebook page
  • Wix Twitter page
Subscribe to get exclusive updates
bottom of page