tmarois/alpaca-php-sdk

此包已被放弃且不再维护。未建议替代包。
此包最新版本(v1.3)没有可用的许可证信息。

Alpaca交易API的PHP SDK

v1.3 2020-01-13 18:54 UTC

This package is auto-updated.

Last update: 2023-01-29 03:48:14 UTC


README

此包作为Alpaca交易API的PHP SDK。

此外,还可以查看Polygon PHP SDK以获取实时数据。

安装

使用Composer安装包。

运行composer require tmarois/alpaca-php-sdk:^1.0

入门

首先,您需要实例化Alpaca对象。

use Alpaca\Alpaca;

$alpaca = new Alpaca("YOUR_API_KEY_ID", "YOUR_API_SECRET_KEY");

示例用法

获取账户信息:获取账户详情

// 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();

获取订单:获取特定订单

$order = $alpaca->orders()->get('ORDER_ID');

获取所有订单:获取所有开放订单的数组

// 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);

取消订单:取消特定订单

$orders = $alpaca->orders()->cancel('ORDER_ID);

取消所有订单:取消所有开放订单

$orders = $alpaca->orders()->cancelAll();

创建订单:创建新订单

$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' => ''
]);

替换订单:替换现有订单

$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' => ''
]);

获取仓位:获取开放仓位

$position = $alpaca->positions()->get('SYMBOL');

获取所有仓位:获取所有开放仓位

$positions = $alpaca->positions()->getAll();

仓位实体响应

{
  "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"
}

关闭仓位:关闭仓位

$alpaca->positions()->close('SYMBOL');

关闭所有仓位:关闭所有开放仓位

$alpaca->positions()->closeAll();

获取活动:获取账户活动,如订单成交、股息等。

// 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');

有关更多信息,请参阅Alpaca文档,如果想要扩展此功能,请提交pull request或请求添加您想要的功能。谢谢!

贡献

任何人都可以为alpaca-php-sdk做出贡献。请在发现意外问题时提交问题,或者发送pull request以进行改进。

许可证

alpaca-php-sdk(本仓库)是一个开源软件,遵循MIT许可协议

此SDK与alpaca.markets、Alpaca Securities LLC和AlpacaDB无关,作为非官方SDK,旨在为PHP应用程序提供一个简单解决方案。**使用风险自负**。如果您在使用SDK时遇到任何问题,请提交问题或拉取请求。