algo2t / alphatrade

Python APIs for SAS Online Alpha Trade Web platform for creating algo trading using python
https://algo2t.github.io/alphatrade
MIT License
44 stars 23 forks source link

SAS Online API endpoints changed - receiving error - unable to trade #56

Closed ronaldpaul closed 1 year ago

ronaldpaul commented 1 year ago

Since today morning I am continuously receiving the following error message when I try to place the order. The automated TOTP is generated through pyotp as we know. I am not sure why it is not working properly. Earlier this issue was coming up randomly and it would work after trying multiple times. However, now it doesn't work at all.

{"data":{},"error_code":49008,"message":"Invalid TOTP, please try again","status":"error"}

nevatia commented 1 year ago

Invalid totp error does not come while placing order. It may come at the time of login itself ,if any. Check your code.

ronaldpaul commented 1 year ago

Following is the code that generates the totp. Should I change anything? In the second line, for 'Totp' I pass the shortcode which was displayed below the barcode while configuring the Google Authenticator.

try: pin = pyotp.TOTP(Totp) totp = f"{int(pin.now()):06d}" if len(pin.now()) <=5 else pin.now() sas = AlphaTrade(login_id=login_id, password=password, twofa=totp, master_contracts_to_download=["NFO","NSE"], tokenPath = path)

It has been working fine so far. Not sure why it throws the error suddenly. I just followed whatever is mentioned in the documentation.

nevatia commented 1 year ago

Sync your machine time. Let it auto adjust through internet. Make sure to have time zone as chennai,kolkata...

ronaldpaul commented 1 year ago

Time zone and auto sync is all active only. But still it was failing. I debugged the code and found that the profile URLs are not working anymore properly. For example, the profile login URL mentioned in the AlphaTrade.py file https://alpha.sasonline.in/api/v3/profile is returning 'Not Found' error. Check the following line (link to the code).

https://github.com/algo2t/alphatrade/blob/326a0502ab8a06689ef6ba010b67de34090dba4b/alphatrade/alphatrade.py#L276

I think maybe they pulled down the old API since they appear to be having a new API with the documentation stored at https://drive.google.com/file/d/15HMf1ieewe2FgHxZJ-YIGeDtyNVEgCQ2/view

nevatia commented 1 year ago

I dont think so. May need to try two three times while login. Once logged in , no such totp issue must come. Or are you logging in the web too , simultaneously/parallely

ronaldpaul commented 1 year ago

Nope. I am not logging-in through Web or Mobile. Only through the python script. All these days it was working fine. Rarely it used to throw this error. But at-least that endpoint use to work. Not it is returning 'not found' error. So, creating the AlphaTrade object itself is not possible and because of that we are getting this error. It is not actually mainly related to invalid TOTP error, it is mainly that we session is not getting created since the profile URL is returning not found error.

nevatia commented 1 year ago

Ok. Then others must have reported too. Lets wait..

ronaldpaul commented 1 year ago

@nevatia Were you able to run the code and test it out now?

nevatia commented 1 year ago

I am outside. So cant check now. May check tomorrow night

algo2t commented 1 year ago

All endpoints are now changed so I modified endpoint paths to v1 but no testing done. Kindly test and report bugs. Pull Requests are welcomed.

Time zone and auto sync is all active only. But still it was failing. I debugged the code and found that the profile URLs are not working anymore properly. For example, the profile login URL mentioned in the AlphaTrade.py file https://alpha.sasonline.in/api/v3/profile is returning 'Not Found' error. Check the following line (link to the code).

https://github.com/algo2t/alphatrade/blob/326a0502ab8a06689ef6ba010b67de34090dba4b/alphatrade/alphatrade.py#L276

I think maybe they pulled down the old API since they appear to be having a new API with the documentation stored at https://drive.google.com/file/d/15HMf1ieewe2FgHxZJ-YIGeDtyNVEgCQ2/view

ronaldpaul commented 1 year ago

@algo2t By v1 endpoints, do you mean the official API provided by SAS Online? Because, that is where I see v1 URIs.

rahulmr commented 1 year ago

@algo2t By v1 endpoints, do you mean the official API provided by SAS Online? Because, that is where I see v1 URIs.

Have you checked the new code? But it will no longer have the websocket implementation

ronaldpaul commented 1 year ago

@algo2t By v1 endpoints, do you mean the official API provided by SAS Online? Because, that is where I see v1 URIs.

Have you checked the new code? But it will no longer have the websocket implementation

Hi @rahulmr , have you checked the library https://github.com/TradelabTechnologiesIndia/pyoauthbridge ? It does have a websocket implementation as wsclient.py

This library is what SAS Online provides as their official API client library. Generating Oauth2 is the issue, but I think we can generate the token in a similar way we do in AlphaTrade.

Do you have plans to fix this library for the updated APIs bro? Because since there has been no update for the past couple of days, I have started rewriting my logic using pyoauthbridge. Should I continue with it or I can wait for a while for AlphaTrade to be fixed? Thanks again for your tremendous effort and support.

algo2t commented 1 year ago

I am closing this issue and waiting for people to come back with new bugs, because of my limited time. I have updated the endpoints and created a new release v1.0.0 as well. python -m pip install --upgrade alphatrade (before that make sure you go through the updated readme)