Displaying live price quotes (and world times and unread email) in your i3status bar

Save clicks by displaying live prices, world times and email unread counts in your standard i3status bar

Tired of flicking between charts, or worse, forgetting to check charts, and wanting to make the most of the new read_file function of the latest i3status version, I set about making it more useful.

Here’s what the end result looked like:

i3status price ticker, email and times

Adding world times to your i3status bar

I added world times by simply using the tztime command as follows:

tztime Tokyo {
        format = "JP %H:%M"
	timezone = "Asia/Tokyo"
}

You can get a list of timezones available by using ls in the /usr/share/zoneinfo directory. Etc/UTC was a nice option to see, but less necessary in my case, being located in GMT locally.

Adding currency price quotes to the i3status bar

For this stage I took two approaches. One used an API - Coinbase for one and a “free” Forex API I found elsewhere. Coinbase worked fine, will paste the snippet used in the rates bash script for that below:

BTCUSD=$(/usr/bin/curl -s http://api.coindesk.com/v1/bpi/currentprice/USD.json | jq -r '.bpi["USD"]["rate"]' > $BTCTXT)

Which writes the price to a file and then i3status.conf can read that and print at its normal update intervals.

The free FX API worked for a week and then returned NULL, presumably because they want you to provide “banners and images and links” for their service on the “website” you’re using it for.

So I wrote a quick indi for MT5, which is basically running constantly when the PC is on, to grab live prices on every minute close and write them to a file.

MT5 indicator to write prices to file

I then copy that file, using the rates bash script, from the Wine MT5 folder to switch it from a binary format (the TXT file output was even worse for some reason - UTF issues I think) and get it closer to plain text. There are still NULLs in the file, so I had to run a little sed line on it to clean out those. Finally I had a clean price to read into the i3status config.

It looks like this for one price - copy and paste for as many as you want, of course:

cp "/media/nvme/.winemt5/drive_c/Program Files/Pepperstone MetaTrader 5/MQL5/Files/EURUSD.txt" /tmp/EU.txt

# Get file formatting closer to plaintext
cat /tmp/EU.txt > /tmp/EU2.txt

# Remove NULLs
sed -i 's/\x0//g' /tmp/EU2.txt

So instead of saving API data to file, the script now copies the binary text file that MT5 produces to a new .txt file, which I then cat into another file to clean it up via stdin, run sed on it to strip out the remaining nulls and read that into i3status.conf as follows:

read_file EU { 
	format = "EU %content"
	format_bad = "EU -"
	path = "/tmp/EU2.txt"
	max_characters = 5
#	separator_block_width = 100
#	separator = false
}        

I’ll leave those separator options in place in case they are useful to you. It’s how I group the various data on the line at the moment, with a 100 pixel gap.

The script is called by crontab every minute. It’s quite lightweight, as you can see, with all the “heavy lifting” being done by MT5 itself now.

Adding unread email counts to the i3status bar

As I still use command line email, it’s quite simple to get a count from the maildir files, write that to a file and read it into the i3status config.

  maildirsBOX="$HOME/.mail/USER/Inbox/new/"
  boxtxt="/tmp/BOX1.txt"
  
  box="$(find $maildirsBOX -type f | wc -l )"
  echo $box > $boxtxt
  
  if [ $box == '0' ]
  then
	rm "$boxtxt"
  fi

The reason for deleting the text file if no unread emails are there is to use the i3status format_bad syntax to make it clearer (bright green) when there is an email in the inbox. You can see three red dashes in the image at the top of this post (right-click > view image if not clear) and that means I’m up to date.

I can share the scripts for any of the above in full if there’s any niche interest, but you should be able to work away with what’s here if you’re reading this in the first place :D

EDIT: Looks like you can do this in Windows, using DesktopInfo and the indi I’ve made above to write the prices to file. I’ll put a guide together if there’s any interest!

Subscribe to the mailing list for updates, discounts and offers




Published by and tagged <a href="/tags/automation/">automation</a> and <a href="/tags/linux/">linux</a> using 699 words.

Next:
Previous: