Advertisement
JudeAustin

BTC-E Trade Script Simple

May 18th, 2013
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.58 KB | None | 0 0
  1. <?php
  2. function btce_query($method, array $req = array()) {
  3.         // API settings
  4.         $key = ''; // your API-key
  5.         $secret = ''; // your Secret-key
  6.  
  7.         $req['method'] = $method;
  8.         $mt = explode(' ', microtime());
  9.         $req['nonce'] = $mt[1];
  10.  
  11.         // generate the POST data string
  12.         $post_data = http_build_query($req, '', '&');
  13.  
  14.         $sign = hash_hmac("sha512", $post_data, $secret);
  15.  
  16.         // generate the extra headers
  17.         $headers = array(
  18.                 'Sign: '.$sign,
  19.                 'Key: '.$key,
  20.         );
  21.  
  22.         // our curl handle (initialize if required)
  23.         static $ch = null;
  24.         if (is_null($ch)) {
  25.                 $ch = curl_init();
  26.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  27.                 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; BTCE PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
  28.         }
  29.         curl_setopt($ch, CURLOPT_URL, 'https://btc-e.com/tapi/');
  30.         curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  31.         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  32.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  33.  
  34.         // run the query
  35.         $res = curl_exec($ch);
  36.         if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
  37.         $dec = json_decode($res, true);
  38.         if (!$dec) throw new Exception('Invalid data received, please make sure connection is working and requested API exists');
  39.         return $dec;
  40. }
  41.  
  42.  
  43. $result = btce_query("getInfo");
  44.  
  45.  
  46. echo "Bitcoins:";
  47. echo "\n";
  48. echo "".print_r($result['return']['funds']['btc'])."";
  49. echo "\n";
  50. echo "USD:";
  51. echo "\n";
  52. echo "".print_r($result['return']['funds']['usd'])."";
  53. echo "\n";
  54.  
  55. echo "High: $high";
  56. echo "\n";
  57.  
  58. echo "Low: $low";
  59. echo "\n";
  60.  
  61. echo "Last: $last";
  62. echo "\n";
  63.  
  64. if ($result['return']['funds']['btc'] > .1)
  65. {
  66. $sell = btce_query("Trade", array("pair" => "btc_usd", "type" => "sell", "amount" => .998, "rate" => "Price you want to sell at.)); //sell 1 BTC @ $1 less than high.
  67. echo "<pre>".print_r($sell, true)."</pre>";
  68. }
  69. else
  70. {
  71.        echo "No Bitcoins to trade at this time.";
  72. }
  73.  
  74. echo "\n";
  75.  
  76. if ($result['return']['funds']['usd'] > .1 * $last)
  77. {
  78. $buy = btce_query("Trade", array("pair" => "btc_usd", "type" => "buy", "amount" => 1, "rate" => "Price you want to buy at.)); //buy 1 BTC @ $3 less than high.
  79. echo "<pre>".print_r($buy, true)."</pre>";
  80. }
  81. else
  82. {
  83.         echo "No USD to trade at this time.";
  84. }
  85.  
  86. echo "\n";
  87.  
  88. $orders = btce_query("OrderList");
  89. echo "".print_r($orders['return'], 1)."";
  90.  
  91.  
  92. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement