Finance-Robinhood

 view release on metacpan or  search on metacpan

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

package Finance::Robinhood::Account;
use 5.010;
use strict;
use warnings;
use Carp;
our $VERSION = "0.21";
use Moo;
use JSON::Tiny qw[decode_json];
use strictures 2;
use namespace::clean;
#
has $_ => (is => 'ro', required => 1, writer => "_set_$_")
    for (
     qw[account_number buying_power cash cash_available_for_withdrawal
     cash_held_for_orders deactivated deposit_halted margin_balances
     max_ach_early_access_amount only_position_closing_trades sma
     sma_held_for_orders sweep_enabled type uncleared_deposits unsettled_funds
     withdrawal_halted]
    );
has $_ =>
    (is => 'bare', required => 1, writer => "_set_$_", reader => "_get_$_")
    for (qw[url]);
has $_ => (is       => 'ro',
           required => 1,
           coerce   => \&Finance::Robinhood::_2_datetime
) for (qw[updated_at]);
has $_ => (is => 'bare', required => 1, accessor => "_get_$_", weak_ref => 1)
    for (qw[rh]);

sub positions {
    my ($self, $type) = @_;
    my ($status, $result, $raw) = $self->_get_rh()->_send_request(
        'GET',
        sprintf(Finance::Robinhood::endpoint('accounts/positions'),
                $self->account_number()
            )
            . sub {
            my $opt = shift;
            return '' if !ref $opt || ref $type ne 'HASH';
            return '?cursor=' . $opt->{cursor} if defined $opt->{cursor};
            return '?nonzero=' . ($opt->{nonzero} ? 'true' : 'false')
                if defined $opt->{nonzero};
            return '';
        }
            ->($type)
    );
    return
        Finance::Robinhood::_paginate($self->_get_rh(), $result,
                                      'Finance::Robinhood::Position');
}

sub portfolio {
    my ($self) = @_;
    my ($status, $result, $raw)
        = $self->_get_rh()->_send_request('GET',
                                    Finance::Robinhood::endpoint('portfolios')
                                        . $self->account_number()
                                        . '/');
    return $result;
}

sub historicals {
    my ($self, $interval, $span) = @_;
    my ($status, $result, $raw)
        = $self->_get_rh()->_send_request('GET',
                        Finance::Robinhood::endpoint('portfolios/historicals')
                            . $self->account_number()
                            . "/?interval=$interval&span=$span");
    return () if $status != 200;
    for (@{$result->{equity_historicals}}) {
        $_->{begins_at} = Finance::Robinhood::_2_datetime($_->{begins_at});
    }
    return $result;
}
1;

=encoding utf-8

=head1 NAME

Finance::Robinhood::Account - Single Robinhood Trade Account

=head1 SYNOPSIS

    use Finance::Robinhood;

    my $rh = Finance::Robinhood->new( token => ... );
    my $account = $rh->accounts()->{results}[0];

=head1 DESCRIPTION

This class represents a single account. Objects are usually created by
Finance::Robinhood's C<accounts( ... )> method rather than directly.

=head1 METHODS

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

=head2 C<portfolio( )>

Gets a quick rundown of the account's financial standing. Results are returned
as a hash with the following keys:

    adjusted_equity_previous_close      Total balance as of previous close +/- after hours trading
    equity                              Total balance
    equity_previous_close               Total balance as of previous close
    excess_margin
    extended_hours_equity               Total valance including after hours trading
    extended_hours_market_value         Market value of securities including after hours trading
    last_core_equity                    Total balance
    last_core_market_value              Market value of securities
    market_value                        Marekt value of securities

=head2 C<historicals( ... )>

    $account->historicals( '5minute', 'day' );

Returns historical data about your portfolio. The first argument is an interval
time and must be either C<5minute>, C<10minute>, C<day>, or C<week>.

The second argument is a span of time indicating how far into the past you
would like to retrieve and may be one of the following: C<day>, C<week>,
C<year>, or C<5year>.

Results are returned as a hash with the following keys:



( run in 1.087 second using v1.01-cache-2.11-cpan-54e63673c56 )