Skip to content

Commit

Permalink
Added request method for Ticker
Browse files Browse the repository at this point in the history
  • Loading branch information
butschster committed Mar 2, 2018
1 parent 3f1e753 commit 423f14f
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 2 deletions.
22 changes: 21 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Butschster\Kraken\Contracts\Order as OrderContract;
use Butschster\Kraken\Exceptions\KrakenApiErrorException;
use Butschster\Kraken\Objects\{
Balance, BalanceCollection, OrdersCollection, OrderStatus, Pair, PairCollection
Balance, BalanceCollection, OrdersCollection, OrderStatus, Pair, PairCollection, Ticker, TickerCollection
};
use Carbon\Carbon;
use GuzzleHttp\ClientInterface as HttpClient;
Expand Down Expand Up @@ -83,6 +83,26 @@ public function getAssetPairs($pair = null, string $info = 'info'): PairCollecti
});
}

/**
* Get ticker information
*
* @param string|array $pair comma delimited list of asset pairs to get info on
* @return TickerCollection|Ticker[]
* @throws KrakenApiErrorException
*/
public function getTicker($pair): TickerCollection
{
if (is_array($pair)) {
$pair = implode(',', $pair);
}

$result = $this->request('Ticker', ['pair' => $pair]);

return (new TickerCollection($result))->map(function ($information, $pair) {
return new Ticker($pair, $information);
});
}

/**
* Get account balance
*
Expand Down
11 changes: 10 additions & 1 deletion src/Contracts/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Butschster\Kraken\Contracts;

use Butschster\Kraken\Objects\{
BalanceCollection, Balance, OrdersCollection, Pair, OrderStatus, PairCollection
BalanceCollection, Balance, OrdersCollection, Pair, OrderStatus, PairCollection, Ticker, TickerCollection
};
use Butschster\Kraken\Exceptions\KrakenApiErrorException;
use Carbon\Carbon;
Expand All @@ -25,6 +25,15 @@ interface Client
*/
public function getAssetPairs($pair = null, string $info = 'info'): PairCollection;

/**
* Get ticker information
*
* @param string|array $pair comma delimited list of asset pairs to get info on
* @return TickerCollection|Ticker[]
* @throws KrakenApiErrorException
*/
public function getTicker($pair): TickerCollection;

/**
* Make API call
*
Expand Down
33 changes: 33 additions & 0 deletions src/Objects/Pair.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function __construct(string $pair, array $information)
}

/**
* Pair name
*
* @return string
*/
public function name(): string
Expand All @@ -33,10 +35,41 @@ public function name(): string
}

/**
* Alternate pair name
*
* @return string
*/
public function altname(): string
{
return $this->information['altname'];
}

/**
* Asset id of base component
*
* @return string
*/
public function base(): string
{
return $this->information['base'];
}

/**
* Asset id of quote component
* @return string
*/
public function quote(): string
{
return $this->information['quote'];
}

/**
* Volume lot size
*
* @return string
*/
public function lot(): string
{
return $this->information['lot'];
}
}
148 changes: 148 additions & 0 deletions src/Objects/Ticker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

namespace Butschster\Kraken\Objects;

class Ticker
{
/**
* @var array
*/
protected $data;

/**
* @var string
*/
protected $pair;

/**
* @param string $pair
* @param array $data
*/
public function __construct(string $pair, array $data)
{
$this->data = $data;
$this->pair = $pair;
}

/**
* @return string
*/
public function pair(): string
{
return $this->pair;
}

/**
* Ask price
* @return mixed
*/
public function askPrice(): float
{
return (float) array_get($this->data, 'a.0', 0);
}

/**
* Ask whole lot volume
* @return mixed
*/
public function askWholeLotVolume(): float
{
return (float) array_get($this->data, 'a.1', 0);
}

/**
* Ask lot volume
* @return mixed
*/
public function askLotVolume(): float
{
return (float) array_get($this->data, 'a.2', 0);
}

/**
* Bid price
* @return mixed
*/
public function bidPrice(): float
{
return (float) array_get($this->data, 'b.0', 0);
}

/**
* Bid whole lot volume
* @return mixed
*/
public function bidWholeLotVolume(): float
{
return (float) array_get($this->data, 'b.1', 0);
}

/**
* Bid lot volume
* @return mixed
*/
public function bidLotVolume(): float
{
return (float) array_get($this->data, 'b.2', 0);
}

/**
* Last trade closed price
*
* @return float
*/
public function lastClosedPrice(): float
{
return (float) array_get($this->data, 'c.0', 0);
}

/**
* Last trade closed price
*
* @return float
*/
public function lastLotVolume(): float
{
return (float) array_get($this->data, 'c.1', 0);
}

/**
* Today volume
*
* @return float
*/
public function volumeToday(): float
{
return (float) array_get($this->data, 'v.0', 0);
}

/**
* Last 24 hours volume
*
* @return float
*/
public function volumePrevious(): float
{
return (float) array_get($this->data, 'v.1', 0);
}

/**
* Today volume weighted average price
*
* @return float
*/
public function avgPriceVolumeToday(): float
{
return (float) array_get($this->data, 'p.0', 0);
}

/**
* Last 24 volume weighted average price
*
* @return float
*/
public function avgPriceVolumPrevious(): float
{
return (float) array_get($this->data, 'v.1', 0);
}
}
10 changes: 10 additions & 0 deletions src/Objects/TickerCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Butschster\Kraken\Objects;

use Illuminate\Support\Collection;

class TickerCollection extends Collection
{

}

0 comments on commit 423f14f

Please sign in to comment.