devstar/alpaca-php-sdk
Composer 安装命令:
composer require devstar/alpaca-php-sdk
包简介
PHP SDK for the Alpaca Trading API
README 文档
README
This package acts as a PHP SDK for the Alpaca Trading API.
Also, check out the Polygon PHP SDK for real time data.
Installation
Use Composer to install package.
Run composer require devstar/alpaca-php-sdk
Getting Started
First you need to instantiate the Alpaca object.
use Alpaca\Alpaca; $alpaca = new Alpaca("YOUR_API_KEY_ID", "YOUR_API_SECRET_KEY");
Example Usage
Get Account: Get the account details
// this will pull a request from alpaca to get account details $account = $alpaca->account(); // here are some helper methods to quickly get the details $number = $account->number(); $id = $account->id(); $status = $account->status(); $buyingPower = $account->buyingPower(); $cash = $account->cash(); $longValue = $account->longValue(); $shortValue = $account->shortValue(); $portfolioValue = $account->portfolioValue(); // gets the raw array from Alpaca // view documentation for the correct keys $raw = $account->raw();
Get Order: Get a specific order
$order = $alpaca->orders()->get('ORDER_ID');
Get All Orders: Get an array of all open orders
// get all open orders $orders = $alpaca->orders()->getAll(); // get orders of specific types // type: open, closed, or all $orders = $alpaca->orders()->getAll($type,$limit,$dateFrom,$dateTo,$direction);
Cancel An Order: Cancel a specific order
$orders = $alpaca->orders()->cancel('ORDER_ID);
Cancel All Orders: Cancel all open orders
$orders = $alpaca->orders()->cancelAll();
Create An Order: Create a new order
$alpaca->orders()->create([ // stock to purchase 'symbol' => 'AAPL', // how many shares 'qty' => 1, // buy or sell 'side' => 'buy', // market, limit, stop, or stop_limit 'type' => 'market', // day, gtc, opg, cls, ioc, fok. // @see https://docs.alpaca.markets/orders/#time-in-force 'time_in_force' => 'day', // required if type is limit or stop_limit // 'limit_price' => 0, // required if type is stop or stop_limit // 'stop_price' => 0, 'extended_hours' => false, // optional if adding custom id // 'client_order_id' => '' ]);
Replace An Order: Replaces an existing order
$alpaca->orders()->replace('ORDER_ID',[ 'qty' => 1, // required if type is limit or stop_limit // 'limit_price' => 0, // required if type is stop or stop_limit // 'stop_price' => 0, // day, gtc, opg, cls, ioc, fok. // @see https://docs.alpaca.markets/orders/#time-in-force 'time_in_force' => 'day', // optional if adding custom id // 'client_order_id' => '' ]);
Get Position: Get an open position
$position = $alpaca->positions()->get('SYMBOL');
Get All Positions: Get all open positions
$positions = $alpaca->positions()->getAll();
Position Entity Response:
{
"asset_id": "904837e3-3b76-47ec-b432-046db621571b",
"symbol": "AAPL",
"exchange": "NASDAQ",
"asset_class": "us_equity",
"avg_entry_price": "100.0",
"qty": "5",
"side": "long",
"market_value": "600.0",
"cost_basis": "500.0",
"unrealized_pl": "100.0",
"unrealized_plpc": "0.20",
"unrealized_intraday_pl": "10.0",
"unrealized_intraday_plpc": "0.0084",
"current_price": "120.0",
"lastday_price": "119.0",
"change_today": "0.0084"
}
Close a Position: Close a position
$alpaca->positions()->close('SYMBOL');
Close All Positions: Close all open positions
$alpaca->positions()->closeAll();
Get Activity: Get the account activity, like order fills, dividends etc.
// type can be many, such as FILL, DIV, TRANS etc // view on this page https://docs.alpaca.markets/api-documentation/api-v2/account-activities/ $activity = $alpaca->activity()->get('TYPE');
There are more in the Alpaca Documentation than what is presented above, if you want to extend this, please send in a pull request or request features you'd like to see added. Thanks!
Contributions
Anyone can contribute to alpaca-php-sdk. Please do so by posting issues when you've found something that is unexpected or sending a pull request for improvements.
License
alpaca-php-sdk (This Repository) is open-sourced software licensed under the MIT license.
This SDK has no affiliation with alpaca.markets, Alpaca Securities LLC and AlpacaDB and acts as an unofficial SDK designed to be a simple solution with using PHP applications. Use at your own risk. If you have any issues with the SDK please submit an issue or pull request.
devstar/alpaca-php-sdk 适用场景与选型建议
devstar/alpaca-php-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 08 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「alpaca」 「market」 「stocks」 「trading api」 「stock market」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 devstar/alpaca-php-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 devstar/alpaca-php-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 devstar/alpaca-php-sdk 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
YML (Yandex Market Language) parser
PHP SDK for the Alpaca trade API
A PSR-7 compatible library for making CRUD API endpoints
Official PHP client (SDK) for the Newsdata.io News API — fetch real-time, historical, crypto, and stock-market news via REST with validation, retries, and error handling.
Bandwidth API library client for PHP
A package to build AlpacaJS forms via Laravel.
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2024-08-02