sammarks/stockfighter

股票交易员API包装器。

dev-master 2015-12-24 03:32 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:52:55 UTC


README

我在我的解决方案仓库中使用了这个库,并想与世界分享它。我的解决方案仓库可以在这里找到(剧透警告)。

安装

只需使用Composer安装此库。

composer install sammarks/stockfighter

或者您可以在项目中通过以下方式要求使用它

composer require sammarks/stockfighter

注意:由于此包依赖于我的phpws分支,您还需要指定存储库设置。将以下内容添加到您的composer.json

"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/sammarks/phpws"
    }
],

此外,由于库依赖于这样的分支,您需要确保项目的最小稳定性设置为 "dev:"

"minimum-stability": "dev"

用法

此库配置得非常接近Stockfighter API文档的URL结构。话虽如此,以下是一个库用法的示例

use \Marks\Stockfighter\Stockfighter;

// Set the API key.
Stockfighter::setApiKey('apikey');

// Create an instance of the API.
$stockfighter = new Stockfighter();

// Check if the API is working.
$api_working = $stockfighter->heartbeat();

// Check if a venue exists and is working.
$test_working = $stockfighter->venue('test')->heartbeat();

// Get all stocks in a venue.
$stocks = $stockfighter->venue('test')->stocks();

// Get information about a stock.
$stock_info = $stockfighter->venue('test')->stock('ABCD')->info();

// Order some ABCD stock.
$order = $stockfighter->venue('test')->stock('ABCD')->order($account, $price, $quantity, $direction, $order_type);
// Direction and Order Type have constants in the Order class, like Order::DIRECTION_BUY,
// Order::DIRECTION_SELL, Order::TYPE_MARKET, etc.

Web Sockets

您还可以使用WebSockets连接并监听报价。以下是一个示例

// Create a websocket instance.
$websocket = $this->stockfighter->getWebSocketCommunicator()->quotes($account, $venue, $stock);

// Set the receive callback.
$websocket->receive(function (Quote $quote) {
	// Do stuff with the quote...
	// Once you're done with the websocket connection, return true
	// from this method and the connection will be closed.
});

// Open the connection.
$websocket->connect();

异步调用

此库使用Guzzle进行HTTP请求,它使用PSR-7承诺。当然,我已在库中包含了承诺的支持。以下是如何使用异步调用下订单的示例

// Assuming you already have a stockfighter instance...
// Here's an example that places an order asynchronously.
$stockfighter->venue('test')->stock('ABCD')->orderAsync($account, $price, $quantity, $direction,
	$order_type)->then(function (Order $order) {
		echo "Oh boy, the order finished! " . $order->totalFulfilled;	
	}, function (StockfighterRequestException $e) {
		echo "Oh no, there was an error with the order! " . $e->getMessage();	
	});

事件循环

重要说明:如果您使用Web Sockets或异步调用,您需要初始化ReactPHP事件循环。通常,这作为应用程序的最后一个调用完成(因为它是一个阻塞方法)。在应用程序结束前调用以下代码,完成所有初始化和处理逻辑

// Start the Event Loop.
$stockfighter->run();

贡献

如果您在我的Stockfighter库中发现了错误,或者因为您在自己的Stockfighter解决方案中使用它而想改进它,只需向我发送pull request即可!我保证我会非常乐意接受建议。