Advertisement
joric

Mt.Gox trading bot (simplebot.py). PoC, do not use!

Jan 5th, 2012
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.83 KB | None | 0 0
  1. import sys
  2. from urllib import urlencode
  3. import urllib2
  4. import time
  5. from hashlib import sha512
  6. from hmac import HMAC
  7. import base64
  8. import json
  9. import sys
  10. from urllib2 import URLError
  11. from datetime import datetime
  12.  
  13. from mtgox_config import *
  14.  
  15. FETCH = True
  16. ORDER = False
  17.  
  18. def get_nonce():
  19.     return int(time.time()*100000)
  20.  
  21. def sign_data(secret, data):
  22.     return base64.b64encode(str(HMAC(secret, data, sha512).digest()))
  23.      
  24. class requester:
  25.     def __init__(self, auth_key, auth_secret):
  26.         self.auth_key = auth_key
  27.         self.auth_secret = base64.b64decode(auth_secret)
  28.        
  29.     def build_query(self, req={}):
  30.         req["nonce"] = get_nonce()
  31.         post_data = urlencode(req)
  32.         headers = {}
  33.         headers["User-Agent"] = "GoxApi"
  34.         headers["Rest-Key"] = self.auth_key
  35.         headers["Rest-Sign"] = sign_data(self.auth_secret, post_data)
  36.         return (post_data, headers)
  37.        
  38.     def perform(self, path, args):
  39.         data, headers = self.build_query(args)
  40.         req = urllib2.Request("https://mtgox.com/api/0/"+path, data, headers)
  41.         res = urllib2.urlopen(req, data)
  42.         return json.load(res)
  43.  
  44. class SimpleBot:
  45.     def __init__(self):
  46.         self.r = requester(MTGOX_API_KEY, MTGOX_API_SECRET)
  47.         pass
  48.  
  49.     def process(self):
  50.  
  51.         fee = 0.0055
  52.         interest = 0.0005
  53.  
  54.         vwap = 3.970505
  55.         btc = 1.702021
  56.         usd = 0.002398
  57.         btc = 0.000001
  58.         usd = 6.892910
  59.         last = 3.970505
  60.  
  61.         if FETCH:
  62.             trade_data = self.r.perform("getOrders.php", {})
  63.             ticker = self.r.perform("data/ticker.php", {})["ticker"];
  64.             info = self.r.perform("info.php", {});
  65.             btc = float(trade_data["btcs"])
  66.             usd = float(trade_data["usds"])
  67.             last = float(ticker["last"])
  68.             vwap = float(ticker["vwap"])
  69.             fee = float(info['Trade_Fee']) / 100
  70.  
  71.             orders = trade_data['orders']
  72.  
  73.             #remove all orders
  74.             for order in orders:
  75.                 typestring = "sell" if order['type'] == 1 else "buy"
  76.                 print "-- Orders --"
  77.                 print typestring, order['amount'], "@", order['price']
  78.                 if ORDER:
  79.                     self.r.perform("cancelOrder.php", { oid: order["oid"], type: order['type'] });
  80.  
  81.         #sanity check
  82.         if (vwap < 1 or vwap > 1000) or (btc < 1 and usd < 1) or (fee < 0.001 or fee > 0.01):
  83.             exit()
  84.  
  85.         print "-- Balance --"
  86.         print "BTC: %f USD: %f vwap: %f last: %f fee: %f interest: %f" % (btc, usd, vwap, last, fee, interest)
  87.  
  88.         order_type = 0
  89.         order_id = '';
  90.  
  91.         best = last
  92.  
  93.         if abs(1 - last / vwap) > 0.1:
  94.             best = vwap
  95.  
  96.         if btc * best < usd:
  97.             print "-- Buying --"
  98.             order_type = 0
  99.             buy_coef = 1.0 - fee - interest
  100.             price = best * buy_coef
  101.             amount = usd / price
  102.             print "placing order: buy %f @ %f (%f * %f) => %f" % (amount, price, best, buy_coef, amount * price)
  103.             if ORDER:
  104.                 order_id = self.r.perform("buyBTC.php", {amount: amount, price: price})["oid"];
  105.  
  106.         else:
  107.             print "-- Selling --"
  108.             order_type = 1
  109.             sell_coef = 1.0 + fee + interest
  110.             price = best * sell_coef
  111.             amount = btc
  112.             print "placing order: sell %f @ %f (%f * %f) => %f" % (amount, price, best, sell_coef, amount * price)
  113.             if ORDER:
  114.                 order_id = self.r.perform("sellBTC.php", {amount: amount, price: price})["oid"];
  115.  
  116.         return {"btc" : btc, "usd" : usd, "avg" : vwap,
  117.             "amount" : amount, "price" : price, "type": order_type, "oid" : order_id }
  118.  
  119. def main():
  120.     bot = SimpleBot()
  121.     bot.process()
  122.  
  123. if __name__ == "__main__":
  124.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement