binance public api (markets)

disabled by default, and not enough infos about symbols (no coin label etc)
This commit is contained in:
Tanguy Pruvot 2017-11-01 02:38:45 +01:00
parent bdad68d11b
commit 028d661056
4 changed files with 128 additions and 4 deletions

View file

@ -19,6 +19,7 @@ function BackendPricesUpdate()
updateHitBTCMarkets(); updateHitBTCMarkets();
updateYobitMarkets(); updateYobitMarkets();
updateAlcurexMarkets(); updateAlcurexMarkets();
updateBinanceMarkets();
updateBterMarkets(); updateBterMarkets();
//updateEmpoexMarkets(); //updateEmpoexMarkets();
updateJubiMarkets(); updateJubiMarkets();
@ -653,7 +654,7 @@ function updateYobitMarkets()
$last_checked = cache()->get($exchange.'-deposit_address-check-'.$symbol); $last_checked = cache()->get($exchange.'-deposit_address-check-'.$symbol);
if ($last_checked) continue; if ($last_checked) continue;
sleep(1); // for the tapi nonce sleep(1); // for the api nonce
$address = yobit_api_query2('GetDepositAddress', array("coinName"=>$symbol)); $address = yobit_api_query2('GetDepositAddress', array("coinName"=>$symbol));
if (!empty($address) && isset($address['return']) && $address['success']) { if (!empty($address) && isset($address['return']) && $address['success']) {
$addr = $address['return']['address']; $addr = $address['return']['address'];
@ -851,7 +852,7 @@ function updateCryptopiaMarkets()
$addr = arraySafeVal($addresses, $symbol); $addr = arraySafeVal($addresses, $symbol);
if ($needCurrencyQueries) { if ($needCurrencyQueries) {
if(!$coin->installed) continue; if(!$coin->installed) continue;
sleep(1); sleep(2);
$query = cryptopia_api_user('GetDepositAddress', array('Currency'=>$symbol)); $query = cryptopia_api_user('GetDepositAddress', array('Currency'=>$symbol));
$dep = objSafeVal($query,'Data'); $dep = objSafeVal($query,'Data');
$addr = objSafeVal($dep,'Address'); $addr = objSafeVal($dep,'Address');
@ -1026,6 +1027,48 @@ function updateNovaMarkets()
} }
} }
function updateBinanceMarkets()
{
$exchange = 'binance';
if (exchange_get($exchange, 'disabled')) return;
$tickers = binance_api_query('ticker/allBookTickers');
if(!is_array($tickers)) return;
$list = getdbolist('db_markets', "name='$exchange'");
foreach($list as $market)
{
$coin = getdbo('db_coins', $market->coinid);
if(!$coin) continue;
$symbol = $coin->getOfficialSymbol();
if (market_get($exchange, $symbol, "disabled")) {
$market->disabled = 1;
$market->message = 'disabled from settings';
$market->save();
continue;
}
$pair = $symbol.'BTC';
foreach ($tickers as $ticker) {
if ($pair != $ticker->symbol) continue;
$price2 = ($ticker->bidPrice+$ticker->askPrice)/2;
$market->price = AverageIncrement($market->price, $ticker->bidPrice);
$market->price2 = AverageIncrement($market->price2, $price2);
$market->pricetime = time();
if ($market->disabled < 9) $market->disabled = (floatval($ticker->bidQty) < 0.01);
$market->save();
if (empty($coin->price2)) {
$coin->price = $market->price;
$coin->price2 = $market->price2;
$coin->save();
}
}
}
}
function updateBterMarkets() function updateBterMarkets()
{ {
$exchange = 'bter'; $exchange = 'bter';

View file

@ -8,6 +8,7 @@ function updateRawcoins()
// debuglog(__FUNCTION__); // debuglog(__FUNCTION__);
exchange_set_default('alcurex', 'disabled', true); exchange_set_default('alcurex', 'disabled', true);
exchange_set_default('binance', 'disabled', true);
exchange_set_default('bter', 'disabled', true); exchange_set_default('bter', 'disabled', true);
exchange_set_default('empoex', 'disabled', true); exchange_set_default('empoex', 'disabled', true);
exchange_set_default('coinexchange', 'disabled', true); exchange_set_default('coinexchange', 'disabled', true);
@ -193,6 +194,21 @@ function updateRawcoins()
} }
} }
if (!exchange_get('binance', 'disabled')) {
$list = binance_api_query('ticker/allBookTickers');
if(is_array($list))
{
dborun("UPDATE markets SET deleted=true WHERE name='binance'");
foreach($list as $ticker) {
$base = substr($ticker->symbol, -3, 3);
// XXXBTC XXXETH BTCUSDT (no separator!)
if ($base != 'BTC') continue;
$symbol = substr($ticker->symbol, 0, strlen($ticker->symbol)-3);
updateRawCoin('binance', $symbol);
}
}
}
if (!exchange_get('nova', 'disabled')) { if (!exchange_get('nova', 'disabled')) {
$list = nova_api_query('markets'); $list = nova_api_query('markets');
if(is_object($list) && !empty($list->markets)) if(is_object($list) && !empty($list->markets))
@ -325,7 +341,7 @@ function updateRawCoin($marketname, $symbol, $name='unknown')
} }
} }
if (in_array($marketname, array('nova','askcoin','coinexchange','coinsmarkets','hitbtc'))) { if (in_array($marketname, array('nova','askcoin','binance','coinexchange','coinsmarkets','hitbtc'))) {
// don't polute too much the db with new coins, its better from exchanges with labels // don't polute too much the db with new coins, its better from exchanges with labels
return; return;
} }

View file

@ -0,0 +1,62 @@
<?php
// markets /api/v1/ticker/allBookTickers
function binance_api_query($method)
{
$uri = "https://www.binance.com/api/v1/$method";
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$res = curl_exec($ch);
$obj = json_decode($res);
return $obj;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// manual update of one market
function binance_update_market($market)
{
$exchange = 'binance';
if (is_string($market))
{
$symbol = $market;
$coin = getdbosql('db_coins', "symbol=:sym", array(':sym'=>$symbol));
if(!$coin) return false;
$pair = strtoupper($symbol).'BTC';
$market = getdbosql('db_markets', "coinid={$coin->id} AND name='$exchange'");
if(!$market) return false;
} else if (is_object($market)) {
$coin = getdbo('db_coins', $market->coinid);
if(!$coin) return false;
$symbol = $coin->getOfficialSymbol();
$pair = strtoupper($symbol).'BTC';
if (!empty($market->base_coin)) $pair = strtoupper($symbol).$market->base_coin;
}
$t1 = microtime(true);
$tickers = binance_api_query('ticker/allBookTickers');
if(empty($tickers) || !is_array($tickers)) return false;
foreach ($tickers as $t) {
if ($t->symbol == $pair) $ticker = $t;
}
if (!isset($ticker)) return false;
$price2 = ($ticker->bidPrice+$ticker->askPrice)/2;
$market->price = AverageIncrement($market->price, $ticker->bidPrice);
$market->price2 = AverageIncrement($market->price2, $price2);
$market->pricetime = time();
$market->save();
$apims = round((microtime(true) - $t1)*1000,3);
user()->setFlash('message', "$exchange $symbol price updated in $apims ms");
return true;
}

View file

@ -25,6 +25,7 @@ require_once("bter.php");
require_once("empoex.php"); require_once("empoex.php");
require_once("jubi.php"); require_once("jubi.php");
require_once("alcurex.php"); require_once("alcurex.php");
require_once("binance.php");
require_once("cryptopia.php"); require_once("cryptopia.php");
require_once("hitbtc.php"); require_once("hitbtc.php");
require_once("livecoin.php"); require_once("livecoin.php");
@ -69,7 +70,9 @@ function getMarketUrl($coin, $marketName)
} }
if($market == 'alcurex') if($market == 'alcurex')
$url = "https://alcurex.org/index.php/crypto/market?pair={$lowsymbol}_{$lowbase}"; $url = "https://alcurex.com/#{$symbol}-{$base}";
else if($market == 'binance')
$url = "https://www.binance.com/trade.html?symbol={$symbol}_{$base}";
else if($market == 'bittrex') else if($market == 'bittrex')
$url = "https://bittrex.com/Market/Index?MarketName={$base}-{$symbol}"; $url = "https://bittrex.com/Market/Index?MarketName={$base}-{$symbol}";
else if($market == 'poloniex') else if($market == 'poloniex')