markets: add stocks.exchange public api

disabled by default, optional...
This commit is contained in:
Tanguy Pruvot 2017-11-22 12:00:44 +01:00
parent f7a04abf06
commit 6ced60c84e
4 changed files with 133 additions and 0 deletions

View file

@ -27,6 +27,7 @@ function BackendPricesUpdate()
updateNovaMarkets();
updateCoinExchangeMarkets();
updateCoinsMarketsMarkets();
updateStocksExchangeMarkets();
updateTradeSatoshiMarkets();
updateShapeShiftMarkets();
@ -1339,6 +1340,53 @@ function updateCoinsMarketsMarkets()
}
}
function updateStocksExchangeMarkets()
{
$exchange = 'stocksexchange';
if (exchange_get($exchange, 'disabled')) return;
$list = stocksexchange_api_query('ticker');
if(empty($list) || !is_array($list)) return;
foreach($list as $m)
{
$e = explode('_', $m->market_name);
$symbol = strtoupper($e[0]); $base = $e[1];
if($base != 'BTC') continue;
$coin = getdbosql('db_coins', "symbol=:sym", array(':sym'=>$symbol));
if(!$coin) continue;
$market = getdbosql('db_markets', "coinid={$coin->id} AND name='$exchange' AND IFNULL(base_coin,'') IN ('','BTC')");
if(!$market) continue;
$symbol = $coin->getOfficialSymbol();
if (market_get($exchange, $symbol, "disabled")) {
$market->disabled = 1;
$market->message = 'disabled from settings';
$market->save();
continue;
}
$market->disabled = ($m->bid == 0);
$price2 = ((double)$m->ask + (double)$m->bid)/2;
$market->price2 = AverageIncrement($market->price2, $price2);
$market->price = AverageIncrement($market->price, (double)$m->bid);
$market->priority = -1; // not ready for trading
$market->txfee = $m->sell_fee_percent;
//debuglog("$exchange: $symbol price set to ".bitcoinvaluetoa($market->price));
$market->pricetime = time(); // $m->updated_time;
$market->save();
if (empty($coin->price2)) {
$coin->price = $market->price;
$coin->price2 = $market->price2;
$coin->save();
}
}
}
function updateTradeSatoshiMarkets()
{
$exchange = 'tradesatoshi';

View file

@ -15,6 +15,7 @@ function updateRawcoins()
exchange_set_default('coinsmarkets', 'disabled', true);
exchange_set_default('jubi', 'disabled', true);
exchange_set_default('nova', 'disabled', true);
exchange_set_default('stocksexchange', 'disabled', true);
exchange_set_default('tradesatoshi', 'disabled', true);
settings_prefetch_all();
@ -225,6 +226,23 @@ function updateRawcoins()
}
}
if (!exchange_get('stocksexchange', 'disabled')) {
$list = stocksexchange_api_query('markets');
if(is_array($list))
{
dborun("UPDATE markets SET deleted=true WHERE name='stocksexchange'");
foreach($list as $item) {
if ($item->partner != 'BTC')
continue;
if ($item->active == false)
continue;
$symbol = strtoupper($item->currency);
$name = trim($item->currency_long);
updateRawCoin('stocksexchange', $symbol, $name);
}
}
}
if (!exchange_get('empoex', 'disabled')) {
$list = empoex_api_query('marketinfo');
if(is_array($list))

View file

@ -33,6 +33,7 @@ require_once("nova.php");
require_once("coinexchange.php");
require_once("coinsmarkets.php");
require_once("cryptowatch.php");
require_once("stocksexchange.php");
require_once("tradesatoshi.php");
/* Format an exchange coin Url */
@ -101,6 +102,8 @@ function getMarketUrl($coin, $marketName)
$url = "https://www.livecoin.net/trade/?currencyPair={$symbol}%2F{$base}";
else if($market == 'nova')
$url = "https://novaexchange.com/market/{$base}_{$symbol}/";
else if($market == 'stocksexchange')
$url = "https://stocks.exchange/trade/$symbol/$base";
else if($market == 'tradesatoshi')
$url = "https://tradesatoshi.com/Exchange?market={$symbol}_{$base}";
else if($market == 'yobit')

View file

@ -0,0 +1,64 @@
<?php
// markets https://stocks.exchange/api2/markets
// prices https://stocks.exchange/api2/ticker
// prices https://stocks.exchange/api2/prices (unused)
function stocksexchange_api_query($method)
{
$uri = "https://stocks.exchange/api2/$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 stocksexchange_update_market($market)
{
$exchange = 'stocksexchange';
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 = stocksexchange_api_query('ticker');
if(empty($tickers) || !is_array($tickers)) return false;
foreach ($tickers as $t) {
if ($t->market_name == $pair) $ticker = $t;
}
if (!isset($ticker)) return false;
$price2 = ($ticker->bid+$ticker->ask)/2;
$market->price = AverageIncrement($market->price, $ticker->bid);
$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;
}