Updated script to speak the volume of the current trade if it exceeds a threshold and also to speak the running volume each time it increments by a user-specified amount. This way, you can cook dinner, take a dump, sleep, etc, and still listen for market moves.
--original post---
Below is a simple perl script to login to the experimental telnet server @ bitcoincharts.com and print the volume, price, date (in human format).
1. This can run on either Linux or Mac (differences in the date command are handled for each).
2. For Mac, you could very easily add a subroutine to speak thresholds using the "say" command. For example:
`say "Holy shit, someone traded one hundred thousand BTC!"`
If anyone wants an example, let me know...
3. Change the @INC path within the BEGIN block to wherever you've installed the Telnet.pm library, which you can pick up at:
http://search.cpan.org/~jrogers/Net-Telnet-3.03/lib/Net/Telnet.pm4. Change the $exchange variable to whatever exchange you want to follow (run 'telnet bitcointcharts 27007' to see what strings they use for the other exchanges.
Happy trading. Improvements, and other scripts? Dump 'em here.
Thanks!
qo
Sample output (columns are Price, Volume, Running volume since script started, Date):
dhcp-171-71-25-244:btc qo$ ./btc.pl
Connected to bitcoincharts.com 27007 ts is Net::Telnet=GLOB(0x7f931b0ed520)
5.86227000 1.02120000 1.02120000 Wed Jan 18 15:10:48 PST 2012
5.85922000 0.31027261 1.33147261 Wed Jan 18 15:10:48 PST 2012
5.85817000 1.02426000 2.35573261 Wed Jan 18 15:10:48 PST 2012
5.85407000 1.02733000 3.38306261 Wed Jan 18 15:10:48 PST 2012
5.85117000 0.03173000 3.41479261 Wed Jan 18 15:10:48 PST 2012
etc...
Script:
#!/usr/bin/perl
#
# Author: qo
# Summary: Print current trade price, volume, running volume total, and time
# If running on Mac, use "say" command to speak the current running
# volume total with each total volume increment of $volumeIncrement
#
# Version History
#
# Date Version Comment
# 20120118 101 Speak the volume of the current trade if it's above
# $volumeTradeThreshold
# 20120118 101 Speak the running volume ($volumeTotal) with each increment
# of $volumeIncrement (this is controlled by $volumeTarget which
# is incremented by $volumeIncrement each time it's triggered).
# 20120117 100 Initial version
#------------------------------------------------------------------------------
BEGIN {
push @INC, "/Path/to/your/perl/lib/directory";
}
use Net::Telnet;
# Connection settings
local $exchange = "mtgoxUSD";
local $host = "bitcoincharts.com";
local $port = "27007";
local $timeout = 6000;
# Volume thresholds and statistics
local $volumeCurrent = 0;
local $volumeTotal = 0;
local $volumeIncrement = 500;
local $volumeTarget = $volumeIncrement;
local $volumeTradeThreshold = 400;
# Files, etc.
local $say = "/usr/bin/say";
local $logFile = "./btc.log";
local $os = getos();
my $ts = connectBtc($host,$port,$timeout,$logFile);
while(1) {
@op = $ts->waitfor('/\}/');
foreach (@op) {
chomp;
# {"volume": 0.16041285, "timestamp": 1326854332, "price": 6.27502, "symbol": "mtgoxUSD", "id": 16329594}
if(/$exchange/) {
my ($volume,$date,$price) = (split(" "))[1,3,5];
$date =~ s/,//g;
$price =~ s/,//g;
my $udate = udate($date);
$volumeTotal += $volume;
printf("%14.8f %15.8f %18.8f %29s\n",$price,$volume,$volumeTotal,$udate);
trackRunningVolume();
trackCurrentTradeVolume($volume);
}
}
}
sub connectBtc {
my($host,$port,$timeout,$logFile) = @_;
my $prefix = "connect:";
my $ts = new Net::Telnet (Host => $host,
Port => $port,
Timeout => $timeout,
Input_log => $logFile);
print "Connected to $host $port ts is $ts\n";
return($ts);
}
sub udate {
my($epoch) = @_;
my $prefix = "udate:";
my $op = "";
if($os eq "Darwin") {
$op = `date -j -f "%s" "$epoch"`;
}elsif($os eq "Linux") {
my $udate = '@' . $epoch;
$op = `date -d $udate`;
}else{
print "Unsupported os ($os). Exiting.\n";
exit;
}
chomp($op);
return($op);
}
sub getos {
my $os = `uname`;
chomp ($os);
return($os);
}
sub trackRunningVolume {
if($volumeTotal >= $volumeTarget) {
$volumeTarget += $volumeIncrement;
my $value = round($volumeTotal);
speak("Volume since script started is $value");
}
}
sub trackCurrentTradeVolume {
my($currentTradeVolume) = @_;
if($currentTradeVolume >= $volumeTradeThreshold) {
my $value = round($currentTradeVolume);
speak("Volume of current trade is $value");
}
}
sub round {
my($value) = @_;
return(sprintf("%d", $value));
}
sub speak {
my($phrase) = @_;
if($os ne "Darwin") {
return;
}
`$say \"$phrase\"`;
}