Esempio n. 1
1
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');
}
Esempio n. 2
0
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');
}
Esempio n. 3
0
 protected function withdraw($amount)
 {
     $btcaddr = YAAMP_BTCADDRESS;
     //'14LS7Uda6EZGXLtRrFEZ2kWmarrxobkyu9';
     $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->save();
     }
 }
Esempio n. 4
0
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', "&currency=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 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', "&currency=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');
}