if ("BUY" == $_POST['type']) { // Set array of file names for buy databases $fNames = array($_SERVER['DOCUMENT_ROOT'] . "/data/buy_posted.dat", $_SERVER['DOCUMENT_ROOT'] . "/data/buy.dat"); $prefix = "B"; } else { $errMessage .= "Unexpected type given. Abort!<br />\n"; $bContinue = false; } } } // -Find trade in buy_posted.dat, buy.dat, sell.dat, or sell_posted.dat and remove it. if ($bContinue) { // Check each file given for the IDs, if they exist, carry on. $fName = ""; foreach ($fNames as $check) { if (false != GetTradeInfo($check, $_POST['ID_to_cancel'])) { $fName = $check; } } // ID wasn't found, abort. if ("" == $fName) { $bContinue = false; $errMessage .= "Could not find specified ID in specified type.<br />\n"; } } // Remove it from our trades queue - but only if it's still in buy/sell_posted.dat // Reasoning: -if it's already in buy/sell.dat, the algorithm is running fairly well, and // the cancelation wasn't performed too quickly after the posting. So, the computer will // get to it and when it checks for the ID in the PracticeAccount queue and sees it missing, // it will skip the trade then. Also, removing it from buy/sell.dat messes up statistics // gathered from there, etc., etc.
function display_portfolio($portfolioID) { // Still include the portfolio select bit, because really it's quite short. if ($portfolioID != 0) { select_portfolio(); } // As part of the portfolio, display: // 1) Graph (use the one from armain.php) echo "<iframe src=\"/graph.php\" width=\"700px\" height=\"350px\" style=\"padding: 0px; margin: 0px;\" seamless>Your browser does not support iframes!</iframe>\n"; // 2) A buy AND sell tab, listed "amount, price, cost" with a button. Enter any two values, third will be calculated. if (isset($_SESSION['user']['ActivePortfolio'])) { echo "<div class='infoBox' id='BuySellTab'>\n"; echo " <table style='width: 100%;'>\n<tr>"; echo "<td>USD Balance: \$" . number_format($_SESSION['user']['ActivePortfolio']['Balance_USD'], 6) . "</td><td>Bitcoin Balance: " . number_format($_SESSION['user']['ActivePortfolio']['Balance_BTC'], 6) . "</td>"; // TODO: Update to reflect actual balances echo "</tr><tr>\n<td style='border: solid 1px black; width: 50%'>\n"; echo " <form action='/Planning/bitcoin_trade.php' method='post'>\n"; echo " <table><tr><td width='100%'>"; echo " <input type='hidden' name='action' value='buy'>\n"; echo " <input type='hidden' name='Return_URL' value='" . $_SERVER['REQUEST_URI'] . "'>\n"; echo " Amount BTC to buy:</td><td><input name='btcb_btc_amount' id='btcb_btc_amount' style='width: 100px;' type='text' value='0.01' onblur=\"document.getElementById('btcb_usd_cost').value=(document.getElementById('btcb_btc_amount').value * document.getElementById('btcb_btc_price').value).toPrecision(6);\">\n</td></tr><td>"; echo " Price Per BTC:</td><td><input name='btcb_btc_price' id='btcb_btc_price' type='text' style='width: 100px;' value='100.05' onblur=\"document.getElementById('btcb_usd_cost').value=(document.getElementById('btcb_btc_amount').value * document.getElementById('btcb_btc_price').value).toPrecision(6);\"></td><td>\n"; echo " <input type='submit' name='btcb_submit' value='Buy'></td></tr>\n<td>"; echo " Cost (in USD):</td><td><input name='btcb_usd_cost' id='btcb_usd_cost' type='text' style='width: 100px;' value='1.0005' onblur=\"document.getElementById('btcb_btc_amount').value=(document.getElementById('btcb_usd_cost').value / document.getElementById('btcb_btc_price').value).toPrecision(6);\"></td></tr>\n</table>"; echo " </form>"; echo " </td>\n<td style='border: solid 1px black'>"; echo " <form action='/Planning/bitcoin_trade.php' method='post'>\n"; echo " <table><tr><td width='100%'>"; echo " <input type='hidden' name='action' value='sell'>\n"; echo " <input type='hidden' name='Return_URL' value='" . $_SERVER['REQUEST_URI'] . "'>\n"; echo " Amount BTC to sell:</td><td><input name='btcs_btc_amount' id='btcs_btc_amount' style='width: 100px;' type='text' value='0.01' onblur=\"document.getElementById('btcs_usd_cost').value=(document.getElementById('btcs_btc_amount').value * document.getElementById('btcs_btc_price').value).toPrecision(6);\">\n</td></tr><td>"; echo " Price Per BTC:</td><td><input name='btcs_btc_price' id='btcs_btc_price' type='text' style='width: 100px;' value='100.05' onblur=\"document.getElementById('btcs_usd_cost').value=(document.getElementById('btcs_btc_amount').value * document.getElementById('btcs_btc_price').value).toPrecision(6);\"></td><td>\n"; echo " <input type='submit' name='btcs_submit' value='Sell'></td></tr>\n<td>"; echo " Gain (in USD):</td><td><input name='btcs_usd_cost' id='btcs_usd_cost' type='text' style='width: 100px;' value='1.0005' onblur=\"document.getElementById('btcs_btc_amount').value=(document.getElementById('btcs_usd_cost').value / document.getElementById('btcs_btc_price').value).toPrecision(6);\"></td></tr>\n</table>"; echo " </form>"; echo " </td>\n</tr>\n</table>\n"; echo "</div>\n"; } // 3) Pending transaction spot... use for pending transactions ( Transaction type, transaction value, transaction volume ) if (isset($_SESSION['user']['ActivePortfolio'])) { echo "<div class='infoBox' id='PendingTransactions'>\n"; echo "<!--Form for cancelling transactions--><form id='cancelForm' action='/Planning/bitcoin_trade.php' method='post'>\n"; echo "<input type='hidden' name='action' value='cancel'>\n"; echo "<input type='hidden' name='Return_URL' value='" . $_SERVER['REQUEST_URI'] . "'>\n"; echo "<input type='hidden' name='ID_to_cancel' id='tcID' value=''>\n"; echo "<input type='hidden' name='type' id='tcTYPE' value=''>\n</form>"; echo " <table style='width:100%'>\n"; $pendingTransactions = explode(",", $_SESSION['user']['ActivePortfolio']['Pending']); $index = 1; foreach ($pendingTransactions as $PendingTransaction) { if ("B" == $PendingTransaction[0]) { echo "<tr style='background-color: #E0FFFF;'><td>{$index}</td><td>"; $index++; echo "Buy</td>\n"; if ($tradeInfo = GetTradeInfo($_SERVER['DOCUMENT_ROOT'] . "/data/buy_posted.dat", substr($PendingTransaction, 1))) { echo "<td>" . $tradeInfo[2] . " BTC x</td><td>\$" . $tradeInfo[3] . "</td><td>\$" . $tradeInfo[2] * $tradeInfo[3] . "</td>"; echo "<td><input type='button' value='Cancel Trade' onclick=\"document.getElementById('tcID').value='" . $tradeInfo[0] . "'; document.getElementById('tcTYPE').value='BUY'; document.getElementById('cancelForm').submit();\" /></td></tr>\n"; } else { if ($tradeInfo = GetTradeInfo($_SERVER['DOCUMENT_ROOT'] . "/data/buy.dat", substr($PendingTransaction, 1))) { echo "<td>" . $tradeInfo[2] . " BTC x</td><td>\$" . $tradeInfo[3] . "</td><td>\$" . $tradeInfo[2] * $tradeInfo[3] . "</td>"; echo "<td><input type='button' value='Cancel Trade' onclick=\"document.getElementById('tcID').value='" . $tradeInfo[0] . "'; document.getElementById('tcTYPE').value='BUY'; document.getElementById('cancelForm').submit();\" /></td></tr>\n"; } } } else { if ("S" == $PendingTransaction[0]) { echo "<tr style='background-color: #FFFFE0;'><td>{$index}</td><td>"; $index++; echo "Sell</td>\n"; if ($tradeInfo = GetTradeInfo($_SERVER['DOCUMENT_ROOT'] . "/data/sell_posted.dat", substr($PendingTransaction, 1))) { echo "<td>" . $tradeInfo[2] . " BTC x</td><td>\$" . $tradeInfo[3] . "</td><td>\$" . $tradeInfo[2] * $tradeInfo[3] . "</td>"; echo "<td><input type='button' value='Cancel Trade' onclick=\"document.getElementById('tcID').value='" . $tradeInfo[0] . "'; document.getElementById('tcTYPE').value='SELL'; document.getElementById('cancelForm').submit();\" /></td></tr>\n"; } else { if ($tradeInfo = GetTradeInfo($_SERVER['DOCUMENT_ROOT'] . "/data/sell.dat", substr($PendingTransaction, 1))) { echo "<td>" . $tradeInfo[2] . " BTC x</td><td>\$" . $tradeInfo[3] . "</td><td>\$" . $tradeInfo[2] * $tradeInfo[3] . "</td>"; echo "<td><input type='button' value='Cancel Trade' onclick=\"document.getElementById('tcID').value='" . $tradeInfo[0] . "'; document.getElementById('tcTYPE').value='SELL'; document.getElementById('cancelForm').submit();\" /></td></tr>\n"; } } } else { echo "No trades in system.</td>\n"; } } } echo "</table>\n"; echo "</div>\n"; // 4) Pending / History section - display pending transactions FIRST, then a brief history of recent transactions. echo "<div class='infoBox' id='History'>\n"; echo " <p>Here's a history of all of your bitcoin transactions:</p>\n"; $history = explode("-", $_SESSION['user']['ActivePortfolio']['History']); array_pop($history); echo "<table style='width: 100%'>\n"; echo "<tr><td>Trade ID</td><td>Bitcoins in/out</td><td>USD in/out</td></tr>\n"; foreach ($history as $line) { $lineData = explode(",", $line); if ("S" == $lineData[0][0]) { // Sale echo "<tr style='background-color: #FFFFC8;'><td>Sale #" . $lineData[0] . "</td><td>" . $lineData[1] . "</td><td>" . $lineData[2] . "</td></tr>"; } else { // Buy echo "<tr style='background-color: #C8FFFF;'><td>Purchase #" . $lineData[0] . "</td><td>" . $lineData[1] . "</td><td>" . $lineData[2] . "</td></tr>"; } } echo "</table>"; echo "</div>\n"; } }