balances: fill the btc 'onsell' field too (locked)

and restore bitstamp & cexio behavior to act like other trading exchanges

todo: kraken if possible... real pain, need to parse open orders..
This commit is contained in:
Tanguy Pruvot 2018-01-27 06:16:19 +01:00
parent 88106d8be2
commit 0f04d6d824
16 changed files with 72 additions and 35 deletions

View file

@ -86,7 +86,8 @@ function getBitstampBalances()
if (is_object($savebalance)) {
$balances = bitstamp_api_user('balance');
if (is_array($balances)) {
$savebalance->balance = arraySafeVal($balances, 'btc_balance');
$savebalance->balance = arraySafeVal($balances, 'btc_balance',0.) - arraySafeVal($balances, 'btc_reserved');
$savebalance->onsell = arraySafeVal($balances, 'btc_reserved');
$savebalance->save();
}
}

View file

@ -100,7 +100,8 @@ function getCexIoBalances()
$balances = cexio_api_user('balance');
if (is_array($balances)) {
$b = arraySafeVal($balances, 'BTC');
$savebalance->balance = arraySafeVal($b, 'available',0.) + arraySafeVal($b, 'orders',0.);
$savebalance->balance = arraySafeVal($b, 'available',0.);
$savebalance->onsell = arraySafeVal($b, 'orders',0.);
$savebalance->save();
}
}

View file

@ -25,6 +25,7 @@ function doBinanceTrading($quick=false)
if ($balance->asset == 'BTC') {
if (is_object($savebalance)) {
$savebalance->balance = $balance->free;
$savebalance->onsell = $balance->locked;
$savebalance->save();
}
continue;

View file

@ -27,7 +27,8 @@ function doBittrexTrading($quick=false)
$savebalance = getdbosql('db_balances', "name='$exchange'");
if (is_object($savebalance)) {
$savebalance->balance = 0;
$savebalance->balance = 0.;
$savebalance->onsell = 0.;
$savebalance->save();
}
@ -36,6 +37,7 @@ function doBittrexTrading($quick=false)
if ($balance->Currency == 'BTC') {
if (is_object($savebalance)) {
$savebalance->balance = $balance->Available;
$savebalance->onsell = (double) $balance->Balance - (double) $balance->Available;
$savebalance->save();
}
continue;

View file

@ -28,6 +28,7 @@ function doBleutradeTrading($quick=false)
$savebalance = getdbosql('db_balances', "name='$exchange'");
if (is_object($savebalance)) {
$savebalance->balance = 0;
$savebalance->onsell = 0;
$savebalance->save();
}
@ -36,6 +37,7 @@ function doBleutradeTrading($quick=false)
if ($balance->Currency == 'BTC') {
if (is_object($savebalance)) {
$savebalance->balance = $balance->Available;
$savebalance->onsell = (double) $balance->Balance - (double) $balance->Available;
$savebalance->save();
}
continue;

View file

@ -27,6 +27,7 @@ function doBterTrading($quick=false)
if ($symbol == 'BTC') {
if (is_object($savebalance)) {
$savebalance->balance = $balance->available;
$savebalance->onsell = arraySafeVal($balances['locked_funds'],$symbol,0);
$savebalance->save();
}
continue;

View file

@ -39,6 +39,7 @@ function doCCexTrading($quick=false)
if ($symbol == 'BTC') {
if (!is_object($savebalance)) continue;
$savebalance->balance = arraySafeVal($balance,'Available');
$savebalance->onsell = arraySafeVal($balance,'Balance',0.) - arraySafeVal($balance,'Available');
$savebalance->save();
continue;
}

View file

@ -27,6 +27,7 @@ function doCryptopiaTrading($quick=false)
$savebalance = getdbosql('db_balances', "name='$exchange'");
if (is_object($savebalance)) {
$savebalance->balance = 0;
$savebalance->onsell = 0;
$savebalance->save();
}
@ -36,6 +37,7 @@ function doCryptopiaTrading($quick=false)
if ($balance->Symbol == 'BTC') {
if (is_object($savebalance)) {
$savebalance->balance = $balance->Available;
$savebalance->onsell = $balance->HeldForTrades;
$savebalance->save();
}
continue;

View file

@ -25,6 +25,7 @@ function doHitBTCTrading($quick=false)
if ($balance->currency_code == 'BTC') {
if (is_object($savebalance)) {
$savebalance->balance = $balance->cash;
$savebalance->onsell = $balance->reserved;
$savebalance->save();
}
continue;

View file

@ -10,12 +10,16 @@ function doKrakenTrading($quick=false)
$balances = kraken_api_user('Balance');
if(!$balances || !is_array($balances)) return;
//$total = kraken_api_user('TradeBalance', array('asset'=>'XXBT'));
//if(!$total || !is_array($total)) return;
foreach($balances as $symbol => $balance)
{
if ($symbol == 'BTC') {
$db_balance = getdbosql('db_balances', "name='$exchange'");
if ($db_balance) {
$db_balance->balance = $balance;
//$db_balance->onsell = (double) $total['result']['tb'] - $balance;
$db_balance->save();
}
continue;

View file

@ -25,6 +25,7 @@ function doKuCoinTrading($quick=false)
if ($balance->coinType == 'BTC') {
if (is_object($savebalance)) {
$savebalance->balance = $balance->balance;
$savebalance->onsell = $balance->freezeBalance;
$savebalance->save();
}
continue;

View file

@ -52,13 +52,17 @@ function doLiveCoinTrading($quick = false)
foreach ($balances as $balance) {
if ($balance->currency == 'BTC' && $balance->type == "available") {
if (!is_object($savebalance)) {
continue;
}
if (!is_object($savebalance)) continue;
$savebalance->balance = $balance->value;
$savebalance->save();
continue;
}
if ($balance->currency == 'BTC' && $balance->type == "trade") {
if (!is_object($savebalance)) continue;
$savebalance->onsell = $balance->value;
$savebalance->save();
continue;
}
if ($updatebalances) {
// store available balance in market table

View file

@ -38,6 +38,7 @@ function doNovaTrading($quick=false)
$savebalance = getdbosql('db_balances', "name='{$exchange}'");
if (is_object($savebalance)) {
$savebalance->balance = 0;
$savebalance->onsell = 0;
$savebalance->save();
} else {
dborun("INSERT INTO balances (name,balance) VALUES ('$exchange',0)");
@ -49,6 +50,7 @@ function doNovaTrading($quick=false)
if ($balance->currency == 'BTC') {
if (is_object($savebalance)) {
$savebalance->balance = $balance->amount;
$savebalance->onsell = $balance->amount_trades;
$savebalance->save();
}
continue;

View file

@ -33,7 +33,8 @@ function doPoloniexTrading()
{
if ($symbol == 'BTC') {
if (is_object($savebalance)) {
$savebalance->balance = $balance['available'];
$savebalance->balance = arraySafeVal($balance,'available');
$savebalance->onsell = arraySafeVal($balance,'onOrders');
$savebalance->save();
}
continue;

View file

@ -37,6 +37,7 @@ function doYobitTrading($quick=false)
if ($symbol == 'btc') {
if (is_object($savebalance)) {
$savebalance->balance = $amount;
$savebalance->onsell = arraySafeVal($balances['return']['funds_incl_orders'],$symbol,0.) - $amount;
$savebalance->save();
}
continue;
@ -51,6 +52,7 @@ function doYobitTrading($quick=false)
$market = getdbosql('db_markets', "coinid=:coinid AND name='$exchange'", array(':coinid'=>$coin->id));
if (!$market) continue;
$market->balance = $amount;
$market->ontrade = arraySafeVal($balances['return']['funds_incl_orders'],$symbol,0.) - $amount;
$market->balancetime = time();
$market->save();
}

View file

@ -222,34 +222,6 @@ echo '</thead>';
// ----------------------------------------------------------------------------------------------------
if (YAAMP_ALLOW_EXCHANGE) {
echo '<tr class="ssrow"><td>sell orders</td>';
foreach($markets as $market)
{
$exchange = $market->name;
$onsell = bitcoinvaluetoa(dboscalar("select sum(amount*bid) from orders where market='$exchange'"));
$salebalances[$exchange] = $onsell;
if($onsell > 0.2)
echo '<td align="right" style="color: white; background-color: #d9534f">'.$onsell.'</td>';
else if($onsell > 0.1)
echo '<td align="right" style="color: white; background-color: #f0ad4e">'.$onsell.'</td>';
else if($onsell == 0.0)
echo '<td align="right">-</td>';
else
echo '<td align="right">'.$onsell.'</td>';
$total_onsell += $onsell;
}
$total_onsell = bitcoinvaluetoa($total_onsell);
echo '<td align="right" style="color: white; background-color: #c5b47f">'.$total_onsell.'</td>';
echo '</tr>';
} // YAAMP_ALLOW_EXCHANGE
// ----------------------------------------------------------------------------------------------------
echo '<tr class="ssrow"><td>BTC</td>';
foreach($markets as $market)
{
@ -274,6 +246,45 @@ echo '</tr>';
// ----------------------------------------------------------------------------------------------------
echo '<tr class="ssrow"><td>orders</td>';
if (YAAMP_ALLOW_EXCHANGE) {
// yaamp mode
foreach($markets as $market) {
$exchange = $market->name;
$onsell = bitcoinvaluetoa(dboscalar("SELECT sum(amount*bid) FROM orders WHERE market='$exchange'"));
$salebalances[$exchange] = $onsell;
if($onsell > 0.2)
echo '<td align="right" style="color: white; background-color: #d9534f">'.$onsell.'</td>';
else if($onsell > 0.1)
echo '<td align="right" style="color: white; background-color: #f0ad4e">'.$onsell.'</td>';
else if($onsell == 0.0)
echo '<td align="right">-</td>';
else
echo '<td align="right">'.$onsell.'</td>';
$total_onsell += $onsell;
}
} else {
// yiimp mode
$ontrade = dbolist("SELECT name, onsell FROM balances B ORDER by name");
foreach($ontrade as $row) {
$exchange = $row['name'];
$onsell = bitcoinvaluetoa($row['onsell']);
$salebalances[$exchange] = $onsell;
echo '<td align="right">'.($onsell == 0 ? '-' : $onsell).'</td>';
$total_onsell += (double) $onsell;
}
}
$total_onsell = bitcoinvaluetoa($total_onsell);
echo '<td align="right">'.$total_onsell.'</td>';
echo '</tr>';
// ----------------------------------------------------------------------------------------------------
$t = time() - 48*60*60;
$altmarkets = dbolist("
SELECT B.name, SUM((M.balance+M.ontrade)*M.price) AS balance