PHPackages                             ragingprodigy/alpaca-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. ragingprodigy/alpaca-php

ActiveLibrary

ragingprodigy/alpaca-php
========================

Alpaca Trade API Client for PHP

1.1.2(4y ago)1123[3 PRs](https://github.com/ragingprodigy/Alpaca-PHP/pulls)Apache-2.0PHPPHP ^7.2

Since Jun 30Pushed 3y agoCompare

[ Source](https://github.com/ragingprodigy/Alpaca-PHP)[ Packagist](https://packagist.org/packages/ragingprodigy/alpaca-php)[ RSS](/packages/ragingprodigy-alpaca-php/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (4)Versions (7)Used By (0)

Alpaca-PHP
==========

[](#alpaca-php)

Alpaca Trade API for PHP [![Build Status](https://camo.githubusercontent.com/bfe06d73d2c3870ecb92ed1a09eede8cce8f54bb5ccd1b1f56543e107f48ada4/68747470733a2f2f7472617669732d63692e6f72672f726167696e6770726f646967792f416c706163612d5048502e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/ragingprodigy/Alpaca-PHP) [![Build Status](https://camo.githubusercontent.com/23c96fad34127caabb0428f49df50fa61a3788066bc020fc02b92337bef0ac13/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f2532302d636f6d706f7365722d79656c6c6f77677265656e2e737667)](https://packagist.org/packages/ragingprodigy/alpaca-php)

Overview
--------

[](#overview)

This is a PHP Client for [Alpaca](https://alpaca.markets/) ([General Alpaca API Documentation](https://docs.alpaca.markets/api-documentation/web-api/)). Alpaca API lets you build and trade with real-time market data for free.

### Table of Contents

[](#table-of-contents)

1. [Installation](#installation)
2. [Basic Usage](#basic-usage)
3. [Full Scale Example](#full-scale-example)
4. [Methods](#methods)

#### Installation

[](#installation)

The package can be installed via Composer.

```
composer require ragingprodigy/alpaca-php

```

#### Basic Usage

[](#basic-usage)

To use the Library, simply create an instance of the client as shown below:

```
use RagingProdigy\Alpaca\Client;
use RagingProdigy\Alpaca\Config;

// Paper Trading Client
$client = new Client(new Config('apiKey', 'secretKey'));

// Live Trading Client
$client = new Client(new Config('apiKey', 'secretKey', false));

// Overriding Trading and Data Api URLs
$client = new Client(new Config('apiKey', 'secretKey', true, 'https://trading.url/v2/', 'https://data.url/v1/'));
```

Under the Hood, the library makes use of the Guzzle Http client library to send HTTP requests. You can also provide a custom ClientInterface as the second argument to the `Client` constructor in order to further customize the GuzzleHttp Client.

You can then access the available APIs by calling:

```
$account = $client->getAccount(); // Returns an instance of "Ragingprodigy\Alpaca\Entities\Account"
$orders = $client->getOrders(); // Returns an array of "Ragingprodigy\Alpaca\Entities\Order" instances
```

#### Full Scale Example

[](#full-scale-example)

Here's how you can use the library to print out account information, submit a limit order, and print out bars.

```
use RagingProdigy\Alpaca\Client;
use RagingProdigy\Alpaca\Config;
use RagingProdigy\Alpaca\Constants\BarTimeFrame;
use RagingProdigy\Alpaca\Constants\DataStream;
use RagingProdigy\Alpaca\Constants\OrderAction;
use RagingProdigy\Alpaca\Constants\OrderType;
use RagingProdigy\Alpaca\Constants\TimeInForce;
use RagingProdigy\Alpaca\Entities\AccountUpdateEvent;
use RagingProdigy\Alpaca\Entities\OrderUpdateEvent;
use RagingProdigy\Alpaca\Entities\UpdateEvent;
use Ratchet\Client\WebSocket;

$alpacaClient = new Client(new Config('api.key', 'secret.key'));

// Listen for Updates
$alpacaClient->connectToStreams([DataStream::ACCOUNT_UPDATES, DataStream::TRADE_UPDATES], static function (WebSocket $webSocket, UpdateEvent $event = null) {
    if ($event instanceof OrderUpdateEvent) {
        echo "Order Update Event Received: $event\n";
    }

    if ($event instanceof AccountUpdateEvent) {
        echo "Account Update Event Received: $event\n";
    }
});

// Get Account Info
$account = $alpacaClient->getAccount();

echo "\nAccount Information: \n";
echo "\tCreated At: {$account->getCreatedAt()}\n";
echo "\tBuying Power: $ {$account->getBuyingPower()}\n";
echo "\tPortfolio Value: $ {$account->getPortfolioValue()}\n\n";

// Get Market Clock
$clock = $alpacaClient->getClock();

echo "Clock: \n";
echo "\tCurrent Time: {$clock->getTimestamp()}\n";
echo "\tMarket Next Open Time: {$clock->getNextOpen()}\n";
echo "\tMarket Next Close Time: {$clock->getNextClose()}\n\n";

// Request Purchase Order
$order = $alpacaClient->requestNewOrder(
    'AAPL',
    5,
    OrderAction::BUY,
    OrderType::MARKET,
    TimeInForce::DAY
);

echo "Market Order Response: \n";
echo "\tSymbol: {$order->getSymbol()}\n";
echo "\tClient Order Id: {$order->getClientOrderId()}\n";
echo "\tQuantity: {$order->getQuantity()}\n";
echo "\tType: {$order->getType()}\n";
echo "\tCreated At: {$order->getCreatedAt()}\n\n";

// Find Order By ID
$marketOrder = $alpacaClient->getOrder($order->getId());

echo "Market Order by ID Response: \n";
echo "\tSymbol: {$marketOrder->getSymbol()}\n";
echo "\tClient Order Id: {$marketOrder->getClientOrderId()}\n";
echo "\tQuantity: {$marketOrder->getQuantity()}\n";
echo "\tType: {$marketOrder->getType()}\n";
echo "\tCreated At: {$marketOrder->getCreatedAt()}\n\n";

// Find Order By Client Order ID
$orderByClientOrderId = $alpacaClient->getOrderByClientOrderId($order->getClientOrderId());

echo "Market Order by ID Response: \n";
echo "\tSymbol: {$orderByClientOrderId->getSymbol()}\n";
echo "\tClient Order Id: {$orderByClientOrderId->getClientOrderId()}\n";
echo "\tQuantity: {$orderByClientOrderId->getQuantity()}\n";
echo "\tType: {$orderByClientOrderId->getType()}\n";
echo "\tCreated At: {$orderByClientOrderId->getCreatedAt()}\n\n";

// Cancel Order
$alpacaClient->cancelOrder($order->getId()); // Would raise an exception if not successful

// Get Bars
$bars = $alpacaClient->getBars(['AMZN'], BarTimeFrame::DAY, 4);

echo "Bars Response: \n";
foreach ($bars as $bar) {
    echo "\t========================\n";
    echo "\tSymbol: {$bar->getSymbol()}\n";
    echo "\tOpen: {$bar->getO()}\n";
    echo "\tHigh: {$bar->getH()}\n";
    echo "\tLow: {$bar->getL()}\n";
    echo "\tClose: {$bar->getC()}\n";
    echo "\tVolume: {$bar->getV()}\n";
}
```

The output for the code above would be similar to:

```
Account Information:
        Created At: 2019-06-26T15:19:01.819453Z
        Buying Power: $ 398861.06
        Portfolio Value: $ 100003.99

Clock:
        Current Time: 2019-07-01T03:14:37.582893836-04:00
        Market Next Open Time: 2019-07-01T09:30:00-04:00
        Market Next Close Time: 2019-07-01T16:00:00-04:00

Market Order Response:
        Symbol: AAPL
        Client Order Id: 309626b7-5569-4ae5-bb16-d93930b8b82b
        Quantity: 5
        Type: market
        Created At: 2019-07-01T07:14:39.022538874Z

Order Update Event: {"event":"new","order":{"asset_class":"us_equity","asset_id":"b0b6dd9d-8b9b-48a9-ba46-b9d54906e415","canceled_at":null,"client_order_id":"abea2d0b-2340-4034-a50c-b4f09cde1e0b","created_at":"2019-07-01T07:12:16.177026Z","expired_at":null,"failed_at":null,"filled_at":null,"filled_avg_price":null,"filled_qty":"0","id":"b3332b17-b7dc-4a55-9a3c-dfd0c50ecc62","limit_price":null,"order_type":"market","qty":"5","side":"buy","status":"new","stop_price":null,"submitted_at":"2019-07-01T07:12:16.165806Z","symbol":"AAPL","time_in_force":"day","type":"market","updated_at":"2019-07-01T07:12:20.373783079Z"}}

Market Order by ID Response:
        Symbol: AAPL
        Client Order Id: 309626b7-5569-4ae5-bb16-d93930b8b82b
        Quantity: 5
        Type: market
        Created At: 2019-07-01T07:14:39.022539Z

Market Order by ID Response:
        Symbol: AAPL
        Client Order Id: 309626b7-5569-4ae5-bb16-d93930b8b82b
        Quantity: 5
        Type: market
        Created At: 2019-07-01T07:14:39.022539Z

Order Update Event: {"event":"canceled","order":{"asset_class":"us_equity","asset_id":"b0b6dd9d-8b9b-48a9-ba46-b9d54906e415","canceled_at":"2019-07-01T07:12:20.369944583Z","client_order_id":"abea2d0b-2340-4034-a50c-b4f09cde1e0b","created_at":"2019-07-01T07:12:16.177026Z","expired_at":null,"failed_at":null,"filled_at":null,"filled_avg_price":null,"filled_qty":"0","id":"b3332b17-b7dc-4a55-9a3c-dfd0c50ecc62","limit_price":null,"order_type":"market","qty":"5","side":"buy","status":"canceled","stop_price":null,"submitted_at":"2019-07-01T07:12:16.165806Z","symbol":"AAPL","time_in_force":"day","type":"market","updated_at":"2019-07-01T07:12:20.384272245Z"},"timestamp":"2019-07-01T07:12:20.369944583Z"}

Bars Response:
        ========================
        Symbol: AAPL
        Open: 198.43
        High: 199.26
        Low: 195.29
        Close: 195.57
        Volume: 18361798
        ========================
        Symbol: AAPL
        Open: 197.83
        High: 200.99
        Low: 197.35
        Close: 199.8
        Volume: 22499633
        ========================
        Symbol: AAPL
        Open: 200.29
        High: 201.57
        Low: 199.57
        Close: 199.73
        Volume: 15460769
        ========================
        Symbol: AAPL
        Open: 199.24
        High: 199.4
        Low: 197.05
        Close: 197.92
        Volume: 18773228

```

#### Methods

[](#methods)

All API methods are available on the Alpaca Client (`RagingProdigy\Alpaca\Client`).

##### Account API

[](#account-api)

Calls `GET /account` and returns the current account

```
$client->getAccount(); //Account
```

##### Orders API

[](#orders-api)

###### Request New Order

[](#request-new-order)

Calls `POST /orders` and creates a new order.

```
$client->requestNewOrder(
    string $symbol,
    int $quantity,
    string $action, // OrderAction::BUY, OrderAction::SELL
    string $type, // OrderType::MARKET, OrderType::STOP, OrderType::LIMIT, OrderType::STOP_LIMIT,
    string $timeInForce, // TimeInForce::DAY, TimeInForce::GTC, TimeInForce::OPG, TimeInForce::IOC, TimeInForce::FOK
    float $limitPrice = null,
    float $stopPrice = null,
    bool $extendedHours = false,
    $clientOrderId = null
); // Order
```

###### Get Orders

[](#get-orders)

Calls `GET /orders` and returns an array of Order objects.

```
$client->getOrders(
    string $status = OrderStatus::OPEN, // OrderStatus::CLOSED, OrderStatus::OPEN, OrderStatus::ALL
    int $limit = 50,
    DateTime $after = null,
    DateTime $until = null,
    string $direction = Sorting::DESCENDING // Sorting::ASCENDING, Sorting::DESCENDING
); // Order[]
```

###### Get Order By ID

[](#get-order-by-id)

Calls `GET /orders/{id}` and returns an Order.

```
$client->getOrder(string $orderId); // Order
```

###### Get Order By Client Order Id

[](#get-order-by-client-order-id)

Calls `GET /orders:by_client_order_id` and returns an order by `client_order_id`. You can set `client_order_id` while creating your orders to easily keep track of your orders.

```
$client->getOrderByClientOrderId(string $clientOrderId); // Order
```

###### Cancel an Order

[](#cancel-an-order)

Calls `DELETE /orders/{id}` and deletes an Order.

```
$client->cacnelOrder(string $orderId); // void
```

Raises an `AlpacaApiException` when it fails.

##### Positions API

[](#positions-api)

###### Get Position

[](#get-position)

Calls `GET /positions/{symbol}` and returns a Position.

```
$client->getOpenPosition(string $symbol); // Position
```

###### Get All Position

[](#get-all-position)

Calls `GET /positions` and returns an array of Position objects.

```
$client->getOpenPositions(); // Position[]
```

##### Assets API

[](#assets-api)

###### Get Assets

[](#get-assets)

Calls `GET /assets/` and returns an array of Assets matching your criteria.

```
$client->getAssets(
    string $status = '' // '', AssetStatus::ACTIVE, AssetStatus::INACTIVE
); // Asset[]
```

###### Get Asset Information

[](#get-asset-information)

Calls `GET /assets/{symbol}` and returns an Asset entity.

```
$client->getAsset(string $assetIdOrSymbol); // Asset
```

##### Calendar API

[](#calendar-api)

Calls `GET/calendar` and returns the market calendar for the selected dates

```
$client->getCalendar(DateTime $start = null, DateTime $end = null); // Calendar[]
```

##### Data API

[](#data-api)

###### Get Bars

[](#get-bars)

```
$client->getBars(
    array $symbols, // up to 200 symbols
    string $timeFrame, // BarTimeFrame::MINUTE, BarTimeFrame::FIVE_MINUTES, BarTimeFrame::FIFTEEN_MINUTES, BarTimeFrame::DAY
    int $limit = 100,
    DateTime $start = null,
    DateTime $end = null
); // Bar[]
```

#### Tests

[](#tests)

In order to run the tests, install the dev-dependencies (`composer install --dev`) and then run:

```
    vendor/bin/phpunit -c phpunit.xml.dist
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~233 days

Total

4

Last Release

1806d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4d96ec3139fb7148ff34d87e51fef7fcb1f7b3faba7d176c97c8135293c35fb1?d=identicon)[ragingprodigy](/maintainers/ragingprodigy)

---

Top Contributors

[![ragingprodigy](https://avatars.githubusercontent.com/u/1816083?v=4)](https://github.com/ragingprodigy "ragingprodigy (37 commits)")

---

Tags

alpacatrading

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ragingprodigy-alpaca-php/health.svg)

```
[![Health](https://phpackages.com/badges/ragingprodigy-alpaca-php/health.svg)](https://phpackages.com/packages/ragingprodigy-alpaca-php)
```

###  Alternatives

[neuron-core/neuron-ai

The PHP Agentic Framework.

1.8k245.3k20](/packages/neuron-core-neuron-ai)[jaggedsoft/php-binance-api

PHP Binance API is an asynchronous PHP library for the Binance API designed to be easy to use.

644179.0k4](/packages/jaggedsoft-php-binance-api)[zerodha/phpkiteconnect

The PHP client library for the Kite Connect trading APIs Resources

463.4k2](/packages/zerodha-phpkiteconnect)[jeffreyhyer/alpaca-trade-api-php

PHP SDK for the Alpaca trade API

285.0k](/packages/jeffreyhyer-alpaca-trade-api-php)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

255.2k](/packages/aedart-athenaeum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
