Skip to main content

A Python library for the unofficial Avanza API

Project description

Avanza

A Python library for the unofficial Avanza API. This library is based on the existing JavaScript library Avanza.

Please note that this is only a proof of concept, hence not meant to be used by anyone.

It might also be valuable to note that I am not affiliated with Avanza Bank AB in any way. The underlying API can be taken down or changed without warning at any point in time.

Getting a TOTP Secret

NOTE: Since May 2018 two-factor authentication is required to log in.

Here are the steps to get your TOTP Secret:

  1. Go to Mina Sidor > Profil > Sajtinställningar > Tvåfaktorsinloggning and click "Återaktivera". (Only do this step if you have already set up two-factor auth.)
  2. Click "Aktivera" on the next screen.
  3. Select "Annan app för tvåfaktorsinloggning".
  4. Click "Kan du inte scanna QR-koden?" to reveal your TOTP Secret.
  5. Done! From now on all you have to do is supply your secret in the constructor as in the examples below.

Example

Authenticate and fetch account overview:

from avanza import Avanza
avanza = Avanza({
    'username': 'MY_USERNAME',
    'password': 'MY_PASSWORD',
    'totpSecret': 'MY_TOTP_SECRET'
})

overview = avanza.get_overview()

Get info about a certain account

from avanza import Avanza, TimePeriod

avanza = Avanza({
    'username': 'MY_USERNAME',
    'password': 'MY_PASSWORD',
    'totpSecret': 'MY_TOTP_SECRET'
})

accountId = 'XXXXXXX'

report = avanza.get_insights_report(
    accountId,
    TimePeriod.ONE_WEEK
)

Place an order

from avanza import Avanza, OrderType

avanza = Avanza({
    'username': 'MY_USERNAME',
    'password': 'MY_PASSWORD',
    'totpSecret': 'MY_TOTP_SECRET'
})

account_id = 'XXXXXXX'
order_book_id = 'XXXXXX'
buy_price = 13.37
valid_until = date.fromisoformat('2011-11-11')
volume = 42

result = avanza.place_order(
    account_id,
    order_book_id,
    OrderType.BUY,
    buy_price,
    valid_until,
    volume
)

Subscribe to real time data

import asyncio
from avanza import Avanza, ChannelType

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.QUOTES,
        "19002", # OMX Stockholm 30
        callback
    )

def main():
    avanza = Avanza({
        'username': 'MY_USERNAME',
        'password': 'MY_PASSWORD',
        'totpSecret': 'MY_TOTP_SECRET'
    })

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

if __name__ == "__main__":
    main()

LICENSE

MIT license. See the LICENSE file for details.

RESPONSIBILITIES

The author of this software is not responsible for any indirect damages (foreseeable or unforeseeable), such as, if necessary, loss or alteration of or fraudulent access to data, accidental transmission of viruses or of any other harmful element, loss of profits or opportunities, the cost of replacement goods and services or the attitude and behavior of a third party.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

avanza-api-2.2.0.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

avanza_api-2.2.0-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file avanza-api-2.2.0.tar.gz.

File metadata

  • Download URL: avanza-api-2.2.0.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.4

File hashes

Hashes for avanza-api-2.2.0.tar.gz
Algorithm Hash digest
SHA256 79b572291621a1b09eb9d9b04ca97805b0c1717a11f1e7e7ad9352d6442a5387
MD5 481a03f889dc71282fc222d3d953e388
BLAKE2b-256 6cb14096bfb4ddd8b756001914e4f2aedd6149555f3b2f65f394091fd9b5346e

See more details on using hashes here.

File details

Details for the file avanza_api-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: avanza_api-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.4

File hashes

Hashes for avanza_api-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 793197f186c0f65fda7951217720c4e7a07123e31fb231c453456aa7636c26e6
MD5 535463cdf756a81249783bfd8ce944f2
BLAKE2b-256 b9d55972bdbdc8938eed38f03092ef6a612358ac59c25bb38856919f55e8c3df

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page