Back to Top

Wednesday, May 29, 2013

Connecting to the MtGox market data feed using Perl

For a recent project I needed some realistic market data for an electronic exchange. Seeing how MtGox provides free and open access to theirs (thank you!) I chose them. However none of the examples floating around the internet seemed to work, so I whipped one up using Net::Async::WebSocket::Client. Enjoy:

use IO::Async::Loop;
use Net::Async::WebSocket::Client;

my $client = Net::Async::WebSocket::Client->new(
        on_frame => sub {
                my ( $self, $frame ) = @_;
                print "\n", $frame, "\n";
        },
);

my $loop = IO::Async::Loop->new;
$loop->add( $client );

$client->connect(
        host => 'websocket.mtgox.com',
        service => 80,
        url => "ws://websocket.mtgox.com:80/mtgox",
        on_connected => sub {},
        on_connect_error => sub { die "Cannot connect - $_[-1]" },
        on_resolve_error => sub { die "Cannot resolve - $_[-1]" },
);

$loop->loop_forever;

(it is basically the sample program for the module, with the MtGox market data URL hardcoded).

0 comments:

Post a Comment

You can use some HTML tags, such as <b>, <i>, <a>. Comments are moderated, so there will be a delay until the comment appears. However if you comment, I follow.