[Solved] Math Results Inaccurate PHP [closed]


In the line

$open = fopen("http://quote.yahoo.com/d/quotes.csv?s=$symbol&f=sl1d1t1c1ohgv&e=.csv", "r");

You need to substitute the actual symbol, not leave it as a string in the query. Maybe something like this:

$open = fopen("http://quote.yahoo.com/d/quotes.csv?s=".$symbol."&f=sl1d1t1c1ohgv&e=.csv", "r");

to concatenate the symbol will help.

If you have an invalid request, you are probably looking at garbage.

Also – you need to make this request before you do any math with it – in other words, you have to compute quote_1 before you use the value to compute the profit.

Here is a ridiculously simplified version of what I am talking about. See if you understand why this is different than what you did, and how to get back “from here to there”:

<?php
    $symbol = "GE";
    $entry = 23;
    $open = fopen("http://quote.yahoo.com/d/quotes.csv?s=".$symbol."&f=sl1d1t1c1ohgv&e=.csv", "r");
    $quote = fread($open, 1000);
    fclose($open);
    $quote = str_replace("\"", "", $quote);
    $quote = explode(",", $quote);
    $lastPrice = (float)$quote[1];
    $profit = $lastPrice - $entry;
    echo "current ".$symbol." quote: ".$lastPrice;
    echo "; entry: ".$entry."; profit: ".$profit;
?>

You can see this in action at http://www.floris.us/SO/stocks.php . I made no attempts at making it pretty – just getting the math right. Right now the output is

current GE quote: 23.32; entry: 23; profit: 0.32

I hope it’s helpful.

7

solved Math Results Inaccurate PHP [closed]