Categories
Coding

Static Imports

Okay , I admit defeat. Static imports are not that bad. Particularly when you are using libraries like ASM and you have to deal with tons and tons of constants. It’s nice to be able to CTRL-SPACE in Eclipse and get the constant name without having to explicitly type the enclosing interface name every time, just by adding the static modifier to the import:

import static org.objectweb.asm.Constants.*;

That saves quite a bit of typing in the average ASM client program.

Categories
Coding

Plotting Yahoo! Stock Charts with Gnuplot

If you have ever gone to Yahoo! Finance and downloaded the historical share prices as a CSV file, a logical next step is to plot the data contained within the file. I recently used gnuplot to do some plotting of these types of files, but ran into a couple of issues. Firstly, gnuplot can’t handle CSV files correctly (unless I’m missing something obvious), at least, not CSV files with date information in them. A typical CSV download looks like this:

Date,Open,High,Low,Close,Volume,Adj. Close*
7-Oct-05,2025.00,2070.00,2070.00,2070.00,237,2070.00
6-Oct-05,2025.00,2070.00,1980.00,2070.00,1073,2070.00

In order to get gnuplot to read this, I had to do a couple of things. Firstly, I had to explicitly tell gnuplot that I was using a date-type variable for the x-axis (found here):

set xdata time
set timefmt “%d-%b-%y”
set format x “%b %d”

The next thing I needed to do was use the inbuilt capability of gnuplot to pass a file through an external filter (in this case sed), in order to strip out the commas and replace them with tabs, which gnuplot can handle more comfortably (after setting up some cosmetic-related features):

gnuplot>set grid
gnuplot>unset key
gnuplot>unset title
gnuplot> plot “< sed 's/,/\t/g' /cygdrive/c/Temp/table.csv" every ::2 using 1:2:4:3:5 with candlesticks

Note that I am telling gnuplot to ignore the header line in the CSV file (the every directive), and passing it five different pieces of information (numbered columns $1 through to $5) that it needs to generate the candlestick diagram.

Here’s the result:

Stock Price Diagram

Categories
Coding

More Bash Scripting

When trying to write a simple one-liner that counted lines of code in a source subdirectory, I finally came up with the following:

export SUM=0;for f in $(find src -name "*.java"); do export SUM=$(($SUM + $(wc -l $f | awk '{ print $1 }'))); done; echo $SUM

This has the distinction of using the Bash arithmetic operator $(()) or $[[]]. Basically, it creates a variable called SUM, then internally loops around for each file name returned by the find command. It then passes the filename to wc -l to count the lines, and then to awk to strip out everything but the line count. This sequence of commands is encapsulated into a variable using command substitution (the $() bit), and used in conjunction with the arithmetic operator ($(())) to update the add the current line count to the existing SUM variable.

Of course, this is a crude attempt to count LOC, and is probably more suited for more general counting, since it does not take into account blank lines, comments, etc.