Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
add roundStep function to help with stepSize
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Eyrick authored Sep 7, 2018
2 parents 22f0455 + 9752109 commit 1a72414
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 24 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@ $api = new Binance\API( "somefile.json" );
$api->caOverride = true;
```

#### Get latest price of a symbol
#### Get latest price of all symbols
```php
$ticker = $api->prices();
print_r($ticker); // List prices of all symbols
echo "Price of BNB: {$ticker['BNBBTC']} BTC.".PHP_EOL;
print_r($ticker);
```

<details>
Expand Down Expand Up @@ -172,7 +171,13 @@ Price of BNB: 0.00021479 BTC.
```
</details>

### Get miniTicker for all symbols
#### Get latest price of a symbol
```php
$price = $api->price("BNBBTC");
echo "Price of BNB: {$price} BTC.".PHP_EOL;
```

#### Get miniTicker for all symbols
```php
$api->miniTicker(function($api, $ticker) {
print_r($ticker);
Expand Down
11 changes: 7 additions & 4 deletions examples/0.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
require '../vendor/autoload.php';
$api = new Binance\API("<api key>","<secret>");

// Get latest price of all symbols
$tickers = $api->prices();
print_r($tickers); // List prices of all symbols

// Get latest price of a symbol
$ticker = $api->prices();
print_r($ticker); // List prices of all symbols
echo "Price of BNB: {$ticker['BNBBTC']} BTC.\n";
$price = $api->price('BNBBTC');
echo "Price of BNB: {$price} BTC.\n";

// Get all of your positions, including estimated BTC value
$balances = $api->balances($ticker);
$balances = $api->balances($tickers);
print_r($balances);

// Get all bid/ask prices
Expand Down
9 changes: 6 additions & 3 deletions examples/get_prices.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
// use config from ~/.confg/jaggedsoft/php-binance-api.json
$api = new Binance\API();

// Get latest price of all symbols
$tickers = $api->prices();
print_r($tickers); // List prices of all symbols

// Get latest price of a symbol
$ticker = $api->prices();
print_r($ticker); // List prices of all symbols
echo "Price of BNB: {$ticker['BNBBTC']} BTC.\n";
$price = $api->price('BNBBTC');
echo "Price of BNB: {$price} BTC.\n";
5 changes: 2 additions & 3 deletions examples/home_directory_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@

$api = new Binance\API();

$ticker = $api->prices();
print_r($ticker); // List prices of all symbols
echo "Price of BNB: {$ticker['BNBBTC']} BTC.".PHP_EOL;
$tickers = $api->prices();
print_r($tickers); // List prices of all symbols
7 changes: 3 additions & 4 deletions examples/proxy_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
// use config from ~/.confg/jaggedsoft/php-binance-api.json
$api = new Binance\API("","",["useServerTime"=>false],$proxyConf);

$ticker = $api->prices();
print_r($ticker); // List prices of all symbols
echo "Price of BNB: {$ticker['BNBBTC']} BTC.".PHP_EOL;
$tickers = $api->prices();
print_r($tickers); // List prices of all symbols

// Get balances for all of your positions, including estimated BTC value
$balances = $api->balances($ticker);
$balances = $api->balances($tickers);
print_r($balances);
echo "BTC owned: ".$balances['BTC']['available'].PHP_EOL;
echo "ETH owned: ".$balances['ETH']['available'].PHP_EOL;
Expand Down
4 changes: 2 additions & 2 deletions examples/ratelimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
$api = new Binance\RateLimiter($api);

// Get latest price of a symbol
$ticker = $api->prices();
echo "Price of BNB: {$ticker['BNBBTC']} BTC.\n";
$price = $api->price("BNBBTC");
echo "Price of BNB: {$price} BTC.\n";

while(true)
{
Expand Down
12 changes: 8 additions & 4 deletions examples/single/single.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
require 'binance-api-single.php';
$api = new Binance("<key>","<secret>");

$ticker = $api->prices();
print_r($ticker); // List prices of all symbols
echo "Price of BNB: {$ticker['BNBBTC']} BTC.".PHP_EOL;
// Get latest price of all symbols
$tickers = $api->prices();
print_r($tickers); // List prices of all symbols

// Get latest price of a symbol
$price = $api->price("BNBBTC");
echo "Price of BNB: {$price} BTC.".PHP_EOL;

// Get balances for all of your positions, including estimated BTC value
$balances = $api->balances($ticker);
$balances = $api->balances($tickers);
print_r($balances);
echo "BTC owned: ".$balances['BTC']['available'].PHP_EOL;
echo "ETH owned: ".$balances['ETH']['available'].PHP_EOL;
Expand Down
27 changes: 27 additions & 0 deletions php-binance-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,21 @@ public function prices()
return $this->priceData($this->httpRequest("v3/ticker/price"));
}

/**
* price get the latest price of a symbol
*
* $price = $api->price( "ETHBTC" );
*
* @return array with error message or array with symbol price
* @throws \Exception
*/
public function price(string $symbol)
{
$ticker = $this->httpRequest("v3/ticker/price", "GET", ["symbol" => $symbol]);

return $ticker['price'];
}

/**
* bookPrices get all bid/asks prices
*
Expand Down Expand Up @@ -1481,6 +1496,18 @@ private function depthData(string $symbol, array $json)
"asks" => $asks,
];
}

/**
* roundStep rounds number with given step
* @param $value price
* @param $stepSize parameter from exchangeInfo
* @return rounded value. example: roundStep(1.2345, 0.1) = 1.2
*
*/
public function roundStep($value, $stepSize = 0.1) {
$precision = strlen(substr(strrchr(rtrim($value,'0'), '.'), 1));
return round((($value / $stepSize) | 0) * $stepSize, $precision);
}

/**
* getTransfered gets the total transfered in b,Kb,Mb,Gb
Expand Down

0 comments on commit 1a72414

Please sign in to comment.