lib/Finance/Robinhood/Equity/Order.pm

filter: Statementstmt 34.2% 36 / 105 Branchbran 0.0% 0 / 40 Conditioncond 19.5% 8 / 41 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 60.0% 18 / 30 Podpod 100% 10 / 10 totaltotal 28.0% 72 / 257 CC 37 SCAR 29.7
CC
37
Cov
24%
_test_executions
66.2
_test_can_cancel
51.5
cancel
30.0
CRAP 19.4
« lib/Finance/Robinhood/Forex/Order.pm ↑ Summary lib/Finance/Robinhood.pm »
1 package Finance::Robinhood::Equity::Order;
2  
3 =encoding utf-8
4  
5 =for stopwords watchlist watchlists untradable urls
6  
7 =head1 NAME
8  
9 Finance::Robinhood::Equity::Order - Represents a Single Equity Order
10  
11 =head1 SYNOPSIS
12  
13 use Finance::Robinhood;
14 my $rh = Finance::Robinhood->new->login('user', 'pass');
15 my $orders = $rh->equity_orders();
16  
17 for my $order ($orders->all) {
18 $order->cancel if $order->can_cancel;
19 }
20  
21 =head1 METHOD
22  
23 =cut
24  
25 1 our $VERSION = '0.92_003';
26 1 ▶use Mojo::Base-base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Finance::Robinhood::Equity::Account;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Finance::Robinhood::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Finance::Robinhood::Equity::Position;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Finance::Robinhood::Equity::Order::Execution;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 ▶sub _test__init {
Subroutine _test__init: called CC=1 SCAR=3.5
34 1 my $rh = t::Utility::rh_instance(1);
35 0 my $order = $rh->equity_orders->current;
36 0 isa_ok($order, __PACKAGE__);
37 0 t::Utility::stash('ORDER', $order); # Store it for later
38 }
39 0 ▶use overload '""' => sub ($s, @) { $s->{url} }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
40  
41 1 ▶sub _test_stringify {
Subroutine _test_stringify: called CC=2 SCAR=9.2
42 1 ▶ t::Utility::stash('ORDER') // skip_all();
Condition50%1/2
t::Utility::stash("ORDER") // skip_all()
l!l
01
MC/DC0%0/2
t::Utility::stash("ORDER") // skip_all()
t::Utility::stash("ORDER") skip_all()
Truth table33%1/3
A // B
A= t::Utility::stash("ORDER")B= skip_all()
ABresult
1X1
011
000
43 0 like(+t::Utility::stash('ORDER'),
44 qr'https://api.robinhood.com/orders/.+/');
45 }
46 #
47 1 has _rh => undef => weak => 1;
48 1 has ['average_price', 'cumulative_quantity',
49 'extended_hours', 'fees',
50 'id', 'override_day_trade_checks',
51 'override_dtbp_checks', 'price',
52 'quantity', 'ref_id',
53 'reject_reason', 'response_category',
54 'side', 'state',
55 'stop_price', 'time_in_force',
56 'trigger', 'type',
57 'url'
58 ];
59  
60 =head2 C<can_cancel( )>
61  
62 Returns true if the order can be cancelled.
63  
64 =cut
65  
66 0 ▶sub can_cancel ($s) { defined $s->{cancel} ? !0 : !1 }
Subroutine can_cancel: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $s->{'cancel'} ? : 0 0 0
67  
68 1 ▶sub _test_can_cancel {
Subroutine _test_can_cancel: called CC=7 SCAR=39.4
69 1 my $rh = t::Utility::rh_instance(1);
70 0 my $orders = $rh->equity_orders;
71 0 my ($filled, $cancelled);
72 0 ▶ while ($orders->has_next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orders->has_next) 0 0 0
73 0 my $order = $orders->next;
74  
75 #$filled = $order if $order->state eq 'queued';
76 0 ▶ $cancelled = $order if $order->state eq 'cancelled';
Branch: 1 branch
BranchTrueFalseTotal
if $order->state eq "cancelled" 0 0 0
77 0 ▶ last if $filled && $cancelled;
Branch: 1 branch
BranchTrueFalseTotal
if $filled and $cancelled 0 0 0
Condition0%0/3
$filled and $cancelled
!ll&&!rl&&r
000
MC/DC0%0/2
$filled and $cancelled
$filled $cancelled
Truth table0%0/3
A and B
A= $filledB= $cancelled
ABresult
0X0
100
111
78 }
79 SKIP: {
80 0 skip('I need to create a new equity order here', 1);
81 0 ▶ $filled // skip('Cannot find a filled equity order', 1);
Condition0%0/3
$filled // skip("Cannot find a filled equity order", 1)
l!l&&r!l&&!r
000
MC/DC0%0/2
$filled // skip("Cannot find a filled equity order", 1)
$filled skip("Cannot find a filled equity order", 1)
Truth table0%0/3
A // B
A= $filledB= skip("Cannot find a filled equity order", 1)
ABresult
1X1
011
000
82 0 my $execution = $filled->executions->[0];
83 0 isa_ok($execution, 'Finance::Robinhood::Equity::Order::Execution');
84 }
85 SKIP: {
86 0 ▶ $cancelled // skip('Cannot find a cancelled equity order', 1);
Condition0%0/3
$cancelled // skip("Cannot find a cancelled equity order", 1)
l!l&&r!l&&!r
000
MC/DC0%0/2
$cancelled // skip("Cannot find a cancelled equity order", 1)
$cancelled skip("Cannot find a cancelled equity order", 1)
Truth table0%0/3
A // B
A= $cancelledB= skip("Cannot find a cancelled equity order", 1)
ABresult
1X1
011
000
87 0 is($cancelled->can_cancel, !1, 'cancelled order cannot be cancelled');
88 }
89 todo(
90 "Place an order and test if it can be cancelled then cancel it, reload, and retest it"
91 0 ▶ => sub { pass('ugh') });
Subroutine __ANON__: not called CC=1 SCAR=6.9
92 }
93  
94 =head2 C<account( )>
95  
96 Returns the related Finance::Robinhood::Equity::Account object.
97  
98 =cut
99  
100 0 ▶sub account ($s) {
Subroutine account: not called CC=3 SCAR=24.8
Pod documented
101 0 my $res = $s->_rh->_get($s->{account});
102 $res->is_success
103 ? Finance::Robinhood::Equity::Account->new(_rh => $s->_rh,
104 0 ▶ %{$res->json})
Branch: 2 branches
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
$res->is_server_error ? : 0 0 0
105 : Finance::Robinhood::Error->new(
106 $res->is_server_error ? (details => $res->message) : $res->json);
107 }
108  
109 1 ▶sub _test_account {
Subroutine _test_account: called CC=2 SCAR=9.2
110 1 ▶ t::Utility::stash('ORDER') // skip_all('No order object in stash');
Condition50%1/2
t::Utility::stash("ORDER") // skip_all("No order object in stash")
l!l
01
MC/DC0%0/2
t::Utility::stash("ORDER") // skip_all("No order object in stash")
t::Utility::stash("ORDER") skip_all("No order object in stash")
Truth table33%1/3
A // B
A= t::Utility::stash("ORDER")B= skip_all("No order object in stash")
ABresult
1X1
011
000
111 0 isa_ok(t::Utility::stash('ORDER')->account,
112 'Finance::Robinhood::Equity::Account');
113 }
114  
115 =head2 C<position( )>
116  
117 Returns the related Finance::Robinhood::Equity::Position object.
118  
119 =cut
120  
121 0 ▶sub position ($s) {
Subroutine position: not called CC=3 SCAR=24.8
Pod documented
122 0 my $res = $s->_rh->_get($s->{position});
123 $res->is_success
124 ? Finance::Robinhood::Equity::Position->new(_rh => $s->_rh,
125 0 ▶ %{$res->json})
Branch: 2 branches
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
$res->is_server_error ? : 0 0 0
126 : Finance::Robinhood::Error->new(
127 $res->is_server_error ? (details => $res->message) : $res->json);
128 }
129  
130 1 ▶sub _test_position {
Subroutine _test_position: called CC=2 SCAR=9.2
131 1 ▶ t::Utility::stash('ORDER') // skip_all('No order object in stash');
Condition50%1/2
t::Utility::stash("ORDER") // skip_all("No order object in stash")
l!l
01
MC/DC0%0/2
t::Utility::stash("ORDER") // skip_all("No order object in stash")
t::Utility::stash("ORDER") skip_all("No order object in stash")
Truth table33%1/3
A // B
A= t::Utility::stash("ORDER")B= skip_all("No order object in stash")
ABresult
1X1
011
000
132 0 isa_ok(t::Utility::stash('ORDER')->position,
133 'Finance::Robinhood::Equity::Position');
134 }
135  
136 =head2 C<average_price( )>
137  
138 Average price per share for all executions so far.
139  
140 =head2 C<cancel( )>
141  
142 $order->cancel();
143  
144 If the order can be cancelled, this method will do it.
145  
146 Be aware that the order is still active for about a second after this is called
147 so add a 'smart' delay here and then call C<reload( )> to update the object
148 correctly.
149  
150 =cut
151  
152 0 ▶sub cancel ($s) {
Subroutine cancel: not called CC=5 SCAR=34.0
Pod documented
153 0 ▶ $s->can_cancel // return $s;
Condition0%0/2
$s->can_cancel // (return $s)
l!l
00
MC/DC0%0/1
$s->can_cancel // (return $s)
$s->can_cancel
Truth table0%0/2
A
A= $s->can_cancel
Aresult
11
00
154 0 my $res = $s->_rh->_post($s->{cancel});
155 0 ▶ $s->reload && return $s if $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_success 0 0 0
Condition0%0/5
$res->is_success && ($s->reload && (return $s))
!ll&&!rl&&r
000
$s->reload and return $s
!ll
00
MC/DC0%0/2
$res->is_success && ($s->reload && (return $s))
$res->is_success $s->reload
Truth table0%0/3
A && B
A= $res->is_successB= $s->reload
ABresult
0X0
100
111
156 0 ▶ Finance::Robinhood::Error->new(
Branch: 1 branch
BranchTrueFalseTotal
$res->is_server_error ? : 0 0 0
157 $res->is_server_error ? (details => $res->message) : $res->json);
158 }
159  
160 =head2 C<created_at( )>
161  
162 Returns a Time::Moment object.
163  
164 =cut
165  
166 0 ▶sub created_at ($s) {
Subroutine created_at: not called CC=1 SCAR=6.9
Pod documented
167 0 Time::Moment->from_string($s->{created_at});
168 }
169  
170 1 ▶sub _test_created_at {
Subroutine _test_created_at: called CC=2 SCAR=9.2
171 1 ▶ t::Utility::stash('ORDER') // skip_all('No order object in stash');
Condition50%1/2
t::Utility::stash("ORDER") // skip_all("No order object in stash")
l!l
01
MC/DC0%0/2
t::Utility::stash("ORDER") // skip_all("No order object in stash")
t::Utility::stash("ORDER") skip_all("No order object in stash")
Truth table33%1/3
A // B
A= t::Utility::stash("ORDER")B= skip_all("No order object in stash")
ABresult
1X1
011
000
172 0 isa_ok(t::Utility::stash('ORDER')->created_at, 'Time::Moment');
173 }
174  
175 =head2 C<cumulative_quantity( )>
176  
177 Number of shares that have been bought or sold with this order's execution(s)
178 so far.
179  
180 =head2 C<executions( )>
181  
182 Returns a list of related Finance::Robinhood::Equity::Order::Execution objects
183 if applicable.
184  
185 =cut
186  
187 0 ▶sub executions ($s) {
Subroutine executions: not called CC=1 SCAR=6.9
Pod documented
188 map {
189 Finance::Robinhood::Equity::Order::Execution->new(_rh => $s->_rh,
190 0 %{$_})
191 0 } @{$s->{executions}};
192 }
193  
194 1 ▶sub _test_executions {
Subroutine _test_executions: called CC=8 SCAR=41.9
195 1 my $rh = t::Utility::rh_instance(1);
196 0 my $orders = $rh->equity_orders;
197 0 my ($filled, $rejected);
198 0 ▶ while ($orders->has_next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orders->has_next) 0 0 0
199 0 my $order = $orders->next;
200 0 ▶ $filled = $order if $order->state eq 'filled';
Branch: 1 branch
BranchTrueFalseTotal
if $order->state eq "filled" 0 0 0
201 0 ▶ $rejected = $order if $order->state eq 'rejected';
Branch: 1 branch
BranchTrueFalseTotal
if $order->state eq "rejected" 0 0 0
202 0 ▶ last if $filled && $rejected;
Branch: 1 branch
BranchTrueFalseTotal
if $filled and $rejected 0 0 0
Condition0%0/3
$filled and $rejected
!ll&&!rl&&r
000
MC/DC0%0/2
$filled and $rejected
$filled $rejected
Truth table0%0/3
A and B
A= $filledB= $rejected
ABresult
0X0
100
111
203 }
204 SKIP: {
205 0 ▶ $filled // skip('Cannot find a filled equity order', 1);
Condition0%0/3
$filled // skip("Cannot find a filled equity order", 1)
l!l&&r!l&&!r
000
MC/DC0%0/2
$filled // skip("Cannot find a filled equity order", 1)
$filled skip("Cannot find a filled equity order", 1)
Truth table0%0/3
A // B
A= $filledB= skip("Cannot find a filled equity order", 1)
ABresult
1X1
011
000
206 0 my ($execution) = $filled->executions;
207 0 isa_ok($execution, 'Finance::Robinhood::Equity::Order::Execution');
208 }
209 SKIP: {
210 0 ▶ $rejected // skip('Cannot find a rejected equity order', 1);
Condition0%0/3
$rejected // skip("Cannot find a rejected equity order", 1)
l!l&&r!l&&!r
000
MC/DC0%0/2
$rejected // skip("Cannot find a rejected equity order", 1)
$rejected skip("Cannot find a rejected equity order", 1)
Truth table0%0/3
A // B
A= $rejectedB= skip("Cannot find a rejected equity order", 1)
ABresult
1X1
011
000
211 0 is([$rejected->executions], [], 'rejected order has no executions');
212 }
213 }
214  
215 =head2 C<extended_hours( )>
216  
217 Returns a true value if this order is set to execute during extended hours and
218 pre-market.
219  
220 =head2 C<fees( )>
221  
222 Fees charged by Robinhood. Always C<0.00>.
223  
224 =head2 C<id( )>
225  
226 UUID used to identify this specific order.
227  
228 =head2 C<instrument( )>
229  
230 Returns the related Finance::Robinhood::Equity::Instrument object.
231  
232 =cut
233  
234 0 ▶sub instrument($s) {
Subroutine instrument: not called CC=3 SCAR=24.8
Pod documented
235 0 my $res = $s->_rh->_get($s->{instrument});
236 $res->is_success
237 ? Finance::Robinhood::Equity::Instrument->new(_rh => $s->_rh,
238 0 ▶ %{$res->json})
Branch: 2 branches
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
$res->is_server_error ? : 0 0 0
239 : Finance::Robinhood::Error->new(
240 $res->is_server_error ? (details => $res->message) : $res->json);
241 }
242  
243 1 ▶sub _test_instrument {
Subroutine _test_instrument: called CC=2 SCAR=9.2
244 1 ▶ t::Utility::stash('ORDER') // skip_all('No order object in stash');
Condition50%1/2
t::Utility::stash("ORDER") // skip_all("No order object in stash")
l!l
01
MC/DC0%0/2
t::Utility::stash("ORDER") // skip_all("No order object in stash")
t::Utility::stash("ORDER") skip_all("No order object in stash")
Truth table33%1/3
A // B
A= t::Utility::stash("ORDER")B= skip_all("No order object in stash")
ABresult
1X1
011
000
245 0 isa_ok(t::Utility::stash('ORDER')->instrument,
246 'Finance::Robinhood::Equity::Instrument');
247 }
248  
249 =head2 C<last_transaction_at( )>
250  
251 Returns a Time::Moment object if applicable.
252  
253 =cut
254  
255 0 ▶sub last_transaction_at($s) {
Subroutine last_transaction_at: not called CC=2 SCAR=17.9
Pod documented
256 defined $s->{last_transaction_at}
257 ? Time::Moment->from_string($s->{last_transaction_at})
258 0 ▶ : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $s->{'last_transaction_at'} ? : 0 0 0
259 }
260  
261 1 ▶sub _test_last_transaction_at {
Subroutine _test_last_transaction_at: called CC=3 SCAR=19.2
262 1 ▶ t::Utility::stash('ORDER') // skip_all('No order in stash');
Condition50%1/2
t::Utility::stash("ORDER") // skip_all("No order in stash")
l!l
01
MC/DC0%0/2
t::Utility::stash("ORDER") // skip_all("No order in stash")
t::Utility::stash("ORDER") skip_all("No order in stash")
Truth table33%1/3
A // B
A= t::Utility::stash("ORDER")B= skip_all("No order in stash")
ABresult
1X1
011
000
263 0 my $last_transaction_at = t::Utility::stash('ORDER')->last_transaction_at;
264 0 ▶ skip_all('No transactions... goodbye') if !defined $last_transaction_at;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $last_transaction_at 0 0 0
265 0 isa_ok($last_transaction_at, 'Time::Moment');
266 }
267  
268 =head2 C<override_day_trade_checks( )>
269  
270 A boolean value indicating whether this order bypassed PDT checks.
271  
272 =head2 C<override_dtbp_checks( )>
273  
274 A boolean value indicating whether this order bypassed day trade buying power
275 checks.
276  
277 Risky Regulation T violation warning!
278  
279 =head2 C<price( )>
280  
281 The price used as an exact limit (for limit and stop-limit orders) or as the
282 price to collar from (for market and stop-loss orders).
283  
284 =head2 C<quantity( )>
285  
286 Returns the total number of shares in this order.
287  
288 =head2 C<ref_id( )>
289  
290 Client generated UUID is returned.
291  
292 =head2 C<reject_reason( )>
293  
294 If the order was rejected, this will be filled with the reason.
295  
296 =head2 C<response_category( )>
297  
298 Returns the response category if applicable.
299  
300 =head2 C<side( )>
301  
302 Indicates if this is an order to C<buy> or C<sell>.
303  
304 =head2 C<state( )>
305  
306 One of the following:
307  
308 =over
309  
310 =item C<queued>
311  
312 =item C<new>
313  
314 =item C<unconformed>
315  
316 =item C<confirmed>
317  
318 =item C<partially_filled>
319  
320 =item C<filled>
321  
322 =item C<rejected>
323  
324 =item C<canceled>
325  
326 =item C<failed>
327  
328 =item C<voided>
329  
330 =back
331  
332 =head2 C<stop_price( )>
333  
334 Returns a float if the trigger type is C<stop>, otherwise, C<undef>.
335  
336 =head2 C<time_in_force( )>
337  
338 Returns the Time-in-Force value. C<gfd> (good for day) or C<gtc> (good 'til
339 cancelled).
340  
341 =head2 C<trigger( )>
342  
343 Returns the trigger. C<stop> or C<immediate>.
344  
345 =head2 C<type( )>
346  
347 Returns the order type. C<limit> or C<market>.
348  
349 =head2 C<updated_at( )>
350  
351 Returns a Time::Moment object.
352  
353 =cut
354  
355 0 ▶sub updated_at($s) {
Subroutine updated_at: not called CC=1 SCAR=6.9
Pod documented
356 0 Time::Moment->from_string($s->{updated_at});
357 }
358  
359 1 ▶sub _test_updated_at {
Subroutine _test_updated_at: called CC=2 SCAR=9.2
360 1 ▶ t::Utility::stash('ORDER') // skip_all('No order object in stash');
Condition50%1/2
t::Utility::stash("ORDER") // skip_all("No order object in stash")
l!l
01
MC/DC0%0/2
t::Utility::stash("ORDER") // skip_all("No order object in stash")
t::Utility::stash("ORDER") skip_all("No order object in stash")
Truth table33%1/3
A // B
A= t::Utility::stash("ORDER")B= skip_all("No order object in stash")
ABresult
1X1
011
000
361 0 isa_ok(t::Utility::stash('ORDER')->updated_at, 'Time::Moment');
362 }
363  
364 =head2 C<reload( )>
365  
366 $order->reload();
367  
368 Reloads the data for this order from the API server.
369  
370 Use this if you think the status or some other info might have changed.
371  
372 =cut
373  
374 0 ▶sub reload($s) {
Subroutine reload: not called CC=3 SCAR=24.8
Pod documented
375 0 my $res = $s->_rh->_get($s->{url});
376 $_[0]
377 = $res->is_success
378 ? Finance::Robinhood::Equity::Order->new(_rh => $s->_rh,
379 0 ▶ %{$res->json})
Branch: 2 branches
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
$res->is_server_error ? : 0 0 0
380 : Finance::Robinhood::Error->new(
381 $res->is_server_error ? (details => $res->message) : $res->json);
382 }
383  
384 1 ▶sub _test_reload {
Subroutine _test_reload: called CC=2 SCAR=10.5
385 1 ▶ t::Utility::stash('ORDER') // skip_all('No order object in stash');
Condition50%1/2
t::Utility::stash("ORDER") // skip_all("No order object in stash")
l!l
01
MC/DC0%0/2
t::Utility::stash("ORDER") // skip_all("No order object in stash")
t::Utility::stash("ORDER") skip_all("No order object in stash")
Truth table33%1/3
A // B
A= t::Utility::stash("ORDER")B= skip_all("No order object in stash")
ABresult
1X1
011
000
386 0 t::Utility::stash('ORDER')->reload;
387 0 isa_ok(t::Utility::stash('ORDER'), 'Finance::Robinhood::Equity::Order');
388 }
389  
390 =head1 LEGAL
391  
392 This is a simple wrapper around the API used in the official apps. The author
393 provides no investment, legal, or tax advice and is not responsible for any
394 damages incurred while using this software. This software is not affiliated
395 with Robinhood Financial LLC in any way.
396  
397 For Robinhood's terms and disclosures, please see their website at
398 https://robinhood.com/legal/
399  
400 =head1 LICENSE
401  
402 Copyright (C) Sanko Robinson.
403  
404 This library is free software; you can redistribute it and/or modify it under
405 the terms found in the Artistic License 2. Other copyrights, terms, and
406 conditions may apply to data transmitted through this module. Please refer to
407 the L<LEGAL> section.
408  
409 =head1 AUTHOR
410  
411 Sanko Robinson E<lt>sanko@cpan.orgE<gt>
412  
413 =cut
414  
415 1 1;
« lib/Finance/Robinhood/Forex/Order.pm ↑ Summary lib/Finance/Robinhood.pm »