File Coverage

lib/Finance/GDAX/API/Position.pm
Criterion Covered Total %
statement 14 25 56.0
branch 0 4 0.0
condition n/a
subroutine 5 7 71.4
pod 2 2 100.0
total 21 38 55.2


line stmt bran cond sub pod time code
1             package Finance::GDAX::API::Position;
2             our $VERSION = '0.01';
3 1     1   23191 use 5.20.0;
  1         3  
4 1     1   5 use warnings;
  1         1  
  1         24  
5 1     1   438 use Moose;
  1         493057  
  1         7  
6 1     1   9613 use Finance::GDAX::API;
  1         3  
  1         40  
7 1     1   8 use namespace::autoclean;
  1         1  
  1         8  
8              
9             extends 'Finance::GDAX::API';
10              
11             has 'repay_only' => (is => 'rw',
12             isa => 'Bool',
13             );
14              
15             sub get {
16 0     0 1   my $self = shift;
17 0           $self->method('GET');
18 0           $self->path('/position');
19 0           return $self->send;
20             }
21              
22             sub close {
23 0     0 1   my $self = shift;
24 0           $self->method('POST');
25 0           $self->path('/position/close');
26 0 0         if (defined $self->repay_only) {
27 0 0         my $r = $self->repay_only ? 'true' : 'false';
28 0           $self->body({ repay_only => $r });
29             }
30 0           return $self->send;
31             }
32             __PACKAGE__->meta->make_immutable;
33             1;
34              
35             =head1 NAME
36              
37             Finance::GDAX::API::Position - Overview of profile
38              
39             =head1 SYNOPSIS
40              
41             use Finance::GDAX::API::Position;
42              
43             $overview = Finance::GDAX::API::Position->new;
44              
45             # Hash of profile positions
46             $positions = $overview->get;
47              
48             =head2 DESCRIPTION
49              
50             Returns an overview of profile information in hash form, including
51             profile status, funding (margin), accounts, margin call, etc, as
52             documented in the "get" method.
53              
54             Also there is a "close" method which is left mostly undescribed in the
55             API as to what it actually does, but is included here for fun and
56             danger.
57              
58             =head1 ATTRIBUTES
59              
60             =head2 C<repay_only> $boolean
61              
62             This attribute is associated with the "close" method and is
63             boolean. The GDAX API docs do not say what it does or means, nor if it
64             is required.
65              
66             =head1 METHODS
67              
68             =head2 C<get>
69              
70             Returns a hash representing an overview of the position/account
71             information.
72              
73             The API documents the hash structure as follows:
74              
75             {
76             "status": "active",
77             "funding": {
78             "max_funding_value": "10000",
79             "funding_value": "622.48199522418175",
80             "oldest_outstanding": {
81             "id": "280c0a56-f2fa-4d3b-a199-92df76fff5cd",
82             "order_id": "280c0a56-f2fa-4d3b-a199-92df76fff5cd",
83             "created_at": "2017-03-18T00:34:34.270484Z",
84             "currency": "USD",
85             "account_id": "202af5e9-1ac0-4888-bdf5-15599ae207e2",
86             "amount": "545.2400000000000000"
87             }
88             },
89             "accounts": {
90             "USD": {
91             "id": "202af5e9-1ac0-4888-bdf5-15599ae207e2",
92             "balance": "0.0000000000000000",
93             "hold": "0.0000000000000000",
94             "funded_amount": "622.4819952241817500",
95             "default_amount": "0"
96             },
97             "BTC": {
98             "id": "1f690a52-d557-41b5-b834-e39eb10d7df0",
99             "balance": "4.7051564815292853",
100             "hold": "0.6000000000000000",
101             "funded_amount": "0.0000000000000000",
102             "default_amount": "0"
103             }
104             },
105             "margin_call": {
106             "active": true,
107             "price": "175.96000000",
108             "side": "sell",
109             "size": "4.70515648",
110             "funds": "624.04210048"
111             },
112             "user_id": "521c20b3d4ab09621f000011",
113             "profile_id": "d881e5a6-58eb-47cd-b8e2-8d9f2e3ec6f6",
114             "position": {
115             "type": "long",
116             "size": "0.59968368",
117             "complement": "-641.91999958602800000000000000",
118             "max_size": "1.49000000"
119             },
120             "product_id": "BTC-USD"
121             }
122              
123             The structure is explained in the API thusly:
124              
125             =head3 Status
126              
127             =over
128              
129             The status of the profile. If active, the profile can be used for
130             trading. If pending, the profile is currently being created. If
131             locked, the profile is undergoing a rebalance. If default, you were
132             not able repay funding after a margin call or expired funding and now
133             have a default.
134              
135             =back
136              
137             =head3 Funding [margin]
138              
139             =over
140              
141             Holds details about the open/outstanding fundings taken out in the
142             margin profile.
143              
144             funding_value is the value of all outstanding fundings in USD. This
145             value is updated every time you draw or repay funding.
146              
147             max_funding_value is maximum value of fundings in USD that you can
148             have oustanding. This value can restrict you from drawing more
149             funding.
150              
151             oldest_outstanding is the oldest funding record you have
152             outstanding. This is important as funding can only remain outstanding
153             for 27 days and 22 hours before being automatically closed and
154             settled. It is recommended that you manually settle or claim the
155             funding before it expires.
156              
157             =back
158              
159             =head3 Accounts
160              
161             =over
162              
163             The accounts in the profile indexed by their currency.
164              
165             =back
166              
167             =head3 Margin Call [margin]
168              
169             =over
170              
171             Holds details about the resting margin call. To attempt to ensure you
172             can repay funding we place a hidden stop like order on the book. When
173             the last trade price hits or goes past price the margin call will
174             trigger issuing a market order to rebalance your profile so each
175             account has enough funds to repay all outstanding funding records.
176              
177             If each account's balance is large enough to repay the its
178             funded_amount, active will be false signifying your profile does not
179             have a resting margin call.
180              
181             =back
182              
183             =head2 C<close>
184              
185             The GDAX API docs do not currently say what this method does. But it
186             does have an either optional or required attribute that can be set,
187             "repay_only".
188              
189             =cut
190              
191              
192             =head1 AUTHOR
193              
194             Mark Rushing <mark@orbislumen.net>
195              
196             =head1 COPYRIGHT AND LICENSE
197              
198             This software is copyright (c) 2017 by Home Grown Systems, SPC.
199              
200             This is free software; you can redistribute it and/or modify it under
201             the same terms as the Perl 5 programming language system itself.
202              
203             =cut
204