Bitcoin Forum

Bitcoin => Bitcoin Discussion => Topic started by: joeyjoe on September 22, 2011, 03:41:54 PM



Title: Grab the price of bitcoin?
Post by: joeyjoe on September 22, 2011, 03:41:54 PM
Wheres a good place to return the current price of bitcoins. Im preferbly after a PHP script that just returns the price, or some TCP connection or somthing?

the reason is so i can use it to display the price on my arudino


Title: Re: Grab the price of bitcoin?
Post by: error on September 22, 2011, 11:44:25 PM
You'll need to do your own error checking; this isn't particularly robust.

Code:
function mtgox()
{
        $mtgox = file_get_contents("https://www.mtgox.com/code/data/ticker.php");
        if (!$mtgox) return 0;
        $mtgox = json_decode($mtgox);
        return $mtgox->ticker->last;
}


Title: Re: Grab the price of bitcoin?
Post by: ne1 on September 23, 2011, 12:29:31 AM
That sound cool, is love to have an arguing mtgox ticker.  I've got an arduino, Ethernet shield and an lcd.  Love to see where you get with this.


Title: Re: Grab the price of bitcoin?
Post by: pent on September 23, 2011, 01:14:34 PM
You'll need to do your own error checking; this isn't particularly robust.

Code:
function mtgox()
{
        $mtgox = file_get_contents("https://www.mtgox.com/code/data/ticker.php");
        if (!$mtgox) return 0;
        $mtgox = json_decode($mtgox);
        return $mtgox->ticker->last;
}
not ideal. I'll advice to use volume-based rate. In this script it calculates average volume-based rate for last 3 hours.
Code:
<?
$ch=curl_init('https://mtgox.com/code/data/getTrades.php');
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($ch,CURLOPT_TIMEOUT,15);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
$headers = array(
//      "Host: mtdox.com",
        "User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30",
        "Accept: */*",
        "Accept-Language: en-US;q=0.8,en;q=0.4",
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ret=curl_exec($ch);
if (curl_errno($ch)!=0) {
    echo curl_error($ch);
    curl_close($ch);
    exit;
}
curl_close($ch);

$o=json_decode($ret,true);
if (!is_array($o)) exit("Invalid data");

$o1=array_reverse($o);
$start=$o1[0]['date'];
$end=$start-3*60*60;
$total_btc=0;
$total_usd=0;
$cnt=0;
for (reset($o1);list(,$v)=each($o1);) {
    if ($v['date']<$end) break;
    $total_usd=$total_usd + $v['price']*$v['amount'];
    $total_btc+=$v['amount'];
    $cnt++;
}

if ($total_usd <=0 || $total_btc<=0) exit("Zero volume");

$rate=$total_usd / $total_btc;

echo "Rate is $rate USD/BTC\n";

Also, mtgox has some 'vwap' key in ticker array. I don't know what exact meaning it has, but it is near to the value my script provides. Maybe for different time period.


Title: Re: Grab the price of bitcoin?
Post by: Cryptoman on September 23, 2011, 01:18:10 PM
OP, how are you able to run a PHP script on an Arduino?


Title: Re: Grab the price of bitcoin?
Post by: joeyjoe on September 27, 2011, 11:56:40 AM
OP, how are you able to run a PHP script on an Arduino?

Run a PHP script on a webserver, and grab the webserver text to the arduino then display on LCD.

I have managed to connect the arduino directly to the ticker, but have to work out a way to parse the output more easily,