fhqvst / avanza

A JavaScript client for the unofficial Avanza API
MIT License
229 stars 59 forks source link

Subscribe to channel `Avanza.POSITIONS` not working? #7

Closed pudgereyem closed 6 years ago

pudgereyem commented 6 years ago

First of all, awesome project man 👏

Issue

When trying to subscribe for my own positions, using subscribe I don't get any responses.
Does this work for you? If so, could you provide some example code?

This is what my code looks like

// where accountId is something like "5298387"
avanza.subscribe(Avanza.POSITIONS, accountId, (quote) => {
  console.log('Received quote:', quote)
})

Other

Also, when I read the documentation for channel Avanza.POSITIONS:

Data about your own positions. Expects an accountId or a combination of orderbookId_accountId.

I wonder, when are you using accountId and when are you using orderbookId_accountId?

fhqvst commented 6 years ago

Apparently the POSITIONS-channel require you to provide an orderbookId, as well as a sequence of all your accountIds. I've updated the docs regarding that.

Assuming I have three accounts: 123, 456 and 789, here's what subscribing to my positions for orderbookId 5479 might look like.

avanza.subscribe(Avanza.POSITIONS, '5479_123,456,789', (position) => {
  console.log('Updated position:', position)
})

If you want to subscribe to changes for all instruments, use the DEALS-channel instead.

pudgereyem commented 6 years ago

Hey @fhqvst,

I'll explain first what I'm trying to do, so it's easier to understand what I'm trying to achieve.
Right now I'm just trying to build an "Real time overview of all the stocks I own". It's wrapped inside an Electron app, and lives in the menubar.

To achieve this, I'm first using getPositions to get all my positions, and then using subscribe to get updates for the positions. But I'm not sure what channel I should use to get these updates. Do you have any idea?

Questions

  1. What is an orderbookId, is it the ID for a certain instrument/stock?
  2. When subscribing for POSITIONS-channel, why would we pass a orderBookId and multiple accountIds? I was thinking that the POSITIONS-channel would give me updates about ALL the stocks in the accounts I provide
  3. I tried the DEALS-channel but I don't get any updates. Could you provide an example?
  4. How are you inspecting the API, as in what tools are you using?

I think it would be cool if we could provide better documentation for the different channels. What they are supposed to return, and how they differ. Would love to see more information in the docs.

fhqvst commented 6 years ago

Hi! Sorry for the late response, very busy at the moment.

1. orderbookId is almost always the ID representing an instrument/stock, but Avanza seems to model orderbooks and instruments as separate entities, and so there is no guarantee that there exists one orderbook for every instrument and vice versa.

  1. I agree, it might seem misleading. This module is really only a wrapper around some endpoints I found, which the iPhone app (as well as the Avanza website) uses to retrieve data. Because of this, most endpoints are specifically made for a certain screen. In the POSITIONS-case, it is used on the website while you're placing orders (look at the bottom of the screen).

In the same manner, converting this unofficial API into a nice RESTful getOrder(), addOrder(), ... type of thing becomes very inefficient. Calls to an endpoint might leave you without the data you expected because that data is not used in the screen which the endpoint was designed for.

Hopefully PSD2 makes this module obsolete in 2018! :)

The DEALS-channel should work exactly like POSITIONS minus the orderbookId. Let's again assume that 123, 456, and 789 are the accountIds for all of my accounts. Hit me up if you're still not receiving any data and I'll have a look at it.

avanza.subscribe(Avanza.DEALS, '_123,456,789', (deal) => {
  console.log('Received new deal:', deal)
})
  1. It's actually really uncomplicated. Have a look at Charles Proxy.

pudgereyem commented 6 years ago

Hi @fhqvst, no worries, thanks for answering

  1. Ok!
  2. I understand, awesome with PSD2.
  3. Yeah, I can't manage to set it up right *

* I have 5 accounts, all 7 digits long. But I don't get any response. I was thinking that this may be expected when the market is closed? Do you get any data when subscribing this time a day? I'm not sure how you could investigate this further without me taking up to much of your time.. Really appreciate your help.

  1. I installed Charles Proxy, good stuff!
fhqvst commented 6 years ago

The DEALS-channel will give you updates when a transaction is performed (which can only occur when the market is open).

easa commented 2 years ago

@fhqvst Hi there,
This subscribe function doesn't work on the positions channel, I tried parameters such as:

  1. <stockid>_<accountid1>,<accountid2>
  2. _<accountid1>,<accountid2>
  3. <stockid>_<accountid1>
  4. <accountid1>
  5. <stockid> And yet doesn't get anything. so I'm interested, how can I access the protocols and some deeper documentation of the Avanza API? Also, do I do anything wrong here? or it is a real issue? thank you in advance.