Custom Component Avanza Stock

After using Home Assistant for more than a year I finally decided to try and write my first custom component. A sensor which tracks the latest price of your favourite stock. Even though it is a Swedish bank it is possible to find stocks from the following countries:

  • Sweden
  • USA
  • Denmark
  • Norway
  • Finland
  • Canada
  • Belgium
  • France
  • Italy
  • Netherlands
  • Portugal
  • Germany

Further instructions can be found on the github page.

I personally use this to send a summary of my stocks change in percent every day after the stock market closes. Would love some feedback if you try this out, find a bug or have a suggestion for improvement.

11 Likes

Updated to version 0.0.2

It is now possible to select what attributes to monitor. If you used any of the following attributes you will need to update your configuration.

  • id
  • highestPrice
  • lowestPrice
  • totalVolumeTraded
  • totalValueTraded

Check github for more information.

Now supporting custom_updater! :grin:

Version 0.0.3 released!

It is now possible to monitor the available key ratios (directYield, priceEarningsRatio, volatility).

Version 0.0.4 released!

Added some company related information (description, marketCapital, sector, totalNumberOfShares)

Another release, 0.0.5!

You can now see the next dividends (add it to monitored_conditions) ex date, payout date and amount per share.

0.0.6 released :smiley:

The api returned “expired” dividends, those are now ignored by the component. The dividend0_* attributes is the next upcoming dividend, if known.

0.0.7 released, only changes it to use async setup.

Release of version 1.0.0 is getting closer, I think I’ve listed most of the things I need/want to do in the github issues.

1 Like

Release again, 0.0.8! Fixed the issue where some stocks had a dividend date but the amount were zero, these are now ignored.

i haven’t tried you component yet, but I will some day! Just wanted to let you know that I appreciate you updating this thread as I had totally forgot that it existed. Thanks in advance for a great component!

Happy to hear that :slight_smile: I’ve mostly tried this on swedish stocks, so there could be some bugs with other countries.

Since I track 20+ stocks and I don’t like to write that much yaml I created a simple script to generate all the sensors. Makes it easy if you want to change monitored conditions for all of them.

# Define stocks
stocks = []
stocks.append(('NAME', STOCK_ID))

# Define monitored conditions
monitored_conditions = []
monitored_conditions.append('change')
monitored_conditions.append('changePercent')
monitored_conditions.append('dividends')
monitored_conditions.append('name')
monitored_conditions.append('tickerSymbol')

# Generate sensors
with open('sensor/stock.yaml', 'w') as f:
    f.write('# -----------------------------------\n')
    f.write('# sensor/stock.yaml\n')
    f.write('# -----------------------------------\n')

    for (stock_name, stock_id) in stocks:
        f.write('\n')
        f.write('# {0} Stock Price\n'.format(stock_name))
        f.write('- platform: avanza_stock\n')
        f.write('  name: Stock {0}\n'.format(stock_name))
        f.write('  stock: {0}\n'.format(stock_id))
        f.write('  monitored_conditions:\n')
        for condition in monitored_conditions:
            f.write('    - {0}\n'.format(condition))

Maybe that will help you or someone else get started quickly.

0.0.9 released! Fixed an issue where a dividend would be filitered out onen day to early.

Any chance you could get this into the main branch? Been looking for a stick solution like this.

Eventually I hope I’ll find the time to get it into home assistant as a normal component. But I don’t see that happening before the summer.

Custom components are really easy to install, let me know if I can be of assistance :slight_smile:

Finally decided to realease version 1.0.0! It includes some cleanup and an optional configuration ‘shares’, could be used to calculate todays profit/loss or total value.

Now I will focus updating the documentation and adding examples of how I use it in automations, this will also includes scripts like the one I posted earlier.

I will also try to makes the update async, but haven’t played around with that before some it might take some time.

Just found your stocks component, which looks very nice, but

unfortunately I tried the first two stocks, and both are not available.
IBM.F - IBM in Frankfurt - 851399
and
BMW3.F - BMW Vorzugsaktie in Frankfurt - 519003


Any chance to watch those with your solution?

I don’t know anything about the german stock market but it seems the only available german market place at avanza is Equiduct, maybe that gives a clue? I can’t find the stocks you are reffering to at their site either, where did you get the ids?

The short names are from Mac stocks app.
The other numbers are the WKN (or the ISIN): I took those from onvista.de

IBM Frankfurt: WKN 851399, ISIN US4592001014
BMW Frankfurt: WKN 519003, ISIN DE0005190037

Xetra as market place would also be okay.

Ok, then unfourtenately I don’t think thoose will work. You need to find the ids on avanza.se, they do not provide stocks from Xetra :frowning:

You can track IBM in USA https://www.avanza.se/aktier/om-aktien.html/4175/international-business-machines-corp