aeron7 / nsepython

The Unofficial Python Wrapper for NSEIndia API
https://unofficed.com/nse-python/
GNU General Public License v3.0
214 stars 109 forks source link

index_history not working #46

Open marc-odp opened 1 month ago

marc-odp commented 1 month ago

index_history doesn't seem to work, even using the recent updated version of rahu.py of @buzzvolt https://github.com/buzzvolt/nsepython/blob/master/nsepython/rahu.py

Traceback (most recent call last):
  File "/mnt/c/python/misc/portfolio.py", line 138, in <module>
    test = plr.index_history('NIFTY 50','2000-01-01','2023-01-01')
  File "/mnt/c/python/misc/rahu.py", line 748, in index_history
    payload = json.loads(payload["d"])
KeyError: 'd'
buzzvolt commented 1 month ago

@marc-odp see https://github.com/aeron7/nsepython/pull/40

marc-odp commented 3 weeks ago

I tried using the fork of Mayank @mraj602 (see also in Pull request) but I don't manage to make it work. Do you have an example using index_history() ? I tried like this but I am getting an error.

import rahu
test = rahu.index_history('NIFTY 50','2003-02-01','2023-01-01')
print(test)
mraj602 commented 3 weeks ago

I tried using the fork of Mayank @mraj602 (see also in Pull request) but I don't manage to make it work. Do you have an example using index_history() ? I tried like this but I am getting an error.

import rahu
test = rahu.index_history('NIFTY 50','2003-02-01','2023-01-01')
print(test)

Hi @marc-odp Please make sure you use the date in %d-%b-%Y format. For example, it can be 01-Jan-2023. For safety, I usually prefer using it this way:

import nsepython as nse
from datetime import date
start_date = date(2023,1,1)
end_date = date(2024,1,1)
result = nse.index_history("NIFTY 50", start_date.strftime("%d-%b-%Y"), end_date.strftime("%d-%b-%Y"))
print(result)

If you still face errors, please post the error as well.

Note: It is normal for the API to throw errors during the nighttime (+5:30 GMT) as the servers are under maintenance. At such times, the code would run around 2 minutes only to give you JSONDecodeError.

marc-odp commented 3 weeks ago

Thanks Mayank @mraj602 It works now. I wonder how you found out about the new var 'indexName'... Is this API documented somewhere ? With Postman I was getting cryptic responses from the server. -marc-

mraj602 commented 2 weeks ago

Hi @marc-odp To figure out what is going on with the API, I went to www.niftyindices.com and searched for their historical data section. While searching for any index history, I did a network inspection of the post request that the website was sending to its backend server and made changes accordingly. As per my research, I don't think there is any proper API or documentation for this. We are just replicating the requests performed on their website in Python.

buzzvolt commented 2 weeks ago

Currently, nseindia.com and niftyindices.com are like trying to hit a moving target in a carnival game. Without an official SDK or API, any code aiming to interact with these websites must stay agile and ready for change. If you're relying on this project, you're either at the mercy of the author to keep things up-to-date or you'll need to fork the project and keep the wheels turning yourself.

However, it might be wiser to give the author a break and appreciate the significant work they’ve done to consolidate existing major Python repositories (nsepy et all) that track NSE data into a unified, functional code base. Instead of reinventing the wheel, why not contribute to this repository with your own change requests? With a bit of good will and community effort, we might just get the author to keep integrating these changes. Who knows, maybe they'll even transform it into a classy, object-oriented code base! But hey, no pressure!

marc-odp commented 2 weeks ago

@buzzvolt In fact, I am only interested in NSE index historical data. I can really imagine the headache it is to maintain the whole code base. I wonder why niftyindices.com keep changing their system though.

buzzvolt commented 1 week ago

well, the reasons could be multifold -- team org changes at NSE-IT etc or they have to also constantly deal with international cyberbugs/worms/dogs etc to ward off their threats from time to time etc. feel like someday just visiting their NSE office at Mumbai and take a training/communication from them :-P

On the other hand, I am really grateful to NSE actually because they have adopted good open standards and also are kind of generous with their REST api calls without any auth requirements, unlike the other exchanges across the world who are only driven by their commercial interests and do not really provide any useful data even after the payments.

buzzvolt commented 1 week ago

If you look at BSE, they still do not have any such good interfaces available with them as what NSE provides already. However, BSE has borrowed the management leadership of NSE recently and they are also trying to cover some grounds on the tech and data exchange improvements, so who knows, they might start giving competition and perhaps in good time, world will adjust the balances..