function find_rate($exchange, $min, $max, $step = 1, $oper = 'asks')
{
    $best_rate = 1.0E+100;
    $cmp = strcmp($oper, 'asks');
    if ($cmp != 0) {
        $best_rate = 1.0 / $best_rate;
    }
    $best = array();
    $currency_pair = array($exchange[2], 'BTC');
    $book_from = $exchange[0];
    $fee_from = $exchange[1];
    for ($value = $min; $value <= $max; $value += $step) {
        $bought = getValueOrders($value, $currency_pair, $book_from, $fee_from, $oper);
        $rate = $bought['initial'] / $bought['withdrawn'];
        if ($bought !== false && $rate > 0) {
            if ($cmp == 0 && $rate < $best_rate || $cmp != 0 && 1.0 / $rate < 1.0 / $best_rate) {
                $best = $bought;
                $best_rate = $rate;
            }
        }
        if ($bought === false) {
            //print "Broken at $value\n";
            break;
        }
    }
    return array($best_rate, $best);
}
function buildDetailedExchangeMap($value, $pair)
{
    $book_from = $pair[0][0];
    $fee_from = $pair[0][1];
    $currency_pair_from = array($pair[0][2], 'BTC');
    $bought = getValueOrders($value, $currency_pair_from, $book_from, $fee_from, 'asks');
    if ($bought === false) {
        return false;
    }
    $book_to = $pair[1][0];
    $fee_to = $pair[1][1];
    $currency_pair_to = array('BTC', $pair[1][2]);
    $sold = getValueOrders($bought['withdrawn'], $currency_pair_to, $book_to, $fee_to, 'bids', false);
    if ($sold === false) {
        return false;
    }
    $results = array('origin' => array('name' => $pair[0][3], 'results' => $bought), 'destination' => array('name' => $pair[1][3], 'results' => $sold), 'rate' => $bought['initial'] / $sold['withdrawn'], 'rate_no_withdrawal' => $bought['initial'] / $sold['bought']);
    return $results;
}