PHP code example of butschster / kraken-api-client
1. Go to this page and download the library: Download butschster/kraken-api-client library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
butschster / kraken-api-client example snippets
$client = new \Butschster\Kraken\Client(
new GuzzleHttp\Client(),
new \Butschster\Kraken\NonceGenerator(),
(new \Butschster\Kraken\Serializer\SerializerFactory())->build(),
'api-key',
'api-secret'
);
$client->getAccountBalance();
$client = new \Butschster\Kraken\WebsocketClient(
(new \Butschster\Kraken\Serializer\SerializerFactory())->build(),
\React\EventLoop\Factory::create()
);
$client->connectToPublicServer(...);
use App\Http\Controllers\Controller;
use Butschster\Kraken\Contracts\Client;
class BalanceController extends Controller {
public function getBalance(Client $client)
{
$balances = $client->getAccountBalance();
...
}
}
/** @var \Butschster\Kraken\Contracts\Client $client */
// Public request
$client->request('public/Spread', RecentSpreadsResponse::class, array $params): RecentSpreadsResponse;
// Private request
$balance = $client->request('private/Balance', \Butschster\Kraken\Responses\AccountBalanceResponse::class, array $params);
/** @var \Butschster\Kraken\Contracts\Client $client */
$response = $client->getServerTime();
$response->time; // DateTimeInterface
$response->rfc1123; // string:"Sun, 21 Mar 21 14:23:14 +0000
/** @var \Butschster\Kraken\Contracts\Client $client */
$response = $client->getSystemStatus();
$response->status; // string:"online"
$response->timestamp; // DateTimeImmutable
use Butschster\Kraken\ValueObjects\AssetClass;
/** @var \Butschster\Kraken\Contracts\Client $client */
$response = $client->getAssetInfo(array $assets = ['all'], ?AssetClass $class = null);
foreach ($response as $asset => $info) {
$asset; // string: "XXBT"
$info->class; // string:"currency"
$info->altname; // string:"XBT"
$info->decimals; // int:10
$info->displayDecimals; // int:5
}
use Butschster\Kraken\ValueObjects\AssetPair;
use Butschster\Kraken\ValueObjects\TradableInfo;
use Butschster\Kraken\Responses\Entities\Fee;
/** @var \Butschster\Kraken\Contracts\Client $client */
$pair = new AssetPair('XXBTCZUSD', 'XETHXXBT');
$info = TradableInfo::leverage();
$response = $client->getTradableAssetPairs($pair, $info);
foreach ($response as $pair => $assetPair) {
$pair ;// string:"XETHXXBT"
$assetPair->altname; // string:"ETHXBT"
$assetPair->wsname; // string:"ETH/XBT"
$assetPair->classBase; // string:"currency"
$assetPair->base; // string:"XETH"
$assetPair->classQuote; // string:"currency"
$assetPair->quote; // string:"XXBT"
$assetPair->pairDecimals; // int:5
$assetPair->lotDecimals; // int:8
$assetPair->lotMultiplier; // int:1
$assetPair->leverageBuy; // array:[2,3,4,5]
$assetPair->leverageSell; // array:[2,3,4,5]
$assetPair->feeVolumeCurrency; // string:"ZUSD"
$assetPair->marginCall; // int:80
$assetPair->marginStop; // int:40
$assetPair->ordermin; // string:"0.005"
foreach ($assetPair->fees as $fee) {
$fee->getPercentFee(); // float:0.2
$fee->getVolume(); // int:2500000
}
foreach ($assetPair->feesMaker as $fee) {
$fee->getPercentFee(); // float:0.2
$fee->getVolume(); // int:2500000
}
}
use Brick\Math\BigDecimal;
/** @var \Butschster\Kraken\Contracts\Client $client */
$response = $client->getTickerInformation(['XBTUSD', 'XXBTZUSD', ...]);
foreach ($response as $pair => $tickerInfo) {
$pair; // string: "XXBTZUSD"
$tickerInfo->ask->getLotVolume(); // BigDecimal
$tickerInfo->ask->getPrice(); // BigDecimal
$tickerInfo->ask->getWholeLotVolume(); // BigDecimal
$tickerInfo->bid->getLotVolume(); // BigDecimal
$tickerInfo->bid->getPrice(); // BigDecimal
$tickerInfo->bid->getWholeLotVolume(); // BigDecimal
$tickerInfo->lastTradeClosed[0]; // string:"52641.10000"
$tickerInfo->lastTradeClosed[1]; // string:"0.00080000"
$tickerInfo->volume->getLast24Hours(); // BigDecimal
$tickerInfo->volume->getToday(); // BigDecimal
$tickerInfo->volumeWightedAveragePrice->getLast24Hours(); // BigDecimal
$tickerInfo->volumeWightedAveragePrice->getToday(); // BigDecimal
$tickerInfo->trades->getLast24Hours(); // int:0
$tickerInfo->trades->getToday(); // int:10
$tickerInfo->low->getToday(); // BigDecimal
$tickerInfo->low->getLast24Hours(); // BigDecimal
$tickerInfo->high->getToday(); // BigDecimal
$tickerInfo->high->getLast24Hours(); // BigDecimal
$tickerInfo->openingPrice; // BigDecimal
}
/** @var \Butschster\Kraken\Contracts\Client $client */
use Brick\Math\BigDecimal;
$response = $client->getOrderBook(['XBTUSD', 'XXBTZUSD'], 100);
foreach ($response as $pair => $orders) {
foreach ($orders->asks as $order) {
$order->getPrice(); // BigDecimal
$order->getVolume(); // BigDecimal
$order->getTimestamp(); // int:1616663113
$order->getDate(); // DateTimeInterface
}
foreach ($orders->bids as $order) {
$order->getPrice(); // BigDecimal
$order->getVolume(); // BigDecimal
$order->getTimestamp(); // int:1616663113
$order->getDate(); // DateTimeInterface
}
}
/** @var \Butschster\Kraken\Contracts\Client $client */
use Brick\Math\BigDecimal;
$response = $client->getAccountBalance();
foreach ($response as $balance) {
$balance->getAsset(); // string:"ZUSD"
$balance->getBalance(); // BigDecimal
}
/** @var \Butschster\Kraken\Contracts\Client $client */
use Brick\Math\BigDecimal;
$response = $client->getTradeBalance();
$response->equivalentBalance; // BigDecimal
$response->tradeBalance; // BigDecimal
$response->marginAmount; // BigDecimal
$response->net; // BigDecimal
$response->cost; // BigDecimal
$response->valuation; // BigDecimal
$response->equity; // BigDecimal
$response->freeMargin; // BigDecimal
$response->marginLevel; // BigDecimal
/** @var \Butschster\Kraken\Contracts\Client $client */
use Brick\Math\BigDecimal;
$response = $client->getOpenOrders();
foreach ($response as $txId => $order) {
$txId; // string:"OQCLML-BW3P3-BUCMWZ"
$order->refId; // string|null
$order->userRef; // int:0
$order->status; // string:"online"
$order->openTimestamp; // int:1616666559.8974
$order->startTimestamp; // int:0
$order->expireTimestamp; // int:0
$order->description->pair; // string:"XBTUSD"
$order->description->type; // string:"buy"
$order->description->orderType; // string:"limit"
$order->description->price; // BigDecimal
$order->description->secondaryPrice; // BigDecimal
$order->description->leverage; // string:"none"
$order->description->order; // string:"buy 1.25000000 XBTUSD @ limit 30010.0"
$order->description->close; // string:""
$order->volume; // BigDecimal
$order->volumeExecuted; // BigDecimal
$order->cost; // BigDecimal
$order->fee; // BigDecimal
$order->price; // BigDecimal
$order->stopPrice; // BigDecimal
$order->limitPrice; // BigDecimal
$order->miscellaneous; // array<string>
$order->flags; // array:["fciq"]
$order->trades; // array:["TCCCTY-WE2O6-P3NB37"]
}
/** @var \Butschster\Kraken\Contracts\Client $client */
use Brick\Math\BigDecimal;
$start = \Carbon\Carbon::now()->subDay();
$end = \Carbon\Carbon::now();
$response = $client->getClosedOrders(
start: $start, end: $end, offset: 100
);
$response->count; // 5
foreach ($response->closed as $txId => $order) {
$txId; // string:"OQCLML-BW3P3-BUCMWZ"
$order->refId; // string|null
$order->userRef; // int:0
$order->status; // string:"canceled"
$order->reason; // string:"User requested"
$order->openTimestamp; // int:1616666559.8974
$order->startTimestamp; // int:0
$order->expireTimestamp; // int:0
$order->description->pair; // string:"XBTUSD"
$order->description->type; // string:"buy"
$order->description->orderType; // string:"limit"
$order->description->price; // BigDecimal
$order->description->secondaryPrice; // BigDecimal
$order->description->leverage; // string:"none"
$order->description->order; // string:"buy 1.25000000 XBTUSD @ limit 30010.0"
$order->description->close; // string:""
$order->volume; // BigDecimal
$order->volumeExecuted; // BigDecimal
$order->cost; // BigDecimal
$order->fee; // BigDecimal
$order->price; // BigDecimal
$order->stopPrice; // BigDecimal
$order->limitPrice; // BigDecimal
$order->miscellaneous; // array<string>
$order->flags; // array:["fciq"]
$order->trades; // array:["TCCCTY-WE2O6-P3NB37"]
}
/** @var \Butschster\Kraken\Contracts\Client $client */
use Brick\Math\BigDecimal;
$response = $client->queryOrdersInfo(['OBCMZD-JIEE7-77TH3F', 'OMMDB2-FSB6Z-7W3HPO']);
foreach ($response as $txId => $order) {
$txId; // string:"OQCLML-BW3P3-BUCMWZ"
$order->refId; // string|null
$order->userRef; // int:0
$order->status; // string:"canceled"
$order->reason; // string:"User requested"
$order->openTimestamp; // int:1616666559.8974
$order->startTimestamp; // int:0
$order->expireTimestamp; // int:0
$order->description->pair; // string:"XBTUSD"
$order->description->type; // string:"buy"
$order->description->orderType; // string:"limit"
$order->description->price; // BigDecimal
$order->description->secondaryPrice; // BigDecimal
$order->description->leverage; // string:"none"
$order->description->order; // string:"buy 1.25000000 XBTUSD @ limit 30010.0"
$order->description->close; // string:""
$order->volume; // BigDecimal
$order->volumeExecuted; // BigDecimal
$order->cost; // BigDecimal
$order->fee; // BigDecimal
$order->price; // BigDecimal
$order->stopPrice; // BigDecimal
$order->limitPrice; // BigDecimal
$order->miscellaneous; // array<string>
$order->flags; // array:["fciq"]
$order->trades; // array:["TCCCTY-WE2O6-P3NB37"]
}
/** @var \Butschster\Kraken\Contracts\Client $client */
use Butschster\Kraken\ValueObjects\CloseOrder;
use Butschster\Kraken\ValueObjects\OrderDirection;
use Butschster\Kraken\ValueObjects\OrderType;
$order = new \Butschster\Kraken\Requests\AddOrderRequest(
OrderType::stopLoss(), OrderDirection::sell(), 'XXBTZUSD'
);
$order->setCloseOrder(new CloseOrder(OrderType::stopLoss(), '38000', '36000'));
$order->setPrice('45000.1');
$order->...
$response = $client->addOrder($order);
$response->description->order; // string:"sell 2.12340000 XBTUSD @ limit 45000.1 with 2:1 leverage"
$response->description->close; // string:"close position @ stop loss 38000.0 -> limit 36000.0"
$response->txId; // array:["OUF4EM-FRGI2-MQMWZD"]
/** @var \Butschster\Kraken\Contracts\Client $client */
$response = $client->cancelOrder('OYVGEW-VYV5B-UUEXSK');
$response; // int:1
/** @var \Butschster\Kraken\Contracts\Client $client */
$response = $client->cancelAllOrders();
$response; // int:1
/** @var \Butschster\Kraken\Contracts\Client $client */
$response = $client->cancelAllOrdersAfter(60);
$response->currentTime; // DateTimeInterface
$response->triggerTime; // DateTimeInterface
/** @var \Butschster\Kraken\Contracts\Client $client */
use Brick\Math\BigDecimal;
$response = $client->getDepositMethods('XBT');
$response->method; // string:"Bitcoin"
$response->limit; // int|bool:false
$response->fee; // BigDecimal
$response->generatedAddress; // bool:true
$response->addressSetupFee; // string:""
/** @var \Butschster\Kraken\Contracts\Client $client */
$response = $client->getDepositAddresses('XBT', 'Bitcoin');
foreach ($response as $address) {
$address->address; // string:"2N9fRkx5JTWXWHmXzZtvhQsufvoYRMq9ExV"
$address->expireTimestamp; // int:0
$address->new; // bool:true
}
/** @var \Butschster\Kraken\Contracts\Client $client */
use Brick\Math\BigDecimal;
$response = $client->getWithdrawalInformation('XBT', 'btc_testnet_with1', BigDecimal::of(0.725));
$response->method; // string:"Bitcoin"
$response->limit; // BigDecimal
$response->amount; // BigDecimal
$response->fee; // BigDecimal
/** @var \Butschster\Kraken\Contracts\Client $client */
$response = $client->getWebsocketsToken();
$response->token; // string:"1Dwc4lzSwNWOAwkMdqhssNNFhs1ed606d1WcF3XfEMw"
$response->expires; // int:900
$response->expiresAt(); // DateTimeInterface
namespace App\Console\Commands\ExampleCommand;
use Butschster\Kraken\Contracts\WebsocketClient;
use Butschster\Kraken\Websocket\Connection;
class ExampleCommand extends Command
{
...
public function handle(WebsocketClient $client)
{
$client->connectToPublicServer(function (Connection $connection) {
...
});
}
}
/** @var \Butschster\Kraken\Contracts\WebsocketClient $client */
use Butschster\Kraken\Websocket\Connection;
use Butschster\Kraken\Websocket\Requests\Ping;
use Butschster\Kraken\Websocket\Timer;
$client->connectToPublicServer(function (Connection $connection) {
$connection->sendEvent(new Ping());
// or
$connection->addPeriodicTimer(
new Timer(5, new Ping())
);
});
$client->connectToPrivateServer('websocket-token', function (Connection $connection) {
...
});
/** @var \Butschster\Kraken\Contracts\WebsocketClient $client */
use Butschster\Kraken\Websocket\Connection;
use Butschster\Kraken\Websocket\Requests\HeartBeat;
use Butschster\Kraken\Websocket\Timer;
$client->connectToPublicServer(function (Connection $connection) {
$connection->sendEvent(new HeartBeat());
// or
$connection->addPeriodicTimer(
new Timer(5, new HeartBeat())
);
});
/** @var \Butschster\Kraken\Contracts\WebsocketClient $client */
use Butschster\Kraken\Websocket\Connection;
use Butschster\Kraken\Websocket\Requests\Subscribe;
$client->connectToPublicServer(function (Connection $connection) {
$connection->sendEvent(
new Subscribe(
'ticker',
["XBT/USD", "XBT/EUR"]
)
);
$connection->onMessage(function (string $message) {
// Handle message
});
});
/** @var \Butschster\Kraken\Contracts\WebsocketClient $client */
// For public events you have to use \Butschster\Kraken\Contracts\WebsocketEvent interface
// For private events you have to use \Butschster\Kraken\Contracts\PrivateWebsocketEvent interface
class OwnTradesEvent implements \Butschster\Kraken\Contracts\PrivateWebsocketEvent {
public string $event = 'subscribe';
public array $subscription = [
'name' => 'ownTrades'
];
public function setToken(string $token) : void{
$this->subscription['token'] = $toke;
}
}
$client->connectToPrivateServer('token', function (Connection $connection) {
$connection->sendEvent(
new OwnTradesEvent()
);
});