$results_portfolio = mysql_query("SELECT id, name, manager FROM portfolios WHERE id > 1 ORDER BY id ASC");
while ($row = mysql_fetch_row($results_portfolio)) {
?>
|
echo $row[1];?> Portfolio
Portfolio Manager: echo $row[2];?>
View Trades
|
Company |
Symbol |
No. of Shares |
Latest Price |
% of Portfolio1 |
Market Value |
Return2 |
% Return3 |
| |
|
|
|
|
|
|
|
$results_stock = mysql_query("SELECT name, symbol, shares, avgprice FROM stocks WHERE portfolio = '$row[0]'");
unset($pricesArray, $valuesArray, $retArray);
$pricesArray;
$valuesArray;
$retArray;
while ($stock = mysql_fetch_row($results_stock)) {
$ticker = $stock[1];
$open = fopen("http://download.finance.yahoo.com/d/quotes.csv?s=$ticker&f=l1", "r");
$read = fread($open, 2000);
fclose($open);
$read = str_replace("\"", "", $read);
$read = explode(",", $read);
$price = $read[0];
$ticker_suffix = substr(strtoupper($ticker), -3);
switch ($ticker_suffix) {
case ".TO":
if (empty($fxCADUSD)) {
$fxOpen = fopen("http://download.finance.yahoo.com/d/quotes.csv?s=CADUSD=X&f=l1","r");
$fxRead = fread($fxOpen,2000);
fclose($fxOpen);
$fxRead = str_replace("\"","",$fxRead);
$fxRead = explode(",",$fxRead);
$fxCADUSD = $fxRead[0];
}
$price = $price * $fxCADUSD;
break;
case ".MX":
if (empty($fxMXNUSD)) {
$fxOpen = fopen("http://download.finance.yahoo.com/d/quotes.csv?s=MXNUSD=X&f=l1","r");
$fxRead = fread($fxOpen,2000);
fclose($fxOpen);
$fxRead = explode(",", str_replace("\"","",$fxRead));
$fxMXNUSD = $fxRead[0];
}
$price = $price * $fxMXNUSD;
break;
}
$pricesArray[] = round($price,2);
$valuesArray[] = round($stock[2]*$price,2); // shares * price
$retArray[] = round(($stock[2]*$price) + (($stock[2]*(-1))*$stock[3]),2); // current position value plus delta cash at trade-time
}
$result_cash = mysql_query("SELECT cash FROM portfolios WHERE id = '$row[0]'");
$cash = mysql_result($result_cash,0,"cash");
$equity = array_sum($valuesArray);
$totalValue = $cash + $equity;
$cash_pct = round($cash/$totalValue,4)*100;
$totalReturn = round($totalValue-100000,2);
$totalReturn_pct = (round($totalValue/100000-1,4))*100;
if($totalReturn >= 0) $totalReturn_text = "$".$totalReturn;
else {
$temp = $totalReturn * (-1);
$totalReturn_text = "-$".$temp."";
}
if($totalReturn_pct >= 0) $totalReturn_pct_text = $totalReturn_pct."%";
else {
$temp = $totalReturn_pct * (-1);
$totalReturn_pct_text = "-".$temp."%";
}
$outperformance = round($totalValue-$totalValue_SP,2);
$outperformance_pct = $totalReturn_pct - $totalReturn_SP_pct;
if($outperformance >= 0) $outperformance_text = "$".$outperformance;
else {
$temp = $outperformance * (-1);
$outperformance_text = "-$".$temp."";
}
if($outperformance_pct >= 0) $outperformance_pct_text = $outperformance_pct."%";
else {
$temp = $outperformance_pct * (-1);
$outperformance_pct_text = "-".$temp."%";
}
mysql_data_seek($results_stock,0);
$x = 0;
$color = true;
while ($stock = mysql_fetch_row($results_stock)) {
$name = $stock[0];
$ticker = $stock[1];
$shares = round($stock[2],5);
$avgprice = $stock[3];
$price = $pricesArray[$x];
$value = $valuesArray[$x];
$position = round($value/$totalValue,4)*100;
// $position = round(($shares*$avgprice)/1000,2);
// $return = round($value-$avgprice*$stock[2],2); <-- our old code
$return = $retArray[$x];
// $return_pct = (round($price/$avgprice-1,4))*100; <-- our old code
$sharesign = abs($shares)/$shares;
$return_pct = (round($price/$avgprice-1,4))*100*$sharesign;
if($return >= 0) $return_text = "$".$return;
else {
$temp = $return * (-1);
$return_text = "-$".$temp."";
}
if($return_pct >= 0) $return_pct_text = $return_pct."%";
else {
$temp = $return_pct * (-1);
$return_pct_text = "-".$temp."%";
}
if($color) {
?>
| echo $name;?> |
echo $ticker;?> |
echo $shares;?> |
$ echo $price;?> |
echo $position;?>% |
$ echo round($value,2);?> |
echo $return_text;?> |
echo $return_pct_text;?> |
$color = false;
}
else {
?>
| echo $name;?> |
echo $ticker;?> |
echo $shares;?> |
$ echo $price;?> |
echo $position;?>% |
$ echo $value;?> |
echo $return_text;?> |
echo $return_pct_text;?> |
$color = true;
}
$x++;
}
if($color) {
?>
| |
|
|
|
|
|
|
|
}
?>
| |
|
|
|
|
|
|
|
| Cash |
|
|
|
echo $cash_pct;?>% |
$ echo $cash;?> |
|
|
|
| Total Account Value |
|
|
|
100.00% |
$ echo round($totalValue,2);?> |
echo $totalReturn_text;?> |
echo $totalReturn_pct_text;?> |
| Standard and Poor's 500 Index |
|
|
|
|
$ echo round($totalValue_SP,2);?> |
echo $totalReturn_SP_text;?> |
echo $totalReturn_SP_pct_text;?> |
|
| Outperformance |
|
|
|
|
echo $outperformance_text;?> |
echo $outperformance_text;?> |
echo $outperformance_pct_text;?> |
|
|
}
mysql_close();
?>
Notes on Calculations:
1
% of Portfolio: Percentage ratio of the market value of the asset to the total value of the portfolio.
2
Return: The difference between the cost basis and current market value of the current holdings of the portfolio at the specified date. The cost basis, however, is not explicitly displayed in any of the tables above. However, it can be calculated by subtracting the return from the current market value of the stock.
3
% Return: The percentage ratio of the return (see above) from the asset to the cost basis of the current holdings of the stock.
|
|