function trade_price($btc, $fiat, $verbose = false) { if (gmp_cmp($btc, 0) == 0) { return ''; } if ($verbose) { return "(" . _("price") . " " . fiat_and_btc_to_price(gmp_strval($fiat), gmp_strval($btc)) . ")"; } else { return fiat_and_btc_to_price(gmp_strval($fiat), gmp_strval($btc)); } }
function get_ticker_data() { $query = "\n SELECT\n ROUND(MAX(a_amount/b_amount), " . PRICE_PRECISION . ") AS high,\n ROUND(MIN(a_amount/b_amount), " . PRICE_PRECISION . ") AS low,\n SUM(a_amount/b_amount) AS sum_of_prices,\n COUNT(*) AS number_of_prices,\n SUM(a_amount) AS sum_of_a_amounts,\n SUM(b_amount) AS sum_of_b_amounts,\n SUM(b_amount) AS vol\n FROM\n transactions\n WHERE\n b_amount > 0\n AND timest > NOW() - INTERVAL 1 DAY;\n "; $result = do_query($query); $row = get_row($result); if (isset($row['vol'])) { $sum_of_prices = $row['sum_of_prices']; $number_of_prices = $row['number_of_prices']; $sum_of_a_amounts = $row['sum_of_a_amounts']; $sum_of_b_amounts = $row['sum_of_b_amounts']; $high = $row['high']; $low = $row['low']; $avg = fiat_and_btc_to_price($sum_of_prices, $number_of_prices); $vwap = fiat_and_btc_to_price($sum_of_a_amounts, $sum_of_b_amounts); $vol = internal_to_numstr($row['vol'], BTC_PRECISION); } else { $high = $low = $avg = $vwap = $vol = 0; } $exchange_fields = calc_exchange_rate(CURRENCY, 'BTC', BASE_CURRENCY::B); if (!$exchange_fields) { $buy = 0; } else { list($total_amount, $total_want_amount, $buy) = $exchange_fields; } $exchange_fields = calc_exchange_rate('BTC', CURRENCY, BASE_CURRENCY::A); if (!$exchange_fields) { $sell = 0; } else { list($total_amount, $total_want_amount, $sell) = $exchange_fields; } $last = get_last_price(); // for testing layout when all stats have 4 decimal places // return array('21.1234', '20.1234', '21.6789', '21.4567', '1234567.3456', '21.5543', '21.2345', '22.1257'); return array($high, $low, $avg, $vwap, $vol, $last, $buy, $sell); }
function test_price($fiat, $btc) { echo "full: " . bcdiv($fiat, $btc, 8) . " rounded: " . fiat_and_btc_to_price($fiat, $btc, 'round') . "; up: " . fiat_and_btc_to_price($fiat, $btc, 'up') . "; down: " . fiat_and_btc_to_price($fiat, $btc, 'down') . "<br>\n"; }
echo "<h3>Recent Trades</h3>\n"; $query = "\n SELECT txid,\n a_amount,\n a_orderid,\n b_amount,\n b_orderid,\n " . sql_format_date("t.timest") . " AS timest,\n a.uid AS a_uid,\n b.uid AS b_uid\n FROM transactions AS t\n JOIN orderbook AS a\n ON a.orderid = a_orderid\n JOIN orderbook AS b\n ON b.orderid = b_orderid\n WHERE b_amount > 0\n AND t.timest > NOW() - INTERVAL 1 DAY\n ORDER BY txid DESC;\n"; $result = do_query($query); $first = true; $amount_fiat_total = $amount_btc_total = '0'; $mine = 0; while ($row = mysql_fetch_assoc($result)) { $txid = $row['txid']; $a_amount = $row['a_amount']; $a_orderid = $row['a_orderid']; $b_amount = $row['b_amount']; $b_orderid = $row['b_orderid']; $timest = $row['timest']; $a_uid = $row['a_uid']; $b_uid = $row['b_uid']; $price = fiat_and_btc_to_price($a_amount, $b_amount); $amount_fiat_total = gmp_add($amount_fiat_total, $a_amount); $amount_btc_total = gmp_add($amount_btc_total, $b_amount); $a_amount_str = internal_to_numstr($a_amount, FIAT_PRECISION); $b_amount_str = internal_to_numstr($b_amount, BTC_PRECISION); if (string_is_zero($a_amount_str) || string_is_zero($b_amount_str)) { continue; } if ($first) { $first = false; echo "<table class='display_data'>\n"; echo "<tr>"; echo "<th class='right'>" . _("TID") . "</th>"; if ($is_admin) { echo "<th>User</th>"; }
echo _("Trades"); ?> </th> </tr><?php do { $orderid = $row['orderid']; $amount = $row['amount']; $initial_amount = $row['initial_amount']; $type = $row['type']; $initial_want_amount = $row['initial_want_amount']; $want_type = $row['want_type']; $timest = $row['timest']; // $timest = str_replace(" ", "<br/>", $timest); $status_code = $row['status']; $status = translate_order_code($status_code); $price = $type == 'BTC' ? fiat_and_btc_to_price($initial_want_amount, $initial_amount) : fiat_and_btc_to_price($initial_amount, $initial_want_amount); $percent_complete = sprintf("%.0f", bcdiv(gmp_strval(gmp_mul(gmp_sub($initial_amount, $amount), 100)), $initial_amount, 1)); $trade_count = count_transactions($orderid); $give_precision = $type == 'BTC' ? BTC_PRECISION : FIAT_PRECISION; $want_precision = $type == 'BTC' ? FIAT_PRECISION : BTC_PRECISION; echo " ", active_table_row("active", "?page=view_order&orderid={$orderid}"), "\n"; echo " <td class='right'>" . internal_to_numstr($initial_amount, $give_precision) . " {$type}</td>\n"; echo " <td class='right'>" . internal_to_numstr($initial_want_amount, $want_precision) . " {$want_type}</td>\n"; echo " <td class='right'>{$price}</td>\n"; echo " <td>{$timest}</td>\n"; echo " <td>{$status} ({$percent_complete}%)</td>\n"; echo " <td>{$trade_count}</td>\n"; echo " </tr>\n"; } while ($row = mysql_fetch_assoc($result)); echo "</table></div>"; }
function show_mini_orderbook_table($bids) { global $buy, $sell, $is_logged_in; echo "<table class='display_data'>"; echo "<tr><th colspan=5 class='center'>" . ($bids ? sprintf(_("People Buying BTC for %s"), CURRENCY) : sprintf(_("People Selling BTC for %s"), CURRENCY)) . "</th></tr>" . "<tr><th valign='bottom' class='right'>" . sprintf(_("Price"), CURRENCY) . "</th>" . "<th colspan=2 valign='bottom' class='center'>" . _("Depth") . "</th>" . "<th colspan=2 valign='bottom' class='center'>" . _("Cumulative Depth") . "</th>" . "</tr>"; echo "<tr><th></th><th class='center'>BTC</th><th class='center'>" . CURRENCY . "</th><th class='center'>BTC</th><th class='center'>" . CURRENCY . "</th></tr>\n"; $limit = ''; if ($bids) { $price = "amount/want_amount"; $amount = "want_amount"; $want_type = "BTC"; $order = "DESC"; if ($buy) { $limit = "AND {$price} > " . $buy * (1 - ORDERBOOK_PRICE_RANGE_PERCENTAGE / 100); } } else { $price = "want_amount/amount"; $amount = "amount"; $want_type = CURRENCY; $order = "ASC"; if ($sell) { $limit = "AND {$price} < " . $sell * (1 + ORDERBOOK_PRICE_RANGE_PERCENTAGE / 100); } } $result = do_query("\n SELECT\n uid = {$is_logged_in} as me,\n orderid,\n amount,\n want_amount\n FROM\n orderbook\n WHERE\n want_type = '{$want_type}' AND\n status='OPEN'\n {$limit}\n ORDER BY\n {$price} {$order}, timest ASC\n "); $last_price = $fiat_amount_at_price = $btc_amount_at_price = $total_fiat_amount = $total_btc_amount = "0"; $mine = $mine_count = 0; while ($row = mysql_fetch_array($result)) { $have_amount = $row['amount']; $want_amount = $row['want_amount']; $orderid = $row['orderid']; if ($bids) { $btc_amount = $want_amount; $fiat_amount = $have_amount; } else { $btc_amount = $have_amount; $fiat_amount = $want_amount; } $price = fiat_and_btc_to_price($fiat_amount, $btc_amount, $bids ? 'down' : 'up'); if ($price == $last_price) { $fiat_amount_at_price = gmp_add($fiat_amount_at_price, $fiat_amount); $btc_amount_at_price = gmp_add($btc_amount_at_price, $btc_amount); } else { if ($last_price) { show_mini_orderbook_table_row($last_orderid, $want_type, $last_price, $last_have, $last_want, $fiat_amount_at_price, $btc_amount_at_price, $total_fiat_amount, $total_btc_amount, $mine); $mine = 0; } $last_price = $price; $fiat_amount_at_price = $fiat_amount; $btc_amount_at_price = $btc_amount; } $last_have = $have_amount; $last_want = $want_amount; $last_orderid = $orderid; $total_fiat_amount = gmp_add($total_fiat_amount, $fiat_amount); $total_btc_amount = gmp_add($total_btc_amount, $btc_amount); if ($row['me']) { $mine = $orderid; $mine_count++; } } if ($last_price) { show_mini_orderbook_table_row($last_orderid, $want_type, $last_price, $last_have, $last_want, $fiat_amount_at_price, $btc_amount_at_price, $total_fiat_amount, $total_btc_amount, $mine); } echo "</table>\n"; return $mine_count; }
function display_transactions($uid, $orderid) { global $is_logged_in, $is_admin; $ordselq = ''; if (!$orderid) { $sort = "DESC"; } else { $sort = "ASC"; $ordselq = " AND orderbook.orderid='{$orderid}' "; } $query = "\n SELECT\n orderbook.orderid AS orderid,\n IF(transactions.a_orderid=orderbook.orderid, 'A', 'B') AS who,\n transactions.a_amount AS a_amount,\n transactions.b_amount AS b_amount,\n transactions.a_commission AS a_commission,\n transactions.b_commission AS b_commission,\n orderbook.type AS type,\n orderbook.want_type AS want_type,\n " . sql_format_date("transactions.timest") . " AS timest\n FROM transactions\n JOIN orderbook\n ON\n transactions.a_orderid=orderbook.orderid\n OR transactions.b_orderid=orderbook.orderid\n WHERE orderbook.uid='{$uid}' {$ordselq}\n ORDER BY transactions.txid {$sort};\n "; $result = do_query($query); $first = true; $a_total = 0; $b_total = 0; $commission_total = 0; $count = 0; while ($row = mysql_fetch_assoc($result)) { $count++; $who = $row['who']; $a_amount = $row['a_amount']; $b_amount = $row['b_amount']; $a_commission = $row['a_commission']; $b_commission = $row['b_commission']; if ($who == 'B') { list($a_amount, $b_amount) = array($b_amount, $a_amount); $b_commission = $a_commission; } // skip cancelled orders since we already show those if ((int) $b_amount == -1) { continue; } if ($first) { $first = false; ?> <div class='content_box'> <h3> <?php if ($is_logged_in == $uid) { echo _("Your trades") . " "; } else { echo _("Trades") . " "; } if ($orderid) { echo _('for this order'); } ?> </h3> <table class='display_data'> <tr> <?php if (!$orderid) { ?> <th class='right'><?php echo _("Order"); ?> </th> <?php } ?> <th class='right'><?php echo _("You gave"); ?> </th> <th class='right'><?php echo _("You got"); ?> </th> <th class='right'><?php echo _("Commission"); ?> </th> <th class='right'><?php echo _("Price"); ?> </th> <th class='center'><?php echo _("Time"); ?> </th> </tr><?php } $a_total = gmp_add($a_total, $a_amount); $b_total = gmp_add($b_total, $b_amount); $commission_total = gmp_add($commission_total, $b_commission); if ($b_amount) { $commission_percent = bcdiv(bcmul($b_commission, 100), $b_amount, 3); } else { $commission_percent = 0; } $b_amount = gmp_sub($b_amount, $b_commission); $type = $row['type']; $want_type = $row['want_type']; $price = 0; if ($type == 'BTC') { if ($a_amount) { $price = fiat_and_btc_to_price($b_amount, $a_amount); } } else { if ($b_amount) { $price = fiat_and_btc_to_price($a_amount, $b_amount); } } $this_orderid = $row['orderid']; $timest = $row['timest']; $give_precision = $type == 'BTC' ? BTC_PRECISION : FIAT_PRECISION; $want_precision = $type == 'BTC' ? FIAT_PRECISION : BTC_PRECISION; if (!$orderid) { echo " ", active_table_row("active", "?page=view_order&orderid={$this_orderid}"), "\n"; } else { echo " <tr>\n"; } echo " "; if (!$orderid) { echo "<td class='right'>{$this_orderid}</td>"; } echo "<td class='right'>" . internal_to_numstr($a_amount, $give_precision) . " {$type}</td>"; echo "<td class='right'>" . internal_to_numstr($b_amount, $want_precision) . " {$want_type}</td>"; echo "<td class='right'>" . internal_to_numstr($b_commission, $want_precision) . " {$want_type} (", sprintf("%.2f", $commission_percent), "%)</td>"; echo "<td class='right'>{$price}</td>"; echo "<td class='right'>{$timest}</td>\n"; echo " </tr>\n"; } // if we showed any table at all if (!$first) { // if we need to show a summary line if ($orderid && $count > 1) { $commission_percent = bcdiv(bcmul(gmp_strval($commission_total), 100), gmp_strval($b_total), 3); $b_total = gmp_sub($b_total, $commission_total); $price = 0; if ($type == 'BTC') { if ($a_total) { $price = fiat_and_btc_to_price($b_total, $a_total); } } else { if ($b_total) { $price = fiat_and_btc_to_price($a_total, $b_total); } } $a_total = internal_to_numstr($a_total, $give_precision); $b_total = internal_to_numstr($b_total, $want_precision); $commission_total = internal_to_numstr($commission_total, $want_precision); echo " <tr>\n"; echo " <td class='right'>--------</td><td class='right'>--------</td><td class='right'>--------</td><td class='right'>--------</td>\n"; echo " <td></td>\n"; echo " </tr>\n"; echo " <tr>\n"; echo " <td class='right'>{$a_total} {$type}</td><td class='right'>{$b_total} {$want_type}</td><td class='right'>{$commission_total} {$want_type} (", sprintf("%.2f", $commission_percent), "%)</td><td class='right'>{$price}</td>\n"; echo " <td></td>\n"; echo " </tr>\n"; } echo "</table>\n"; echo "<p>" . _("The 'you got' column is the amount you received after commission was taken off.") . "</p>"; echo "<p>" . _("The 'price' column shows the effective price of the trade, after commission.") . "</p>"; echo "</div>\n"; } }