-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitstamp.pl
78 lines (63 loc) · 1.62 KB
/
bitstamp.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env perl
use strict;
use warnings;
use 5.14.0;
use Data::Dumper;
use Mojo::UserAgent;
use Mojo::JSON qw /j/;
my $lastPrice = 0;
my $ua = Mojo::UserAgent->new();
$ua->inactivity_timeout(0);
$ua->on(error => sub {
my ($tx, $error) = @_;
say "*** ERROR: $error";
});
$ua->websocket("ws://ws.pusherapp.com/app/de504dc5763aeef9ff52?protocol=5" => sub {
my ($ua, $tx) = @_;
say 'WebSocket handshake failed!' and return unless $tx->is_websocket;
$tx->on(error => sub {
my ($tx, $error) = @_;
say "*** ERROR: $error";
});
$tx->on(finish => sub {
my ($tx, $code, $reason) = @_;
$reason //= "Unknown reason!";
say "WebSocket closed with status $code - $reason.";
});
$tx->on(message => \&on_message);
$tx->on(trade => \&on_trade);
$tx->send({
json => {
event => "pusher:subscribe",
data => { channel => "live_trades" },
}
});
});
sub on_message {
my ($tx, $msg) = @_;
my $data = j($msg);
given ($data->{event}) {
when ("pusher:connection_established") { say " * Connected to API" }
when ("pusher_internal:subscription_succeeded") { say " * Subscribed to channel " . $data->{channel} }
when ("trade") { $tx->emit("trade", $data); }
default { say " * UNHANDLED MSG: " . Dumper $data }
}
}
sub on_trade {
my ($tx, $msg) = @_;
my $trade = j($msg->{data});
my ($price, $amount) = ($trade->{price}, $trade->{amount});
my $trend;
if($lastPrice < $price) {
$trend = "UP";
}
elsif($lastPrice > $price) {
$trend = "DOWN";
}
else {
$trend = "SAME";
}
$lastPrice = $price;
printf(' * Trade: %.3fBTC @ $%.2f - '."$trend\n", $amount, $price);
}
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;