Skip to content

Commit

Permalink
Add getAvgPrice method to the binance API
Browse files Browse the repository at this point in the history
  • Loading branch information
sabramooz committed Jan 19, 2021
1 parent 0a8cd55 commit 99d8e63
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
],
"require": {
"php": ">=5.6.4",
"laravel/framework": ">=6",
"ext-curl": "*",
"laravel/framework": ">=6"
"ext-json": "*"
},
"autoload": {
"psr-4": {
Expand Down
71 changes: 40 additions & 31 deletions src/BinanceAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace sabramooz\binance;

use Exception;

class BinanceAPI
{
protected $key; // API key
Expand Down Expand Up @@ -70,7 +72,7 @@ function setAPI($key, $secret)
* Get ticker
*
* @return mixed
* @throws \Exception
* @throws Exception
*/
public function getTickers()
{
Expand All @@ -84,7 +86,7 @@ public function getTickers()
* @param array $params Required and optional parameters
* @param string $method GET, POST, PUT, DELETE
* @return mixed
* @throws \Exception
* @throws Exception
*/
private function request($url, $params = [], $method = 'GET')
{
Expand All @@ -101,13 +103,13 @@ private function request($url, $params = [], $method = 'GET')
//Get result
$result = curl_exec($this->curl);
if ($result === false)
throw new \Exception('CURL error: ' . curl_error($this->curl));
throw new Exception('CURL error: ' . curl_error($this->curl));

// decode results
$result = json_decode($result, true);

if (!is_array($result) || json_last_error())
throw new \Exception('JSON decode error');
throw new Exception('JSON decode error');

return $result;

Expand All @@ -117,14 +119,29 @@ private function request($url, $params = [], $method = 'GET')
* Get ticker
*
* @return mixed
* @throws \Exception
* @throws Exception
*/
public function getTicker($symbol)
{
$data = [
'symbol' => $symbol
];
return $this->request('v3/ticker/price', $data);
return $this->request('v3/ticker/price?symbol=' . $symbol , $data);
}

/**
* Get ticker
*
* @param $symbol
* @return mixed
* @throws Exception
*/
public function getAvgPrice($symbol)
{
$data = [
'symbol' => $symbol
];
return $this->request('v3/avgPrice?symbol='.$symbol, $data);
}

public function getCurrencies()
Expand All @@ -134,7 +151,6 @@ public function getCurrencies()
}



//------ PRIVATE API CALLS ----------
/*
* getBalances
Expand All @@ -153,7 +169,7 @@ public function getCurrencies()
* Current exchange trading rules and symbol information
*
* @return mixed
* @throws \Exception
* @throws Exception
*/
public function getMarkets()
{
Expand All @@ -165,14 +181,12 @@ public function getMarkets()
* Get current account information
*
* @return mixed
* @throws \Exception
* @throws Exception
*/
public function getBalances()
{

$b = $this->privateRequest('v3/account');
return $b['balances'];

}

/**
Expand All @@ -182,7 +196,7 @@ public function getBalances()
* @param array $params Required and optional parameters
* @param string $method GET, POST, PUT, DELETE
* @return mixed
* @throws \Exception
* @throws Exception
*/
private function privateRequest($url, $params = [], $method = 'GET')
{
Expand Down Expand Up @@ -217,12 +231,12 @@ private function privateRequest($url, $params = [], $method = 'GET')
//Get result
$result = curl_exec($this->curl);
if ($result === false)
throw new \Exception('CURL error: ' . curl_error($this->curl));
throw new Exception('CURL error: ' . curl_error($this->curl));

// decode results
$result = json_decode($result, true);
if (!is_array($result) || json_last_error())
throw new \Exception('JSON decode error');
throw new Exception('JSON decode error');

return $result;

Expand All @@ -234,7 +248,7 @@ private function privateRequest($url, $params = [], $method = 'GET')
* @param string $symbol Currency pair
* @param int $limit Limit of trades. Max. 500
* @return mixed
* @throws \Exception
* @throws Exception
*/
public function getRecentTrades($symbol = 'BNBBTC', $limit = 500)
{
Expand All @@ -250,22 +264,17 @@ public function getRecentTrades($symbol = 'BNBBTC', $limit = 500)

public function getOpenOrders()
{


$b = $this->privateRequest('v3/openOrders');
return $b;

}

public function getAllOrders($symbol)
{

$data = [
'symbol' => $symbol
];
$b = $this->privateRequest('v3/allOrders', $data);
return $b;

}

/**
Expand All @@ -274,7 +283,7 @@ public function getAllOrders($symbol)
* @param string $symbol Asset pair to trade
* @param string $quantity Amount of trade asset
* @return mixed
* @throws \Exception
* @throws Exception
*/
public function marketSell($symbol, $quantity)
{
Expand All @@ -290,7 +299,7 @@ public function marketSell($symbol, $quantity)
* @param string $type MARKET, LIMIT, STOP_LOSS, STOP_LOSS_LIMIT, TAKE_PROFIT, TAKE_PROFIT_LIMIT, LIMIT_MAKER
* @param bool $price Limit price
* @return mixed
* @throws \Exception
* @throws Exception
*/
public function trade($symbol, $quantity, $side, $type = 'MARKET', $price = false)
{
Expand All @@ -315,7 +324,7 @@ public function trade($symbol, $quantity, $side, $type = 'MARKET', $price = fals
* @param string $symbol Asset pair to trade
* @param string $quantity Amount of trade asset
* @return mixed
* @throws \Exception
* @throws Exception
*/
public function marketBuy($symbol, $quantity)
{
Expand All @@ -329,7 +338,7 @@ public function marketBuy($symbol, $quantity)
* @param string $quantity Amount of trade asset
* @param float $price Limit price to sell
* @return mixed
* @throws \Exception
* @throws Exception
*/
public function limitSell($symbol, $quantity, $price)
{
Expand All @@ -345,7 +354,7 @@ public function limitSell($symbol, $quantity, $price)
* @param string $quantity Amount of trade asset
* @param float $price Limit price to buy
* @return mixed
* @throws \Exception
* @throws Exception
*/
public function limitBuy($symbol, $quantity, $price)
{
Expand All @@ -356,12 +365,12 @@ public function limitBuy($symbol, $quantity, $price)
* Deposit Address
* @param string $symbol Asset symbol
* @return mixed
**/
*
* @throws Exception
*/
public function depositAddress($symbol)
{

return $this->wapiRequest("v3/depositAddress.html", ['asset' => $symbol]);

}

/**
Expand All @@ -371,7 +380,7 @@ public function depositAddress($symbol)
* @param array $params Required and optional parameters
* @param string $method GET, POST, PUT, DELETE
* @return mixed
* @throws \Exception
* @throws Exception
*/
private function wapiRequest($url, $params = [], $method = 'GET')
{
Expand Down Expand Up @@ -406,12 +415,12 @@ private function wapiRequest($url, $params = [], $method = 'GET')
//Get result
$result = curl_exec($this->curl);
if ($result === false)
throw new \Exception('CURL error: ' . curl_error($this->curl));
throw new Exception('CURL error: ' . curl_error($this->curl));

// decode results
$result = json_decode($result, true);
if (!is_array($result) || json_last_error())
throw new \Exception('JSON decode error');
throw new Exception('JSON decode error');

return $result;

Expand Down

0 comments on commit 99d8e63

Please sign in to comment.