Finance-Alpaca
view release on metacpan or search on metacpan
lib/Finance/Alpaca/Cookbook.pod view on Meta::CPAN
=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),
limit => 5
);
# See how much AAPL moved in that timeframe
my $start_price = $bars{AAPL}[0]->open;
my $end_price = $bars{AAPL}[-1]->close;
my $percent_change = ( $end_price - $start_price ) / $start_price * 100;
CORE::say sprintf 'AAPL moved %f%% over the last 5 days.', $percent_change;
=head1 Order Examples
This section contains examples of some of the things you can do with order
objects through our API. For additional help understanding different types of
orders and how they behave once theyâre placed, please see the L<order
page|https://alpaca.markets/docs/trading-on-alpaca/orders/>.
=head2 Place New Orders
Submit a market order to buy 1 share of Apple at market price.
$alpaca->create_order(
symbol => 'AAPL',
qty => 1,
side => 'buy',
type => 'market',
time_in_force => 'day'
);
Submit a limit order to attempt to sell 1 share of AMD at a particular price
(C<$20.50>) when the market opens.
$alpaca->create_order(
symbol => 'AMD',
qty => 1,
side => 'sell',
type => 'limit',
time_in_force => 'opg',
limit_price => 20.50,
);
=head2 Submit Short Orders
( run in 0.980 second using v1.01-cache-2.11-cpan-a5162978ef8 )