Certainly! Here's an example of Python code that utilizes the NSEpy library to retrieve and analyze Nifty 50 data including Open Interest (OI), Foreign Institutional Investor (FII) data, and Domestic Institutional Investor (DII) data:
import nsepy
import pandas as pd
def get_oi_data(stock_index, start_date, end_date):
oi_data = nsepy.get_history(index=stock_index, start=start_date, end=end_date, index_series=True, futures=True, option=True)
return oi_data
def get_fii_dii_data(stock_index, start_date, end_date):
fii_data = nsepy.fii.FII().get(start_date=start_date, end_date=end_date)
dii_data = nsepy.dii.DII().get(start_date=start_date, end_date=end_date)
return fii_data, dii_data
def analyze_nifty50_data(stock_index, start_date, end_date):
# Get Open Interest (OI) data
oi_data = get_oi_data(stock_index, start_date, end_date)
# Get FII and DII data
fii_data, dii_data = get_fii_dii_data(stock_index, start_date, end_date)
# Analyze OI data
max_oi_call = oi_data['OI Calls'].max()
max_oi_put = oi_data['OI Puts'].max()
max_oi_call_date = oi_data[oi_data['OI Calls'] == max_oi_call].index[0].date()
max_oi_put_date = oi_data[oi_data['OI Puts'] == max_oi_put].index[0].date()
# Analyze FII and DII data
fii_inflow = fii_data['FII D1 Inflow'].sum()
dii_inflow = dii_data['DII D1 Inflow'].sum()
# Print the analysis results
print("Nifty 50 Analysis:")
print("Max OI in Calls: {} on {}".format(max_oi_call, max_oi_call_date))
print("Max OI in Puts: {} on {}".format(max_oi_put, max_oi_put_date))
print("FII Inflow: {}".format(fii_inflow))
print("DII Inflow: {}".format(dii_inflow))
def main():
# Set the start and end dates
start_date = '2021-01-01'
end_date = '2021-12-31'
# Analyze Nifty 50 data
analyze_nifty50_data('NIFTY 50', start_date, end_date)
if __name__ == "__main__":
main()
In this code, the get_oi_data function uses the NSEpy library to fetch Open Interest (OI) data for the specified stock index (in this case, 'NIFTY 50') within the given start and end dates. The get_fii_dii_data function retrieves Foreign Institutional Investor (FII) and Domestic Institutional Investor (DII) data for the selected stock index. The analyze_nifty50_data function analyzes the OI, FII, and DII data and prints the results.
In the main function, you can set the start and end dates according to the period you want to analyze. The analyze_nifty50_data function is then called with the stock index, start date, and end date as parameters.
Ensure you have the nsepy library installed (pip install nsepy) before running this code. Feel free to modify or add additional analysis as per your requirement.