File Coverage

lib/Finance/GDAX/API/Product.pm
Criterion Covered Total %
statement 25 67 37.3
branch 3 16 18.7
condition 2 15 13.3
subroutine 8 12 66.6
pod 6 6 100.0
total 44 116 37.9


line stmt bran cond sub pod time code
1             package Finance::GDAX::API::Product;
2             our $VERSION = '0.01';
3 1     1   18675 use 5.20.0;
  1         5  
4 1     1   4 use warnings;
  1         3  
  1         19  
5 1     1   406 use Moose;
  1         439664  
  1         8  
6 1     1   7491 use Finance::GDAX::API;
  1         4  
  1         41  
7 1     1   468 use Finance::GDAX::API::TypeConstraints;
  1         3  
  1         40  
8 1     1   8 use namespace::autoclean;
  1         3  
  1         6  
9              
10             extends 'Finance::GDAX::API';
11              
12             has 'id' => (is => 'rw',
13             isa => 'Str',
14             );
15              
16             # for order_book
17             has 'level' => (is => 'rw',
18             isa => 'ProductLevel',
19             default => 1,
20             );
21              
22             # for historic_rates
23             has 'start' => (is => 'rw',
24             isa => 'Str',
25             );
26             has 'end' => (is => 'rw',
27             isa => 'Str',
28             );
29             has 'granularity' => (is => 'rw',
30             isa => 'PositiveInt',
31             );
32              
33             sub list {
34 0     0 1 0 my $self = shift;
35 0         0 $self->method('GET');
36 0         0 $self->path('/products');
37 0         0 return $self->send;
38             }
39              
40             sub order_book {
41 1     1 1 69982 my ($self, $product_id) = @_;
42 1   33     29 $product_id = $product_id || $self->id;
43 1 50       10 die 'order_book requires a product id' unless $product_id;
44 0         0 $self->id($product_id);
45 0         0 my $path = "/products/$product_id?level=" . $self->level;
46 0         0 $self->method('GET');
47 0         0 $self->path($path);
48 0         0 return $self->send;
49             }
50              
51             sub ticker {
52 0     0 1 0 my ($self, $product_id) = @_;
53 0   0     0 $product_id = $product_id || $self->id;
54 0 0       0 die 'ticker requires a product id' unless $product_id;
55 0         0 $self->id($product_id);
56 0         0 my $path = "/products/$product_id/ticker";
57 0         0 $self->method('GET');
58 0         0 $self->path($path);
59 0         0 return $self->send;
60             }
61              
62             sub trades {
63 0     0 1 0 my ($self, $product_id) = @_;
64 0   0     0 $product_id = $product_id || $self->id;
65 0 0       0 die 'trades requires a product id' unless $product_id;
66 0         0 $self->id($product_id);
67 0         0 my $path = "/products/$product_id/trades";
68 0         0 $self->method('GET');
69 0         0 $self->path($path);
70 0         0 return $self->send;
71             }
72              
73             sub historic_rates {
74 1     1 1 3687 my ($self, $product_id) = @_;
75 1   33     5 $product_id = $product_id || $self->id;
76 1 50       3 die 'historic rates requires a product id' unless $product_id;
77 1         45 $self->id($product_id);
78 1 50       31 die 'historic rates requires start time' unless $self->start;
79 0 0         die 'historic rates requires end time' unless $self->end;
80 0 0         die 'historic rates requires granularity' unless $self->granularity;
81            
82 0           my $path = "/products/$product_id/candles";
83 0           $path .= '?start=' . $self->start;
84 0           $path .= '&end=' . $self->end;
85 0           $path .= '&granularity=' . $self->granularity;
86 0           $self->method('GET');
87 0           $self->path($path);
88 0           return $self->send;
89             }
90              
91             sub day_stats {
92 0     0 1   my ($self, $product_id) = @_;
93 0   0       $product_id = $product_id || $self->id;
94 0 0         die 'day_stats requires a product id' unless $product_id;
95 0           $self->id($product_id);
96 0           my $path = "/products/$product_id/stats";
97 0           $self->method('GET');
98 0           $self->path($path);
99 0           return $self->send;
100             }
101              
102             __PACKAGE__->meta->make_immutable;
103             1;
104              
105             =head1 NAME
106              
107             Finance::GDAX::API::UserAccount - Product Information
108              
109             =head1 SYNOPSIS
110              
111             use Finance::GDAX::API::Product;
112              
113             $product = Finance::GDAX::API::Product->new;
114              
115             # List of all products
116             $products = $product->list;
117              
118             =head2 DESCRIPTION
119              
120             Returns various information about GDAX products.
121              
122             =head1 ATTRIBUTES
123              
124             =head2 C<id> $string
125              
126             The GDAX product id. Necessary for order_book, get_trades,
127             historic_rates (or passed as parameter to method).
128              
129             =head2 C<level> $integer (default: 1)
130              
131             The detail level in the return hash of the method order_book:
132              
133             Level Description
134             1 Only the best bid and ask
135             2 Top 50 bids and asks (aggregated)
136             3 Full order book (non aggregated)
137              
138             Levels 1 and 2 are aggregated and return the number of orders at each
139             level. Level 3 is non-aggregated and returns the entire order book.
140              
141             =head2 C<start> $datetime_string (ISO8601)
142              
143             Start time for the historic_rates method.
144              
145             =head2 C<end> $datetime_string (ISO8601)
146              
147             End time for the historic_rates method.
148              
149             =head2 C<granularity> $seconds
150              
151             Granularity in seconds for the historic_rates method.
152              
153             =head1 METHODS
154              
155             =head2 C<list>
156              
157             Returns a list of available currency pairs for trading.
158              
159             [
160             {
161             "id": "BTC-USD",
162             "base_currency": "BTC",
163             "quote_currency": "USD",
164             "base_min_size": "0.01",
165             "base_max_size": "10000.00",
166             "quote_increment": "0.01"
167             }
168             ]
169              
170             The base_min_size and base_max_size fields define the min and max
171             order size. The quote_increment field specifies the min order price as
172             well as the price increment.
173              
174             The order price must be a multiple of this increment (i.e. if the
175             increment is 0.01, order prices of 0.001 or 0.021 would be rejected).
176              
177             =head2 C<order_book> [$product_id]
178              
179             Returns a hash of open orders for a product. The $product_id can be
180             passed in as a parameter to the method, or the attribute "id" can be
181             set. The parameter takes precidence.
182              
183             The amount of detail returned is customized with the "level" attribute.
184              
185             By default, only the inside (i.e. best) bid and ask are returned. This
186             is equivalent to a book depth of 1 level. If you would like to see a
187             larger order book, specify the level query parameter.
188              
189             If a level is not aggregated, then all of the orders at each price
190             will be returned. Aggregated levels return only one size for each
191             active price (as if there was only a single order for that size at the
192             level).
193              
194             Level 1:
195              
196             {
197             "sequence": "3",
198             "bids": [
199             [ price, size, num-orders ],
200             ],
201             "asks": [
202             [ price, size, num-orders ],
203             ]
204             }
205              
206             Level 2:
207              
208             {
209             "sequence": "3",
210             "bids": [
211             [ price, size, num-orders ],
212             [ "295.96", "4.39088265", 2 ],
213             ...
214             ],
215             "asks": [
216             [ price, size, num-orders ],
217             [ "295.97", "25.23542881", 12 ],
218             ...
219             ]
220             }
221              
222             Level 3:
223              
224             {
225             "sequence": "3",
226             "bids": [
227             [ price, size, order_id ],
228             [ "295.96","0.05088265","3b0f1225-7f84-490b-a29f-0faef9de823a" ],
229             ...
230             ],
231             "asks": [
232             [ price, size, order_id ],
233             [ "295.97","5.72036512","da863862-25f4-4868-ac41-005d11ab0a5f" ],
234             ...
235             ]
236             }
237              
238             The GDAX API warns you that abuse of level 3 polling with cause your
239             access to be limited or blocked -- to use websocket streams instead.
240              
241             =head2 C<ticker> [$product_id]
242              
243             Returns snapshot information about the last trade (tick), best bid/ask
244             and 24h volume.
245              
246             Takes parameter product_id or uses "id" attribute.
247              
248             Real-time updates
249              
250             Polling is discouraged in favor of connecting via the websocket stream
251             and listening for match messages.
252              
253             {
254             "trade_id": 4729088,
255             "price": "333.99",
256             "size": "0.193",
257             "bid": "333.98",
258             "ask": "333.99",
259             "volume": "5957.11914015",
260             "time": "2015-11-14T20:46:03.511254Z"
261             }
262              
263             =head2 C<trades> [$product_id]
264              
265             Return an array of hashes of the latest trades for a given product_id,
266             which can be a parameter to the method or the attribute "id".
267              
268             [{
269             "time": "2014-11-07T22:19:28.578544Z",
270             "trade_id": 74,
271             "price": "10.00000000",
272             "size": "0.01000000",
273             "side": "buy"
274             }, {
275             "time": "2014-11-07T01:08:43.642366Z",
276             "trade_id": 73,
277             "price": "100.00000000",
278             "size": "0.01000000",
279             "side": "sell"
280             }]
281              
282             Side
283              
284             The trade side indicates the maker order side. The maker order is the
285             order that was open on the order book. buy side indicates a down-tick
286             because the maker was a buy order and their order was
287             removed. Conversely, sell side indicates an up-tick.
288              
289             =head2 C<historic_rates> [$product_id]
290              
291             Returns an array of arrays of historic rates for a product. The array
292             buckets are ordered as follows:
293              
294             [
295             [ time, low, high, open, close, volume ],
296             [ 1415398768, 0.32, 4.2, 0.35, 4.2, 12.3 ],
297             ...
298             ]
299              
300             This method requires that the attributes "start", "end" and
301             "granularity" are set.
302              
303             Each bucket is an array of the following information:
304              
305             time bucket start time
306             low lowest price during the bucket interval
307             high highest price during the bucket interval
308             open opening price (first trade) in the bucket interval
309             close closing price (last trade) in the bucket interval
310             volume volume of trading activity during the bucket interval
311              
312             =head2 C<day_stats> [$product_id]
313              
314             Returns a hash of the stats for the given $product_id (or "id"
315             attribute) accumulated for the last 24 hours.
316              
317             API:
318              
319             Get 24 hr stats for the product. volume is in base currency
320             units. open, high, low are in quote currency units.
321              
322             {
323             "open": "34.19000000",
324             "high": "95.70000000",
325             "low": "7.06000000",
326             "volume": "2.41000000"
327             }
328              
329             (however actual dump of data shows more):
330              
331             $VAR1 = {
332             'open' => 0,
333             'low' => 0,
334             'high' => 0,
335             'last' => '9999999999.00000000',
336             'volume_30day' => '2295.91760955',
337             'volume' => 0
338             };
339              
340              
341             =cut
342              
343              
344             =head1 AUTHOR
345              
346             Mark Rushing <mark@orbislumen.net>
347              
348             =head1 COPYRIGHT AND LICENSE
349              
350             This software is copyright (c) 2017 by Home Grown Systems, SPC.
351              
352             This is free software; you can redistribute it and/or modify it under
353             the same terms as the Perl 5 programming language system itself.
354              
355             =cut
356