Building a stock reminder Twitter bot with Python and Alpha Vantage API

Luis Garcia
3 min readDec 26, 2020

2020 is (finally!) coming to an end and investors are eager to share their stock market predictions for the upcoming year.

I’d been feeling the itch to work on a side project for a while and I thought - wouldn’t it be cool to easily set these reminders and share their results on Twitter?

With this in mind, I decided to build a sort of Twitter reminder tool like the awesome RemindMe_OfThisbot but specifically oriented towards stock predictions. (I was also partly inspired by the amazing Trump2Cash bot).

After some rough sketches on the white board plus a little research, I decided on the following tools to implement my idea:

  1. Tweepy - an easy to use library for accessing the Twitter API
  2. Alpha Vantage - a free API for fetching real time and historical stock exchange data
  3. Peewee ORM for saving and querying our data
  4. Heroku cloud provider for deployment

And so a few days of coding later, the first MVP for stock_reminder bot was born:

Features:

  1. Mentioning the bot:

To use the bot, simply mention @stock_reminder with one or more cashtags followed by a stock ticker or cryptocurrency symbol and a reminder date.

Alternatively, you can also mention the bot with just a date in a reply to another tweet that contains stocks as shown below:

The bot will then get back to you on the specified date with your investment results (the below examples use made-up data):

2. Shorting securities:

For predictions that involve short-selling securities, add the word “short”. E.g. “@stock_reminder short $BTC in 2 months”.

3. Generating reports:

Additionally, the bot can also produce rating reports when it is mentioned with a ticker symbol and the words “report” or “analyse”/”analyze”:

The scores and final recommendations are pulled from the financial modelling prep api.

Implementation 🛠️:

All source code is available on github. There’s a few open issues and contributions are welcome! For a high-level overview of how the bot works, I’ve created the following flowcharts:

Every 2 minutes, a Heroku clock process (a scheduled job), runs the bot’s reply_to_mentions method which replies to any new mentions and creates reminder objects if they’re valid.

Once all mentions have been replied, the same job executes publish_reminders, which checks whether any of the saved reminders are due and publishes them if that is the case.

Overall this was a fun project to work on. It was a good opportunity to improve my knowledge on Docker and I also set up a CI pipeline for running automated tests using TravisCI. Some of the challenges included parsing dates in different formats, handling all use cases (API limit exceeded, no stocks found…etc.) and avoiding duplicate replies. I also made sure to follow TDD and have plenty of test coverage. I was lucky enough to get loads of useful feedback from the r/Python community and some users pointed out flaws like not handling stock splits or dividends as well as other enhancements. If you have any ideas or suggestions on how to further extend the bot or there’s any cool features you’d like to add feel free to comment or open an issue on github!

--

--