tmarois / alpaca-php-sdk
PHP SDK for the Alpaca Trading API
Installs: 73
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 0
Open Issues: 0
Type:package
Requires
- php: >=7
- guzzlehttp/guzzle: ~6.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: 6.*
This package is auto-updated.
Last update: 2023-01-29 03:48:14 UTC
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 tmarois/alpaca-php-sdk:^1.0
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.