0% found this document useful (0 votes)
136 views

Power Query Keval

The document contains Power Query M code to extract option chain data from a URL, transform it into a table, and retrieve specific fields. It makes a request to an API endpoint to get JSON data, expands and transforms the records into columns, extracts key fields like strike price and expiry date, and concatenates some fields together.

Uploaded by

gp
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views

Power Query Keval

The document contains Power Query M code to extract option chain data from a URL, transform it into a table, and retrieve specific fields. It makes a request to an API endpoint to get JSON data, expands and transforms the records into columns, extracts key fields like strike price and expiry date, and concatenates some fields together.

Uploaded by

gp
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

let cookies=()=>

in
cookies

let
Source = Json.Document(Web.Contents("https://www.nseindia.com/api/option-chain-
indices?symbol=NIFTY", [Headers=[#"Accept-Encoding"="gzip, deflate", #"Accept-
Language"="en-US,en", #"User-Agent"="Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36
OPR/68.0.3618.206",
Cookie="8A87B46ABA4CAFF3B69F913708597828~xM8YKIspOw4k2OTekhqVI8Ft8AHi/RYKbvLqfKirkb
afd1XOqJELenPBKr4Y+FAgbqei34v6NKmWyp1RWvhDhh2jrLXAela7ZdmyrHShEPCaVopVDul8R91B2SbFs
hwrUsS7yKn5+cmpmaF25zGeiAjHTfTMnii7F2E1slCnkEo="]])),
records = Source[records],
data = records[data],
#"Converted to Table" = Table.FromList(data, Splitter.SplitByNothing(), null,
null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table",
"Column1", {"strikePrice", "expiryDate", "CE", "PE"}, {"strikePrice", "expiryDate",
"CE", "PE"})
in
#"Expanded Column1"

#"Removed Columns" = Table.RemoveColumns(#"Expanded Column1.PE",


{"Column1.strikePrice", "Column1.expiryDate"}),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",
{{"CE.strikePrice", type text}, {"PE.strikePrice", type text}, {"CE.expiryDate",
type text}, {"PE.expiryDate", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "CEStrikeExpiry", each
[CE.strikePrice]&[CE.expiryDate]),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "PEStrikeExpiry", each
[PE.strikePrice]&[PE.expiryDate])
in
#"Added Custom1"
let
cookiestr = Get_Cookie(),
symbol_name = Get_Symbol(),
symbol_type= Get_Symbol_Type(),
base_url = if symbol_type= "Index"
then let InnerOutput1 = "https://www.nseindia.com/api/option-chain-
indices?symbol="
in InnerOutput1
else if symbol_type="Stock"
then let InnerOutput1 = "https://www.nseindia.com/api/option-chain-
equities?symbol="
in InnerOutput1
else "Nothing",
Source = Json.Document(Web.Contents(base_url & symbol_name,
[Headers=[#"Accept-Encoding"="gzip, deflate", #"Accept-Language"="en-US,en",
#"User-Agent"="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/80.0.3987.163 Safari/537.36", Cookie="cookiestr"]])),
records = Source[records],
data = records[data],
#"Converted to Table" = Table.FromList(data, Splitter.SplitByNothing(), null,
null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table",
"Column1", {"strikePrice", "expiryDate", "CE", "PE"}, {"Column1.strikePrice",
"Column1.expiryDate", "Column1.CE", "Column1.PE"}),
#"Expanded Column1.CE" = Table.ExpandRecordColumn(#"Expanded Column1",
"Column1.CE", {"strikePrice", "expiryDate", "openInterest", "changeinOpenInterest",
"pchangeinOpenInterest", "totalTradedVolume", "impliedVolatility", "lastPrice",
"change", "pChange", "underlyingValue"}, {"CE.strikePrice", "CE.expiryDate",
"CE.openInterest", "CE.changeinOpenInterest", "CE.pchangeinOpenInterest",
"CE.totalTradedVolume", "CE.impliedVolatility", "CE.lastPrice", "CE.change",
"CE.pChange", "CE.underlyingValue"}),
#"Expanded Column1.PE" = Table.ExpandRecordColumn(#"Expanded Column1.CE",
"Column1.PE", {"strikePrice", "expiryDate", "openInterest", "changeinOpenInterest",
"pchangeinOpenInterest", "totalTradedVolume", "impliedVolatility", "lastPrice",
"change", "pChange", "underlyingValue"}, {"PE.strikePrice", "PE.expiryDate",
"PE.openInterest", "PE.changeinOpenInterest", "PE.pchangeinOpenInterest",
"PE.totalTradedVolume", "PE.impliedVolatility", "PE.lastPrice", "PE.change",
"PE.pChange", "PE.underlyingValue"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Column1.PE",
{"Column1.strikePrice", "Column1.expiryDate"}),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",
{{"CE.strikePrice", type text}, {"PE.strikePrice", type text}, {"CE.expiryDate",
type text}, {"PE.expiryDate", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "CEStrikeExpiry", each
[CE.strikePrice]&[CE.expiryDate]),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "PEStrikeExpiry", each
[PE.strikePrice]&[PE.expiryDate])
in
#"Added Custom1"

=INDEX(RawData!A:X,MATCH('MAIN OI'!M9&'MAIN OI'!$R$3,RawData!W:W,FALSE),9)

You might also like