Chavithra / degiro-connector

This is yet another library to access Degiro's API.
BSD 3-Clause "New" or "Revised" License
220 stars 48 forks source link

Error 400 after some hours #33

Closed VVIERV00 closed 2 years ago

VVIERV00 commented 2 years ago

Hi! first, thank you for the work you have put into this API. With the session still running (I can fetch the portfolio) given enough time, all actions return this error 400 Client Error: Bad Request for url: https://trader.degiro.nl/trading/secure/v5/checkOrder;jsessionid=D1AA41FB58C704CXXXXXXXXX.prod_a_XXX_X?intAccount=XXXXXXXX&sessionId=D1AA41FB58C704CXXXXXX.prod_a_XXX_X

The URL seems to be the same as when it performs the actions at the beginning of execution. Could it be an error with the sessionId?

Chavithra commented 2 years ago

Hi,

I think you are referring to the session timeout, there is a limit of time a session is valid when unused.

Here are the sections related to session timeout in the documentation :

Is that what you are looking for ?

VVIERV00 commented 2 years ago

I am referring to that the problem only happens with Orders, not Update's. I don't know why, I will try to debug. The final exception is NoneType' object has no attribute 'confirmation_id'.

It then shouldn't have relation with timeouts

Chavithra commented 2 years ago

Issue Can you describe how to reproduce this issue ?

Code Is it possible to show the code which fails ?

VVIERV00 commented 2 years ago

Sorry for the late reply. I can confirm it is still failing:

order = Order(
                action=actionType,
                order_type=orderType,
                price=lastPrice,
                product_id=productId,
                size=shares,
                time_type=Order.TimeType.GOOD_TILL_DAY,)
        checking_response = self.trading_api.check_order(order=order)
        time.sleep(0.7)
        try:
            confirmation_id = checking_response.confirmation_id
            free_space_new = checking_response.free_space_new
            response_datetime = checking_response.response_datetime
            transaction_fees = checking_response.transaction_fees
            transaction_opposite_fees = checking_response.transaction_opposite_fees
            transaction_taxes = checking_response.transaction_taxes
            logging.debug(f"DEGIRO_{actionType}_CHECK_RESPONSE confirmation_id {confirmation_id} free_space_new {free_space_new} response_datetime {response_datetime} transaction_fees {transaction_fees} transaction_opposite_fees {transaction_opposite_fees} transaction_taxes {transaction_taxes}")
            confirmation_response = self.trading_api.confirm_order(
                        confirmation_id=confirmation_id, order=order
            )
            logging.debug(f"DEGIRO_{actionType}_RESPONSE confirmation_response {confirmation_response} checking_response {checking_response}")
        except Exception as e:

The exception e contains: 'NoneType' has no attribute 'confirmationId And right before an exception is caught in action_check_order:134

try:
            response_raw = session.send(prepped, verify=False)
            response_raw.raise_for_status()
            response_dict = response_raw.json()
        except Exception as e:

Exception e: 400 Client Error: Bad Request for url: https://trader.degiro.nl/trading/secure/v5/checkOrder;jsessionid=D1AA41FB58C704CXXXXXXXXX.prod_a_XXX_X?intAccount=XXXXXXXX&sessionId=D1AA41FB58C704CXXXXXX.prod_a_XXX_X

Other API calls still seem to work properly (such as fetching portfolio). And the problem seems to happen after some hours of the API running, but still can't tell.

Chavithra commented 2 years ago

Questions

1/ So the call to check_order method failed ?

2/ Is it failing at every single call of check_order ? randomly ? or after a certain duration ?

Looks like Degiro's website is being updated these days, that might be related.

VVIERV00 commented 2 years ago

I found the error! DeGiro doesn't keep an updated margin, also happens on the web. Sometimes you will make an operation, but the margin will take a while to update. Did you know about the issue and any workaround?

Chavithra commented 2 years ago

How did you find out about the margin ?

VVIERV00 commented 2 years ago

Making operations and checking the website margin showed. If lucky it will refresh in a couple of seconds, but there are times that it take up to 20 minutes to refresh. Degiro's website actually provides the date of the margin calculation. Using the margin to calculate ops (let say you want to use 10% of the available margin) can lead to failures as it's incorrect data.

Chavithra commented 2 years ago

The website doesn't seem to use any API call to calculate this margin.

I am interested in knowing more on that margin.

Feel free to join me on Discord if you want to see that together.

Thanks

VVIERV00 commented 2 years ago

I will eventually have to deal with that margin, so expect me on Discord. Thank you again.