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.

Installation

pip install avanza-api

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. Generate the TOTP code using the python code below and paste the TOTP code in the field below where you found the TOTP Secret.
  6. Done! From now on all you have to do is supply your secret in the constructor as in the examples below.

Generate TOTP code:

import hashlib
import pyotp
totp = pyotp.TOTP('MY_TOTP_SECRET', digest=hashlib.sha1)
print(totp.now())

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'
})

report = avanza.get_insights_report(
    account_id='XXXXXXX',
    time_period=TimePeriod.ONE_WEEK
)

Place an order

from avanza import Avanza, OrderType

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

result = avanza.place_order(
    account_id='XXXXXXX',
    order_book_id='XXXXXX',
    order_type=OrderType.BUY,
    price=13.37,
    valid_until=date.fromisoformat('2011-11-11'),
    volume=42
)

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-3.0.1.tar.gz (20.1 kB view details)

Uploaded Source

Built Distribution

avanza_api-3.0.1-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: avanza-api-3.0.1.tar.gz
  • Upload date:
  • Size: 20.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for avanza-api-3.0.1.tar.gz
Algorithm Hash digest
SHA256 e7f2d792a9655495d3e69576860610f5d18834629fe6dbc02c6855bef7170bd5
MD5 21f388e19c93835157025897e36099db
BLAKE2b-256 98b35751c301f7bb84db887e1d305788a68acb4ef39318cb3312c264264829cb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: avanza_api-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 18.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for avanza_api-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 77d5947ce7004111c7f7793e91ba69544613647f210377e93918c77da2faebf5
MD5 a8c497de8bd9b931ad3de050e69a768b
BLAKE2b-256 de6b48f3ae2a6b020a316c803710a450371bc8b3b461bf381617cc0508f1dcf2

See more details on using hashes here.

Supported by

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