Author

Topic: Need help with btc-e api (Read 3512 times)

newbie
Activity: 1
Merit: 0
January 11, 2014, 07:25:36 PM
#8
I was working on this when we decided to switch our code to python, hope it helps
Code:


...The Test...



  
$content
=file_get_contents("https://btc-e.com/api/2/btc_usd/ticker"); 
$data=json_decode($content);
$contentLTCUSD=file_get_contents("https://btc-e.com/api/2/ltc_usd/ticker"); 
$dataLU=json_decode($contentLTCUSD);
$contentLTCBTC=file_get_contents("https://btc-e.com/api/2/ltc_btc/ticker"); 
$dataLB=json_decode($contentLTCBTC);
echo 
"Data fetched from system at the following system times:";
echo 
nl2br ("\n ");
Echo 
"BTC_USD: ";
echo 
$data->ticker->server_time;
echo 
nl2br ("\n ");
Echo 
"LTC_USD: ";
echo 
$dataLU->ticker->server_time;
echo 
nl2br ("\n ");
echo 
"LTC_BTC: ";
echo 
$dataLB->ticker->server_time;
echo 
nl2br ("\n ");




echo 
"CURRENT BTC_USD PRICE: ";
echo 
nl2br ("\n ");
Echo 
"last: $";
echo 
$data->ticker->last;
echo 
nl2br ("\n "); 
Echo 
"buy: $";
echo 
$data->ticker->buy;
echo 
nl2br ("\n "); 
Echo 
"sell: $";
echo 
$data->ticker->sell
$tickerlast $data->ticker->last;
$tickerbuy $data->ticker->buy;
$tickersell $data->ticker->sell;
echo 
nl2br ("\n ");

echo 
"CURRENT LTC_USD PRICE: ";
echo 
nl2br ("\n ");
Echo 
"last: $";
echo 
$dataLU->ticker->last;
echo 
nl2br ("\n "); 
Echo 
"buy: $";
echo 
$dataLU->ticker->buy;
echo 
nl2br ("\n "); 
Echo 
"sell: $";
echo 
$dataLU->ticker->sell
$tickerlastLU $dataLU->ticker->last;
$tickerbuyLU $dataLU->ticker->buy;
$tickersellLU $dataLU->ticker->sell;
echo 
nl2br ("\n "); 

echo 
"CURRENT LTC_BTC PRICE: ";
echo 
nl2br ("\n ");
Echo 
"last: $";
echo 
$dataLB->ticker->last;
echo 
nl2br ("\n "); 
Echo 
"buy: $";
echo 
$dataLB->ticker->buy;
echo 
nl2br ("\n "); 
Echo 
"sell: $";
echo 
$dataLB->ticker->sell
$tickerlastLB $dataLB->ticker->last;
$tickerbuyLB $dataLB->ticker->buy;
$tickersellLB $dataLB->ticker->sell;
echo 
nl2br ("\n "); 

?>








I designed it to automatically reload every 2min, what it will do is get btc USD data ltc USD data and btc ltc data and display last buy and sell prices for all three. Modify it all you want. If you need any help just let me know.

Like what you see? Feel free to donate BTC to 1JtxTAtnda5wcJJj78UkN2QwsUBEesmHAN

JVarhol
hero member
Activity: 699
Merit: 501
Coinpanion.io - Copy Successful Crypto Traders
April 12, 2013, 11:51:44 AM
#7
Hi, is anyone else having problems today using API from BTCe for automatic trading?
They keep knocking me out of data feed every 5 minutes, then i have to wait for half an hour before the server start to give response to my requests. 5 minutes later i'm off again!
Everything has been working well until today, i can't understand the reasons of this issue  Huh
It's curious that if i try to access https://btc-e.com/api/2/btc_usd/depth from mozilla everything works fine, everytime i refresh the page.
If i send a request to download the string at the same address via my webClient in VB.NET it works only for a very short period of time let's say 5 minutes then their server stop responding to me.
legendary
Activity: 2408
Merit: 1009
Legen -wait for it- dary
December 27, 2012, 12:30:02 AM
#6
How would I use this to echo out the last trade on the btc/usd pair?

YGPM!
newbie
Activity: 3
Merit: 0
December 26, 2012, 06:24:48 PM
#5
This is a snippet from my BTC-e data feed in sig (Shameless plug  Tongue).
This code requires the use of a couple additional includes, and cURL to be compiled with SSL support.
Code:
        FILE *headerfile= 0;

CURL *curl;
   
curl_global_init(CURL_GLOBAL_DEFAULT);
        curl = curl_easy_init();
 
  if(curl) {
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
        curl_easy_setopt(curl, CURLOPT_URL, "https://btc-e.com/api/2/btc_usd/trades");
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 0);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 0);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handle_data);  //This requires a function to put the data into a string for output

CURLcode res= curl_easy_perform(curl);

// Check for errors. If good, reformat it for use
if (res == 0){
Format_Record(args);
curl_easy_reset(curl);
}
else
cerr<< "Error: "<< res <
/* always cleanup */
    curl_easy_cleanup(curl);
  }
 
  curl_global_cleanup();

How would I use this to echo out the last trade on the btc/usd pair?
newbie
Activity: 3
Merit: 0
December 26, 2012, 06:22:31 PM
#4
You are using the pubic api when getting ticker it does not need all that validation stuff a simple get will do it for you. An example of doing it I found in python below as you can see easy to do.

Code:

#!/usr/bin/env python
# If you find this sample useful, please feel free to donate :)
......

import httplib
import urllib
import json

conn = httplib.HTTPSConnection("btc-e.com")
conn.request("GET", "/api/2/ltc_btc/ticker")
response = conn.getresponse()

print response.status, response.reason
print json.load(response)

conn.close()

Which gives a result like this when run for ltc/btc just change the pair to what you want then parse the json output to get the final result wanted.

Code:

./ltc_btc_ticker.py
200 OK
{u'ticker': {u'sell': 0.0057299999999999999, u'buy': 0.00577, u'last': 0.0057299999999999999, u'vol': 708.32758999999999, u'vol_cur': 122349.03096, u'high': 0.0058700000000000002, u'low': 0.0057099999999999998, u'server_time': 1356545001, u'avg': 0.00579}}

I really need something that works in php, because i've never used python before. Thanks for your help anyway.
legendary
Activity: 2408
Merit: 1009
Legen -wait for it- dary
December 26, 2012, 03:35:48 PM
#3
This is a snippet from my BTC-e data feed in sig (Shameless plug  Tongue).
This code requires the use of a couple additional includes, and cURL to be compiled with SSL support.
Code:
        FILE *headerfile= 0;

CURL *curl;
   
curl_global_init(CURL_GLOBAL_DEFAULT);
        curl = curl_easy_init();
 
  if(curl) {
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
        curl_easy_setopt(curl, CURLOPT_URL, "https://btc-e.com/api/2/btc_usd/trades");
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 0);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 0);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handle_data);  //This requires a function to put the data into a string for output

CURLcode res= curl_easy_perform(curl);

// Check for errors. If good, reformat it for use
if (res == 0){
Format_Record(args);
curl_easy_reset(curl);
}
else
cerr<< "Error: "<< res <
/* always cleanup */
    curl_easy_cleanup(curl);
  }
 
  curl_global_cleanup();
SAC
sr. member
Activity: 322
Merit: 250
December 26, 2012, 01:13:00 PM
#2
You are using the pubic api when getting ticker it does not need all that validation stuff a simple get will do it for you. An example of doing it I found in python below as you can see easy to do.

Code:

#!/usr/bin/env python
# If you find this sample useful, please feel free to donate :)
......

import httplib
import urllib
import json

conn = httplib.HTTPSConnection("btc-e.com")
conn.request("GET", "/api/2/ltc_btc/ticker")
response = conn.getresponse()

print response.status, response.reason
print json.load(response)

conn.close()

Which gives a result like this when run for ltc/btc just change the pair to what you want then parse the json output to get the final result wanted.

Code:

./ltc_btc_ticker.py
200 OK
{u'ticker': {u'sell': 0.0057299999999999999, u'buy': 0.00577, u'last': 0.0057299999999999999, u'vol': 708.32758999999999, u'vol_cur': 122349.03096, u'high': 0.0058700000000000002, u'low': 0.0057099999999999998, u'server_time': 1356545001, u'avg': 0.00579}}
newbie
Activity: 3
Merit: 0
December 26, 2012, 05:03:29 AM
#1
I am trying to use btc-e's api to echo out the last trade on the btc/usd pair, using php. This is what I have done so far. Can anyone tell me how to make this work? Thanks.

function btce_query($method, array $req = array()) {
        // API settings
        $key = ''; // your API-key
        $secret = ''; // your Secret-key
 
        $req['method'] = $method;
        $mt = explode(' ', microtime());
        $req['nonce'] = $mt[1];
       
        // generate the POST data string
        $post_data = http_build_query($req, '', '&');
 
        $sign = hash_hmac("sha512", $post_data, $secret);
 
        // generate the extra headers
        $headers = array(
                'Sign: '.$sign,
                'Key: '.$key,
        );
 
        // our curl handle (initialize if required)
        static $ch = null;
        if (is_null($ch)) {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; BTCE PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
        }
        curl_setopt($ch, CURLOPT_URL, 'https://btc-e.ru/tapi/');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
 
        // run the query
        $res = curl_exec($ch);
        if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
        $dec = json_decode($res, true);
        if (!$dec) throw new Exception('Invalid data received, please make sure connection is working and requested API exists');
        return $dec;
}

$query = btce_query("2/btc_usd/ticker");
$btc_last =  ("".$cur_avg=$query['ticker']['last']."\n");
echo $btc_last;
?>
Jump to: