Finance-Alpaca

 view release on metacpan or  search on metacpan

MANIFEST  view on Meta::CPAN

lib/Finance/Alpaca/Struct/Trade.pm
lib/Finance/Alpaca/Struct/TradeActivity.pm
lib/Finance/Alpaca/Struct/TradeUpdate.pm
lib/Finance/Alpaca/Struct/Watchlist.pm
lib/Finance/Alpaca/TradeStream.pm
lib/Finance/Alpaca/Types.pm
minil.toml
t/00_compile.t
t/01_account.t
t/02_clock.t
t/03_calendar.t
t/04_assets.t
t/05_orders.t
t/06_bars.t
t/07_quotes.t
t/08_trades.t
t/09_orders.t
t/10_positions.t
t/11_portfolio.t
t/12_watchlists.t
t/13_configuration.t

README.md  view on Meta::CPAN

    say sprintf
        $clock->timestamp->strftime('It is %l:%M:%S %p on a %A and the market is %%sopen!'),
        $clock->is_open ? '' : 'not ';

Returns a [Finance::Alpaca::Struct::Clock](https://metacpan.org/pod/Finance%3A%3AAlpaca%3A%3AStruct%3A%3AClock) object.

The clock endpoint serves the current market timestamp, whether or not the
market is currently open, as well as the times of the next market open and
close.

## `calendar( [...] )`

        my @days = $camelid->calendar(
            start => Time::Moment->now,
            end   => Time::Moment->now->plus_days(14)
        );
        for my $day (@days) {
            say sprintf '%s the market opens at %s Eastern',
                $day->date, $day->open;
        }

Returns a list of [Finance::Alpaca::Struct::Calendar](https://metacpan.org/pod/Finance%3A%3AAlpaca%3A%3AStruct%3A%3ACalendar) objects.

The calendar endpoint serves the full list of market days from 1970 to 2029.

The following parameters are accepted:

- `start` - The first date to retrieve data for (inclusive); Time::Moment object or RFC3339 string
- `end` - The last date to retrieve data for (inclusive); Time::Moment object or RFC3339 string

Both listed parameters are optional. If neither is provided, the calendar will
begin on January 1st, 1970.

## `assets( [...] )`

    say $_->symbol
        for sort { $a->symbol cmp $b->symbol } @{ $camelid->assets( status => 'active' ) };

Returns a list of [Finance::Alpaca::Struct::Asset](https://metacpan.org/pod/Finance%3A%3AAlpaca%3A%3AStruct%3A%3AAsset) objects.

The assets endpoint serves as the master list of assets available for trade and

lib/Finance/Alpaca.pm  view on Meta::CPAN

        $tx = $s->ua->start($tx);
        return to_Account( $tx->result->json );
    }

    sub clock ($s) {
        my $tx = $s->ua->build_tx( GET => $s->endpoint . '/v2/clock' );
        $tx = $s->ua->start($tx);
        return to_Clock( $tx->result->json );
    }

    sub calendar ( $s, %params ) {
        my $params = '';
        $params .= '?' . join '&', map {
            $_ . '='
                . ( ref $params{$_} eq 'Time::Moment' ? $params{$_}->to_string() : $params{$_} )
        } keys %params if keys %params;
        my $tx = $s->ua->build_tx( GET => $s->endpoint . '/v2/calendar' . $params );
        $tx = $s->ua->start($tx);
        return @{ ( ArrayRef [Calendar] )->assert_coerce( $tx->result->json ) };
    }

    sub assets ( $s, %params ) {
        my $params = '';
        $params .= '?' . join '&', map {
            $_ . '='
                . ( ref $params{$_} eq 'Time::Moment' ? $params{$_}->to_string() : $params{$_} )
        } keys %params if keys %params;

lib/Finance/Alpaca.pm  view on Meta::CPAN

    say sprintf
        $clock->timestamp->strftime('It is %l:%M:%S %p on a %A and the market is %%sopen!'),
        $clock->is_open ? '' : 'not ';

Returns a L<Finance::Alpaca::Struct::Clock> object.

The clock endpoint serves the current market timestamp, whether or not the
market is currently open, as well as the times of the next market open and
close.

=head2 C<calendar( [...] )>

        my @days = $camelid->calendar(
            start => Time::Moment->now,
            end   => Time::Moment->now->plus_days(14)
        );
        for my $day (@days) {
            say sprintf '%s the market opens at %s Eastern',
                $day->date, $day->open;
        }

Returns a list of L<Finance::Alpaca::Struct::Calendar> objects.

The calendar endpoint serves the full list of market days from 1970 to 2029.

The following parameters are accepted:

=over

=item C<start> - The first date to retrieve data for (inclusive); Time::Moment object or RFC3339 string

=item C<end> - The last date to retrieve data for (inclusive); Time::Moment object or RFC3339 string

=back

Both listed parameters are optional. If neither is provided, the calendar will
begin on January 1st, 1970.

=head2 C<assets( [...] )>

    say $_->symbol
        for sort { $a->symbol cmp $b->symbol } @{ $camelid->assets( status => 'active' ) };

Returns a list of L<Finance::Alpaca::Struct::Asset> objects.

The assets endpoint serves as the master list of assets available for trade and

lib/Finance/Alpaca/Cookbook.pod  view on Meta::CPAN


You can check if the market is open now, or view what times the market will be
open or closed on a particular date.

    # Check if the market is open now
    my $clock = $alpaca->clock;
    CORE::say 'The market is ' . ( $clock->is_open ? 'open!' : 'closed.' );

    # Check when the market was open on Dec. 1, 2018
    my $date = Time::Moment->new( year => 2018, month => 12, day => 1 );
    my ($day) = $alpaca->calendar( start => $date, end => $date );
    CORE::say sprintf 'The market opened at %s and closed at %s on %s.', $day->open, $day->close,
        $day->date;

=head1 Market Data Examples

Alpaca provides market data from various sources.

=head2 Get Historical Price and Volume Data

Using the C<bars( )> method, you can see what a stock price was at a particular

lib/Finance/Alpaca/Struct/Calendar.pm  view on Meta::CPAN


=encoding utf-8

=head1 NAME

Finance::Alpaca::Struct::Calendar - A Single Calendar Date Object

=head1 SYNOPSIS

    use Finance::Alpaca;
    my @days = $camelid->calendar(
        start => Time::Moment->now,
        end   => Time::Moment->now->plus_days(14)
    );
    for my $day (@days) {
        say sprintf '%s the market opens at %s Eastern',
            $day->date, $day->open;
    }

=head1 DESCRIPTION

The calendar endpoint serves the full list of market days from 1970 to 2029. It
can also be queried by specifying a start and/or end time to narrow down the
results. In addition to the dates, the response also contains the specific open
and close times for the market days, taking into account early closures.

=head1 Properties

A calendar day contains the following properties:

    $day->date();

=over

=item C<date> - Date string in "%Y-%m-%d" format

=item C<open> - The time the market opens at on this date in "%H:%M" format

=item C<close> - The time the market closes at on this date in "%H:%M" format

lib/Finance/Alpaca/Struct/Calendar.pm  view on Meta::CPAN

This library is free software; you can redistribute it and/or modify it under
the terms found in the Artistic License 2. Other copyrights, terms, and
conditions may apply to data transmitted through this module.

=head1 AUTHOR

Sanko Robinson E<lt>sanko@cpan.orgE<gt>

=cut

# https://alpaca.markets/docs/api-documentation/api-v2/calendar/

t/03_calendar.t  view on Meta::CPAN

use strict;
use Test2::V0;
use lib '../lib', './lib';
#
use Finance::Alpaca;
my $alpaca = Finance::Alpaca->new(
    paper => 1,
    keys  => [ 'PKZBFZQFCKV2QLTVIGLA', 'HD4LPxBHTUTjwxR6SBeOX1rIiWHRHPDdbv7n2pI0' ]
);
my @days = $alpaca->calendar(
    start => '2021-04-14T16:20:00Z',
    end   => '2021-04-28T16:40:00Z'
);
isa_ok( $days[0], 'Finance::Alpaca::Struct::Calendar' );
done_testing;
1;



( run in 0.938 second using v1.01-cache-2.11-cpan-5dc5da66d9d )