line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Finance::Alpaca::TradeStream 0.9904 { |
2
|
17
|
|
|
17
|
|
130
|
use strictures 2; |
|
17
|
|
|
|
|
132
|
|
|
17
|
|
|
|
|
695
|
|
3
|
17
|
|
|
17
|
|
3316
|
use Moo; |
|
17
|
|
|
|
|
57
|
|
|
17
|
|
|
|
|
101
|
|
4
|
17
|
|
|
17
|
|
5547
|
use feature 'signatures'; |
|
17
|
|
|
|
|
37
|
|
|
17
|
|
|
|
|
1228
|
|
5
|
17
|
|
|
17
|
|
104
|
no warnings 'experimental::signatures'; |
|
17
|
|
|
|
|
33
|
|
|
17
|
|
|
|
|
594
|
|
6
|
17
|
|
|
17
|
|
93
|
use Types::Standard qw[ArrayRef CodeRef Dict Enum InstanceOf Str]; |
|
17
|
|
|
|
|
32
|
|
|
17
|
|
|
|
|
126
|
|
7
|
17
|
|
|
17
|
|
25045
|
use Mojo::Promise; |
|
17
|
|
|
|
|
43
|
|
|
17
|
|
|
|
|
200
|
|
8
|
|
|
|
|
|
|
# |
9
|
17
|
|
|
17
|
|
564
|
use lib './lib/'; |
|
17
|
|
|
|
|
36
|
|
|
17
|
|
|
|
|
167
|
|
10
|
17
|
|
|
17
|
|
9512
|
use Finance::Alpaca::Struct::TradeUpdate qw[to_TradeUpdate]; |
|
17
|
|
|
|
|
58
|
|
|
17
|
|
|
|
|
150
|
|
11
|
|
|
|
|
|
|
# |
12
|
|
|
|
|
|
|
has tx => ( is => 'rwp', isa => InstanceOf ['Mojo::Transaction::WebSocket'], predicate => 1 ); |
13
|
|
|
|
|
|
|
has cb => ( is => 'ro', isa => CodeRef, required => 1 ); |
14
|
|
|
|
|
|
|
has subscriptions => ( is => 'rwp', isa => ArrayRef [Str], default => sub { [] }, lazy => 1 ); |
15
|
|
|
|
|
|
|
|
16
|
0
|
|
|
0
|
0
|
|
sub authorize ( $s, $ua, $keys, $paper ) { |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
#use Data::Dump; |
19
|
|
|
|
|
|
|
#ddx $keys; |
20
|
0
|
|
|
|
|
|
$ua->inactivity_timeout(120); # XXX - Testing! |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# warn( |
23
|
|
|
|
|
|
|
# $paper ? 'wss://paper-api.alpaca.markets/stream' : 'wss://api.alpaca.markets/stream' ); |
24
|
0
|
|
|
|
|
|
$ua->websocket_p( |
25
|
|
|
|
|
|
|
( |
26
|
|
|
|
|
|
|
$paper |
27
|
|
|
|
|
|
|
? 'wss://paper-api.alpaca.markets/stream' |
28
|
|
|
|
|
|
|
: 'wss://api.alpaca.markets/stream' |
29
|
|
|
|
|
|
|
) |
30
|
|
|
|
|
|
|
)->then( |
31
|
0
|
|
|
0
|
|
|
sub ($tx) { |
|
0
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
my $promise = Mojo::Promise->new; |
33
|
0
|
|
|
|
|
|
$s->_set_tx($tx); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
#$tx->on( finish => sub { $promise->resolve } ); |
36
|
|
|
|
|
|
|
# my $promise = Mojo::Promise->new; |
37
|
|
|
|
|
|
|
#$tx->on( finish => sub { $promise->resolve } ); |
38
|
|
|
|
|
|
|
$tx->on( |
39
|
0
|
|
|
|
|
|
finish => sub ( $tx, $code, $reason = '' ) { |
40
|
0
|
|
|
|
|
|
warn "WebSocket closed with status $code. $reason"; |
41
|
0
|
|
|
|
|
|
$promise->resolve; |
42
|
|
|
|
|
|
|
} |
43
|
0
|
|
|
|
|
|
); |
44
|
0
|
|
|
|
|
|
$tx->on( error => sub ( $e, $err ) { warn "This looks bad: $err" } ); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
$tx->on( |
46
|
0
|
|
|
|
|
|
json => sub ( $tx, $msg = () ) { |
47
|
0
|
0
|
|
|
|
|
if ( $msg->{stream} eq 'authorization' ) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
48
|
0
|
0
|
|
|
|
|
if ( $msg->{data}{status} eq 'authorized' ) { |
49
|
0
|
|
|
|
|
|
$s->subscribe( streams => ['trade_updates'] ); |
50
|
0
|
|
|
|
|
|
$promise->resolve(); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
elsif ( $msg->{stream} eq 'listening' ) { |
54
|
0
|
|
|
|
|
|
$s->_set_subscriptions( $msg->{data}{streams} ); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
elsif ( $msg->{stream} eq 'trade_updates' ) { |
57
|
0
|
|
|
|
|
|
$s->cb->( to_TradeUpdate( $msg->{data} ) ); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
else { |
60
|
|
|
|
|
|
|
#warn 'unknown data'; |
61
|
|
|
|
|
|
|
#...; |
62
|
0
|
|
|
|
|
|
$s->cb->($msg); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
#$tx->finish; |
66
|
|
|
|
|
|
|
} |
67
|
0
|
|
|
|
|
|
); |
68
|
0
|
|
|
|
|
|
$tx->send( |
69
|
|
|
|
|
|
|
{ |
70
|
|
|
|
|
|
|
json => { |
71
|
|
|
|
|
|
|
action => 'authenticate', |
72
|
|
|
|
|
|
|
data => { key_id => $keys->[0], secret_key => $keys->[1] } |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
); |
76
|
0
|
|
|
|
|
|
return $promise; |
77
|
|
|
|
|
|
|
} |
78
|
0
|
|
|
|
|
|
)->catch( |
79
|
0
|
|
|
0
|
|
|
sub ($err) { |
|
0
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
warn "WebSocket error: $err"; |
81
|
|
|
|
|
|
|
} |
82
|
0
|
0
|
|
|
|
|
); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
0
|
|
|
0
|
0
|
|
sub subscribe ( $s, %params ) { |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
$s->tx->send( { json => { action => 'listen', data => {%params} } } ); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
0
|
|
|
0
|
0
|
|
sub unsubscribe ( $s, %params ) { # XXX - Grep current list |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
$s->tx->send( { json => { action => 'listen', data => {%params} } } ); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
1; |
94
|
|
|
|
|
|
|
__END__ |