Finance-Alpaca
view release on metacpan or search on metacpan
lib/Finance/Alpaca/Cookbook.pod view on Meta::CPAN
=encoding utf-8
=head1 NAME
Finance::Alpaca::Cookbook - Simple Examples to Get You Started
=head1 Account Examples
=head2 View Account Information
You can see various information about your account, such as the amount of
buying power available or whether or not it has a L<PDT
flag|https://forum.alpaca.markets/t/general-trading/2339>.
my $account = $alpaca->account;
print 'Account is currently restricted from trading. ' if $account->trading_blocked;
printf '$%f is available as buying power', $account->buying_power;
=head2 View Gain/Loss of Portfolio
You can use the information from the account endpoint to do things like
calculating the daily profit or loss of your account.
my $account = $alpaca->account;
my $balance_change = $account->equity - $account->last_equity;
printf q[Today's portfolio balance change: $%.4f], $balance_change;
=head1 Assets Examples
Listed instruments can be gathered en mass or individually.
=head2 Get a List of Assets
The C<assets( )> method will return a list of US equities.
# Get a list of all active assets.
my @active_assets = $alpaca->assets( status => 'active' );
# Filter the assets down to just those that are NASDAQ listed.
my @nasdaq_assets = grep { $_->exchange eq 'NASDAQ' } @active_assets;
=head2 See If a Particular Asset is Tradable on Alpaca
my $asset = $alpaca->asset('AAPL');
say $asset->tradable ? 'We can trade AAPL' : 'Asset not found for AAPL';
=head1 Check Market Hours
As important as price data, knowing when and if the market is open is basic
info to consider.
=head2 See if the Market is Open
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
time.
# Get daily price data for AAPL over the last 5 trading days.
my %bars = $alpaca->bars(
symbol => 'AAPL',
timeframe => '1Min',
start => Time::Moment->now->with_hour(9)->minus_days(7),
end => Time::Moment->now->minus_minutes(20),
( run in 2.888 seconds using v1.01-cache-2.11-cpan-56fb94df46f )