From cc74123db26950670e899a41b74597f7023ec637 Mon Sep 17 00:00:00 2001 From: David O Neill Date: Mon, 12 Mar 2018 21:23:59 +0000 Subject: [PATCH] Example/Test files for main functions --- examples/0.example.php | 100 ++++++++++++++++++ examples/agg_trades.php | 11 ++ examples/balances.php | 11 ++ examples/book_prices.php | 11 ++ examples/candles.php | 12 +++ examples/depth.php | 11 ++ examples/get_balance.php | 7 -- examples/get_prices.php | 12 +++ ...ryconfig.php => home_directory_config.php} | 9 ++ examples/open_orders.php | 11 ++ examples/orders.php | 42 ++++++++ examples/orders_test.php | 42 ++++++++ examples/{proxy.php => proxy_config.php} | 4 +- examples/{ => single}/binance-api-single.php | 0 examples/{ => single}/single.php | 0 examples/trade_history.php | 11 ++ examples/web_socket_chart.php | 13 +++ examples/web_socket_depth.php | 18 ++++ examples/web_socket_trades.php | 13 +++ php-binance-api.php | 6 +- 20 files changed, 333 insertions(+), 11 deletions(-) create mode 100644 examples/0.example.php create mode 100644 examples/agg_trades.php create mode 100644 examples/balances.php create mode 100644 examples/book_prices.php create mode 100644 examples/candles.php create mode 100644 examples/depth.php delete mode 100644 examples/get_balance.php create mode 100644 examples/get_prices.php rename examples/{homedirectoryconfig.php => home_directory_config.php} (55%) create mode 100644 examples/open_orders.php create mode 100644 examples/orders.php create mode 100644 examples/orders_test.php rename examples/{proxy.php => proxy_config.php} (83%) rename examples/{ => single}/binance-api-single.php (100%) rename examples/{ => single}/single.php (100%) create mode 100644 examples/trade_history.php create mode 100644 examples/web_socket_chart.php create mode 100644 examples/web_socket_depth.php create mode 100644 examples/web_socket_trades.php diff --git a/examples/0.example.php b/examples/0.example.php new file mode 100644 index 00000000..ec4b0334 --- /dev/null +++ b/examples/0.example.php @@ -0,0 +1,100 @@ +",""); + +// 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"; + +// Get all of your positions, including estimated BTC value +$balances = $api->balances($ticker); +print_r($balances); + +// Get all bid/ask prices +$bookPrices = $api->bookPrices(); +print_r($bookPrices); +echo "BTC owned: ".$balances['BTC']['available']."\n"; +echo "ETH owned: ".$balances['ETH']['available']."\n"; +echo "Estimated Value: ".$api->btc_value." BTC\n"; + +// Place a LIMIT order +//$quantity = 1; +//$price = 0.0005; +//$order = $api->buy("BNBBTC", $quantity, $price); + +// Place a MARKET order +//$quantity = 1; +//$order = $api->buy("BNBBTC", $quantity, 0, "MARKET"); + +// Place a STOP LOSS order +// When the stop is reached, a stop order becomes a market order +//$quantity = 1; +//$price = 0.5; // Try to sell it for 0.5 btc +//$stopPrice = 0.4; // Sell immediately if price goes below 0.4 btc +//$order = $api->sell("BNBBTC", $quantity, $price, "LIMIT", ["stopPrice"=>$stopPrice]); +//print_r($order); + +// Place an ICEBERG order +// Iceberg orders are intended to conceal the true order quantity. +//$quantity = 1; +//$price = 0.5; +//$icebergQty = 10; +//$order = $api->sell("BNBBTC", $quantity, $price, "LIMIT", ["icebergQty"=>$icebergQty]); +//print_r($order); + +// Get Market Depth +//$depth = $api->depth("ETHBTC"); +//print_r($depth); + +// Get Open Orders +//$openorders = $api->openOrders("BNBBTC"); +//print_r($openorders); + +// Get Order Status +//$orderid = "7610385"; +//$orderstatus = $api->orderStatus("ETHBTC", $orderid); +//print_r($orderstatus); + +// Cancel an Order +//$response = $api->cancel("ETHBTC", $orderid); +//print_r($response); + +// Get Trade History +//$history = $api->history("BNBBTC"); +//print_r($history); + +// Get Kline/candlestick data for a symbol +// Periods: 1m,3m,5m,15m,30m,1h,2h,4h,6h,8h,12h,1d,3d,1w,1M +$ticks = $api->candlesticks("BNBBTC", "5m"); +print_r($ticks); + +// Aggregate Trades List +//$trades = $api->aggTrades("BNBBTC"); +//print_r($trades); + +// Trade Updates via WebSocket +//$api->trades(["BNBBTC"], function($api, $symbol, $trades) { +// echo "{$symbol} trades update".PHP_EOL; +// print_r($trades); +//}); + + +// Get complete realtime chart data via WebSockets +//$api->chart(["BNBBTC"], "15m", function($api, $symbol, $chart) { +// echo "{$symbol} chart update\n"; +// print_r($chart); +//}); + + +// Grab realtime updated depth cache via WebSockets +$api->depthCache(["BNBBTC"], function($api, $symbol, $depth) { + echo "{$symbol} depth cache update\n"; + $limit = 11; // Show only the closest asks/bids + $sorted = $api->sortDepth($symbol, $limit); + $bid = $api->first($sorted['bids']); + $ask = $api->first($sorted['asks']); + echo $api->displayDepth($sorted); + echo "ask: {$ask}\n"; + echo "bid: {$bid}\n"; +}); diff --git a/examples/agg_trades.php b/examples/agg_trades.php new file mode 100644 index 00000000..d67dbc44 --- /dev/null +++ b/examples/agg_trades.php @@ -0,0 +1,11 @@ +aggTrades("BNBBTC"); +print_r($trades); diff --git a/examples/balances.php b/examples/balances.php new file mode 100644 index 00000000..6ba75d96 --- /dev/null +++ b/examples/balances.php @@ -0,0 +1,11 @@ +balances(); +print_r($balances); diff --git a/examples/book_prices.php b/examples/book_prices.php new file mode 100644 index 00000000..5f133a9e --- /dev/null +++ b/examples/book_prices.php @@ -0,0 +1,11 @@ +bookPrices(); +print_r($bookPrices); diff --git a/examples/candles.php b/examples/candles.php new file mode 100644 index 00000000..e9eab46d --- /dev/null +++ b/examples/candles.php @@ -0,0 +1,12 @@ +candlesticks("BNBBTC", "5m"); +print_r($ticks); diff --git a/examples/depth.php b/examples/depth.php new file mode 100644 index 00000000..0252f9be --- /dev/null +++ b/examples/depth.php @@ -0,0 +1,11 @@ +depth("ETHBTC"); +print_r($depth); diff --git a/examples/get_balance.php b/examples/get_balance.php deleted file mode 100644 index 754a6c6c..00000000 --- a/examples/get_balance.php +++ /dev/null @@ -1,7 +0,0 @@ -require 'vendor/autoload.php'; -$api = new Binance\API("",""); -$ticker = $api->prices(); -//print_r($ticker); // List prices of all symbols -//echo "Price of BNB: {$ticker['BNBBTC']} BTC.".PHP_EOL; -$balances = $api->balances($ticker); -print_r($balances); diff --git a/examples/get_prices.php b/examples/get_prices.php new file mode 100644 index 00000000..976902ac --- /dev/null +++ b/examples/get_prices.php @@ -0,0 +1,12 @@ +prices(); +print_r($ticker); // List prices of all symbols +echo "Price of BNB: {$ticker['BNBBTC']} BTC.\n"; diff --git a/examples/homedirectoryconfig.php b/examples/home_directory_config.php similarity index 55% rename from examples/homedirectoryconfig.php rename to examples/home_directory_config.php index 4c351e87..45605ead 100644 --- a/examples/homedirectoryconfig.php +++ b/examples/home_directory_config.php @@ -2,6 +2,15 @@ require '../php-binance-api.php'; +/* +mkdir -vp ~/.config/jaggedsoft/ +cat > ~/.config/jaggedsoft/php-binance-api.json << EOF +{ + "api-key": "", + "api-secret": "" +} +*/ + $api = new Binance\API(); $ticker = $api->prices(); diff --git a/examples/open_orders.php b/examples/open_orders.php new file mode 100644 index 00000000..6440bb83 --- /dev/null +++ b/examples/open_orders.php @@ -0,0 +1,11 @@ +openOrders("BNBBTC"); +print_r($openorders); diff --git a/examples/orders.php b/examples/orders.php new file mode 100644 index 00000000..c978430b --- /dev/null +++ b/examples/orders.php @@ -0,0 +1,42 @@ +buy("BNBBTC", $quantity, $price); +print_r($order); + +// Place a MARKET order +$quantity = 1; +$order = $api->buy("BNBBTC", $quantity, 0, "MARKET"); +print_r($order); + +// Place a STOP LOSS order +// When the stop is reached, a stop order becomes a market order +$quantity = 1; +$price = 0.5; // Try to sell it for 0.5 btc +$stopPrice = 0.4; // Sell immediately if price goes below 0.4 btc +$order = $api->sell("BNBBTC", $quantity, $price, "STOP_LOSS", ["stopPrice"=>$stopPrice]); +print_r($order); + +// Place a take profit order +// When the stop is reached, a stop order becomes a market order +$quantity = 1; +$price = 0.5; // Try to sell it for 0.5 btc +$stopPrice = 0.4; // Sell immediately if price goes below 0.4 btc +$order = $api->sell("TRXBTC", $quantity, $price, "TAKE_PROFIT", ["stopPrice"=>$stopPrice]); +print_r($order); + +// Place an ICEBERG order +// Iceberg orders are intended to conceal the true order quantity. +$quantity = 20; +$price = 0.5; +$icebergQty = 10; +$order = $api->sell("BNBBTC", $quantity, $price, "LIMIT", ["icebergQty"=>$icebergQty]); +print_r($order); diff --git a/examples/orders_test.php b/examples/orders_test.php new file mode 100644 index 00000000..7f9a968a --- /dev/null +++ b/examples/orders_test.php @@ -0,0 +1,42 @@ +buyTest("BNBBTC", $quantity, $price); +print_r($order); + +// Place a MARKET order +$quantity = 1; +$order = $api->buyTest("BNBBTC", $quantity, 0, "MARKET"); +print_r($order); + +// Place a STOP LOSS order +// When the stop is reached, a stop order becomes a market order +$quantity = 1; +$price = 0.5; // Try to sell it for 0.5 btc +$stopPrice = 0.4; // Sell immediately if price goes below 0.4 btc +$order = $api->sellTest("BNBBTC", $quantity, $price, "STOP_LOSS", ["stopPrice"=>$stopPrice]); +print_r($order); + +// Place a take profit order +// When the stop is reached, a stop order becomes a market order +$quantity = 1; +$price = 0.5; // Try to sell it for 0.5 btc +$stopPrice = 0.4; // Sell immediately if price goes below 0.4 btc +$order = $api->sellTest("TRXBTC", $quantity, $price, "TAKE_PROFIT", ["stopPrice"=>$stopPrice]); +print_r($order); + +// Place an ICEBERG order +// Iceberg orders are intended to conceal the true order quantity. +$quantity = 20; +$price = 0.5; +$icebergQty = 10; +$order = $api->sellTest("BNBBTC", $quantity, $price, "LIMIT", ["icebergQty"=>$icebergQty]); +print_r($order); diff --git a/examples/proxy.php b/examples/proxy_config.php similarity index 83% rename from examples/proxy.php rename to examples/proxy_config.php index 948472d0..0cb3af36 100644 --- a/examples/proxy.php +++ b/examples/proxy_config.php @@ -10,7 +10,9 @@ 'pass' => 'd00d' ]; -$api = new Binance\API("","",["useServerTime"=>false],$proxyConf); +// @see home_directory_config.php +// 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 diff --git a/examples/binance-api-single.php b/examples/single/binance-api-single.php similarity index 100% rename from examples/binance-api-single.php rename to examples/single/binance-api-single.php diff --git a/examples/single.php b/examples/single/single.php similarity index 100% rename from examples/single.php rename to examples/single/single.php diff --git a/examples/trade_history.php b/examples/trade_history.php new file mode 100644 index 00000000..e1e47530 --- /dev/null +++ b/examples/trade_history.php @@ -0,0 +1,11 @@ +history("TRXBTC"); +print_r($history); diff --git a/examples/web_socket_chart.php b/examples/web_socket_chart.php new file mode 100644 index 00000000..ac60ea55 --- /dev/null +++ b/examples/web_socket_chart.php @@ -0,0 +1,13 @@ +chart(["BNBBTC"], "15m", function($api, $symbol, $chart) { + echo "{$symbol} chart update\n"; + print_r($chart); +}); diff --git a/examples/web_socket_depth.php b/examples/web_socket_depth.php new file mode 100644 index 00000000..2b19fa7a --- /dev/null +++ b/examples/web_socket_depth.php @@ -0,0 +1,18 @@ +depthCache(["BNBBTC"], function($api, $symbol, $depth) { + echo "{$symbol} depth cache update\n"; + $limit = 11; // Show only the closest asks/bids + $sorted = $api->sortDepth($symbol, $limit); + $bid = $api->first($sorted['bids']); + $ask = $api->first($sorted['asks']); + echo $api->displayDepth($sorted); + echo "ask: {$ask}\n"; + echo "bid: {$bid}\n"; +}); diff --git a/examples/web_socket_trades.php b/examples/web_socket_trades.php new file mode 100644 index 00000000..e7a5fc15 --- /dev/null +++ b/examples/web_socket_trades.php @@ -0,0 +1,13 @@ +trades(["BNBBTC"], function($api, $symbol, $trades) { + echo "{$symbol} trades update".PHP_EOL; + print_r($trades); +}); diff --git a/php-binance-api.php b/php-binance-api.php index 1b927717..24853ac7 100644 --- a/php-binance-api.php +++ b/php-binance-api.php @@ -30,7 +30,7 @@ public function __construct($api_key = '', $api_secret = '', $options = ["useSer $this->api_secret = $api_secret; $this->proxyConf = $proxyConf; - if ( isset($options['useServerTime']) && $options['useServerTime'] ) { + if(isset($options['useServerTime']) && $options['useServerTime']) { $this->useServerTime(); } @@ -46,7 +46,7 @@ private function setupApiConfigFromFile() } $contents = json_decode(file_get_contents(getenv("HOME") . "/.config/jaggedsoft/php-binance-api.json"), true); $this->api_key = isset($contents['api-key']) ? $contents['api-key'] : ""; - $this->api_secret = isset($contents['api_secret']) ? $contents['api_secret'] : ""; + $this->api_secret = isset($contents['api-secret']) ? $contents['api-secret'] : ""; } public function buy($symbol, $quantity, $price, $type = "LIMIT", $flags = []) { return $this->order("BUY", $symbol, $quantity, $price, $type, $flags); @@ -133,7 +133,7 @@ public function aggTrades($symbol) { } public function depth($symbol) { $json = $this->httpRequest("v1/depth", "GET", ["symbol"=>$symbol]); - if ( !isset($this->info[$symbol]) ) $this->info[$symbol] = []; + if(!isset($this->info[$symbol])) $this->info[$symbol] = []; $this->info[$symbol]['firstUpdate'] = $json['lastUpdateId']; return $this->depthData($symbol, $json); }