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

Commit

Permalink
Get all openOrders (Thank you omar8792!!) Various improvements by dmz…
Browse files Browse the repository at this point in the history
…oneill
  • Loading branch information
Jon Eyrick authored Mar 19, 2018
2 parents e2e54f6 + 788ec48 commit 8953dd4
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 61 deletions.
10 changes: 8 additions & 2 deletions composer-test.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"require-dev": {
"phpunit/phpunit": "^6",
"php-coveralls/php-coveralls": "^2.0"
"phpunit/phpunit": "^6",
"php-coveralls/php-coveralls": "^2.0",
"ext-curl": "*",
"ratchet/pawl": "^0.3.0",
"react/socket": "^1.0 || ^0.8 || ^0.7",
"ratchet/rfc6455": "^0.2.3",
"react/promise": "2.2.0",
"react/event-loop": "0.4.1"
}
}
89 changes: 78 additions & 11 deletions php-binance-api-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
declare(strict_types=1);

require 'php-binance-api.php';
require 'vendor/autoload.php';

file_put_contents( getenv("HOME") . "/.config/jaggedsoft/php-binance-api.json" ,
"{ \"api-key\": \"z5RQZ9n8JcS3HLDQmPpfLQIGGQN6TTs5pCP5CTnn4nYk2ImFcew49v4ZrmP3MGl5\",
Expand Down Expand Up @@ -206,6 +207,7 @@ public function testHistory() {
}
public function testUseServerTime() {
fwrite(STDOUT, __METHOD__ . "\n");
$this->_testable->useServerTime();
$this->assertTrue( true );
}
public function testTime() {
Expand Down Expand Up @@ -240,19 +242,41 @@ public function testExchangeInfo() {
}
public function testWithdraw() {
fwrite(STDOUT, __METHOD__ . "\n");
$asset = "BTC";
$address = "1C5gqLRs96Xq4V2ZZAR1347yUCpHie7sa";
$amount = 0.2;
$result = $this->_testable->withdraw($asset, $address, $amount, false);
$this->assertTrue( true );
if( isset( $result['code'] ) ) {
fwrite(STDOUT, __METHOD__ . ": withdraw error: " . $result['code'] . ":" . $result['msg'] ."\n");
}
}
public function testDepositAddress() {
fwrite(STDOUT, __METHOD__ . "\n");
$asset = "BTC";
$result = $this->_testable->depositAddress($asset);
$this->assertTrue( true );
if( isset( $result['code'] ) ) {
fwrite(STDOUT, __METHOD__ . ": depsoit error: " . $result['code'] . ":" . $result['msg'] ."\n");
}
}
public function testDepositHistory() {
fwrite(STDOUT, __METHOD__ . "\n");
$asset = "BTC";
$result = $this->_testable->depositHistory();
$this->assertTrue( true );
if( isset( $result['code'] ) ) {
fwrite(STDOUT, __METHOD__ . ": deposit history error: " . $result['code'] . ":" . $result['msg'] ."\n");
}
}
public function testWithdrawHistory() {
fwrite(STDOUT, __METHOD__ . "\n");
$asset = "BTC";
$result = $this->_testable->withdrawHistory();
$this->assertTrue( true );
if( isset( $result['code'] ) ) {
fwrite(STDOUT, __METHOD__ . ": withdraw history error: " . $result['code'] . ":" . $result['msg'] ."\n");
}
}
public function testPrices() {
fwrite(STDOUT, __METHOD__ . "\n");
Expand Down Expand Up @@ -308,14 +332,26 @@ public function testBalances() {
fwrite(STDOUT, __METHOD__ . "\n");
$result = $this->_testable->balances();
$this->assertTrue( ( isset( $result['code'] ) == false ) );
$this->assertTrue( is_array( $result ) );

if( isset( $result['code'] ) ) {
fwrite(STDOUT, __METHOD__ . ": balances error: " . $result['code'] . ":" . $result['msg'] ."\n");
}
}
public function testGetProxyUriString() {
fwrite(STDOUT, __METHOD__ . "\n");
$this->assertTrue( true );

$proxyConf = [
'proto' => 'http',
'address' => '192.168.1.1',
'port' => '8080',
'user' => 'dude',
'pass' => 'd00d'
];

$this->_testable->setProxy( $proxyConf );
$uri = $this->_testable->getProxyUriString();
$this->assertTrue( $uri == $proxyConf['proto'] . "://" . $proxyConf['address'] . ":" . $proxyConf['port'] );
}
public function testHttpRequest() {
fwrite(STDOUT, __METHOD__ . "\n");
Expand All @@ -331,11 +367,22 @@ public function testOrderTest() {
}
public function testCandlesticks() {
fwrite(STDOUT, __METHOD__ . "\n");
$this->assertTrue( true );
$result = $this->_testable->candlesticks("BNBBTC", "5m");
$this->assertTrue( is_array( $result ) );

if( isset( $result['code'] ) ) {
fwrite(STDOUT, __METHOD__ . ": candlesticks error: " . $result['code'] . ":" . $result['msg'] ."\n");
}
}
public function testBalanceData() {
fwrite(STDOUT, __METHOD__ . "\n");
$this->assertTrue( true );
$ticker = $this->_testable->prices();
$result = $this->_testable->balances( $ticker );
$this->assertTrue( is_array( $result ) );

if( isset( $result['code'] ) ) {
fwrite(STDOUT, __METHOD__ . ": balances error: " . $result['code'] . ":" . $result['msg'] ."\n");
}
}
public function testBalanceHandler() {
fwrite(STDOUT, __METHOD__ . "\n");
Expand All @@ -353,10 +400,6 @@ public function testChartData() {
fwrite(STDOUT, __METHOD__ . "\n");
$this->assertTrue( true );
}
public function testTradesData() {
fwrite(STDOUT, __METHOD__ . "\n");
$this->assertTrue( true );
}
public function testBookPriceData() {
fwrite(STDOUT, __METHOD__ . "\n");
$this->assertTrue( true );
Expand All @@ -367,7 +410,17 @@ public function testPriceData() {
}
public function testCumulative() {
fwrite(STDOUT, __METHOD__ . "\n");
$this->assertTrue( true );
$result = $this->_testable->depth( "TRXBTC" );
$this->assertTrue( ( isset( $result['code'] ) == false ) );
$this->assertTrue( isset( $result['bids'] ) );
$this->assertTrue( isset( $result['asks'] ) );
$result = $this->_testable->cumulative( $result );
$this->assertTrue( isset( $result['bids'] ) );
$this->assertTrue( isset( $result['asks'] ) );
$this->assertTrue( is_array( $result['bids'] ) );
$this->assertTrue( is_array( $result['asks'] ) );
$this->assertTrue( count( $result['bids'] ) > 0);
$this->assertTrue( count( $result['asks'] ) > 0);
}
public function testHighstock() {
fwrite(STDOUT, __METHOD__ . "\n");
Expand All @@ -383,15 +436,27 @@ public function testChartHandler() {
}
public function testFirst() {
fwrite(STDOUT, __METHOD__ . "\n");
$this->assertTrue( true );
$arr = array( "one" => 6, "two" => 7, "three" => 8 );
$this->assertTrue( $this->_testable->first( $arr ) == "one" );
$arr = array();
$this->assertTrue( $this->_testable->first( $arr ) == null );
}
public function testLast() {
fwrite(STDOUT, __METHOD__ . "\n");
$this->assertTrue( true );
$arr = array( "one" => 6, "two" => 7, "three" => 8 );
$this->assertTrue( $this->_testable->last( $arr ) == "three" );
$arr = array();
$this->assertTrue( $this->_testable->last( $arr ) == null );
}
public function testDisplayDepth() {
fwrite(STDOUT, __METHOD__ . "\n");
$this->assertTrue( true );
$result = $this->_testable->depth( "TRXBTC" );
$this->assertTrue( ( isset( $result['code'] ) == false ) );
$this->assertTrue( isset( $result['bids'] ) );
$this->assertTrue( isset( $result['asks'] ) );
$result = $this->_testable->displayDepth( $result );
$this->assertTrue( is_string( $result ) );
$this->assertTrue( $result != "" );
}
public function testSortDepth() {
fwrite(STDOUT, __METHOD__ . "\n");
Expand Down Expand Up @@ -428,11 +493,13 @@ public function testMiniTicker() {

public function testGetTransfered() {
fwrite(STDOUT, __METHOD__ . "\n");
$result = $this->_testable->getTransfered();
$this->assertTrue( true );
}

public function testGetRequestCount() {
fwrite(STDOUT, __METHOD__ . "\n");
$result = $this->_testable->getRequestCount();
$this->assertTrue( true );
}
}
108 changes: 60 additions & 48 deletions php-binance-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class API {
public $balances = [];
public $btc_value = 0.00; // value of available assets
public $btc_total = 0.00; // value of available + onOrder assets
public function __construct($api_key = '', $api_secret = '', $options = ["useServerTime"=>false], $proxyConf = null ) {
public function __construct($api_key = '', $api_secret = '', $options = ["useServerTime"=>false], $proxyConf = null) {
$this->api_key = $api_key;
$this->api_secret = $api_secret;
$this->proxyConf = $proxyConf;
Expand Down Expand Up @@ -67,7 +67,7 @@ public function marketBuyTest($symbol, $quantity, $flags = []) {
return $this->orderTest("BUY", $symbol, $quantity, 0, "MARKET", $flags);
}
public function marketSell($symbol, $quantity, $flags = []) {
return $this->order("SELL", $symbol, $quantity, 0, "MARKET");
return $this->order("SELL", $symbol, $quantity, 0, "MARKET", $flags);
}
public function marketSellTest($symbol, $quantity, $flags = []) {
return $this->orderTest("SELL", $symbol, $quantity, 0, "MARKET", $flags);
Expand All @@ -79,7 +79,10 @@ public function orderStatus($symbol, $orderid) {
return $this->httpRequest("v3/order", "GET" ,["symbol"=>$symbol, "orderId"=>$orderid], true);
}
public function openOrders($symbol) {
return $this->httpRequest("v3/openOrders","GET", ["symbol"=>$symbol], true);
$params = [];
if($symbol)
$params = ["symbol"=>$symbol];
return $this->httpRequest("v3/openOrders","GET", $params, true);
}
public function orders($symbol, $limit = 500, $fromOrderId = 1) {
return $this->httpRequest("v3/allOrders", "GET", ["symbol"=>$symbol, "limit"=>$limit, "orderId"=>$fromOrderId], true);
Expand Down Expand Up @@ -140,8 +143,7 @@ public function depth($symbol) {
public function balances($priceData = false) {
return $this->balanceData($this->httpRequest("v3/account", "GET", [], true), $priceData);
}

private function getProxyUriString()
public function getProxyUriString()
{
$uri = isset( $this->proxyConf['proto'] ) ? $this->proxyConf['proto'] : "http";
// https://curl.haxx.se/libcurl/c/CURLOPT_PROXY.html
Expand All @@ -158,6 +160,9 @@ private function getProxyUriString()
if( isset( $this->proxyConf['address'] ) == false ) echo "warning: proxy port not set defaulting to 1080\n";
return $uri;
}
public function setProxy( $proxyconf ) {
$this->proxyConf = $proxyconf;
}

private function httpRequest($url, $method = "GET", $params = [], $signed = false) {
// is cURL installed yet?
Expand Down Expand Up @@ -491,6 +496,56 @@ public function highstock($chart, $include_volume = false) {
return $array;
}

// Gets first key of an array
public function first($array) {
if(count($array)>0) {
return array_keys($array)[0];
}
return null;
}

// Gets last key of an array
public function last($array) {
if(count($array)>0) {
return array_keys(array_slice($array, -1))[0];
}
return null;
}

// Formats nicely for console output
public function displayDepth($array) {
$output = '';
foreach ( ['asks', 'bids'] as $type ) {
$entries = $array[$type];
if ( $type == 'asks' ) $entries = array_reverse($entries);
$output.= "{$type}:".PHP_EOL;
foreach ( $entries as $price => $quantity ) {
$total = number_format($price * $quantity,8,'.','');
$quantity = str_pad(str_pad(number_format(rtrim($quantity,'.0')),10,' ',STR_PAD_LEFT),15);
$output.= "{$price} {$quantity} {$total}".PHP_EOL;
}
//echo str_repeat('-', 32).PHP_EOL;
}
return $output;
}

// Formats depth data for nice display
private function depthData($symbol, $json) {
$bids = $asks = [];
foreach ( $json['bids'] as $obj ) {
$bids[$obj[0]] = $obj[1];
}
foreach ( $json['asks'] as $obj ) {
$asks[$obj[0]] = $obj[1];
}
return $this->depthCache[$symbol] = ["bids"=>$bids, "asks"=>$asks];
}


////////////////////////////////////
// WebSockets
////////////////////////////////////

// For WebSocket Depth Cache
private function depthHandler($json) {
$symbol = $json['s'];
Expand Down Expand Up @@ -524,33 +579,6 @@ private function chartHandler($symbol, $interval, $json) {
$this->charts[$symbol][$interval][$tick] = ["open"=>$open, "high"=>$high, "low"=>$low, "close"=>$close, "volume"=>$volume];
}

// Gets first key of an array
public function first($array) {
return array_keys($array)[0];
}

// Gets last key of an array
public function last($array) {
return array_keys(array_slice($array, -1))[0];
}

// Formats nicely for console output
public function displayDepth($array) {
$output = '';
foreach ( ['asks', 'bids'] as $type ) {
$entries = $array[$type];
if ( $type == 'asks' ) $entries = array_reverse($entries);
$output.= "{$type}:".PHP_EOL;
foreach ( $entries as $price => $quantity ) {
$total = number_format($price * $quantity,8,'.','');
$quantity = str_pad(str_pad(number_format(rtrim($quantity,'.0')),10,' ',STR_PAD_LEFT),15);
$output.= "{$price} {$quantity} {$total}".PHP_EOL;
}
//echo str_repeat('-', 32).PHP_EOL;
}
return $output;
}

// Sorts depth data for display & getting highest bid and lowest ask
public function sortDepth($symbol, $limit = 11) {
$bids = $this->depthCache[$symbol]['bids'];
Expand All @@ -560,22 +588,6 @@ public function sortDepth($symbol, $limit = 11) {
return ["asks"=> array_slice($asks, 0, $limit, true), "bids"=> array_slice($bids, 0, $limit, true)];
}

// Formats depth data for nice display
private function depthData($symbol, $json) {
$bids = $asks = [];
foreach ( $json['bids'] as $obj ) {
$bids[$obj[0]] = $obj[1];
}
foreach ( $json['asks'] as $obj ) {
$asks[$obj[0]] = $obj[1];
}
return $this->depthCache[$symbol] = ["bids"=>$bids, "asks"=>$asks];
}

////////////////////////////////////
// WebSockets
////////////////////////////////////

// Pulls /depth data and subscribes to @depth WebSocket endpoint
// Maintains a local Depth Cache in sync via lastUpdateId. See depth() and depthHandler()
public function depthCache($symbols, $callback) {
Expand Down

0 comments on commit 8953dd4

Please sign in to comment.