Bitcoin Forum
June 03, 2024, 03:07:08 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Simplest way of obtaining MtGox exchange rate via an API?  (Read 1717 times)
knocte (OP)
Newbie
*
Offline Offline

Activity: 44
Merit: 0


View Profile
May 05, 2013, 12:45:53 PM
 #1

Hey guys,

I want to do a simple app that refreshes every minute to tell me the current exchange rate by Mtgox but I have a problem interpreting the JSON that some APIs return:

 - Mtgox's fast ticker returns [this JSON][1]. The question is, what's the value that I have to retrieve? There are a lot and I don't understand them, and I don't find proper docs. (Some people say it's simply the data.last.value element of that JSON, but that doesn't seem to change overtime! unless it returns cached responses of more than 1 minute Sad wasn't the "fast ticker" supposed to be super fast?)

 - Bitcoincharts' API has the [Markets Data endpoint][2], and it returns the market I want under the symbol "mtgoxUSD", however it tells me the date time of latest trade, the last buy/ask, but doesn't tell me **the exchange rate of the latest trade**.

Am I missing something?
Thanks

  [1]: http://data.mtgox.com/api/2/BTCUSD/money/ticker_fast
  [2]: http://bitcoincharts.com/about/markets-api/
MA5H3D
Member
**
Offline Offline

Activity: 99
Merit: 10


View Profile
May 05, 2013, 01:03:30 PM
 #2

I'm not sure about "ticker_fast" but I use this and refresh with jQuery:

<?php
$data = get_content("https://data.mtgox.com/api/2/BTCUSD/money/ticker");

// If that doesn't work (old) try: file_get_contents
// $data = file_get_contents("https://data.mtgox.com/api/2/BTCUSD/money/ticker");

$data = json_decode($data, true);

$spot_sell = $data['data']['sell']['value'];
$spot_buy = $data['data']['buy']['value'];

print_r($spot_sell);
// or
// print_r($spot_buy);
?>
knocte (OP)
Newbie
*
Offline Offline

Activity: 44
Merit: 0


View Profile
May 05, 2013, 01:38:25 PM
 #3

But you're extracting buy/sell, not the exchange rate of the last trade.
daybyter
Legendary
*
Offline Offline

Activity: 965
Merit: 1000


View Profile
May 05, 2013, 01:45:00 PM
 #4

I wrote some Javascript for btc-e, that could be converted to mtgox with minimal modifications:

https://bitcointalk.org/index.php?topic=135392.0

It fetches the depth in an infinite loop and displays the depth.

MA5H3D
Member
**
Offline Offline

Activity: 99
Merit: 10


View Profile
May 05, 2013, 02:04:35 PM
 #5

But you're extracting buy/sell, not the exchange rate of the last trade.

I think the last sell is just "last", so:

$spot_last = $data['data']['last']['value'];
knocte (OP)
Newbie
*
Offline Offline

Activity: 44
Merit: 0


View Profile
May 05, 2013, 02:13:39 PM
 #6

If it's data.last.value, why doesn't it change often? I've been sitting here refeshing the page for 2 minutes and that value is still at 111.04358.
NLNico
Legendary
*
hacker
Offline Offline

Activity: 1876
Merit: 1289


DiceSites.com owner


View Profile WWW
May 05, 2013, 02:32:01 PM
 #7

See here the unofficial documentation about ticker_fast: https://bitbucket.org/nitrous/mtgox-api/overview#markdown-header-moneyticker_fast

Should be data.last.value and there is only a 1 second cache. Make sure it's not the browser that cached it or something? Because for me it is changing just fine (compared to bitcoinity, of course if there is no trade for a minute.. :p)

MA5H3D
Member
**
Offline Offline

Activity: 99
Merit: 10


View Profile
May 05, 2013, 02:36:23 PM
 #8

Is it being cached? I haven't tested it but try this:

<?php
header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.
header('Expires: 0'); // Proxies.

$data = get_content("https://data.mtgox.com/api/2/BTCUSD/money/ticker");

// If that doesn't work (old) try: file_get_contents
// $data = file_get_contents("https://data.mtgox.com/api/2/BTCUSD/money/ticker");

$data = json_decode($data, true);

$spot_last = $data['data']['last']['value'];

print_r($spot_last);
?>
MA5H3D
Member
**
Offline Offline

Activity: 99
Merit: 10


View Profile
May 05, 2013, 03:05:33 PM
 #9

There might be a better way to do it, but i've tested it and this works:

Two pages, change the refresh interval in index.php

Page 1 = index.php -

<!DOCTYPE html>
<head>
      <title>Bitcoin Price</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
      <meta http-equiv="Pragma" content="no-cache">
      <meta http-equiv="Expires" content="0">

      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
      
      <script>
         var auto_refresh = setInterval(
         function()
         {$('.btc-price').load('gox.php');}, 10000);
      </script>
</head>

<body>
<div class="btc-price">Wait 10 Seconds..</div>
</body>

</html>


Page 2 = gox.php -

<?php
header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.
header('Expires: 0'); // Proxies.

$data = file_get_contents("https://data.mtgox.com/api/2/BTCUSD/money/ticker_fast");
$data = json_decode($data, true);
$spot_last = $data['data']['last']['value'];
print_r($spot_last);
?>

Try That.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!