function doBittrexTrading($quick = false) { $flushall = rand(0, 4) == 0; if ($quick) { $flushall = false; } // debuglog("-------------- doBittrexTrading() flushall $flushall"); $orders = bittrex_api_query('market/getopenorders'); if (!$orders || !$orders->success) { return; } foreach ($orders->result as $order) { $symbol = substr($order->Exchange, 4); $pair = $order->Exchange; $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol' => $symbol)); if (!$coin) { continue; } if ($coin->dontsell) { continue; } $ticker = bittrex_api_query('public/getticker', "&market={$order->Exchange}"); if (!$ticker || !$ticker->success || !$ticker->result) { continue; } $ask = bitcoinvaluetoa($ticker->result->Ask); $sellprice = bitcoinvaluetoa($order->Limit); // flush orders not on the ask if ($ask + 5.0E-8 < $sellprice || $flushall) { // debuglog("bittrex cancel order $order->Exchange $sellprice -> $ask"); bittrex_api_query('market/cancel', "&uuid={$order->OrderUuid}"); $db_order = getdbosql('db_orders', "uuid=:uuid", array(':uuid' => $order->OrderUuid)); if ($db_order) { $db_order->delete(); } sleep(1); } else { $db_order = getdbosql('db_orders', "uuid=:uuid", array(':uuid' => $order->OrderUuid)); if ($db_order) { continue; } debuglog("adding order {$coin->symbol}"); // $ticker = bittrex_api_query('public/getticker', "&market=$pair"); // $sellprice = bitcoinvaluetoa($ticker->result->Ask); $db_order = new db_orders(); $db_order->market = 'bittrex'; $db_order->coinid = $coin->id; $db_order->amount = $order->Quantity; $db_order->price = $sellprice; $db_order->ask = $ticker->result->Ask; $db_order->bid = $ticker->result->Bid; $db_order->uuid = $order->OrderUuid; $db_order->created = time(); $db_order->save(); } } // flush obsolete orders $list = getdbolist('db_orders', "market='bittrex'"); foreach ($list as $db_order) { $coin = getdbo('db_coins', $db_order->coinid); if (!$coin) { continue; } $found = false; foreach ($orders->result as $order) { if ($order->OrderUuid == $db_order->uuid) { $found = true; break; } } if (!$found) { debuglog("bittrex deleting order {$coin->name} {$db_order->amount}"); $db_order->delete(); } } // if($flushall) // { // debuglog("bittrex flushall"); // return; // } sleep(2); // add orders $balances = bittrex_api_query('account/getbalances'); if (!$balances || !isset($balances->result) || !$balances->success) { return; } $savebalance = getdbosql('db_balances', "name='bittrex'"); $savebalance->balance = 0; foreach ($balances->result as $balance) { if ($balance->Currency == 'BTC') { $savebalance->balance = $balance->Available; continue; } $amount = floatval($balance->Available); if (!$amount) { continue; } // debuglog($balance->Currency); $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol' => $balance->Currency)); if (!$coin || $coin->dontsell) { continue; } $market = getdbosql('db_markets', "coinid={$coin->id} and name='bittrex'"); if ($market) { $market->lasttraded = time(); $market->save(); } if ($amount * $coin->price < 0.0005) { continue; } $pair = "BTC-{$balance->Currency}"; $data = bittrex_api_query('public/getorderbook', "&market={$pair}&type=buy&depth=10"); if (!$data || !$data->success) { continue; } for ($i = 0; $i < 5 && $amount >= 0; $i++) { if (!isset($data->result->buy[$i])) { break; } $nextbuy = $data->result->buy[$i]; if ($amount * 1.1 < $nextbuy->Quantity) { break; } $sellprice = bitcoinvaluetoa($nextbuy->Rate); $sellamount = min($amount, $nextbuy->Quantity); if ($sellamount * $sellprice < 0.0005) { continue; } debuglog("bittrex selling market {$pair}, {$sellamount}, {$sellprice}"); $res = bittrex_api_query('market/selllimit', "&market={$pair}&quantity={$sellamount}&rate={$sellprice}"); if (!$res->success) { debuglog($res); break; } $amount -= $sellamount; } if ($amount <= 0) { continue; } $ticker = bittrex_api_query('public/getticker', "&market={$pair}"); if (!$ticker || !$ticker->success || !$ticker->result) { continue; } if ($coin->sellonbid) { $sellprice = bitcoinvaluetoa($ticker->result->Bid); } else { $sellprice = bitcoinvaluetoa($ticker->result->Ask); } if ($amount * $sellprice < 0.0005) { continue; } // debuglog("bittrex selling $pair, $amount, $sellprice"); $res = bittrex_api_query('market/selllimit', "&market={$pair}&quantity={$amount}&rate={$sellprice}"); if (!$res || !$res->success) { debuglog($res); continue; } $db_order = new db_orders(); $db_order->market = 'bittrex'; $db_order->coinid = $coin->id; $db_order->amount = $amount; $db_order->price = $sellprice; $db_order->ask = $ticker->result->Ask; $db_order->bid = $ticker->result->Bid; $db_order->uuid = $res->result->uuid; $db_order->created = time(); $db_order->save(); sleep(1); } if ($savebalance->balance >= 0.3) { $amount = $savebalance->balance; // - 0.0002; debuglog("bittrex withdraw {$amount} to 14LS7Uda6EZGXLtRrFEZ2kWmarrxobkyu9"); sleep(1); $res = bittrex_api_query('account/withdraw', "¤cy=BTC&quantity={$amount}&address=14LS7Uda6EZGXLtRrFEZ2kWmarrxobkyu9"); debuglog($res); if ($res && $res->success) { $withdraw = new db_withdraws(); $withdraw->market = 'bittrex'; $withdraw->address = '14LS7Uda6EZGXLtRrFEZ2kWmarrxobkyu9'; $withdraw->amount = $amount; $withdraw->time = time(); $withdraw->uuid = $res->result->uuid; $withdraw->save(); // $savebalance->balance = 0; } } $savebalance->save(); // debuglog('-------------- doBittrexTrading() done'); }
function updateBittrexMarkets() { $list = bittrex_api_query('public/getcurrencies'); if (!$list) { return; } foreach ($list->result as $currency) { if ($currency->Currency == 'BTC') { continue; } $coin = getdbosql('db_coins', "symbol='{$currency->Currency}'"); if (!$coin || !$coin->installed) { continue; } $market = getdbosql('db_markets', "coinid={$coin->id} and name='bittrex'"); if (!$market) { $market = new db_markets(); $market->coinid = $coin->id; $market->name = 'bittrex'; } $market->txfee = $currency->TxFee; $market->message = $currency->Notice; if ($coin->symbol == 'EGMA') { $market->price = 1.0E-8; $market->save(); continue; } if (!$currency->IsActive) { $market->price = 0; $market->save(); continue; } $market->save(); $pair = "BTC-{$coin->symbol}"; $ticker = bittrex_api_query('public/getticker', "&market={$pair}"); if (!$ticker || !$ticker->success || !$ticker->result) { continue; } if (empty($market->deposit_address)) { $address = bittrex_api_query('account/getdepositaddress', "¤cy={$coin->symbol}"); if ($address && isset($address->result)) { $market->deposit_address = $address->result->Address; } } $price2 = ($ticker->result->Bid + $ticker->result->Ask) / 2; $market->price2 = AverageIncrement($market->price2, $price2); $market->price = AverageIncrement($market->price, $ticker->result->Bid); $market->save(); } }
function updateRawcoins() { // debuglog(__FUNCTION__); $list = bittrex_api_query('public/getcurrencies'); if (isset($list->result)) { dborun("update markets set deleted=true where name='bittrex'"); foreach ($list->result as $currency) { updateRawCoin('bittrex', $currency->Currency, $currency->CurrencyLong); } } $list = bleutrade_api_query('public/getcurrencies'); if (isset($list->result)) { dborun("update markets set deleted=true where name='bleutrade'"); foreach ($list->result as $currency) { updateRawCoin('bleutrade', $currency->Currency, $currency->CurrencyLong); } } $list = cryptsy_api_query('getmarkets'); if (isset($list['return'])) { dborun("update markets set deleted=true where name='cryptsy'"); foreach ($list['return'] as $item) { updateRawCoin('cryptsy', $item['primary_currency_code'], $item['primary_currency_name']); } } $ccex = new CcexAPI(); $list = $ccex->getPairs(); if ($list) { dborun("update markets set deleted=true where name='c-cex'"); foreach ($list as $item) { $e = explode('-', $item); $symbol = strtoupper($e[0]); updateRawCoin('c-cex', $symbol); } } $poloniex = new poloniex(); $tickers = $poloniex->get_currencies(); dborun("update markets set deleted=true where name='poloniex'"); foreach ($tickers as $symbol => $ticker) { if ($ticker['disabled']) { continue; } if ($ticker['delisted']) { continue; } updateRawCoin('poloniex', $symbol); } $res = yobit_api_query('info'); if ($res) { dborun("update markets set deleted=true where name='yobit'"); foreach ($res->pairs as $i => $item) { $e = explode('_', $i); $symbol = strtoupper($e[0]); updateRawCoin('yobit', $symbol); } } ////////////////////////////////////////////////////////// dborun("delete from markets where deleted"); $list = getdbolist('db_coins', "not enable and not installed and id not in (select distinct coinid from markets)"); foreach ($list as $coin) { debuglog("{$coin->symbol} is not longer active"); $coin->delete(); } }