function doPoloniexTrading() { // debuglog('-------------- doPoloniexTrading()'); $flushall = rand(0, 4) == 0; $poloniex = new poloniex(); $tickers = $poloniex->get_ticker(); if (!$tickers) { return; } // upgrade orders $coins = getdbolist('db_coins', "enable and id in (select distinct coinid from markets where name='poloniex')"); foreach ($coins as $coin) { if ($coin->dontsell) { continue; } $pair = "BTC_{$coin->symbol}"; if (!isset($tickers[$pair])) { continue; } $orders = $poloniex->get_open_orders($pair); if (!$orders || !isset($orders[0])) { dborun("delete from orders where coinid={$coin->id} and market='poloniex'"); continue; } foreach ($orders as $order) { if (!isset($order['orderNumber'])) { debuglog($order); continue; } if ($order['rate'] > $tickers[$pair]['lowestAsk'] + 5.0E-8 || $flushall) { // debuglog("poloniex cancel order for $pair {$order['orderNumber']}"); $poloniex->cancel_order($pair, $order['orderNumber']); $db_order = getdbosql('db_orders', "uuid=:uuid", array(':uuid' => $order['orderNumber'])); if ($db_order) { $db_order->delete(); } sleep(1); } else { $db_order = getdbosql('db_orders', "uuid=:uuid", array(':uuid' => $order['orderNumber'])); if ($db_order) { continue; } debuglog("poloniex adding order {$coin->symbol}"); $db_order = new db_orders(); $db_order->market = 'poloniex'; $db_order->coinid = $coin->id; $db_order->amount = $order['amount']; $db_order->price = $order['rate']; $db_order->ask = $tickers[$pair]['lowestAsk']; $db_order->bid = $tickers[$pair]['highestBid']; $db_order->uuid = $order['orderNumber']; $db_order->created = time(); $db_order->save(); } } $list = getdbolist('db_orders', "coinid={$coin->id} and market='poloniex'"); foreach ($list as $db_order) { $found = false; foreach ($orders as $order) { if (!isset($order['orderNumber'])) { debuglog($order); continue; } if ($order['orderNumber'] == $db_order->uuid) { $found = true; break; } } if (!$found) { debuglog("poloniex deleting order {$coin->name} {$db_order->amount}"); $db_order->delete(); } } } // add orders $savebalance = getdbosql('db_balances', "name='poloniex'"); $balances = $poloniex->get_balances(); foreach ($balances as $symbol => $balance) { if (!$balance) { continue; } if ($symbol == 'BTC') { $savebalance->balance = $balance; $savebalance->save(); continue; } $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol' => $symbol)); if (!$coin || $coin->dontsell) { continue; } $market = getdbosql('db_markets', "coinid={$coin->id} and name='poloniex'"); if ($market) { $market->lasttraded = time(); $market->save(); } $pair = "BTC_{$symbol}"; if (!isset($tickers[$pair])) { continue; } $sellprice = $tickers[$pair]['highestBid']; if ($balance * $sellprice < 0.0001) { continue; } // debuglog("poloniex selling $pair, $sellprice, $balance"); $res = $poloniex->sell($pair, $sellprice, $balance); if (!isset($res['orderNumber'])) { debuglog($res, 5); continue; } if (!isset($tickers[$pair])) { continue; } $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol' => $symbol)); if (!$coin) { continue; } $db_order = new db_orders(); $db_order->market = 'poloniex'; $db_order->coinid = $coin->id; $db_order->amount = $balance; $db_order->price = $sellprice; $db_order->ask = $tickers[$pair]['lowestAsk']; $db_order->bid = $tickers[$pair]['highestBid']; $db_order->uuid = $res['orderNumber']; $db_order->created = time(); $db_order->save(); } if ($savebalance->balance >= 0.2) { $btcaddr = YAAMP_BTCADDRESS; //'14LS7Uda6EZGXLtRrFEZ2kWmarrxobkyu9'; $amount = $savebalance->balance; // - 0.0002; debuglog("poloniex withdraw {$amount} to {$btcaddr}"); sleep(1); $res = $poloniex->withdraw('BTC', $amount, $btcaddr); debuglog($res); if ($res && $res->success) { $withdraw = new db_withdraws(); $withdraw->market = 'poloniex'; $withdraw->address = $btcaddr; $withdraw->amount = $amount; $withdraw->time = time(); // $withdraw->uuid = $res->result->uuid; $withdraw->save(); } } // debuglog('-------------- doPoloniexTrading() done'); }
protected function sell($amount, $price) { $res = cryptsy_api_query('createorder', array('marketid' => $this->marketid, 'ordertype' => 'Sell', 'quantity' => $amount, 'price' => $price)); if (!$res || !isset($res['orderid'])) { return; } $db_order = new db_orders(); $db_order->market = 'cryptsy'; $db_order->coinid = $this->coin->id; $db_order->amount = $balance; $db_order->price = $sellprice; $db_order->ask = $price; $db_order->bid = $price; $db_order->uuid = $res['orderid']; $db_order->created = time(); $db_order->save(); }
function doCryptsyTrading($quick = false) { $flushall = rand(0, 4) == 0; if ($quick) { $flushall = false; } // debuglog("-------------- doCryptsyTrading() $flushall"); $orders = cryptsy_api_query('allmyorders'); if (!$orders) { return; } foreach ($orders['return'] as $order) { if (!isset($order['marketid'])) { continue; } if (!isset($order['orderid'])) { continue; } $market = getdbosql('db_markets', "marketid=:marketid", array(':marketid' => $order['marketid'])); if (!$market) { continue; } $coin = getdbo('db_coins', $market->coinid); if (!$coin) { continue; } $symbol = $coin->symbol; $ticker = getCryptsyTicker($market->marketid); if (!$ticker || !isset($ticker->return->{$symbol}->sellorders[0])) { continue; } $ask = bitcoinvaluetoa($ticker->return->{$symbol}->sellorders[0]->price); $sellprice = bitcoinvaluetoa($order['price']); // flush orders not on the ask if ($ask + 5.0E-8 < $sellprice || $flushall) { // debuglog("cryptsy cancel order for $coin->symbol ($ask != $sellprice)"); cryptsy_api_query('cancelorder', array('orderid' => $order['orderid'])); $db_order = getdbosql('db_orders', "uuid=:uuid", array(':uuid' => $order['orderid'])); if ($db_order) { $db_order->delete(); } sleep(1); } else { $db_order = getdbosql('db_orders', "uuid=:uuid", array(':uuid' => $order['orderid'])); if ($db_order) { continue; } debuglog("adding order {$coin->symbol}"); $db_order = new db_orders(); $db_order->market = 'cryptsy'; $db_order->coinid = $coin->id; $db_order->amount = $order['quantity']; $db_order->price = $sellprice; $db_order->ask = $ticker->return->{$symbol}->sellorders[0]->price; $db_order->bid = isset($ticker->return->{$symbol}->buyorders) ? $ticker->return->{$symbol}->buyorders[0]->price : 0; $db_order->uuid = $order['orderid']; $db_order->created = time(); $db_order->save(); } } $list = getdbolist('db_orders', "market='cryptsy'"); foreach ($list as $db_order) { $coin = getdbo('db_coins', $db_order->coinid); $found = false; foreach ($orders['return'] as $order) { if (!isset($order['orderid'])) { continue; } if ($order['orderid'] == $db_order->uuid) { $found = true; break; } } if (!$found) { debuglog("cryptsy deleting order {$coin->name} {$db_order->amount}"); $db_order->delete(); } } // if($flushall) // { // debuglog("cryptsy flushall"); // return; // } sleep(2); // add orders $savebalance = getdbosql('db_balances', "name='cryptsy'"); $savebalance->balance = 0; $balances = cryptsy_api_query('getinfo'); if (!$balances) { return; } if (!isset($balances['return'])) { debuglog($balances); return; } foreach ($balances['return']['balances_available'] as $symbol => $balance) { if ($symbol == 'Points') { continue; } if ($symbol == 'BTC') { $savebalance->balance = floatval($balance); continue; } $balance = floatval($balance); if (!$balance) { continue; } $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol' => $symbol)); if (!$coin || $coin->dontsell) { continue; } $market = getdbosql('db_markets', "coinid={$coin->id} and name='cryptsy'"); if (!$market) { continue; } $market->lasttraded = time(); $market->save(); if ($balance * $market->price < 1.0E-5) { continue; } $ticker = getCryptsyTicker($market->marketid); if (!$ticker || !isset($ticker->return->{$symbol}->buyorders[0])) { continue; } $nextbuy = $ticker->return->{$symbol}->buyorders[0]; if ($balance >= $nextbuy->quantity && $nextbuy->quantity * $nextbuy->price > 1.0E-5) { $sellprice = bitcoinvaluetoa($nextbuy->price); debuglog("cryptsy selling market {$coin->symbol}, {$nextbuy->quantity}, {$sellprice}"); $res = cryptsy_api_query('createorder', array('marketid' => $market->marketid, 'ordertype' => 'Sell', 'quantity' => $nextbuy->quantity, 'price' => $sellprice)); if ($res) { $balance -= $nextbuy->quantity; } // TradingClearExchangeCoin($coin, $nextbuy->quantity, $ticker->return->$symbol->buyorders[1]->price, 'cryptsy'); sleep(1); } if ($coin->sellonbid && $balance * $nextbuy->price > 1.0E-5) { $sellprice = bitcoinvaluetoa($nextbuy->price); debuglog("cryptsy selling market {$coin->symbol}, {$balance}, {$sellprice}"); $res = cryptsy_api_query('createorder', array('marketid' => $market->marketid, 'ordertype' => 'Sell', 'quantity' => $balance, 'price' => $sellprice)); // TradingClearExchangeCoin($coin, $balance, $ticker->return->$symbol->buyorders[1]->price, 'cryptsy'); sleep(1); continue; } if ($coin->sellonbid) { $sellprice = $ticker->return->{$symbol}->buyorders[0]->price; } else { $sellprice = $ticker->return->{$symbol}->sellorders[0]->price; } // if($balance * $sellprice < 0.0001) continue; // debuglog("cryptsy selling $coin->symbol, $sellprice, $balance"); $res = cryptsy_api_query('createorder', array('marketid' => $market->marketid, 'ordertype' => 'Sell', 'quantity' => $balance, 'price' => $sellprice)); if (!$res || !isset($res['orderid'])) { continue; } $db_order = new db_orders(); $db_order->market = 'cryptsy'; $db_order->coinid = $coin->id; $db_order->amount = $balance; $db_order->price = $sellprice; $db_order->ask = $ticker->return->{$symbol}->sellorders[0]->price; $db_order->bid = $ticker->return->{$symbol}->buyorders[0]->price; $db_order->uuid = $res['orderid']; $db_order->created = time(); $db_order->save(); } if ($savebalance->balance >= 0.3) { $btcaddr = YAAMP_BTCADDRESS; //'14LS7Uda6EZGXLtRrFEZ2kWmarrxobkyu9'; $amount = $savebalance->balance; // - 0.001; debuglog("cryptsy withdraw {$amount} to {$btcaddr}"); sleep(1); $res = cryptsy_api_query('makewithdrawal', array('address' => $btcaddr, 'amount' => $amount)); debuglog($res); if ($res && $res['success']) { $withdraw = new db_withdraws(); $withdraw->market = 'cryptsy'; $withdraw->address = $btcaddr; $withdraw->amount = $amount; $withdraw->time = time(); // $withdraw->uuid = $res->result->uuid; $withdraw->save(); // $savebalance->balance = 0; } } $savebalance->save(); // debuglog('-------------- doCryptsyTrading() done'); }
function doYobitTrading($quick = false) { $flushall = rand(0, 4) == 0; if ($quick) { $flushall = false; } $coins = getdbolist('db_coins', "installed and id in (select distinct coinid from markets where name='yobit')"); foreach ($coins as $coin) { if ($coin->dontsell) { continue; } $pair = strtolower("{$coin->symbol}_btc"); $orders = yobit_api_query2('ActiveOrders', array('pair' => $pair)); if (isset($orders['return'])) { foreach ($orders['return'] as $uuid => $order) { $ticker = yobit_api_query("ticker/{$pair}"); if (!$ticker) { continue; } if ($order['rate'] > $ticker->{$pair}->sell + 5.0E-8 || $flushall) { // debuglog("yobit cancel order for $pair $uuid"); $res = yobit_api_query2('CancelOrder', array('order_id' => $uuid)); $db_order = getdbosql('db_orders', "uuid=:uuid", array(':uuid' => $uuid)); if ($db_order) { $db_order->delete(); } sleep(1); } else { $db_order = getdbosql('db_orders', "uuid=:uuid", array(':uuid' => $uuid)); if ($db_order) { continue; } debuglog("yobit adding order {$coin->symbol}"); $db_order = new db_orders(); $db_order->market = 'yobit'; $db_order->coinid = $coin->id; $db_order->amount = $order['amount']; $db_order->price = $order['rate']; $db_order->ask = $ticker->{$pair}->sell; $db_order->bid = $ticker->{$pair}->buy; $db_order->uuid = $uuid; $db_order->created = time(); $db_order->save(); } } } $list = getdbolist('db_orders', "coinid={$coin->id} and market='yobit'"); foreach ($list as $db_order) { $found = false; if (isset($orders['return'])) { foreach ($orders['return'] as $uuid => $order) { if ($uuid == $db_order->uuid) { $found = true; break; } } } if (!$found) { debuglog("yobit deleting order {$coin->name} {$db_order->amount}"); $db_order->delete(); } } } sleep(2); ////////////////////////////////////////////////////////////////////////////////////////////////// $savebalance = getdbosql('db_balances', "name='yobit'"); if (!$savebalance) { return; } $savebalance->balance = 0; $balances = yobit_api_query2('getInfo'); if (!$balances || !isset($balances['return'])) { return; } foreach ($balances['return']['funds'] as $symbol => $amount) { // debuglog("$symbol, $amount"); $amount -= 0.0001; if ($amount <= 0) { continue; } if ($symbol == 'btc') { $savebalance->balance = $amount; continue; } $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol' => $symbol)); if (!$coin || $coin->dontsell) { continue; } $market = getdbosql('db_markets', "coinid={$coin->id} and name='yobit'"); if ($market) { $market->lasttraded = time(); $market->save(); } if ($amount * $coin->price < 1.0E-5) { continue; } $pair = "{$symbol}_btc"; $data = yobit_api_query("depth/{$pair}?limit=11"); if (!$data) { continue; } $sold_amount = 0; for ($i = 0; $i < 10 && $amount >= 0; $i++) { if (!isset($data->{$pair}->bids[$i])) { break; } $nextbuy = $data->{$pair}->bids[$i]; if ($amount * 1.1 < $nextbuy[1]) { break; } $sellprice = bitcoinvaluetoa($nextbuy[0]); $sellamount = min($amount, $nextbuy[1]); if ($sellamount * $sellprice < 0.0001) { continue; } debuglog("yobit selling market {$pair}, {$sellamount}, {$sellprice}"); $res = yobit_api_query2('Trade', array('pair' => $pair, 'type' => 'sell', 'rate' => $sellprice, 'amount' => $sellamount)); if (!$res || !$res['success']) { debuglog($res); break; } $amount -= $sellamount; $sold_amount += $sellamount; // sleep(1); } $ticker = yobit_api_query("ticker/{$pair}"); if (!$ticker) { continue; } // if(!$coin->sellonbid && $sold_amount*$coin->price > 0.002) // { // sleep(5); // $buyprice = bitcoinvaluetoa($ticker->$pair->sell); // $buyamount = bitcoinvaluetoa(0.00011/$ticker->$pair->sell); // debuglog("yobit buyback $pair, $buyamount, $buyprice"); // $res = yobit_api_query2('Trade', array('pair'=>$pair, 'type'=>'buy', 'rate'=>$buyprice, 'amount'=>$buyamount)); // sleep(5); // } if ($amount <= 0) { continue; } if ($coin->sellonbid) { $sellprice = bitcoinvaluetoa($ticker->{$pair}->buy); } else { $sellprice = bitcoinvaluetoa($ticker->{$pair}->sell); } if ($amount * $sellprice < 0.0001) { continue; } // debuglog("yobit selling $pair, $amount, $sellprice"); $res = yobit_api_query2('Trade', array('pair' => $pair, 'type' => 'sell', 'rate' => $sellprice, 'amount' => $amount)); if (!$res || !$res['success']) { debuglog($res); continue; } $db_order = new db_orders(); $db_order->market = 'yobit'; $db_order->coinid = $coin->id; $db_order->amount = $amount; $db_order->price = $sellprice; $db_order->ask = $ticker->{$pair}->sell; $db_order->bid = $ticker->{$pair}->buy; $db_order->uuid = $res['return']['order_id']; $db_order->created = time(); $db_order->save(); sleep(1); } $savebalance->save(); }
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 doCCexTrading($quick = false) { $flushall = rand(0, 4) == 0; if ($quick) { $flushall = false; } // debuglog("-------------- doCCexTrading() $flushall"); $ccex = new CcexAPI(); // upgrade orders $coins = getdbolist('db_coins', "enable and id in (select distinct coinid from markets where name='c-cex')"); foreach ($coins as $coin) { if ($coin->dontsell) { continue; } $market2 = getdbosql('db_markets', "coinid={$coin->id} and (name='bittrex' or name='cryptsy')"); if ($market2) { continue; } $pair = strtolower($coin->symbol) . '-btc'; // debuglog("c-cex list order for $pair"); sleep(5); $orders = $ccex->getOrders($pair, 1); if (!$orders || isset($orders['error'])) { continue; } foreach ($orders['return'] as $uuid => $order) { $ticker = $ccex->getTickerInfo($pair); if (!$ticker) { continue; } if ($order['price'] > $ticker['sell'] + 5.0E-8 || $flushall) { // debuglog("c-cex cancel order for $pair $uuid"); sleep(5); $ccex->cancelOrder($uuid); $db_order = getdbosql('db_orders', "uuid=:uuid", array(':uuid' => $uuid)); if ($db_order) { $db_order->delete(); } sleep(1); } else { $db_order = getdbosql('db_orders', "uuid=:uuid", array(':uuid' => $uuid)); if ($db_order) { continue; } debuglog("c-cex adding order {$coin->symbol}"); $db_order = new db_orders(); $db_order->market = 'c-cex'; $db_order->coinid = $coin->id; $db_order->amount = $order['amount']; $db_order->price = $order['price']; $db_order->ask = $ticker['sell']; $db_order->bid = $ticker['buy']; $db_order->uuid = $uuid; $db_order->created = time(); $db_order->save(); } } $list = getdbolist('db_orders', "coinid={$coin->id} and market='c-cex'"); foreach ($list as $db_order) { $found = false; foreach ($orders['return'] as $uuid => $order) { if ($uuid == $db_order->uuid) { $found = true; break; } } if (!$found) { debuglog("c-cex deleting order {$coin->name} {$db_order->amount}"); $db_order->delete(); } } } sleep(2); ////////////////////////////////////////////////////////////////////////////////////////////////// $savebalance = getdbosql('db_balances', "name='c-cex'"); $savebalance->balance = 0; // debuglog("c-cex getbalance"); sleep(5); $balances = $ccex->getBalance(); if (!$balances || !isset($balances['return'])) { return; } foreach ($balances['return'] as $balance) { foreach ($balance as $symbol => $amount) { if (!$amount) { continue; } if ($symbol == 'btc') { $savebalance->balance = $amount; continue; } $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol' => $symbol)); if (!$coin || $coin->dontsell) { continue; } $market = getdbosql('db_markets', "coinid={$coin->id} and name='c-cex'"); if ($market) { $market->lasttraded = time(); $market->save(); } if ($amount * $coin->price < 1.0E-5) { continue; } $pair = "{$symbol}-btc"; //////////////////////// $maxprice = 0; $maxamount = 0; // debuglog("c-cex list order for $pair all"); sleep(5); $orders = $ccex->getOrders($pair, 0); foreach ($orders['return'] as $order) { if ($order['type'] == 'sell') { continue; } if ($order['price'] > $maxprice) { $maxprice = $order['price']; $maxamount = $order['amount']; } } // debuglog("maxbuy for $pair $maxamount $maxprice"); if ($amount >= $maxamount && $maxamount * $maxprice > 1.0E-5) { $sellprice = bitcoinvaluetoa($maxprice); debuglog("c-cex selling market {$pair}, {$maxamount}, {$sellprice}"); sleep(5); $res = $ccex->makeOrder('sell', $pair, $maxamount, $sellprice); if (!$res || !isset($res['return'])) { debuglog($res); } else { $amount -= $maxamount; } sleep(1); } /// $ticker = $ccex->getTickerInfo($pair); if (!$ticker) { continue; } $sellprice = bitcoinvaluetoa($ticker['sell']); // debuglog("c-cex selling $pair, $amount, $sellprice"); sleep(5); $res = $ccex->makeOrder('sell', $pair, $amount, $sellprice); if (!$res || !isset($res['return'])) { continue; } $db_order = new db_orders(); $db_order->market = 'c-cex'; $db_order->coinid = $coin->id; $db_order->amount = $amount; $db_order->price = $sellprice; $db_order->ask = $ticker['sell']; $db_order->bid = $ticker['buy']; $db_order->uuid = $res['return']; $db_order->created = time(); $db_order->save(); } } $savebalance->save(); // debuglog('-------------- doCCexTrading() done'); }
function doBleutradeTrading($quick = false) { $flushall = rand(0, 4) == 0; if ($quick) { $flushall = false; } // debuglog("-------------- dobleutradeTrading() flushall $flushall"); $orders = bleutrade_api_query('market/getopenorders'); if (!$orders) { return; } foreach ($orders->result as $order) { $e = explode('_', $order->Exchange); $symbol = $e[0]; /// "Exchange" : "LTC_BTC", $pair = $order->Exchange; $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol' => $symbol)); if (!$coin) { continue; } if ($coin->dontsell) { continue; } $ticker = bleutrade_api_query('public/getticker', "&market={$pair}"); if (!$ticker || !$ticker->success || !isset($ticker->result[0])) { continue; } $ask = bitcoinvaluetoa($ticker->result[0]->Ask); $sellprice = bitcoinvaluetoa($order->Price); // flush orders not on the ask if ($ask + 5.0E-8 < $sellprice || $flushall) { // debuglog("bleutrade cancel order $order->Exchange $sellprice -> $ask"); bleutrade_api_query('market/cancel', "&orderid={$order->OrderId}"); $db_order = getdbosql('db_orders', "uuid=:uuid", array(':uuid' => $order->OrderId)); if ($db_order) { $db_order->delete(); } sleep(1); } else { $db_order = getdbosql('db_orders', "uuid=:uuid", array(':uuid' => $order->OrderId)); if ($db_order) { continue; } debuglog("bleutrade adding order {$coin->symbol}"); // $ticker = bleutrade_api_query('public/getticker', "&market=$pair"); // $sellprice = bitcoinvaluetoa($ticker->result->Ask); $db_order = new db_orders(); $db_order->market = 'bleutrade'; $db_order->coinid = $coin->id; $db_order->amount = $order->Quantity; $db_order->price = $sellprice; $db_order->ask = $ticker->result[0]->Ask; $db_order->bid = $ticker->result[0]->Bid; $db_order->uuid = $order->OrderId; $db_order->created = time(); $db_order->save(); } } // flush obsolete orders $list = getdbolist('db_orders', "market='bleutrade'"); foreach ($list as $db_order) { $coin = getdbo('db_coins', $db_order->coinid); if (!$coin) { continue; } $found = false; foreach ($orders->result as $order) { if ($order->OrderId == $db_order->uuid) { $found = true; break; } } if (!$found) { debuglog("bleutrade deleting order {$coin->name} {$db_order->amount}"); $db_order->delete(); } } // if($flushall) // { // debuglog("bleutrade flushall got here"); // return; // } sleep(2); // add orders $balances = bleutrade_api_query('account/getbalances'); // debuglog($balances); if (!$balances || !isset($balances->result) || !$balances->success) { return; } $savebalance = getdbosql('db_balances', "name='bleutrade'"); $savebalance->balance = 0; foreach ($balances->result as $balance) { if ($balance->Currency == 'BTC') { $savebalance->balance = $balance->Available; continue; } $amount = floatval($balance->Available); if (!$amount) { continue; } $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol' => $balance->Currency)); if (!$coin || $coin->dontsell) { continue; } $market = getdbosql('db_markets', "coinid={$coin->id} and name='bleutrade'"); if ($market) { $market->lasttraded = time(); $market->save(); } if ($amount * $coin->price < 1.0E-5) { continue; } $pair = "{$balance->Currency}_BTC"; $data = bleutrade_api_query('public/getorderbook', "&market={$pair}&type=BUY&depth=10"); if (!$data) { continue; } // if(!isset($data->result[0])) continue; for ($i = 0; $i < 5; $i++) { if (!isset($data->result->buy[$i])) { break; } $nextbuy = $data->result->buy[$i]; if ($amount < $nextbuy->Quantity || $nextbuy->Quantity * $nextbuy->Rate < 1.0E-5) { break; } $sellprice = bitcoinvaluetoa($nextbuy->Rate); // debuglog("bleutrade selling market $pair, $nextbuy->Quantity, $sellprice"); $res = bleutrade_api_query('market/selllimit', "&market={$pair}&quantity={$nextbuy->Quantity}&rate={$sellprice}"); if (!$res->success) { debuglog($res); break; } $amount -= $nextbuy->Quantity; } $ticker = bleutrade_api_query('public/getticker', "&market={$pair}"); if (!$ticker || !$ticker->success || !isset($ticker->result[0])) { continue; } if ($coin->sellonbid) { $sellprice = bitcoinvaluetoa($ticker->result[0]->Bid); } else { $sellprice = bitcoinvaluetoa($ticker->result[0]->Ask); } if ($amount * $sellprice < 0.0005) { continue; } debuglog("bleutrade selling {$pair}, {$amount}, {$sellprice}"); $res = bleutrade_api_query('market/selllimit', "&market={$pair}&quantity={$amount}&rate={$sellprice}"); if (!$res || !$res->success || !isset($res->result)) { debuglog($res); continue; } $db_order = new db_orders(); $db_order->market = 'bleutrade'; $db_order->coinid = $coin->id; $db_order->amount = $amount; $db_order->price = $sellprice; $db_order->ask = $ticker->result[0]->Ask; $db_order->bid = $ticker->result[0]->Bid; $db_order->uuid = $res->result->orderid; $db_order->created = time(); $db_order->save(); sleep(1); } if ($savebalance->balance >= 0.2) { $btcaddr = YAAMP_BTCADDRESS; //'14LS7Uda6EZGXLtRrFEZ2kWmarrxobkyu9'; $amount = $savebalance->balance; // - 0.0002; debuglog("bleutrade withdraw {$amount} to {$btcaddr}"); sleep(1); $res = bleutrade_api_query('account/withdraw', "¤cy=BTC&quantity={$amount}&address={$btcaddr}"); debuglog($res); if ($res && $res->success) { $withdraw = new db_withdraws(); $withdraw->market = 'bleutrade'; $withdraw->address = $btcaddr; $withdraw->amount = $amount; $withdraw->time = time(); $withdraw->uuid = $res->result->orderid; $withdraw->save(); // $savebalance->balance = 0; } } $savebalance->save(); // debuglog('-------------- dobleutradeTrading() done'); }