line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Finance::Alpaca::TradeStream 0.9902 { |
2
|
1
|
|
|
1
|
|
7
|
use strictures 2; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
39
|
|
3
|
1
|
|
|
1
|
|
175
|
use Moo; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
4
|
1
|
|
|
1
|
|
303
|
use feature 'signatures'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
61
|
|
5
|
1
|
|
|
1
|
|
5
|
no warnings 'experimental::signatures'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
6
|
1
|
|
|
1
|
|
5
|
use Types::Standard qw[ArrayRef CodeRef Dict Enum InstanceOf Str]; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
8
|
|
7
|
1
|
|
|
1
|
|
1445
|
use Mojo::Promise; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
11
|
|
8
|
|
|
|
|
|
|
# |
9
|
1
|
|
|
1
|
|
32
|
use lib './lib/'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
5
|
|
10
|
1
|
|
|
1
|
|
526
|
use Finance::Alpaca::Struct::TradeUpdate qw[to_TradeUpdate]; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
8
|
|
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__ |