From f72ce04be13a31013cd1325d5babda4561b3be2a Mon Sep 17 00:00:00 2001 From: Tanguy Pruvot Date: Sat, 26 Mar 2016 19:37:27 +0100 Subject: [PATCH] markets: new fields + balances update --- sql/2016-03-26-markets.sql | 8 +++ web/yaamp/core/backend/markets.php | 34 +++++++++-- web/yaamp/core/trading/bittrex_trading.php | 34 +++++++---- web/yaamp/core/trading/bleutrade_trading.php | 38 +++++++----- web/yaamp/core/trading/bter_trading.php | 15 ++--- web/yaamp/core/trading/c-cex_trading.php | 22 ++++--- web/yaamp/core/trading/cryptomic_trading.php | 5 +- web/yaamp/core/trading/cryptopia_trading.php | 38 +++++++----- web/yaamp/core/trading/cryptsy_trading.php | 45 ++++++++++---- web/yaamp/core/trading/empoex_trading.php | 31 ++++++++-- web/yaamp/core/trading/kraken_trading.php | 31 +++++----- web/yaamp/core/trading/poloniex_trading.php | 24 ++++---- web/yaamp/core/trading/safecex_trading.php | 63 ++++++++++---------- web/yaamp/core/trading/yobit_trading.php | 29 +++++---- 14 files changed, 269 insertions(+), 148 deletions(-) create mode 100644 sql/2016-03-26-markets.sql diff --git a/sql/2016-03-26-markets.sql b/sql/2016-03-26-markets.sql new file mode 100644 index 0000000..a7b3769 --- /dev/null +++ b/sql/2016-03-26-markets.sql @@ -0,0 +1,8 @@ +-- Recent additions to add after db init (.gz) +-- mysql yaamp -p < file.sql + +ALTER TABLE `markets` ADD `disabled` TINYINT(1) NOT NULL DEFAULT '0' AFTER `coinid`; +ALTER TABLE `markets` ADD `priority` TINYINT(1) NOT NULL DEFAULT '0' AFTER `marketid`; +ALTER TABLE `markets` ADD `ontrade` DOUBLE NOT NULL DEFAULT '0' AFTER `balance`; +ALTER TABLE `markets` ADD `balancetime` INT(11) NULL AFTER `lasttraded`; +ALTER TABLE `markets` ADD `pricetime` INT(11) NULL AFTER `price2`; diff --git a/web/yaamp/core/backend/markets.php b/web/yaamp/core/backend/markets.php index ad25c9d..6d1cbda 100644 --- a/web/yaamp/core/backend/markets.php +++ b/web/yaamp/core/backend/markets.php @@ -36,6 +36,7 @@ function BackendPricesUpdate() $market2->price = $market->price; $market2->price2 = $market->price2; $market2->deposit_address = $market->deposit_address; + $market2->pricetime = $market->pricetime; $market2->save(); } @@ -159,11 +160,11 @@ function updateBleutradeMarkets() } $market->txfee = $currency->TxFee; - if(!$currency->IsActive) + if(!$currency->IsActive && !$market->disabled) { $market->price = 0; + $market->disabled = 1; $market->save(); - continue; } @@ -174,9 +175,14 @@ function updateBleutradeMarkets() $ticker = bleutrade_api_query('public/getticker', "&market=$pair"); if(!$ticker || !$ticker->success || !$ticker->result) continue; + if ($market->price == 0 && $market->disabled) { + $market->disabled = 0; + } + $price2 = ($ticker->result[0]->Bid+$ticker->result[0]->Ask)/2; $market->price2 = AverageIncrement($market->price2, $price2); $market->price = AverageIncrement($market->price, $ticker->result[0]->Bid); + $market->pricetime = time(); if(!empty(EXCH_BLEUTRADE_KEY)) { @@ -246,6 +252,7 @@ function updateKrakenMarkets($force = false) $price2 = ($ticker['b'][0] + $ticker['a'][0]) / 2; $market->price2 = AverageIncrement($market->price2, $price2); $market->price = AverageIncrement($market->price, $ticker['b'][0]); + $market->pricetime = time(); $market->save(); } @@ -307,6 +314,7 @@ function updateBittrexMarkets($force = false) $price2 = ($ticker->result->Bid+$ticker->result->Ask)/2; $market->price2 = AverageIncrement($market->price2, $price2); $market->price = AverageIncrement($market->price, $ticker->result->Bid); + $market->pricetime = time(); $market->save(); @@ -426,6 +434,7 @@ function updateCryptsyMarkets() $market->price2 = AverageIncrement($market->price2, $price2); $market->price = AverageIncrement($market->price, $ticker->return->$symbol->buyorders[0]->price); + $market->pricetime = time(); $market->save(); @@ -508,6 +517,7 @@ function updateCCexMarkets() $market->price2 = AverageIncrement($market->price2, $price2); $market->price = AverageIncrement($market->price, $ticker['buy']); + $market->pricetime = time(); $market->save(); @@ -578,6 +588,7 @@ function updatePoloniexMarkets() $price2 = ($ticker['highestBid']+$ticker['lowestAsk'])/2; $market->price2 = AverageIncrement($market->price2, $price2); $market->price = AverageIncrement($market->price, $ticker['highestBid']); + $market->pricetime = time(); $market->save(); @@ -671,6 +682,7 @@ function updateYobitMarkets() $price2 = ($ticker->$pair->buy + $ticker->$pair->sell) / 2; $market->price2 = AverageIncrement($market->price2, $price2); $market->price = AverageIncrement($market->price, $ticker->$pair->buy); + $market->pricetime = time(); $market->save(); } @@ -702,6 +714,7 @@ function updateJubiMarkets() $price2 = ($ticker->buy+$ticker->sell)/2; $market->price2 = AverageIncrement($market->price2, $price2); $market->price = AverageIncrement($market->price, $ticker->buy*0.95); + $market->pricetime = time(); // debuglog("jubi update $coin->symbol: $market->price $market->price2"); @@ -729,12 +742,14 @@ function updateAlcurexMarkets() if (is_object($last) && !empty($last->$lpair)) { $last = $last->$lpair; $market->price = AverageIncrement($market->price, $last->price); + $market->pricetime = time(); $market->save(); } $last = alcurex_api_query('market', "?pair=$lpair&last=sell"); if (is_object($last) && !empty($last->$lpair)) { $last = $last->$lpair; $market->price2 = AverageIncrement($market->price2, $last->price); + $market->pricetime = time(); $market->save(); } if (empty($coin->price)) { @@ -769,6 +784,7 @@ function updateCryptopiaMarkets() $market->price2 = AverageIncrement($market->price2, $price2); $market->price = AverageIncrement($market->price, $ticker->BidPrice*0.98); $market->marketid = $ticker->TradePairId; + $market->pricetime = time(); $market->save(); if (empty($coin->price)) { $coin->price = $market->price; @@ -829,6 +845,7 @@ function updateBanxioMarkets() if ($ticker->marketname === $pair) { $market->price = AverageIncrement($market->price, $ticker->bid); $market->price2 = AverageIncrement($market->price2, $ticker->last); + $market->pricetime = time(); // debuglog("banx: $pair {$ticker->bid} {$ticker->last} => {$market->price} {$market->price2}"); if (intval($ticker->dayvolume) > 1) $market->save(); @@ -911,6 +928,7 @@ function updateSafecexMarkets() $price2 = ($ticker->bid + $ticker->ask)/2; $market->price2 = AverageIncrement($market->price2, $price2); $market->price = AverageIncrement($market->price, $ticker->bid*0.98); + $market->pricetime = time(); $market->save(); if (empty($coin->price)) { @@ -991,6 +1009,12 @@ function updateBterMarkets() { $coin = getdbo('db_coins', $market->coinid); if(!$coin || !$coin->installed) continue; + if($coin->symbol == 'SFR') { + // black hole: no deposits since months + $market->deleted = 1; + $market->save(); + continue; + } $lowsymbol = strtolower($coin->symbol); $dbpair = $lowsymbol.'_btc'; @@ -999,7 +1023,8 @@ function updateBterMarkets() $market->price = AverageIncrement($market->price, $ticker['buy']); $market->price2 = AverageIncrement($market->price2, $ticker['avg']); - $market->deleted = (floatval($ticker['vol_btc']) < 0.01); + $market->pricetime = time(); + $market->disabled = (floatval($ticker['vol_btc']) < 0.01); $market->save(); if (empty($coin->price2)) { @@ -1030,6 +1055,7 @@ function updateEmpoexMarkets() $market->price = AverageIncrement($market->price, $ticker->bid); $market->price2 = AverageIncrement($market->price2, $ticker->ask); + $market->pricetime = time(); if (floatval($ticker->base_volume_24hr) > 0.01) $market->save(); @@ -1063,7 +1089,7 @@ function updateOtherMarkets() $coin->price2 = $ticker->price; $coin->price = AverageIncrement((float)$coin->price, (float)$coin->price2); if ($coin->save()) { - debuglog("update price of $symbol ".bitcoinvaluetoa($coin->price)); + debuglog("cryptonator: $symbol price set to ".bitcoinvaluetoa($coin->price)); } } } diff --git a/web/yaamp/core/trading/bittrex_trading.php b/web/yaamp/core/trading/bittrex_trading.php index f6c1df2..945d573 100644 --- a/web/yaamp/core/trading/bittrex_trading.php +++ b/web/yaamp/core/trading/bittrex_trading.php @@ -16,35 +16,43 @@ function doBittrexCancelOrder($OrderID=false) function doBittrexTrading($quick=false) { + $exchange = 'bittrex'; + $updatebalances = !YAAMP_ALLOW_EXCHANGE; + $balances = bittrex_api_query('account/getbalances'); if(!$balances || !isset($balances->result) || !$balances->success) return; - $savebalance = getdbosql('db_balances', "name='bittrex'"); - $savebalance->balance = 0; + $savebalance = getdbosql('db_balances', "name='$exchange'"); + if (is_object($savebalance)) { + $savebalance->balance = 0; + $savebalance->save(); + } foreach($balances->result as $balance) { if ($balance->Currency == 'BTC') { - $savebalance->balance = $balance->Available; - $savebalance->save(); + if (is_object($savebalance)) { + $savebalance->balance = $balance->Available; + $savebalance->save(); + } continue; } - if (!YAAMP_ALLOW_EXCHANGE) { + if ($updatebalances) { // store available balance in market table $coins = getdbolist('db_coins', "symbol=:sym OR symbol2=:sym", array(':sym'=>$balance->Currency)); if (empty($coins)) continue; foreach ($coins as $coin) { - $market = getdbosql('db_markets', "coinid=:coinid AND name='bittrex'", array(':coinid'=>$coin->id)); + $market = getdbosql('db_markets', "coinid=:coinid AND name='$exchange'", array(':coinid'=>$coin->id)); if (!$market) continue; - if ($market->balance != $balance->Available) { - $market->balance = $balance->Available; - if (!empty($balance->CryptoAddress) && $market->deposit_address != $balance->CryptoAddress) { - debuglog("bittrex: {$coin->symbol} deposit address updated"); - $market->deposit_address = $balance->CryptoAddress; - } - $market->save(); + $market->balance = $balance->Available; + $market->ontrade = $balance->Balance - $balance->Available; + if (!empty($balance->CryptoAddress) && $market->deposit_address != $balance->CryptoAddress) { + debuglog("$exchange: {$coin->symbol} deposit address updated"); + $market->deposit_address = $balance->CryptoAddress; } + $market->balancetime = time(); + $market->save(); } } } diff --git a/web/yaamp/core/trading/bleutrade_trading.php b/web/yaamp/core/trading/bleutrade_trading.php index fcc29de..0c9255e 100644 --- a/web/yaamp/core/trading/bleutrade_trading.php +++ b/web/yaamp/core/trading/bleutrade_trading.php @@ -16,38 +16,46 @@ function doBleutradeCancelOrder($OrderID=false) function doBleutradeTrading($quick=false) { + $exchange = 'bleutrade'; + $updatebalances = !YAAMP_ALLOW_EXCHANGE; + $balances = bleutrade_api_query('account/getbalances'); if(!$balances || !isset($balances->result) || !$balances->success) return; - $savebalance = getdbosql('db_balances', "name='bleutrade'"); - $savebalance->balance = 0; + $savebalance = getdbosql('db_balances', "name='$exchange'"); + if (is_object($savebalance)) { + $savebalance->balance = 0; + $savebalance->save(); + } foreach($balances->result as $balance) { if ($balance->Currency == 'BTC') { - $savebalance->balance = $balance->Available; - $savebalance->save(); + if (is_object($savebalance)) { + $savebalance->balance = $balance->Available; + $savebalance->save(); + } continue; } - if (!YAAMP_ALLOW_EXCHANGE) { + if ($updatebalances) { // store available balance in market table $coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol", array(':symbol'=>$balance->Currency) ); if (empty($coins)) continue; foreach ($coins as $coin) { - $market = getdbosql('db_markets', "coinid=:coinid AND name='bleutrade'", array(':coinid'=>$coin->id)); + $market = getdbosql('db_markets', "coinid=:coinid AND name='$exchange'", array(':coinid'=>$coin->id)); if (!$market) continue; - if ($market->balance != $balance->Available) { - $market->balance = $balance->Available; - if (!empty($balance->CryptoAddress) && $market->deposit_address != $balance->CryptoAddress) { - debuglog("bleutrade: {$coin->symbol} deposit address updated"); - $market->deposit_address = $balance->CryptoAddress; - } - if (property_exists($balance,'IsActive')) - $market->message = ($balance->IsActive == "true") ? "" : "Disabled"; - $market->save(); + $market->balance = $balance->Available; + $market->ontrade = $balance->Balance - $balance->Available; + if (!empty($balance->CryptoAddress) && $market->deposit_address != $balance->CryptoAddress) { + debuglog("$exchange: {$coin->symbol} deposit address updated"); + $market->deposit_address = $balance->CryptoAddress; } + if (property_exists($balance,'IsActive')) + $market->message = ($balance->IsActive == "true") ? "" : "Disabled"; + $market->balancetime = time(); + $market->save(); } } } diff --git a/web/yaamp/core/trading/bter_trading.php b/web/yaamp/core/trading/bter_trading.php index e63e88a..9d2b013 100644 --- a/web/yaamp/core/trading/bter_trading.php +++ b/web/yaamp/core/trading/bter_trading.php @@ -23,9 +23,10 @@ function doBterTrading($quick=false) foreach($balances['available_funds'] as $symbol => $available) { if ($symbol == 'BTC') { - if (!is_object($savebalance)) continue; - $savebalance->balance = $balance->available; - $savebalance->save(); + if (is_object($savebalance)) { + $savebalance->balance = $balance->available; + $savebalance->save(); + } continue; } @@ -38,10 +39,10 @@ function doBterTrading($quick=false) foreach ($coins as $coin) { $market = getdbosql('db_markets', "coinid=:coinid AND name='{$exchange}'", array(':coinid'=>$coin->id)); if (!$market) continue; - if ($market->balance != $available) { - $market->balance = $available; - $market->save(); - } + $market->balance = $available; + $market->ontrade = arraySafeVal($balances['locked_funds'],$symbol,0); + $market->balancetime = time(); + $market->save(); } } } diff --git a/web/yaamp/core/trading/c-cex_trading.php b/web/yaamp/core/trading/c-cex_trading.php index 3a6f7f2..3ced26f 100644 --- a/web/yaamp/core/trading/c-cex_trading.php +++ b/web/yaamp/core/trading/c-cex_trading.php @@ -17,12 +17,16 @@ function doCCexCancelOrder($OrderID=false, $ccex=false) function doCCexTrading($quick=false) { -// debuglog("-------------- doCCexTrading() $flushall"); + $exchange = 'c-cex'; + $updatebalances = !YAAMP_ALLOW_EXCHANGE; $ccex = new CcexAPI; - $savebalance = getdbosql('db_balances', "name='c-cex'"); - $savebalance->balance = 0; + $savebalance = getdbosql('db_balances', "name='$exchange'"); + if (is_object($savebalance)) { + $savebalance->balance = 0; + $savebalance->save(); + } $balances = $ccex->getBalance(); if(!$balances || !isset($balances['return'])) return; @@ -30,22 +34,22 @@ function doCCexTrading($quick=false) foreach($balances['return'] as $balance) foreach($balance as $symbol=>$amount) { if ($symbol == 'btc') { + if (!is_object($savebalance)) continue; $savebalance->balance = $amount; // (available one) $savebalance->save(); continue; } - if (!YAAMP_ALLOW_EXCHANGE) { + if ($updatebalances) { // store available balance in market table $coins = getdbolist('db_coins', "symbol=:sym OR symbol2=:sym", array(':sym'=>strtoupper($symbol))); if (empty($coins)) continue; foreach ($coins as $coin) { - $market = getdbosql('db_markets', "coinid=:coinid AND name='c-cex'", array(':coinid'=>$coin->id)); + $market = getdbosql('db_markets', "coinid=:coinid AND name='$exchange'", array(':coinid'=>$coin->id)); if (!$market) continue; - if ($market->balance != $amount) { - $market->balance = $amount; - $market->save(); - } + $market->balance = $amount; + $market->balancetime = time(); + $market->save(); } } } diff --git a/web/yaamp/core/trading/cryptomic_trading.php b/web/yaamp/core/trading/cryptomic_trading.php index 5c26763..616133b 100644 --- a/web/yaamp/core/trading/cryptomic_trading.php +++ b/web/yaamp/core/trading/cryptomic_trading.php @@ -36,10 +36,13 @@ function doBanxTrading($quick=false) $market = getdbosql('db_markets', "coinid=:coinid AND name='{$exchange}'", array(':coinid'=>$coin->id)); if (!$market) continue; $market->balance = $balance->available; - if (!empty($balance->cryptoaddress) && $market->deposit_address != $balance->cryptoaddress) { + $market->ontrade = $balance->balance - $balance->available; + $deposit_address = objSafeVal($balance,'cryptoaddress'); + if (!empty($deposit_address) && $market->deposit_address != $balance->cryptoaddress) { debuglog("{$exchange}: {$coin->symbol} deposit address updated"); $market->deposit_address = $balance->cryptoaddress; } + $market->balancetime = time(); $market->save(); } } diff --git a/web/yaamp/core/trading/cryptopia_trading.php b/web/yaamp/core/trading/cryptopia_trading.php index e9c5d73..570179a 100644 --- a/web/yaamp/core/trading/cryptopia_trading.php +++ b/web/yaamp/core/trading/cryptopia_trading.php @@ -16,39 +16,48 @@ function doCryptopiaCancelOrder($OrderID=false) function doCryptopiaTrading($quick=false) { - $savebalance = getdbosql('db_balances', "name='cryptopia'"); - if(!$savebalance) return; + $exchange = 'cryptopia'; + $updatebalances = !YAAMP_ALLOW_EXCHANGE; $balances = cryptopia_api_user('GetBalance'); + if (!is_object($balances)) return; - if (is_object($balances) && is_array($balances->Data)) + $savebalance = getdbosql('db_balances', "name='$exchange'"); + if (is_object($savebalance)) { + $savebalance->balance = 0; + $savebalance->save(); + } + + if (is_array($balances->Data)) foreach($balances->Data as $balance) { if ($balance->Symbol == 'BTC') { - $savebalance->balance = $balance->Available; - $savebalance->save(); + if (is_object($savebalance)) { + $savebalance->balance = $balance->Available; + $savebalance->save(); + } continue; } - if (!YAAMP_ALLOW_EXCHANGE) { + if ($updatebalances) { // store available balance in market table $coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol", array(':symbol'=>$balance->Symbol) ); if (empty($coins)) continue; foreach ($coins as $coin) { - $market = getdbosql('db_markets', "coinid=:coinid AND name='cryptopia'", array(':coinid'=>$coin->id)); + $market = getdbosql('db_markets', "coinid=:coinid AND name='$exchange'", array(':coinid'=>$coin->id)); if (!$market) continue; - if ($market->balance != $balance->Available) { - $market->balance = $balance->Available; - $market->message = $balance->StatusMessage; - if (property_exists($balance, 'Address')) + $market->balance = $balance->Available; + $market->ontrade = $balance->HeldForTrades; + $market->message = $balance->StatusMessage; + if (property_exists($balance, 'Address')) if (!empty($balance->Address) && $market->deposit_address != $balance->Address) { - debuglog("cryptopia: {$coin->symbol} deposit address updated"); + debuglog("$exchange: {$coin->symbol} deposit address updated"); $market->deposit_address = $balance->Address; } - $market->save(); - } + $market->balancetime = time(); + $market->save(); } } } @@ -244,6 +253,7 @@ function doCryptopiaTrading($quick=false) } // auto withdraw + if(is_object($savebalance)) if(floatval(EXCH_AUTO_WITHDRAW) > 0 && $savebalance->balance >= (EXCH_AUTO_WITHDRAW + 0.0002)) { $btcaddr = YAAMP_BTCADDRESS; diff --git a/web/yaamp/core/trading/cryptsy_trading.php b/web/yaamp/core/trading/cryptsy_trading.php index c4abff1..2f6a118 100644 --- a/web/yaamp/core/trading/cryptsy_trading.php +++ b/web/yaamp/core/trading/cryptsy_trading.php @@ -20,32 +20,53 @@ function getCryptsyTicker($marketid) function doCryptsyTrading($quick=false) { - $flushall = rand(0, 4) == 0; - if($quick) $flushall = false; + $exchange = 'cryptsy'; + $updatebalances = !YAAMP_ALLOW_EXCHANGE; -// debuglog("-------------- doCryptsyTrading() $flushall"); - - // add orders - $savebalance = getdbosql('db_balances', "name='cryptsy'"); - $savebalance->balance = 0; + $savebalance = getdbosql('db_balances', "name='$exchange'"); + if (is_object($savebalance)) { + $savebalance->balance = 0; + $savebalance->save(); + } $balances = cryptsy_api_query('getinfo'); if(!$balances) return; if(!isset($balances['return'])) { - debuglog("cryptsy balance: ".arraySafeVal($balances,'error')); + debuglog("$exchange balance: ".arraySafeVal($balances,'error')); return; } + foreach($balances['return']['balances_available'] as $symbol=>$balance) { if($symbol == 'BTC') { - $savebalance->balance = floatval($balance); - $savebalance->save(); - break; + if (is_object($savebalance)) { + $savebalance->balance = floatval($balance); + $savebalance->save(); + } + continue; + } + + if ($updatebalances) { + // store available balance in market table + $coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol", + array(':symbol'=>$symbol) + ); + if (empty($coins)) continue; + foreach ($coins as $coin) { + $market = getdbosql('db_markets', "coinid=:coinid AND name='{$exchange}'", array(':coinid'=>$coin->id)); + if (!$market) continue; + $market->balance = $available; + $market->balancetime = time(); + $market->save(); + } } } if (!YAAMP_ALLOW_EXCHANGE) return; + $flushall = rand(0, 8) == 0; + if($quick) $flushall = false; + $orders = cryptsy_api_query('allmyorders'); if(empty($orders)) return; if(!is_array($orders) || !isset($orders['return'])) { @@ -53,6 +74,7 @@ function doCryptsyTrading($quick=false) return; } + // add orders foreach($orders['return'] as $order) { if(!isset($order['marketid'])) continue; @@ -212,6 +234,7 @@ function doCryptsyTrading($quick=false) $db_order->save(); } + if(is_object($savebalance)) if(floatval(EXCH_AUTO_WITHDRAW) > 0 && $savebalance->balance >= (EXCH_AUTO_WITHDRAW + 0.0002)) { $btcaddr = YAAMP_BTCADDRESS; //'14LS7Uda6EZGXLtRrFEZ2kWmarrxobkyu9'; diff --git a/web/yaamp/core/trading/empoex_trading.php b/web/yaamp/core/trading/empoex_trading.php index 802a22b..a2cacf9 100644 --- a/web/yaamp/core/trading/empoex_trading.php +++ b/web/yaamp/core/trading/empoex_trading.php @@ -2,7 +2,10 @@ function doEmpoexTrading($quick=false) { - $flushall = rand(0, 4) == 0; + $exchange = 'empoex'; + $updatebalances = !YAAMP_ALLOW_EXCHANGE; + + $flushall = rand(0, 8) == 0; if($quick) $flushall = false; $balances = empoex_api_user('account/balance','BTC'); @@ -10,15 +13,35 @@ function doEmpoexTrading($quick=false) if(!$balances || !isset($balances->available) || empty($balances->available)) return; - $savebalance = getdbosql('db_balances', "name='empoex'"); - $savebalance->balance = 0; + $savebalance = getdbosql('db_balances', "name='$exchange'"); + if (is_object($savebalance)) { + $savebalance->balance = 0; + $savebalance->save(); + } foreach($balances->available as $balance) { if($balance->Coin == 'BTC') { + if (!is_object($savebalance)) continue; $savebalance->balance = $balance->Amount; $savebalance->save(); - break; + continue; + } + + if ($updatebalances) { + // store available balance in market table + $coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol", + array(':symbol'=>$balance->Coin) + ); + if (empty($coins)) continue; + foreach ($coins as $coin) { + $market = getdbosql('db_markets', "coinid=:coinid AND name='$exchange'", array(':coinid'=>$coin->id)); + if (!$market) continue; + $market->balance = $balance->Amount; + //$market->ontrade = $balance->held ... todo + $market->balancetime = time(); + $market->save(); + } } } diff --git a/web/yaamp/core/trading/kraken_trading.php b/web/yaamp/core/trading/kraken_trading.php index 401ce13..34353b3 100644 --- a/web/yaamp/core/trading/kraken_trading.php +++ b/web/yaamp/core/trading/kraken_trading.php @@ -2,41 +2,42 @@ function doKrakenTrading($quick=false) { - $flushall = rand(0, 4) == 0; - if($quick) $flushall = false; + $exchange = 'kraken'; + $updatebalances = !YAAMP_ALLOW_EXCHANGE; $balances = kraken_api_user('Balance'); if(!$balances || !is_array($balances)) return; - $savebalance = getdbosql('db_balances', "name='kraken'"); - if (!is_object($savebalance)) return; - - $savebalance->balance = 0; - foreach($balances as $symbol => $balance) { if ($symbol == 'BTC') { - $savebalance->balance = $balance; - $savebalance->save(); + $db_balance = getdbosql('db_balances', "name='$exchange'"); + if ($db_balance) { + $db_balance->balance = $balance; + $db_balance->save(); + } continue; } - if (!YAAMP_ALLOW_EXCHANGE) { + if ($updatebalances) { // store available balance in market table $coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol", array(':symbol'=>$symbol) ); if (empty($coins)) continue; foreach ($coins as $coin) { - $market = getdbosql('db_markets', "coinid=:coinid AND name='kraken'", array(':coinid'=>$coin->id)); + $market = getdbosql('db_markets', "coinid=:coinid AND name='$exchange'", array(':coinid'=>$coin->id)); if (!$market) continue; - if ($market->balance != $balance) { - $market->balance = $balance; - $market->save(); - } + $market->balance = $balance; + $market->balancetime = time(); + $market->save(); } } } if (!YAAMP_ALLOW_EXCHANGE) return; + + $flushall = rand(0, 8) == 0; + if($quick) $flushall = false; + } diff --git a/web/yaamp/core/trading/poloniex_trading.php b/web/yaamp/core/trading/poloniex_trading.php index 08700f4..3b2585b 100644 --- a/web/yaamp/core/trading/poloniex_trading.php +++ b/web/yaamp/core/trading/poloniex_trading.php @@ -20,36 +20,39 @@ function doPoloniexCancelOrder($OrderID=false, $pair=false, $poloniex=false) function doPoloniexTrading() { -// debuglog('-------------- doPoloniexTrading()'); + $exchange = 'poloniex'; + $updatebalances = !YAAMP_ALLOW_EXCHANGE; $poloniex = new poloniex; // add orders - $savebalance = getdbosql('db_balances', "name='poloniex'"); + $savebalance = getdbosql('db_balances', "name='$exchange'"); $balances = $poloniex->get_complete_balances(); if (is_array($balances)) foreach($balances as $symbol => $balance) { if ($symbol == 'BTC') { - $savebalance->balance = $balance['available']; - $savebalance->save(); + if (is_object($savebalance)) { + $savebalance->balance = $balance['available']; + $savebalance->save(); + } continue; } - if (!YAAMP_ALLOW_EXCHANGE) { + if ($updatebalances) { // store available balance in market table $coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol", array(':symbol'=>$symbol) ); if (empty($coins)) continue; foreach ($coins as $coin) { - $market = getdbosql('db_markets', "coinid=:coinid AND name='poloniex'", array(':coinid'=>$coin->id)); + $market = getdbosql('db_markets', "coinid=:coinid AND name='$exchange'", array(':coinid'=>$coin->id)); if (!$market) continue; - if ($market->balance != $balance['available']) { - $market->balance = $balance['available']; - $market->save(); - } + $market->balance = $balance['available']; + $market->ontrade = arraySafeVal($balance,'onOrders',0); + $market->balancetime = time(); + $market->save(); } } } @@ -202,6 +205,7 @@ function doPoloniexTrading() $db_order->save(); } + if(is_object($savebalance)) if(floatval(EXCH_AUTO_WITHDRAW) > 0 && $savebalance->balance >= (EXCH_AUTO_WITHDRAW + 0.0002)) { $btcaddr = YAAMP_BTCADDRESS; diff --git a/web/yaamp/core/trading/safecex_trading.php b/web/yaamp/core/trading/safecex_trading.php index d6f9dca..57ba860 100644 --- a/web/yaamp/core/trading/safecex_trading.php +++ b/web/yaamp/core/trading/safecex_trading.php @@ -20,37 +20,41 @@ function doSafecexCancelOrder($OrderID=false) function doSafecexTrading($quick=false) { + $exchange = 'safecex'; + $updatebalances = !YAAMP_ALLOW_EXCHANGE; + // {"symbol":"BTC","balance":0.01056525,"pending":0,"orders":0,"total":0.01056525,"deposit":"15pQYjcBJxo3RQfJe6C5pYxHcxAjzVyTfv","withdraw":"1E1..."} $balances = safecex_api_user('getbalances'); if(empty($balances)) return; + $savebalance = getdbosql('db_balances', "name='$exchange'"); + foreach($balances as $balance) { if ($balance->symbol == 'BTC') { - $db_balance = getdbosql('db_balances', "name='safecex'"); - if ($db_balance) { - $db_balance->balance = $balance->balance; - $db_balance->save(); + if (is_object($savebalance)) { + $savebalance->balance = $balance->balance; + $savebalance->save(); } continue; } - if (!YAAMP_ALLOW_EXCHANGE) { + if ($updatebalances) { // store available balance in market table $coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol", array(':symbol'=>$balance->symbol) ); if (empty($coins)) continue; foreach ($coins as $coin) { - $market = getdbosql('db_markets', "coinid=:coinid AND name='safecex'", array(':coinid'=>$coin->id)); + $market = getdbosql('db_markets', "coinid=:coinid AND name='$exchange'", array(':coinid'=>$coin->id)); if (!$market) continue; - if ($market->balance != $balance->balance) { - $market->balance = $balance->balance; - if (!empty($balance->deposit) && $market->deposit_address != $balance->deposit) { - debuglog("safecex: {$coin->symbol} deposit address updated"); - $market->deposit_address = $balance->deposit; - } - $market->save(); + $market->balance = $balance->balance; + $market->ontrade = $balance->orders; + if (!empty($balance->deposit) && $market->deposit_address != $balance->deposit) { + debuglog("$exchange: {$coin->symbol} deposit address updated"); + $market->deposit_address = $balance->deposit; } + $market->balancetime = time(); + $market->save(); } } } @@ -96,27 +100,20 @@ function doSafecexTrading($quick=false) debuglog("safecex: cancel order {$order->market} at $sellprice, ask price is now $ask"); sleep(1); doSafecexCancelOrder($order->id); - //safecex_api_user('cancelorder', "&id={$order->id}"); - - //$db_order = getdbosql('db_orders', "market=:market AND uuid=:uuid", array( - // ':market'=>'safecex', ':uuid'=>$order->id - //)); - //if($db_order) $db_order->delete(); - } // store existing orders in the db else { $db_order = getdbosql('db_orders', "market=:market AND uuid=:uuid", array( - ':market'=>'safecex', ':uuid'=>$order->id + ':market'=>$exchange, ':uuid'=>$order->id )); if($db_order) continue; debuglog("safecex: store new order of {$order->amount} {$coin->symbol} at $sellprice BTC"); $db_order = new db_orders; - $db_order->market = 'safecex'; + $db_order->market = $exchange; $db_order->coinid = $coin->id; $db_order->amount = $order->amount; $db_order->price = $sellprice; @@ -129,7 +126,7 @@ function doSafecexTrading($quick=false) } // flush obsolete orders - $list = getdbolist('db_orders', "market='safecex'"); + $list = getdbolist('db_orders', "market='$exchange'"); if (!empty($list) && !empty($orders)) foreach($list as $db_order) { @@ -140,14 +137,14 @@ function doSafecexTrading($quick=false) foreach($orders as $order) { if($order->type != 'sell') continue; if($order->id == $db_order->uuid) { - // debuglog("safecex: order waiting, {$order->amount} {$coin->symbol}"); + // debuglog("$exchange: order waiting, {$order->amount} {$coin->symbol}"); $found = true; break; } } if(!$found) { - debuglog("safecex: delete db order {$db_order->amount} {$coin->symbol}"); + debuglog("$exchange: delete db order {$db_order->amount} {$coin->symbol}"); $db_order->delete(); } } @@ -165,7 +162,7 @@ function doSafecexTrading($quick=false) $symbol = $coin->symbol; if (!empty($coin->symbol2)) $symbol = $coin->symbol2; - $market = getdbosql('db_markets', "coinid={$coin->id} AND name='safecex'"); + $market = getdbosql('db_markets', "coinid={$coin->id} AND name='$exchange'"); if($market) { $market->lasttraded = time(); @@ -197,7 +194,7 @@ function doSafecexTrading($quick=false) if($sellamount*$sellprice < $min_btc_trade) continue; - debuglog("safecex: selling $sellamount $symbol at $sellprice"); + debuglog("$exchange: selling $sellamount $symbol at $sellprice"); sleep(1); $res = safecex_api_user('selllimit', "&market={$pair}&price={$sellprice}&amount={$sellamount}"); if(!$res || $res->status != 'ok') @@ -221,7 +218,7 @@ function doSafecexTrading($quick=false) $sellprice = bitcoinvaluetoa($ticker->ask * $sell_ask_pct); // lowest ask price +5% if($amount*$sellprice < $min_btc_trade) continue; - debuglog("safecex: selling $amount $symbol at $sellprice"); + debuglog("$exchange: selling $amount $symbol at $sellprice"); sleep(1); $res = safecex_api_user('selllimit', "&market={$pair}&price={$sellprice}&amount={$amount}"); if(!$res || $res->status != 'ok') @@ -232,7 +229,7 @@ function doSafecexTrading($quick=false) if (property_exists($res,'id')) { $db_order = new db_orders; - $db_order->market = 'safecex'; + $db_order->market = $exchange; $db_order->coinid = $coin->id; $db_order->amount = $amount; $db_order->price = $sellprice; @@ -245,21 +242,21 @@ function doSafecexTrading($quick=false) } /* withdraw API doesn't exist - $db_balance = getdbosql('db_balances', "name='safecex'"); + $db_balance = getdbosql('db_balances', "name='$exchange'"); if(floatval(EXCH_AUTO_WITHDRAW) > 0 && $db_balance->balance >= (EXCH_AUTO_WITHDRAW + 0.0002)) { $btcaddr = YAAMP_BTCADDRESS; $amount = $db_balance->balance; - debuglog("safecex: withdraw $amount to $btcaddr"); + debuglog("$exchange: withdraw $amount to $btcaddr"); sleep(1); $res = safecex_api_user('withdraw', "¤cy=BTC&amount={$amount}&address={$btcaddr}"); - debuglog("safecex: withdraw: ".json_encode($res)); + debuglog("$exchange: withdraw: ".json_encode($res)); if($res && $res->success) { $withdraw = new db_withdraws; - $withdraw->market = 'safecex'; + $withdraw->market = $exchange; $withdraw->address = $btcaddr; $withdraw->amount = $amount + 0.0002; $withdraw->time = time(); diff --git a/web/yaamp/core/trading/yobit_trading.php b/web/yaamp/core/trading/yobit_trading.php index 8a212d0..8d896fe 100644 --- a/web/yaamp/core/trading/yobit_trading.php +++ b/web/yaamp/core/trading/yobit_trading.php @@ -16,35 +16,40 @@ function doYobitCancelOrder($OrderID=false) function doYobitTrading($quick=false) { - $savebalance = getdbosql('db_balances', "name='yobit'"); - if(!$savebalance) return; - - $savebalance->balance = 0; + $exchange = 'yobit'; + $updatebalances = !YAAMP_ALLOW_EXCHANGE; $balances = yobit_api_query2('getInfo'); if(!$balances || !isset($balances['return'])) return; if(!isset($balances['return']['funds'])) return; + $savebalance = getdbosql('db_balances', "name='$exchange'"); + if (is_object($savebalance)) { + $savebalance->balance = 0; + $savebalance->save(); + } + foreach($balances['return']['funds'] as $symbol => $amount) { if ($symbol == 'btc') { - $savebalance->balance = $amount; - $savebalance->save(); + if (is_object($savebalance)) { + $savebalance->balance = $amount; + $savebalance->save(); + } continue; } - if (!YAAMP_ALLOW_EXCHANGE) { + if ($updatebalances) { // store balance in market table (= available + onorders on yobit) $coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol", array(':symbol'=>strtoupper($symbol)) ); if (empty($coins)) continue; foreach ($coins as $coin) { - $market = getdbosql('db_markets', "coinid=:coinid AND name='yobit'", array(':coinid'=>$coin->id)); + $market = getdbosql('db_markets', "coinid=:coinid AND name='$exchange'", array(':coinid'=>$coin->id)); if (!$market) continue; - if ($market->balance != $amount) { - $market->balance = $amount; - $market->save(); - } + $market->balance = $amount; + $market->balancetime = time(); + $market->save(); } } }