Using Conky To Display US Stock Quotes From AlphaVantage

In my previous tutorials concerning the AlphaVantage API I discussed using the BATCH_ function for downloading multiple US stock quotes, but since that time access to that function has been removed from the API. Since I now intend to show Linux Lite 4.x users how to use the Conky Lite Widget to display quotes, I will discuss a simple solution to that problem as well. I’m going to work with .csv files here and at the outset it is my intention to stay with simple modifiable BASH scripts to get the results I’m after. There are specialized .csv tools like csvkit that can be installed to Linux Lite 4.x that make .csv files a little easier to deal with, but the installed system weight of the package is around 30MB so I’m not going to discuss using it, but rather just stick with good old NIX commands and BASH scripts to get done what I want to get done thus avoiding the added system weight. If you are a Linux Lite 4.x user you should probably read through all my tutorials on working with the AlphaVantage API if you haven’t already. Links are below:

Investment Monitoring Tools Linux Lite 4.x & AlphaVantage API

Investment Data Visualization Linux Lite 4.x & AlphaVantage API

Refining Financial Data Utilization Linux Lite 4.x From AlphaVantage API

Build An HTML Interface For Your AlphaVantage Scripts Linux Lite 4.x

If you already have a litemarkets2 folder like in the tutorials you should have an old batch quotes file in your lists file. I’m going to work with one now for demonstration purposes here. See image below:

As you can see per my previous tutorials I piped what was originally a .csv download to the column function and it displays nicely. To display this file in conky I only need to add the line of code below to the conky lite widget file located at /etc/conky-lite/widget or on my Debian 9.9 box at ~/.conkyrc.

For Linux Lite widget add: ${alignc}${execi 3000 cat ~/litemarkets2/lists/*

For Debian add: ${alignc}${color white}${execi 3000 cat ~/fmd2/lists/*

As you can see in the image below the results are not satisfactory. Because the conky-lite widget has set dimensions relative to the screen display size the output of the file is truncated when displayed. See image below:

In the next image on my Debian machine where my conky has no set dimensions relative to the screen display other than offsets from the margins, though the whole file displays nicely it stretches the other parts of the conky display. See image below:

The solution I’m going to use is to modify the download to display only the three columns: symbol, price, and volume. I’m going to use some NIX and BASH scripts to move the file through two folders and display the results the way I want them in conky. Before I do that I have to correct the problem of the no longer available batch_ function from the API. I’m also going to reduce the number of stocks I’m getting quotes for to three, but in the end you can add more stocks if you like once you understand how I’m moving and modifying the wget scripts and downloads.

If you have a litemarkets2 folder already in your home directory you will have to add a new folder which I will name here ~/litemarkets2/ckyqts. You will also need to add a new shell script which I will name here ~/litemarkets2/useconkyqts.sh. If you’ve followed and added all the files and folders I’ve used since beginning these tutorials your litemarkets2 folder should look like the image below:

I will use the ~/litemarkets2/temp folder to move and modify my wget downloaded files through like in my previous tutorials. The first thing to do is go to the AlphaVantage API and find the right demo script for .csv downloads (which is located in the Quote Endpoint section of the documentation) and run it and save the output to downloads, and also if you like copy it and paste the URL and API script into a new text editor instance. See link and images below:

https://www.alphavantage.co/documentation/

Below is a copy of the correct demo URL and API script for .csv files:

https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=MSFT&apikey=demo&datatype=csv

Next open the downloaded .csv file in your text editor. See image below:

As you can see you are in luck because at least it is a pure .csv file with no white spaces. However the new GLOBAL_ QUOTE function format has added even more columns. Also the header is pretty ugly. I’m going to fix all that now and explain what commands to use and why so that you can get your conky-lite widget to display the file the way you prefer. I’ll start by displaying an image below of my results and an image of my BASH script that modifies the files into the format my conky displays, and then go through the purpose and function of each command.

You can see in the above image the script starts with:

#!/bin/bash

Then a commented out explanation of the line below:

rm -rf ~/litemarkets2/ckyqts/ckydsply.txt

The line is uncommented in the image because I have already run the script. The command itself removes the old text file from each previous quote session allowing for a new one to be made for conky to access.

The next three lines beginning with wget are the code needed to access the API and download your quotes. The -q is for quiet (printing no information to your terminal. The -O is for standard output which we use here because we are not going to print to a file until we have modified the downloaded output, that way we can stick with simple NIX conventions. After symbol= you can enter any stock symbol you want to get a quote for. After apikey= where it is whited out in the image you enter your own AlphaVantage API key. You will notice I am using the temp folder initially to work with the outputs, and notice that I attach no file extension to the output file. Don’t attach one. Just use the convention as I have demonstrated it here giving each stock you’re getting a quote for an output number as it shown: output1, output2, output3, etc. Make sure to enclose the API URL and script in single quotes and get your spacing correct like the lines below: the – after -O has a space before and after, as does the > after csv’ I’ve used a smaller font below so that most of you will be able to see the whole line in your browser, as it is one single line.

wget -q -O - ‘https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=yoursymbolofchoice&apikey=yourAPIkey&datatype=csv’ > ~/litemarkets2/temp/output1

wget -q -O - ‘https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=yoursymbolofchoice&apikey=yourAPIkey&datatype=csv’ > ~/litemarkets2/temp/output2

wget -q -O - ‘https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=yoursymbolofchoice&apikey=yourAPIkey&datatype=csv’ > ~/litemarkets2/temp/output3

As I stated earlier the header is kind of ugly and each stock has its own output so I’m going to remove the header from each output and then combine the three outputs into one output working again in the temp folder. I’m going to use the tail function to do this. Tail counts lines backwards going upward in a delimited file and since there are only two lines I need to use tail -n 1 to extract only the bottom line to a new output file: output4. You will notice that when I work with output1 I just send the tail line to a new output file shown here as output4 though if you quote more stocks say 5 the new single combined output file would then be output6, etc. Simple stuff. To move the tail of output1 to output4 you just need the > sign with a space before and after. The nice thing about delimited files is that you can use the double >> sign to just add a tail one after the other to a single output which is what I have done here with my second and third stock quote outputs, again with a space before and after the >>

tail -n 1 ~/litemarkets2/temp/output1 > ~/litemarkets2/temp/output4

tail -n 1 ~/litemarkets2/temp/output2 >> ~/litemarkets2/temp/output4

tail -n 1 ~/litemarkets2/temp/output3 >> ~/litemarkets2/temp/output4

The next thing I want to do is remove the columns I don’t want to display in conky and format the file by piping it through the column function and save it as a .txt extension file so conky can access and display with a cat command. I can use cut to extract the columns I want to display. The ,” describes the output as comma delimited output, the -f 1,5,6 describe which columns are being extracted, in my case the stock symbol, its current price, and its trading volume. You can use whatever columns you want and as many as you want. The next important thing to do is to pipe the cut through the column function without which the cut would just display in your terminal. That is the pipe symbol | and what follows in the command below. Finally I send the output of the column pipe to a new folder and give it a new file name which I will display in conky.

cut -d “,” -f 1,5,6 ~/litemarkets2/temp/output4 | column -t -s, > ~/litemarkets2/ckyqts/ckydsply.txt

The last command shown here below is to remove all the files from my temp folder leaving it empty for the next time I get quotes. By using the rm commands in your script you can get new quotes whenever you like.

rm -rf ~/litemarkets2/temp/*

Now you have a complete script like mine. Save the file in your litemarkets2 folder as useconkyqts.sh Next open a terminal and enter the command below:

chmod +x ~/litemarkets2/useconkyqts.sh

Log out and log back in again (not always necessary) and you should be able to use your script by opening a terminal and typing: ~/litemarkets2/useconkyqts.sh and then hitting enter. If it appears to do nothing and your prompt returns it probably worked. Check your ~/litemarkets2/ckyqts/ folder for the file ckydsply.txt. If its there you have a success.

Next open as administrator with a text editor your file /etc/conky-lite/widget and add the two lines below wherever you want them to appear in the conky widget, and save the file and conky will display your stock quotes. Colors and text are editable in conky but I will leave that up to you. Because this is a column formatted text/.csv file without an original header do not use the ${alignc} option in conky. All that will do is move the first row of the table, and you would have to do it line by line. We could have used head to add a new header with the script but it’s simpler to just add the header text with conky itself and have easy color options.

${color}Stock Price Volume

${color}${execi 3000 cat ~/litemarkets2/ckyqts/ckydsply.txt}

Below is an image of the default widget with no modifications displaying stock quotes.

Return To Cruz Analytics