Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAD_SUBSCRIPTION_TYPE error for pytr portfolio #37

Closed
tmpccnt opened this issue Apr 4, 2023 · 17 comments · Fixed by #39
Closed

BAD_SUBSCRIPTION_TYPE error for pytr portfolio #37

tmpccnt opened this issue Apr 4, 2023 · 17 comments · Fixed by #39
Labels
bug Something isn't working

Comments

@tmpccnt
Copy link

tmpccnt commented Apr 4, 2023

Description of the bug
I tried to read out my portfolio with pytr portfolio, but got a BAD_SUBSCRIPTION_TYPE error response

To Reproduce

  1. Successfully logged in
  2. Ran the command pytr portfolio
  3. See error

Expected behavior
See my current portfolio

Error log

21:49:12 Found credentials file
21:49:12 Phone: +49176********, PIN: ****
21:49:12 Web session resumed
21:49:12 Logged in
21:49:12 Connecting to websocket ...
21:49:12 Connected to websocket ...
21:49:13 Received error message: '1 E {"errors":[{"errorCode":"BAD_SUBSCRIPTION_TYPE","errorField":null,"errorMessage":"Unknown topic type","meta":{"source":"MAPPER"}}]}'
Traceback (most recent call last):
  File "/home/martin/.local/bin/pytr", line 8, in <module>
    sys.exit(main())
  File "/home/martin/.local/lib/python3.10/site-packages/pytr/main.py", line 215, in main
    Portfolio(login(phone_no=args.phone_no, pin=args.pin, web=not args.applogin)).get()
  File "/home/martin/.local/lib/python3.10/site-packages/pytr/portfolio.py", line 73, in get
    asyncio.get_event_loop().run_until_complete(self.portfolio_loop())
  File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/home/martin/.local/lib/python3.10/site-packages/pytr/portfolio.py", line 16, in portfolio_loop
    _subscription_id, subscription, response = await self.tr.recv()
  File "/home/martin/.local/lib/python3.10/site-packages/pytr/api.py", line 352, in recv
    raise TradeRepublicError(subscription_id, subscription, payload)
pytr.api.TradeRepublicError: ('1', {'type': 'portfolio'}, {'errors': [{'errorCode': 'BAD_SUBSCRIPTION_TYPE', 'errorField': None, 'errorMessage': 'Unknown topic type', 'meta': {'source': 'MAPPER'}}]})

Environment

  • OS: Linux
  • pytr version: 0.1.5

Additional context
It was still working a couple of week ago. Other commands still work. Did they change the API?

@tmpccnt tmpccnt added the bug Something isn't working label Apr 4, 2023
@miry
Copy link
Contributor

miry commented Apr 10, 2023

I have the same. I think it is related to recent changes in Traderepublic like 2% of cash.

@ChemicalEnginer
Copy link

Same here. Worked last week but shows me the same error message

@tmpccnt
Copy link
Author

tmpccnt commented Apr 12, 2023

Short update:
I went into the rabbit hole to learn how to monitor websocket traffic - quite fun actually - and learned there is a new subscription command compactPortfolio, which returns a reduced json with only three entries:
{"positions":[{"instrumentId":"","netSize":"","averageBuyIn":""}]}
But that won't help with the old implementation, as a live update on the current net value is not provided.

@Jensener1
Copy link

Short update: I went into the rabbit hole to learn how to monitor websocket traffic - quite fun actually - and learned there is a new subscription command compactPortfolio, which returns a reduced json with only three entries: {"positions":[{"instrumentId":"","netSize":"","averageBuyIn":""}]} But that won't help with the old implementation, as a live update on the current net value is not provided.

Do you have a list of all available subscription commands?

@miry
Copy link
Contributor

miry commented May 10, 2023

From Web TradeRepublic it seems to get detailed information per stock required to use type: instrument.

Example: sub 23 {"type":"instrument","id":"DE0001111111","jurisdiction":"DE"}

image

@miry
Copy link
Contributor

miry commented May 10, 2023

I pushed my changes to https://github.com/miry/pytr/tree/miry_master

$ git clone miry -b miry_master
$ pip3 install .
$ pytr -v debug portfolio

@ChemicalEnginer
Copy link

@miry With your changes the connection to websocket is possible again, thanks for that. There is a "bug", that the netValue of each position is 0, but you wrote in your code that is a ToDo. I'm curious for the next update.

@miry
Copy link
Contributor

miry commented May 10, 2023

@ChemicalEnginer I think Traderepublic removed old portfolio method becuase it is too big. They replaced it with small compactPortfolio. It requires to send additional request for each ISIN.

I will look next week for missing data.

@miry
Copy link
Contributor

miry commented May 11, 2023

@ChemicalEnginer Improved my changes and also add ability to show Title of the ISIN.

@miry
Copy link
Contributor

miry commented May 11, 2023

Sent PR: #39

@GordonInDaHouse
Copy link

Are there any news about it?

I am not using pytr, but another python tool, which has the same problem now. And I actually just need one value... the total depot amount.
I also had look on it today... and really got mad about their mess now.

My current thoughts about it.
'compactPortfolio' have 3 values:
instrumentId = Needs a separate call to get the details about the instrument.
netSize = How many do I owe.
averageBuyIn = How much did I spend on it. (In case someone wants to know the difference between current value and buy in price.)

So, the task is now to get the instrument data including the current price. Then we can multiply the netSize (which we have) with the current price and we have our personal amount for this instrument. And we have to repeat that with all instruments we have... add them up and we have the total value.

@GordonInDaHouse
Copy link

GordonInDaHouse commented May 22, 2023

PS: I achieved my goal.

I'll share with you my code, maybe it is helpfull, even it is not straight for pytr (I work with https://github.com/Zarathustra2/TradeRepublicApi/).

tr = TrBlockingApi(NUMBER, PIN, locale="de")
tr.login()
TRcash = tr.available_cash()
cash = TRcash[0]["amount"]
TRportfolio = tr.compactPortfolio()
TotalValue = cash
for istrument in TRportfolio["positions"]:
    try:
        pp.pprint(istrument["instrumentId"])
        TRticker = tr.ticker(istrument["instrumentId"])

        print(TRticker["bid"]["price"])
        TotalValue += (istrument["netSize"] * TRticker["bid"]["price"])
        print("Value: " + str( (istrument["netSize"] * TRticker["bid"]["price"])))
        time.sleep(0.5)
    except Exception as err:
        print(f"Unexpected {err=}, {type(err)=}")

print("Total Value: " + str(TotalValue))

I did it like I said.
First I get the available Cash. There is a straight call for it.
Then I get the compact Portfolio and take the instrumentID (one by one) and call the the ticker api point with the isin. From there I receive the current ask and bit price. So I can multiply the netSize with the bid price. Sum everything up (with the cash value) and I have my total value.

Hope it helps a bit.

@GordonInDaHouse
Copy link

Hmm... I was a very happy man today in the morning. My depot value was 10 times higher than yesterday... :-)
Until I checked it in Trade Republic itself... nothing changed there :-( Not rich over night.

What happen?
The ticker request seems to be a websocket. So the requested data is not the data which will be received the next time a payload is coming in. Yesterday, by chance, it was like that. Today I request the ticker for the isin "DE1234" but the next payload from Trade Republic is the ticker for "US1234". Which is also in my portfolio, but still...

So I guess it is needed to use the websocket to collect first all ticker for all positions and then do the math (as far as I understood, this is the way the TR webapp is working). Or there is a way to really collect a single ticker, without websocket.

@miry
Copy link
Contributor

miry commented May 23, 2023

@GordonInDaHouse I have the same understanding. Previous portfolio operation did summarise for us. With a new implementation you need to collect all instruments and create a Total summaries.

In my PR https://github.com/marzzzello/pytr/pull/39/files#r1191328848 I have not work on it, first wanted to check that netValue is valid per single ISIN.

@grafsen
Copy link

grafsen commented Jun 14, 2023

Hello,

Is there a new change in their api or did i make a mistake in the installation of pip?

2023-06-14 18:21:34+0200 main DEBUG logging is set to debug
2023-06-14 18:21:34+0200 account INFO Found credentials file
2023-06-14 18:21:34+0200 account INFO Phone: +49152********, PIN: ****
2023-06-14 18:21:34+0200 account INFO Web session resumed
2023-06-14 18:21:34+0200 account INFO Logged in
2023-06-14 18:21:34+0200 api INFO Connecting to websocket ...
2023-06-14 18:21:35+0200 api INFO Connected to websocket ...
2023-06-14 18:21:35+0200 api DEBUG Subscribing: 'sub 1 {"type": "portfolio"}'
2023-06-14 18:21:35+0200 api DEBUG Subscribing: 'sub 2 {"type": "cash"}'
2023-06-14 18:21:35+0200 api DEBUG Received message: '1 E {"errors":[{"errorCode":"BAD_SUBSCRIPTION_TYPE","errorField":null,"errorMessage":
"Unknown topic type","meta":{"source":"MAPPER"}}]}'

@miry
Copy link
Contributor

miry commented Jun 18, 2023

There is a change in TraderReublic API.
Fast fix is to install fork version: #37 (comment)

@grafsen
Copy link

grafsen commented Jun 18, 2023

Yes, i know! It was my fault. I have make a mistake in the pip installation. Now the fix is working. Thanks for that :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants