Finance-Robinhood

 view release on metacpan or  search on metacpan

lib/Finance/Robinhood/Portfolio.pm  view on Meta::CPAN

package Finance::Robinhood::Portfolio;
use 5.010;
use Carp;
our $VERSION = "0.21";
use Moo;
use strictures 2;
use namespace::clean;
#
sub BUILDARGS {
    my $class = shift;
    return @_ > 1 ?
        {@_}
        : {
          (rh => $_[0][0],
           %{  +Finance::Robinhood::_send_request(
                   $_[0][0],
                   'GET',
                   Finance::Robinhood::endpoint('portfolios') . $_[0][1] . '/'
               )
           }
          )
        };

    # if the scrape failed (bad id, etc.) let Moo error out :)
}
has $_ => (is => 'ro', required => 1)
    for (qw[adjusted_equity_previous_close equity equity_previous_close
         excess_maintenance excess_maintenance_with_uncleared_deposits
         excess_margin excess_margin_with_uncleared_deposits
         extended_hours_equity extended_hours_market_value last_core_equity
         last_core_market_value market_value unwithdrawable_deposits
         url withdrawable_amount]
    );
has $_ => (is       => 'ro',
           required => 1,
           coerce   => \&Finance::Robinhood::_2_datetime
) for (qw[start_date]);
has $_ => (
    is       => 'bare',
    accessor => "_get_$_",
    weak_ref => 1,
    required => 1,

    #lazy     => 1,
    #builder  => sub { shift->account()->_get_rh() }
) for (qw[rh]);
has $_ => (is => 'bare', required => 1, accessor => "_get_$_")
    for (qw[account]);

sub account {
    my $self = shift;
    my $result
        = $self->_get_rh()->_send_request('GET', $self->_get_account());
    return $result
        ?
        Finance::Robinhood::Account->new(rh => $self->_get_rh, %$result)
        : ();
}

sub historicals {
    my ($self, $interval, $span) = @_;
    return
        scalar $self->_get_rh()->_send_request('GET',
                        Finance::Robinhood::endpoint('portfolios/historicals')
                            . $self->id
                            . "/?interval=$interval&span=$span");
}

sub id {
    my $_re = Finance::Robinhood::endpoint('portfolios') . '(.+)/';
    shift->url =~ m[$_re]o;
    $1;
}

sub refresh {
    return $_[0]
        = Finance::Robinhood::Portfolio->new([$_[0]->_get_rh, $_[0]->id]);
}
1;

=encoding utf-8

=head1 NAME

Finance::Robinhood::Portfolio - Current and Historical Financial Standing Information

=head1 SYNOPSIS

    use Finance::Robinhood::Robinhood;

    my $rh = Finance::Robinhood->new(token => ...);

    #
    my $portfolio = $rh->portfolios()->{results}[0];
    print 'I may withdraw $'. $portfolio->withdrawable_amount();

=head1 DESCRIPTION

This class represents a single financial portfolio. Objects are usually
created by Finance::Robinhood. If you're looking for information about your
portfolio, use
C<Finance::Robinhood-E<gt>portfolios()> to gather a list.

=head1 METHODS

This class has several getters and a few methods as follows...

=head2 C<adjusted_equity_previous_close( )>

=head2 C<equity( )>

=head2 C<equity_previous_close( )>

=head2 C<excess_maintenance( )>

=head2 C<excess_maintenance_with_uncleared_deposits( )>

=head2 C<excess_margin( )>

=head2 C<excess_margin_with_uncleared_deposits( )>

=head2 C<extended_hours_equity( )>

=head2 C<extended_hours_market_value( )>

=head2 C<historicals( ... )>

    # Snapshots of basic data for every five minutes of the previous week
    my $progress = $portfolio->historicals('10minute', 'week');



( run in 1.467 second using v1.01-cache-2.11-cpan-364913b4093 )