Sunday, 3 January 2021

Create a PMI report by downloading tweets using tweepy and docx

In 2020, I made a year-to-year return of 36% on my share portfolio. I think I did pretty well.

PMIs are the most important economic indicators I look at in making investment decisions. PMI stands for Purchasing Managers' Index. It is based on the opinion of purchasing managers, surveyed on a monthly basis. A reading of over 50 represents expansion, a reading of under 50 represents contraction, and a reading of 50 represents no change.

There are many organisations in different countries that publish PMIs so it is difficult to find a single place to see all of them at once. However, lucky for us in the 21st century, there are Twitter accounts that tweet PMIs and other economic indicators right after they are published. To make it convenient for me to see the latest PMIs in one place, I decided to write a python script (in a .ipynb file) that downloads the relevant tweets and save them as a report in Micorsoft Word.

Before you start you would need the following:

  • A Twitter developer account so you can connect to Twitter via API and download tweets programmatically.
  • The following libararies: tweepy and docx.

Step 1: Connect to Twitter

You first step is to connect to twitter using your consumer key and consumer secret. I have saved mine to separate files so that they won't appear in the code. To get your own set of keys, you would need a developer account.

Step 2: Download tweets using the API.user_timeline method in tweepy

In my code, I specified it to download the latest tweets from the account "YTradingAdvisor" on Twitter. This account tweets PMIs right after they are published. I also set my code to exclude replies and retweets.

Step 3: Search for "PMI" in tweets and specific countries

Loop through the list of tweets and filter for tweets that contains the word "PMI". Also organise these "PMI" tweets by country for better presentation in our report.

Step 4: Write your tweets to a Word document, organised by country:

Use the Document() function of docx to create a document. Write each list of "PMI" tweets for each country as a paragraph of its own.

This is what the output file looks like:

Now I can generate a PMI report quickly whenever I want and without having to go through a large number of tweets to see the latest PMIs. I plan to generate other reports like this in the future.

For the full code and output, please refer to my Github repo for this project:

https://github.com/wlsamchen01/Finance_Reports

 

References:

A script to download all of a user's tweets into a csv - https://gist.github.com/yanofsky/5436496

Tweepy user-methods - http://docs.tweepy.org/en/latest/api.html#user-methods

docx documentation - https://python-docx.readthedocs.io/en/latest/



Portfolio Optimisation with Python

 Recently I have been busy so I have been neglecting this blog for a very long time. Just want to put out some new content. So there is this...