Sunday, July 31, 2011

Modified PHP bot for trading bitcoins on mtgox

General information about bitcoins (mtgox, mining, etc) here:

I've been messing around with bitcoin, and I found a bot at :

What I did was minimize the bot, and change it so it buys bit coins if they are less than 15 dollars, and I have over 60 bucks in my MtGox account.
If the bitcoins become worth over 24 dollars, sell all of them. I also have the mtgox bitcoin app on my phone in case I need to dump them.

function mtgox_query($path, array $req = array()) { 
   $key='yourkey';      //<----KEY! 
   $secret='yoursecret';   //<-SECRET! 
                   $mt = explode(' ', microtime()); 
                   $req['nonce'] = $mt[1].substr($mt[0], 2, 6); 
                   $post_data = http_build_query($req, '', '&'); 
                   $headers = array( 
                          'Rest-Key: '.$key, 
                          'Rest-Sign: '.base64_encode(hash_hmac('sha512', $post_data, base64_decode($secret), true)), 
                  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; MtGox PHP client; '.php_uname('s').'; PHP/'.phpversion().')'); 
                  curl_setopt($ch, CURLOPT_URL, ''.$path); 
                  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
                  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
                  $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 ma 
  ke sure connection is working and requested API exists'); 
          return $dec; 
 $seconds_wait = 40; 
  $truth =  TRUE; 
          echo "----------------------------------------\n"; 
          echo "USD TOTAL: ".$r['usds']."\n"; 
          echo "BTC TOTAL: ".$r['btcs']."\n\n"; 
  //      echo date('l jS \of F Y h:i:s A')."\n"; 
          echo "LAST:".$cur_last=$decoded['ticker']['last']."\n"; 
          echo "BUY:".$cur_buy=$decoded['ticker']['buy']."\n"; 
          echo "SELL:".$cur_sell=$decoded['ticker']['sell']."\n"; 
          echo "HIGH:".$cur_high=$decoded['ticker']['high']."\n"; 
          echo "AVERAGE:".$cur_avg=$decoded['ticker']['avg']."\n"; 
          echo "VWAP:".$cur_vwap=$decoded['ticker']['vwap']."\n\n"; 
  if(($r['usds'] > 60) && ($cur_buy < 15) ){ 
                  $amount= ($r['usds'] / $cur_buy); 
                  $amount = number_format($amount, 2, '.', ''); 
                  echo " PRICE: $price AMOUNT: $amount"; 
                  echo "Buying Bit coins to cancel transaction hit Ctrl C\n"; 
                  echo "STATUS: ".$decoded['status']."\n"; 
          if($cur_sell > 24 ){ 
                  echo "Selling Bitcoins\n"; 
                  $price = $cur_sell; 
                  $amount = $r['btcs']; 
                  echo "STATUS: ".$decoded['status']."\n"; 
                  $truth = FALSE; 

You can get information about mtgox's api here, and about the bot:

When I run this script I actually start a screen session, then type: php mtgox.php > mtgox.log
After that I open a terminal window and tail -f mtgox.log

No comments: