Saturday, April 15, 2023

Get Trading holiday list using Python

 

Code to  get the list of NSE trading holidays, to use in your algo trading.




import json
from datetime import datetime

import requests

if __name__ == '__main__':
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/109.0',
        'Accept': '*/*',
        'Accept-Language': 'en-US,en;q=0.5',
        # 'Accept-Encoding': 'gzip, deflate, br',
        'Connection': 'keep-alive',
        'Referer': 'https://www.nseindia.com/resources/exchange-communication-holidays',

        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
    }

    params = {
        'type': 'trading',
    }

    response = requests.get('https://www.nseindia.com/api/holiday-master', params=params, headers=headers)

    jsonResponse = response.json()

    for record in jsonResponse['FO']:
        # print(record["tradingDate"])
        print(datetime.strptime(record["tradingDate"], '%d-%b-%Y').strftime('%d/%m/%Y'))

    with open('data.json', 'w') as f:
        json.dump(jsonResponse['FO'], f, ensure_ascii=False, indent=4)

Happy Coding.

No comments:

Post a Comment