Skip to content

Commit

Permalink
Example/Test files for main functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dmzoneill committed Mar 12, 2018
1 parent e510760 commit cc74123
Show file tree
Hide file tree
Showing 20 changed files with 333 additions and 11 deletions.
100 changes: 100 additions & 0 deletions examples/0.example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
require '../vendor/autoload.php';
$api = new Binance\API("<api key>","<secret>");

// 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";
});
11 changes: 11 additions & 0 deletions examples/agg_trades.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

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

// @see home_directory_config.php
// use config from ~/.confg/jaggedsoft/php-binance-api.json
$api = new Binance\API();

// Aggregate Trades List
$trades = $api->aggTrades("BNBBTC");
print_r($trades);
11 changes: 11 additions & 0 deletions examples/balances.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

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

// @see home_directory_config.php
// use config from ~/.confg/jaggedsoft/php-binance-api.json
$api = new Binance\API();

// Get all of your positions, including estimated BTC value
$balances = $api->balances();
print_r($balances);
11 changes: 11 additions & 0 deletions examples/book_prices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

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

// @see home_directory_config.php
// use config from ~/.confg/jaggedsoft/php-binance-api.json
$api = new Binance\API();

// Get all bid/ask prices
$bookPrices = $api->bookPrices();
print_r($bookPrices);
12 changes: 12 additions & 0 deletions examples/candles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

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

// @see home_directory_config.php
// use config from ~/.confg/jaggedsoft/php-binance-api.json
$api = new Binance\API();

// 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);
11 changes: 11 additions & 0 deletions examples/depth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

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

// @see home_directory_config.php
// use config from ~/.confg/jaggedsoft/php-binance-api.json
$api = new Binance\API();

// Get Market Depth
$depth = $api->depth("ETHBTC");
print_r($depth);
7 changes: 0 additions & 7 deletions examples/get_balance.php

This file was deleted.

12 changes: 12 additions & 0 deletions examples/get_prices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

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

// @see home_directory_config.php
// use config from ~/.confg/jaggedsoft/php-binance-api.json
$api = new Binance\API();

// 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";
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

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

/*
mkdir -vp ~/.config/jaggedsoft/
cat > ~/.config/jaggedsoft/php-binance-api.json << EOF
{
"api-key": "<api key>",
"api-secret": "<secret>"
}
*/

$api = new Binance\API();

$ticker = $api->prices();
Expand Down
11 changes: 11 additions & 0 deletions examples/open_orders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

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

// @see home_directory_config.php
// use config from ~/.confg/jaggedsoft/php-binance-api.json
$api = new Binance\API();

// Get Open Orders
$openorders = $api->openOrders("BNBBTC");
print_r($openorders);
42 changes: 42 additions & 0 deletions examples/orders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

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

// @see home_directory_config.php
// use config from ~/.confg/jaggedsoft/php-binance-api.json
$api = new Binance\API();

// Place a LIMIT order
$quantity = 1;
$price = 0.0005;
$order = $api->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);
42 changes: 42 additions & 0 deletions examples/orders_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

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

// @see home_directory_config.php
// use config from ~/.confg/jaggedsoft/php-binance-api.json
$api = new Binance\API();

// Place a LIMIT order
$quantity = 1000;
$price = 0.0005;
$order = $api->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);
4 changes: 3 additions & 1 deletion examples/proxy.php → examples/proxy_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
'pass' => 'd00d'
];

$api = new Binance\API("<key>","<secret>",["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
Expand Down
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions examples/trade_history.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

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

// @see home_directory_config.php
// use config from ~/.confg/jaggedsoft/php-binance-api.json
$api = new Binance\API();

// Get Trade History
$history = $api->history("TRXBTC");
print_r($history);
13 changes: 13 additions & 0 deletions examples/web_socket_chart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

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

// @see home_directory_config.php
// use config from ~/.confg/jaggedsoft/php-binance-api.json
$api = new Binance\API();

// Get complete realtime chart data via WebSockets
$api->chart(["BNBBTC"], "15m", function($api, $symbol, $chart) {
echo "{$symbol} chart update\n";
print_r($chart);
});
18 changes: 18 additions & 0 deletions examples/web_socket_depth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

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

// @see home_directory_config.php
// use config from ~/.confg/jaggedsoft/php-binance-api.json
$api = new Binance\API();

$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";
});
13 changes: 13 additions & 0 deletions examples/web_socket_trades.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

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

// @see home_directory_config.php
// use config from ~/.confg/jaggedsoft/php-binance-api.json
$api = new Binance\API();

// Trade Updates via WebSocket
$api->trades(["BNBBTC"], function($api, $symbol, $trades) {
echo "{$symbol} trades update".PHP_EOL;
print_r($trades);
});
6 changes: 3 additions & 3 deletions php-binance-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit cc74123

Please sign in to comment.