markets: new fields + balances update

This commit is contained in:
Tanguy Pruvot 2016-03-26 19:37:27 +01:00
parent dcce3ff418
commit f72ce04be1
14 changed files with 269 additions and 148 deletions

View file

@ -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`;

View file

@ -36,6 +36,7 @@ function BackendPricesUpdate()
$market2->price = $market->price; $market2->price = $market->price;
$market2->price2 = $market->price2; $market2->price2 = $market->price2;
$market2->deposit_address = $market->deposit_address; $market2->deposit_address = $market->deposit_address;
$market2->pricetime = $market->pricetime;
$market2->save(); $market2->save();
} }
@ -159,11 +160,11 @@ function updateBleutradeMarkets()
} }
$market->txfee = $currency->TxFee; $market->txfee = $currency->TxFee;
if(!$currency->IsActive) if(!$currency->IsActive && !$market->disabled)
{ {
$market->price = 0; $market->price = 0;
$market->disabled = 1;
$market->save(); $market->save();
continue; continue;
} }
@ -174,9 +175,14 @@ function updateBleutradeMarkets()
$ticker = bleutrade_api_query('public/getticker', "&market=$pair"); $ticker = bleutrade_api_query('public/getticker', "&market=$pair");
if(!$ticker || !$ticker->success || !$ticker->result) continue; 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; $price2 = ($ticker->result[0]->Bid+$ticker->result[0]->Ask)/2;
$market->price2 = AverageIncrement($market->price2, $price2); $market->price2 = AverageIncrement($market->price2, $price2);
$market->price = AverageIncrement($market->price, $ticker->result[0]->Bid); $market->price = AverageIncrement($market->price, $ticker->result[0]->Bid);
$market->pricetime = time();
if(!empty(EXCH_BLEUTRADE_KEY)) if(!empty(EXCH_BLEUTRADE_KEY))
{ {
@ -246,6 +252,7 @@ function updateKrakenMarkets($force = false)
$price2 = ($ticker['b'][0] + $ticker['a'][0]) / 2; $price2 = ($ticker['b'][0] + $ticker['a'][0]) / 2;
$market->price2 = AverageIncrement($market->price2, $price2); $market->price2 = AverageIncrement($market->price2, $price2);
$market->price = AverageIncrement($market->price, $ticker['b'][0]); $market->price = AverageIncrement($market->price, $ticker['b'][0]);
$market->pricetime = time();
$market->save(); $market->save();
} }
@ -307,6 +314,7 @@ function updateBittrexMarkets($force = false)
$price2 = ($ticker->result->Bid+$ticker->result->Ask)/2; $price2 = ($ticker->result->Bid+$ticker->result->Ask)/2;
$market->price2 = AverageIncrement($market->price2, $price2); $market->price2 = AverageIncrement($market->price2, $price2);
$market->price = AverageIncrement($market->price, $ticker->result->Bid); $market->price = AverageIncrement($market->price, $ticker->result->Bid);
$market->pricetime = time();
$market->save(); $market->save();
@ -426,6 +434,7 @@ function updateCryptsyMarkets()
$market->price2 = AverageIncrement($market->price2, $price2); $market->price2 = AverageIncrement($market->price2, $price2);
$market->price = AverageIncrement($market->price, $ticker->return->$symbol->buyorders[0]->price); $market->price = AverageIncrement($market->price, $ticker->return->$symbol->buyorders[0]->price);
$market->pricetime = time();
$market->save(); $market->save();
@ -508,6 +517,7 @@ function updateCCexMarkets()
$market->price2 = AverageIncrement($market->price2, $price2); $market->price2 = AverageIncrement($market->price2, $price2);
$market->price = AverageIncrement($market->price, $ticker['buy']); $market->price = AverageIncrement($market->price, $ticker['buy']);
$market->pricetime = time();
$market->save(); $market->save();
@ -578,6 +588,7 @@ function updatePoloniexMarkets()
$price2 = ($ticker['highestBid']+$ticker['lowestAsk'])/2; $price2 = ($ticker['highestBid']+$ticker['lowestAsk'])/2;
$market->price2 = AverageIncrement($market->price2, $price2); $market->price2 = AverageIncrement($market->price2, $price2);
$market->price = AverageIncrement($market->price, $ticker['highestBid']); $market->price = AverageIncrement($market->price, $ticker['highestBid']);
$market->pricetime = time();
$market->save(); $market->save();
@ -671,6 +682,7 @@ function updateYobitMarkets()
$price2 = ($ticker->$pair->buy + $ticker->$pair->sell) / 2; $price2 = ($ticker->$pair->buy + $ticker->$pair->sell) / 2;
$market->price2 = AverageIncrement($market->price2, $price2); $market->price2 = AverageIncrement($market->price2, $price2);
$market->price = AverageIncrement($market->price, $ticker->$pair->buy); $market->price = AverageIncrement($market->price, $ticker->$pair->buy);
$market->pricetime = time();
$market->save(); $market->save();
} }
@ -702,6 +714,7 @@ function updateJubiMarkets()
$price2 = ($ticker->buy+$ticker->sell)/2; $price2 = ($ticker->buy+$ticker->sell)/2;
$market->price2 = AverageIncrement($market->price2, $price2); $market->price2 = AverageIncrement($market->price2, $price2);
$market->price = AverageIncrement($market->price, $ticker->buy*0.95); $market->price = AverageIncrement($market->price, $ticker->buy*0.95);
$market->pricetime = time();
// debuglog("jubi update $coin->symbol: $market->price $market->price2"); // debuglog("jubi update $coin->symbol: $market->price $market->price2");
@ -729,12 +742,14 @@ function updateAlcurexMarkets()
if (is_object($last) && !empty($last->$lpair)) { if (is_object($last) && !empty($last->$lpair)) {
$last = $last->$lpair; $last = $last->$lpair;
$market->price = AverageIncrement($market->price, $last->price); $market->price = AverageIncrement($market->price, $last->price);
$market->pricetime = time();
$market->save(); $market->save();
} }
$last = alcurex_api_query('market', "?pair=$lpair&last=sell"); $last = alcurex_api_query('market', "?pair=$lpair&last=sell");
if (is_object($last) && !empty($last->$lpair)) { if (is_object($last) && !empty($last->$lpair)) {
$last = $last->$lpair; $last = $last->$lpair;
$market->price2 = AverageIncrement($market->price2, $last->price); $market->price2 = AverageIncrement($market->price2, $last->price);
$market->pricetime = time();
$market->save(); $market->save();
} }
if (empty($coin->price)) { if (empty($coin->price)) {
@ -769,6 +784,7 @@ function updateCryptopiaMarkets()
$market->price2 = AverageIncrement($market->price2, $price2); $market->price2 = AverageIncrement($market->price2, $price2);
$market->price = AverageIncrement($market->price, $ticker->BidPrice*0.98); $market->price = AverageIncrement($market->price, $ticker->BidPrice*0.98);
$market->marketid = $ticker->TradePairId; $market->marketid = $ticker->TradePairId;
$market->pricetime = time();
$market->save(); $market->save();
if (empty($coin->price)) { if (empty($coin->price)) {
$coin->price = $market->price; $coin->price = $market->price;
@ -829,6 +845,7 @@ function updateBanxioMarkets()
if ($ticker->marketname === $pair) { if ($ticker->marketname === $pair) {
$market->price = AverageIncrement($market->price, $ticker->bid); $market->price = AverageIncrement($market->price, $ticker->bid);
$market->price2 = AverageIncrement($market->price2, $ticker->last); $market->price2 = AverageIncrement($market->price2, $ticker->last);
$market->pricetime = time();
// debuglog("banx: $pair {$ticker->bid} {$ticker->last} => {$market->price} {$market->price2}"); // debuglog("banx: $pair {$ticker->bid} {$ticker->last} => {$market->price} {$market->price2}");
if (intval($ticker->dayvolume) > 1) if (intval($ticker->dayvolume) > 1)
$market->save(); $market->save();
@ -911,6 +928,7 @@ function updateSafecexMarkets()
$price2 = ($ticker->bid + $ticker->ask)/2; $price2 = ($ticker->bid + $ticker->ask)/2;
$market->price2 = AverageIncrement($market->price2, $price2); $market->price2 = AverageIncrement($market->price2, $price2);
$market->price = AverageIncrement($market->price, $ticker->bid*0.98); $market->price = AverageIncrement($market->price, $ticker->bid*0.98);
$market->pricetime = time();
$market->save(); $market->save();
if (empty($coin->price)) { if (empty($coin->price)) {
@ -991,6 +1009,12 @@ function updateBterMarkets()
{ {
$coin = getdbo('db_coins', $market->coinid); $coin = getdbo('db_coins', $market->coinid);
if(!$coin || !$coin->installed) continue; 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); $lowsymbol = strtolower($coin->symbol);
$dbpair = $lowsymbol.'_btc'; $dbpair = $lowsymbol.'_btc';
@ -999,7 +1023,8 @@ function updateBterMarkets()
$market->price = AverageIncrement($market->price, $ticker['buy']); $market->price = AverageIncrement($market->price, $ticker['buy']);
$market->price2 = AverageIncrement($market->price2, $ticker['avg']); $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(); $market->save();
if (empty($coin->price2)) { if (empty($coin->price2)) {
@ -1030,6 +1055,7 @@ function updateEmpoexMarkets()
$market->price = AverageIncrement($market->price, $ticker->bid); $market->price = AverageIncrement($market->price, $ticker->bid);
$market->price2 = AverageIncrement($market->price2, $ticker->ask); $market->price2 = AverageIncrement($market->price2, $ticker->ask);
$market->pricetime = time();
if (floatval($ticker->base_volume_24hr) > 0.01) if (floatval($ticker->base_volume_24hr) > 0.01)
$market->save(); $market->save();
@ -1063,7 +1089,7 @@ function updateOtherMarkets()
$coin->price2 = $ticker->price; $coin->price2 = $ticker->price;
$coin->price = AverageIncrement((float)$coin->price, (float)$coin->price2); $coin->price = AverageIncrement((float)$coin->price, (float)$coin->price2);
if ($coin->save()) { if ($coin->save()) {
debuglog("update price of $symbol ".bitcoinvaluetoa($coin->price)); debuglog("cryptonator: $symbol price set to ".bitcoinvaluetoa($coin->price));
} }
} }
} }

View file

@ -16,38 +16,46 @@ function doBittrexCancelOrder($OrderID=false)
function doBittrexTrading($quick=false) function doBittrexTrading($quick=false)
{ {
$exchange = 'bittrex';
$updatebalances = !YAAMP_ALLOW_EXCHANGE;
$balances = bittrex_api_query('account/getbalances'); $balances = bittrex_api_query('account/getbalances');
if(!$balances || !isset($balances->result) || !$balances->success) return; if(!$balances || !isset($balances->result) || !$balances->success) return;
$savebalance = getdbosql('db_balances', "name='bittrex'"); $savebalance = getdbosql('db_balances', "name='$exchange'");
if (is_object($savebalance)) {
$savebalance->balance = 0; $savebalance->balance = 0;
$savebalance->save();
}
foreach($balances->result as $balance) foreach($balances->result as $balance)
{ {
if ($balance->Currency == 'BTC') { if ($balance->Currency == 'BTC') {
if (is_object($savebalance)) {
$savebalance->balance = $balance->Available; $savebalance->balance = $balance->Available;
$savebalance->save(); $savebalance->save();
}
continue; continue;
} }
if (!YAAMP_ALLOW_EXCHANGE) { if ($updatebalances) {
// store available balance in market table // store available balance in market table
$coins = getdbolist('db_coins', "symbol=:sym OR symbol2=:sym", array(':sym'=>$balance->Currency)); $coins = getdbolist('db_coins', "symbol=:sym OR symbol2=:sym", array(':sym'=>$balance->Currency));
if (empty($coins)) continue; if (empty($coins)) continue;
foreach ($coins as $coin) { 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) continue;
if ($market->balance != $balance->Available) {
$market->balance = $balance->Available; $market->balance = $balance->Available;
$market->ontrade = $balance->Balance - $balance->Available;
if (!empty($balance->CryptoAddress) && $market->deposit_address != $balance->CryptoAddress) { if (!empty($balance->CryptoAddress) && $market->deposit_address != $balance->CryptoAddress) {
debuglog("bittrex: {$coin->symbol} deposit address updated"); debuglog("$exchange: {$coin->symbol} deposit address updated");
$market->deposit_address = $balance->CryptoAddress; $market->deposit_address = $balance->CryptoAddress;
} }
$market->balancetime = time();
$market->save(); $market->save();
} }
} }
} }
}
if (!YAAMP_ALLOW_EXCHANGE) return; if (!YAAMP_ALLOW_EXCHANGE) return;

View file

@ -16,41 +16,49 @@ function doBleutradeCancelOrder($OrderID=false)
function doBleutradeTrading($quick=false) function doBleutradeTrading($quick=false)
{ {
$exchange = 'bleutrade';
$updatebalances = !YAAMP_ALLOW_EXCHANGE;
$balances = bleutrade_api_query('account/getbalances'); $balances = bleutrade_api_query('account/getbalances');
if(!$balances || !isset($balances->result) || !$balances->success) return; if(!$balances || !isset($balances->result) || !$balances->success) return;
$savebalance = getdbosql('db_balances', "name='bleutrade'"); $savebalance = getdbosql('db_balances', "name='$exchange'");
if (is_object($savebalance)) {
$savebalance->balance = 0; $savebalance->balance = 0;
$savebalance->save();
}
foreach($balances->result as $balance) foreach($balances->result as $balance)
{ {
if ($balance->Currency == 'BTC') { if ($balance->Currency == 'BTC') {
if (is_object($savebalance)) {
$savebalance->balance = $balance->Available; $savebalance->balance = $balance->Available;
$savebalance->save(); $savebalance->save();
}
continue; continue;
} }
if (!YAAMP_ALLOW_EXCHANGE) { if ($updatebalances) {
// store available balance in market table // store available balance in market table
$coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol", $coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol",
array(':symbol'=>$balance->Currency) array(':symbol'=>$balance->Currency)
); );
if (empty($coins)) continue; if (empty($coins)) continue;
foreach ($coins as $coin) { 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) continue;
if ($market->balance != $balance->Available) {
$market->balance = $balance->Available; $market->balance = $balance->Available;
$market->ontrade = $balance->Balance - $balance->Available;
if (!empty($balance->CryptoAddress) && $market->deposit_address != $balance->CryptoAddress) { if (!empty($balance->CryptoAddress) && $market->deposit_address != $balance->CryptoAddress) {
debuglog("bleutrade: {$coin->symbol} deposit address updated"); debuglog("$exchange: {$coin->symbol} deposit address updated");
$market->deposit_address = $balance->CryptoAddress; $market->deposit_address = $balance->CryptoAddress;
} }
if (property_exists($balance,'IsActive')) if (property_exists($balance,'IsActive'))
$market->message = ($balance->IsActive == "true") ? "" : "Disabled"; $market->message = ($balance->IsActive == "true") ? "" : "Disabled";
$market->balancetime = time();
$market->save(); $market->save();
} }
} }
} }
}
if (!YAAMP_ALLOW_EXCHANGE) return; if (!YAAMP_ALLOW_EXCHANGE) return;

View file

@ -23,9 +23,10 @@ function doBterTrading($quick=false)
foreach($balances['available_funds'] as $symbol => $available) foreach($balances['available_funds'] as $symbol => $available)
{ {
if ($symbol == 'BTC') { if ($symbol == 'BTC') {
if (!is_object($savebalance)) continue; if (is_object($savebalance)) {
$savebalance->balance = $balance->available; $savebalance->balance = $balance->available;
$savebalance->save(); $savebalance->save();
}
continue; continue;
} }
@ -38,13 +39,13 @@ function doBterTrading($quick=false)
foreach ($coins as $coin) { foreach ($coins as $coin) {
$market = getdbosql('db_markets', "coinid=:coinid AND name='{$exchange}'", array(':coinid'=>$coin->id)); $market = getdbosql('db_markets', "coinid=:coinid AND name='{$exchange}'", array(':coinid'=>$coin->id));
if (!$market) continue; if (!$market) continue;
if ($market->balance != $available) {
$market->balance = $available; $market->balance = $available;
$market->ontrade = arraySafeVal($balances['locked_funds'],$symbol,0);
$market->balancetime = time();
$market->save(); $market->save();
} }
} }
} }
}
if (!YAAMP_ALLOW_EXCHANGE) return; if (!YAAMP_ALLOW_EXCHANGE) return;

View file

@ -17,12 +17,16 @@ function doCCexCancelOrder($OrderID=false, $ccex=false)
function doCCexTrading($quick=false) function doCCexTrading($quick=false)
{ {
// debuglog("-------------- doCCexTrading() $flushall"); $exchange = 'c-cex';
$updatebalances = !YAAMP_ALLOW_EXCHANGE;
$ccex = new CcexAPI; $ccex = new CcexAPI;
$savebalance = getdbosql('db_balances', "name='c-cex'"); $savebalance = getdbosql('db_balances', "name='$exchange'");
if (is_object($savebalance)) {
$savebalance->balance = 0; $savebalance->balance = 0;
$savebalance->save();
}
$balances = $ccex->getBalance(); $balances = $ccex->getBalance();
if(!$balances || !isset($balances['return'])) return; if(!$balances || !isset($balances['return'])) return;
@ -30,25 +34,25 @@ function doCCexTrading($quick=false)
foreach($balances['return'] as $balance) foreach($balance as $symbol=>$amount) foreach($balances['return'] as $balance) foreach($balance as $symbol=>$amount)
{ {
if ($symbol == 'btc') { if ($symbol == 'btc') {
if (!is_object($savebalance)) continue;
$savebalance->balance = $amount; // (available one) $savebalance->balance = $amount; // (available one)
$savebalance->save(); $savebalance->save();
continue; continue;
} }
if (!YAAMP_ALLOW_EXCHANGE) { if ($updatebalances) {
// store available balance in market table // store available balance in market table
$coins = getdbolist('db_coins', "symbol=:sym OR symbol2=:sym", array(':sym'=>strtoupper($symbol))); $coins = getdbolist('db_coins', "symbol=:sym OR symbol2=:sym", array(':sym'=>strtoupper($symbol)));
if (empty($coins)) continue; if (empty($coins)) continue;
foreach ($coins as $coin) { 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) continue;
if ($market->balance != $amount) {
$market->balance = $amount; $market->balance = $amount;
$market->balancetime = time();
$market->save(); $market->save();
} }
} }
} }
}
if (!YAAMP_ALLOW_EXCHANGE) return; if (!YAAMP_ALLOW_EXCHANGE) return;

View file

@ -36,10 +36,13 @@ function doBanxTrading($quick=false)
$market = getdbosql('db_markets', "coinid=:coinid AND name='{$exchange}'", array(':coinid'=>$coin->id)); $market = getdbosql('db_markets', "coinid=:coinid AND name='{$exchange}'", array(':coinid'=>$coin->id));
if (!$market) continue; if (!$market) continue;
$market->balance = $balance->available; $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"); debuglog("{$exchange}: {$coin->symbol} deposit address updated");
$market->deposit_address = $balance->cryptoaddress; $market->deposit_address = $balance->cryptoaddress;
} }
$market->balancetime = time();
$market->save(); $market->save();
} }
} }

View file

@ -16,42 +16,51 @@ function doCryptopiaCancelOrder($OrderID=false)
function doCryptopiaTrading($quick=false) function doCryptopiaTrading($quick=false)
{ {
$savebalance = getdbosql('db_balances', "name='cryptopia'"); $exchange = 'cryptopia';
if(!$savebalance) return; $updatebalances = !YAAMP_ALLOW_EXCHANGE;
$balances = cryptopia_api_user('GetBalance'); $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) foreach($balances->Data as $balance)
{ {
if ($balance->Symbol == 'BTC') { if ($balance->Symbol == 'BTC') {
if (is_object($savebalance)) {
$savebalance->balance = $balance->Available; $savebalance->balance = $balance->Available;
$savebalance->save(); $savebalance->save();
}
continue; continue;
} }
if (!YAAMP_ALLOW_EXCHANGE) { if ($updatebalances) {
// store available balance in market table // store available balance in market table
$coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol", $coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol",
array(':symbol'=>$balance->Symbol) array(':symbol'=>$balance->Symbol)
); );
if (empty($coins)) continue; if (empty($coins)) continue;
foreach ($coins as $coin) { 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) continue;
if ($market->balance != $balance->Available) {
$market->balance = $balance->Available; $market->balance = $balance->Available;
$market->ontrade = $balance->HeldForTrades;
$market->message = $balance->StatusMessage; $market->message = $balance->StatusMessage;
if (property_exists($balance, 'Address')) if (property_exists($balance, 'Address'))
if (!empty($balance->Address) && $market->deposit_address != $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->deposit_address = $balance->Address;
} }
$market->balancetime = time();
$market->save(); $market->save();
} }
} }
} }
}
if (!YAAMP_ALLOW_EXCHANGE) return; if (!YAAMP_ALLOW_EXCHANGE) return;
@ -244,6 +253,7 @@ function doCryptopiaTrading($quick=false)
} }
// auto withdraw // auto withdraw
if(is_object($savebalance))
if(floatval(EXCH_AUTO_WITHDRAW) > 0 && $savebalance->balance >= (EXCH_AUTO_WITHDRAW + 0.0002)) if(floatval(EXCH_AUTO_WITHDRAW) > 0 && $savebalance->balance >= (EXCH_AUTO_WITHDRAW + 0.0002))
{ {
$btcaddr = YAAMP_BTCADDRESS; $btcaddr = YAAMP_BTCADDRESS;

View file

@ -20,32 +20,53 @@ function getCryptsyTicker($marketid)
function doCryptsyTrading($quick=false) function doCryptsyTrading($quick=false)
{ {
$flushall = rand(0, 4) == 0; $exchange = 'cryptsy';
if($quick) $flushall = false; $updatebalances = !YAAMP_ALLOW_EXCHANGE;
// debuglog("-------------- doCryptsyTrading() $flushall"); $savebalance = getdbosql('db_balances', "name='$exchange'");
if (is_object($savebalance)) {
// add orders
$savebalance = getdbosql('db_balances', "name='cryptsy'");
$savebalance->balance = 0; $savebalance->balance = 0;
$savebalance->save();
}
$balances = cryptsy_api_query('getinfo'); $balances = cryptsy_api_query('getinfo');
if(!$balances) return; if(!$balances) return;
if(!isset($balances['return'])) { if(!isset($balances['return'])) {
debuglog("cryptsy balance: ".arraySafeVal($balances,'error')); debuglog("$exchange balance: ".arraySafeVal($balances,'error'));
return; return;
} }
foreach($balances['return']['balances_available'] as $symbol=>$balance) foreach($balances['return']['balances_available'] as $symbol=>$balance)
{ {
if($symbol == 'BTC') { if($symbol == 'BTC') {
if (is_object($savebalance)) {
$savebalance->balance = floatval($balance); $savebalance->balance = floatval($balance);
$savebalance->save(); $savebalance->save();
break; }
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; if (!YAAMP_ALLOW_EXCHANGE) return;
$flushall = rand(0, 8) == 0;
if($quick) $flushall = false;
$orders = cryptsy_api_query('allmyorders'); $orders = cryptsy_api_query('allmyorders');
if(empty($orders)) return; if(empty($orders)) return;
if(!is_array($orders) || !isset($orders['return'])) { if(!is_array($orders) || !isset($orders['return'])) {
@ -53,6 +74,7 @@ function doCryptsyTrading($quick=false)
return; return;
} }
// add orders
foreach($orders['return'] as $order) foreach($orders['return'] as $order)
{ {
if(!isset($order['marketid'])) continue; if(!isset($order['marketid'])) continue;
@ -212,6 +234,7 @@ function doCryptsyTrading($quick=false)
$db_order->save(); $db_order->save();
} }
if(is_object($savebalance))
if(floatval(EXCH_AUTO_WITHDRAW) > 0 && $savebalance->balance >= (EXCH_AUTO_WITHDRAW + 0.0002)) if(floatval(EXCH_AUTO_WITHDRAW) > 0 && $savebalance->balance >= (EXCH_AUTO_WITHDRAW + 0.0002))
{ {
$btcaddr = YAAMP_BTCADDRESS; //'14LS7Uda6EZGXLtRrFEZ2kWmarrxobkyu9'; $btcaddr = YAAMP_BTCADDRESS; //'14LS7Uda6EZGXLtRrFEZ2kWmarrxobkyu9';

View file

@ -2,7 +2,10 @@
function doEmpoexTrading($quick=false) function doEmpoexTrading($quick=false)
{ {
$flushall = rand(0, 4) == 0; $exchange = 'empoex';
$updatebalances = !YAAMP_ALLOW_EXCHANGE;
$flushall = rand(0, 8) == 0;
if($quick) $flushall = false; if($quick) $flushall = false;
$balances = empoex_api_user('account/balance','BTC'); $balances = empoex_api_user('account/balance','BTC');
@ -10,15 +13,35 @@ function doEmpoexTrading($quick=false)
if(!$balances || !isset($balances->available) || empty($balances->available)) return; if(!$balances || !isset($balances->available) || empty($balances->available)) return;
$savebalance = getdbosql('db_balances', "name='empoex'"); $savebalance = getdbosql('db_balances', "name='$exchange'");
if (is_object($savebalance)) {
$savebalance->balance = 0; $savebalance->balance = 0;
$savebalance->save();
}
foreach($balances->available as $balance) foreach($balances->available as $balance)
{ {
if($balance->Coin == 'BTC') { if($balance->Coin == 'BTC') {
if (!is_object($savebalance)) continue;
$savebalance->balance = $balance->Amount; $savebalance->balance = $balance->Amount;
$savebalance->save(); $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();
}
} }
} }

View file

@ -2,41 +2,42 @@
function doKrakenTrading($quick=false) function doKrakenTrading($quick=false)
{ {
$flushall = rand(0, 4) == 0; $exchange = 'kraken';
if($quick) $flushall = false; $updatebalances = !YAAMP_ALLOW_EXCHANGE;
$balances = kraken_api_user('Balance'); $balances = kraken_api_user('Balance');
if(!$balances || !is_array($balances)) return; 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) foreach($balances as $symbol => $balance)
{ {
if ($symbol == 'BTC') { if ($symbol == 'BTC') {
$savebalance->balance = $balance; $db_balance = getdbosql('db_balances', "name='$exchange'");
$savebalance->save(); if ($db_balance) {
$db_balance->balance = $balance;
$db_balance->save();
}
continue; continue;
} }
if (!YAAMP_ALLOW_EXCHANGE) { if ($updatebalances) {
// store available balance in market table // store available balance in market table
$coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol", $coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol",
array(':symbol'=>$symbol) array(':symbol'=>$symbol)
); );
if (empty($coins)) continue; if (empty($coins)) continue;
foreach ($coins as $coin) { 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) continue;
if ($market->balance != $balance) {
$market->balance = $balance; $market->balance = $balance;
$market->balancetime = time();
$market->save(); $market->save();
} }
} }
} }
}
if (!YAAMP_ALLOW_EXCHANGE) return; if (!YAAMP_ALLOW_EXCHANGE) return;
$flushall = rand(0, 8) == 0;
if($quick) $flushall = false;
} }

View file

@ -20,39 +20,42 @@ function doPoloniexCancelOrder($OrderID=false, $pair=false, $poloniex=false)
function doPoloniexTrading() function doPoloniexTrading()
{ {
// debuglog('-------------- doPoloniexTrading()'); $exchange = 'poloniex';
$updatebalances = !YAAMP_ALLOW_EXCHANGE;
$poloniex = new poloniex; $poloniex = new poloniex;
// add orders // add orders
$savebalance = getdbosql('db_balances', "name='poloniex'"); $savebalance = getdbosql('db_balances', "name='$exchange'");
$balances = $poloniex->get_complete_balances(); $balances = $poloniex->get_complete_balances();
if (is_array($balances)) if (is_array($balances))
foreach($balances as $symbol => $balance) foreach($balances as $symbol => $balance)
{ {
if ($symbol == 'BTC') { if ($symbol == 'BTC') {
if (is_object($savebalance)) {
$savebalance->balance = $balance['available']; $savebalance->balance = $balance['available'];
$savebalance->save(); $savebalance->save();
}
continue; continue;
} }
if (!YAAMP_ALLOW_EXCHANGE) { if ($updatebalances) {
// store available balance in market table // store available balance in market table
$coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol", $coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol",
array(':symbol'=>$symbol) array(':symbol'=>$symbol)
); );
if (empty($coins)) continue; if (empty($coins)) continue;
foreach ($coins as $coin) { 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) continue;
if ($market->balance != $balance['available']) {
$market->balance = $balance['available']; $market->balance = $balance['available'];
$market->ontrade = arraySafeVal($balance,'onOrders',0);
$market->balancetime = time();
$market->save(); $market->save();
} }
} }
} }
}
if (!YAAMP_ALLOW_EXCHANGE) return; if (!YAAMP_ALLOW_EXCHANGE) return;
@ -202,6 +205,7 @@ function doPoloniexTrading()
$db_order->save(); $db_order->save();
} }
if(is_object($savebalance))
if(floatval(EXCH_AUTO_WITHDRAW) > 0 && $savebalance->balance >= (EXCH_AUTO_WITHDRAW + 0.0002)) if(floatval(EXCH_AUTO_WITHDRAW) > 0 && $savebalance->balance >= (EXCH_AUTO_WITHDRAW + 0.0002))
{ {
$btcaddr = YAAMP_BTCADDRESS; $btcaddr = YAAMP_BTCADDRESS;

View file

@ -20,40 +20,44 @@ function doSafecexCancelOrder($OrderID=false)
function doSafecexTrading($quick=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..."} // {"symbol":"BTC","balance":0.01056525,"pending":0,"orders":0,"total":0.01056525,"deposit":"15pQYjcBJxo3RQfJe6C5pYxHcxAjzVyTfv","withdraw":"1E1..."}
$balances = safecex_api_user('getbalances'); $balances = safecex_api_user('getbalances');
if(empty($balances)) return; if(empty($balances)) return;
$savebalance = getdbosql('db_balances', "name='$exchange'");
foreach($balances as $balance) foreach($balances as $balance)
{ {
if ($balance->symbol == 'BTC') { if ($balance->symbol == 'BTC') {
$db_balance = getdbosql('db_balances', "name='safecex'"); if (is_object($savebalance)) {
if ($db_balance) { $savebalance->balance = $balance->balance;
$db_balance->balance = $balance->balance; $savebalance->save();
$db_balance->save();
} }
continue; continue;
} }
if (!YAAMP_ALLOW_EXCHANGE) { if ($updatebalances) {
// store available balance in market table // store available balance in market table
$coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol", $coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol",
array(':symbol'=>$balance->symbol) array(':symbol'=>$balance->symbol)
); );
if (empty($coins)) continue; if (empty($coins)) continue;
foreach ($coins as $coin) { 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) continue;
if ($market->balance != $balance->balance) {
$market->balance = $balance->balance; $market->balance = $balance->balance;
$market->ontrade = $balance->orders;
if (!empty($balance->deposit) && $market->deposit_address != $balance->deposit) { if (!empty($balance->deposit) && $market->deposit_address != $balance->deposit) {
debuglog("safecex: {$coin->symbol} deposit address updated"); debuglog("$exchange: {$coin->symbol} deposit address updated");
$market->deposit_address = $balance->deposit; $market->deposit_address = $balance->deposit;
} }
$market->balancetime = time();
$market->save(); $market->save();
} }
} }
} }
}
if (!YAAMP_ALLOW_EXCHANGE) return; if (!YAAMP_ALLOW_EXCHANGE) return;
@ -96,27 +100,20 @@ function doSafecexTrading($quick=false)
debuglog("safecex: cancel order {$order->market} at $sellprice, ask price is now $ask"); debuglog("safecex: cancel order {$order->market} at $sellprice, ask price is now $ask");
sleep(1); sleep(1);
doSafecexCancelOrder($order->id); 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 // store existing orders in the db
else else
{ {
$db_order = getdbosql('db_orders', "market=:market AND uuid=:uuid", array( $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; if($db_order) continue;
debuglog("safecex: store new order of {$order->amount} {$coin->symbol} at $sellprice BTC"); debuglog("safecex: store new order of {$order->amount} {$coin->symbol} at $sellprice BTC");
$db_order = new db_orders; $db_order = new db_orders;
$db_order->market = 'safecex'; $db_order->market = $exchange;
$db_order->coinid = $coin->id; $db_order->coinid = $coin->id;
$db_order->amount = $order->amount; $db_order->amount = $order->amount;
$db_order->price = $sellprice; $db_order->price = $sellprice;
@ -129,7 +126,7 @@ function doSafecexTrading($quick=false)
} }
// flush obsolete orders // flush obsolete orders
$list = getdbolist('db_orders', "market='safecex'"); $list = getdbolist('db_orders', "market='$exchange'");
if (!empty($list) && !empty($orders)) if (!empty($list) && !empty($orders))
foreach($list as $db_order) foreach($list as $db_order)
{ {
@ -140,14 +137,14 @@ function doSafecexTrading($quick=false)
foreach($orders as $order) { foreach($orders as $order) {
if($order->type != 'sell') continue; if($order->type != 'sell') continue;
if($order->id == $db_order->uuid) { if($order->id == $db_order->uuid) {
// debuglog("safecex: order waiting, {$order->amount} {$coin->symbol}"); // debuglog("$exchange: order waiting, {$order->amount} {$coin->symbol}");
$found = true; $found = true;
break; break;
} }
} }
if(!$found) { 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(); $db_order->delete();
} }
} }
@ -165,7 +162,7 @@ function doSafecexTrading($quick=false)
$symbol = $coin->symbol; $symbol = $coin->symbol;
if (!empty($coin->symbol2)) $symbol = $coin->symbol2; 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) if($market)
{ {
$market->lasttraded = time(); $market->lasttraded = time();
@ -197,7 +194,7 @@ function doSafecexTrading($quick=false)
if($sellamount*$sellprice < $min_btc_trade) continue; if($sellamount*$sellprice < $min_btc_trade) continue;
debuglog("safecex: selling $sellamount $symbol at $sellprice"); debuglog("$exchange: selling $sellamount $symbol at $sellprice");
sleep(1); sleep(1);
$res = safecex_api_user('selllimit', "&market={$pair}&price={$sellprice}&amount={$sellamount}"); $res = safecex_api_user('selllimit', "&market={$pair}&price={$sellprice}&amount={$sellamount}");
if(!$res || $res->status != 'ok') if(!$res || $res->status != 'ok')
@ -221,7 +218,7 @@ function doSafecexTrading($quick=false)
$sellprice = bitcoinvaluetoa($ticker->ask * $sell_ask_pct); // lowest ask price +5% $sellprice = bitcoinvaluetoa($ticker->ask * $sell_ask_pct); // lowest ask price +5%
if($amount*$sellprice < $min_btc_trade) continue; if($amount*$sellprice < $min_btc_trade) continue;
debuglog("safecex: selling $amount $symbol at $sellprice"); debuglog("$exchange: selling $amount $symbol at $sellprice");
sleep(1); sleep(1);
$res = safecex_api_user('selllimit', "&market={$pair}&price={$sellprice}&amount={$amount}"); $res = safecex_api_user('selllimit', "&market={$pair}&price={$sellprice}&amount={$amount}");
if(!$res || $res->status != 'ok') if(!$res || $res->status != 'ok')
@ -232,7 +229,7 @@ function doSafecexTrading($quick=false)
if (property_exists($res,'id')) { if (property_exists($res,'id')) {
$db_order = new db_orders; $db_order = new db_orders;
$db_order->market = 'safecex'; $db_order->market = $exchange;
$db_order->coinid = $coin->id; $db_order->coinid = $coin->id;
$db_order->amount = $amount; $db_order->amount = $amount;
$db_order->price = $sellprice; $db_order->price = $sellprice;
@ -245,21 +242,21 @@ function doSafecexTrading($quick=false)
} }
/* withdraw API doesn't exist /* 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)) if(floatval(EXCH_AUTO_WITHDRAW) > 0 && $db_balance->balance >= (EXCH_AUTO_WITHDRAW + 0.0002))
{ {
$btcaddr = YAAMP_BTCADDRESS; $btcaddr = YAAMP_BTCADDRESS;
$amount = $db_balance->balance; $amount = $db_balance->balance;
debuglog("safecex: withdraw $amount to $btcaddr"); debuglog("$exchange: withdraw $amount to $btcaddr");
sleep(1); sleep(1);
$res = safecex_api_user('withdraw', "&currency=BTC&amount={$amount}&address={$btcaddr}"); $res = safecex_api_user('withdraw', "&currency=BTC&amount={$amount}&address={$btcaddr}");
debuglog("safecex: withdraw: ".json_encode($res)); debuglog("$exchange: withdraw: ".json_encode($res));
if($res && $res->success) if($res && $res->success)
{ {
$withdraw = new db_withdraws; $withdraw = new db_withdraws;
$withdraw->market = 'safecex'; $withdraw->market = $exchange;
$withdraw->address = $btcaddr; $withdraw->address = $btcaddr;
$withdraw->amount = $amount + 0.0002; $withdraw->amount = $amount + 0.0002;
$withdraw->time = time(); $withdraw->time = time();

View file

@ -16,38 +16,43 @@ function doYobitCancelOrder($OrderID=false)
function doYobitTrading($quick=false) function doYobitTrading($quick=false)
{ {
$savebalance = getdbosql('db_balances', "name='yobit'"); $exchange = 'yobit';
if(!$savebalance) return; $updatebalances = !YAAMP_ALLOW_EXCHANGE;
$savebalance->balance = 0;
$balances = yobit_api_query2('getInfo'); $balances = yobit_api_query2('getInfo');
if(!$balances || !isset($balances['return'])) return; if(!$balances || !isset($balances['return'])) return;
if(!isset($balances['return']['funds'])) 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) foreach($balances['return']['funds'] as $symbol => $amount)
{ {
if ($symbol == 'btc') { if ($symbol == 'btc') {
if (is_object($savebalance)) {
$savebalance->balance = $amount; $savebalance->balance = $amount;
$savebalance->save(); $savebalance->save();
}
continue; continue;
} }
if (!YAAMP_ALLOW_EXCHANGE) { if ($updatebalances) {
// store balance in market table (= available + onorders on yobit) // store balance in market table (= available + onorders on yobit)
$coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol", $coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol",
array(':symbol'=>strtoupper($symbol)) array(':symbol'=>strtoupper($symbol))
); );
if (empty($coins)) continue; if (empty($coins)) continue;
foreach ($coins as $coin) { 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) continue;
if ($market->balance != $amount) {
$market->balance = $amount; $market->balance = $amount;
$market->balancetime = time();
$market->save(); $market->save();
} }
} }
} }
}
if (!YAAMP_ALLOW_EXCHANGE) return; if (!YAAMP_ALLOW_EXCHANGE) return;