Qluxzz / avanza

A Python library for the unofficial Avanza API
https://qluxzz.github.io/avanza/
MIT License
85 stars 40 forks source link

get_order_book() no longer works #49

Closed ComradeDiana closed 2 months ago

ComradeDiana commented 2 years ago

The function now returns this error:

requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://www.avanza.se/_mobile/order/stock?orderbookId=123456

ageaenes commented 2 years ago

+1

jlhein commented 2 years ago

+1

tux2000 commented 2 years ago

Do you know if there is any new API endpoint available with that data?

tux2000 commented 2 years ago

Apparently these data are now streamed to the browser through a websocket connection on:

wss://www.avanza.se/_push/cometd

Maybe this is a request for the data:

[{"id":"6","channel":"/meta/subscribe","subscription":"/orderdepths/1019879","clientId":"XXX"}]

And this is a response:

[{"data":{"orderbookId":"1019879","receivedTime":"2022-03-03T08:43:19.266+0100","totalLevel":{"buySide":{"price":594499.00,"volume":361235.00,"volumePercent":49},"sellSide":{"price":1217540.00,"volume":361464.00,"volumePercent":50}},"levels":[{"buySide":{"price":1.67,"volume":350000,"volumePercent":100},"sellSide":{"price":1.69,"volume":350000,"volumePercent":100}},{"buySide":{"price":0.890,"volume":11235,"volumePercent":3},"sellSide":{"price":54.00,"volume":9136,"volumePercent":2}},{"buySide":{"price":null,"volume":null,"volumePercent":null},"sellSide":{"price":57.00,"volume":2328,"volumePercent":0}}],"marketMakerLevelAsk":0,"marketMakerLevelBid":0},"channel":"/orderdepths/1019879"}]
jlhein commented 2 years ago

@tux2000 Are you willing to share the code you used to make that socket call? I have trouble figuring out how to circumvent the current get_order_book() issue.

Any help is much appreciated.

Tobti commented 2 years ago

get_order() now returns a simular response.

jlhein commented 2 years ago

@Tobti get_order() does not seem to work on certificates or warrants. And if I'm not mistaken it returns the info on a pending order, not the actual order book (with order depths as well). Correct me if I'm wrong

pascal-git commented 2 years ago

@tux2000 Are you willing to share the code you used to make that socket call? I have trouble figuring out how to circumvent the current get_order_book() issue.

Any help is much appreciated.

@jlhein, I did not implement an API call with this endpoint, i was just trying to understand how the browser was getting the updates in the orderbook to see if there was any easy fix...

Quite sure that the subscribe_to_id function could be used, similar to the example: https://github.com/Qluxzz/avanza#example but with the ChannelType.ORDERDEPTH

And then there obviously is also get_order_books, which is still working, but its not providing info on market maker positions to what i could see...

Not sure if there might be another API endpoint used by the app, but i have no chance of checking...

Tobti commented 2 years ago

I use get_order() to get the ticksize of a stock and that part do not work any more.

jlhein commented 2 years ago

@pascal-git Thanks for your advice. I'd like to try it but I never got "subscribe_to_id" to function properly. I only received a connection timeout error, and I'm not sure what might be the issue (tried the example code). Would you be willing to share an example snippet that is working for you?

tux2000 commented 2 years ago

I don't use subscriptions in my scripts but this one worked at least to subscribe to the websocket... I dont expect to see actual data there now...

from avanza import Avanza, TimePeriod, InstrumentType, OrderType, TransactionType, ChannelType
import json
import base64
import getpass
import pickle
import asyncio
import logging
import sys

from datetime import date, datetime
from dateutil.relativedelta import relativedelta

# Load credentials from secure storage

avanza = Avanza({
    'username': user,
    'password': pwd,
    'totpSecret': totp
})

def callback(data):
    # Do something with the quotes data here
    print(data)

async def subscribe_to_channel(avanza: Avanza):
    await avanza.subscribe_to_id(
        ChannelType.ORDERDEPTHS,
        "1019879", 
        callback
    )

def main():
    loglevel = logging.DEBUG
    logging.basicConfig(stream=sys.stdout, level=loglevel, format='%(asctime)s %(levelname)s:%(name)s:%(funcName)s:%(message)s')

    asyncio.get_event_loop().run_until_complete(
        subscribe_to_channel(avanza)
    )
    asyncio.get_event_loop().run_forever()

if __name__ == "__main__":
    main()
ComradeDiana commented 2 years ago

Is there still no fix?

RagnarHagberg commented 2 years ago

I use get_order() to get the ticksize of a stock and that part do not work any more.

How did you do that? If the order is incorrect, due to the tick-size being off, I don't get an order id, so I can't get the tick size from get_order(). I want to know even if get_order() doesn't work anymore.

Tobti commented 2 years ago

I use get_order() to get the ticksize of a stock and that part do not work any more.

How did you do that? If the order is incorrect, due to the tick-size being off, I don't get an order id, so I can't get the tick size from get_order(). I want to know even if get_order() doesn't work anymore.

Hi!

I did a work around that created a sell/buy order with the volume of 1 and a "even" price that I guessed is okey according to the tick size. After that I used get_order() to find the acctual tick size, and corrected the first order with correct volume and price. Not a nice way but it worked :)

jlhein commented 2 years ago

For me the issue is that I'm now unable to get the order depth levels. I've not found a fix for this yet, so if anyone has an idea I'm very interested.

00prometheus commented 1 year ago

Order depth levels are in the subscriptions now. Avanza no longer updates the order depths as they change in the REST API, so if you want order depths in real time, you need to use the subscription.

Qluxzz commented 2 months ago

Using get_order_books still seems to work https://qluxzz.github.io/avanza/avanza/avanza.html#Avanza.get_order_books

So it's that, or using websockets as described above.