Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from urllib import urlencode
- import urllib2
- import time
- from hashlib import sha512
- from hmac import HMAC
- import base64
- import json
- import sys
- from urllib2 import URLError
- from datetime import datetime
- from mtgox_config import *
- FETCH = True
- ORDER = False
- def get_nonce():
- return int(time.time()*100000)
- def sign_data(secret, data):
- return base64.b64encode(str(HMAC(secret, data, sha512).digest()))
- class requester:
- def __init__(self, auth_key, auth_secret):
- self.auth_key = auth_key
- self.auth_secret = base64.b64decode(auth_secret)
- def build_query(self, req={}):
- req["nonce"] = get_nonce()
- post_data = urlencode(req)
- headers = {}
- headers["User-Agent"] = "GoxApi"
- headers["Rest-Key"] = self.auth_key
- headers["Rest-Sign"] = sign_data(self.auth_secret, post_data)
- return (post_data, headers)
- def perform(self, path, args):
- data, headers = self.build_query(args)
- req = urllib2.Request("https://mtgox.com/api/0/"+path, data, headers)
- res = urllib2.urlopen(req, data)
- return json.load(res)
- class SimpleBot:
- def __init__(self):
- self.r = requester(MTGOX_API_KEY, MTGOX_API_SECRET)
- pass
- def process(self):
- fee = 0.0055
- interest = 0.0005
- vwap = 3.970505
- btc = 1.702021
- usd = 0.002398
- btc = 0.000001
- usd = 6.892910
- last = 3.970505
- if FETCH:
- trade_data = self.r.perform("getOrders.php", {})
- ticker = self.r.perform("data/ticker.php", {})["ticker"];
- info = self.r.perform("info.php", {});
- btc = float(trade_data["btcs"])
- usd = float(trade_data["usds"])
- last = float(ticker["last"])
- vwap = float(ticker["vwap"])
- fee = float(info['Trade_Fee']) / 100
- orders = trade_data['orders']
- #remove all orders
- for order in orders:
- typestring = "sell" if order['type'] == 1 else "buy"
- print "-- Orders --"
- print typestring, order['amount'], "@", order['price']
- if ORDER:
- self.r.perform("cancelOrder.php", { oid: order["oid"], type: order['type'] });
- #sanity check
- if (vwap < 1 or vwap > 1000) or (btc < 1 and usd < 1) or (fee < 0.001 or fee > 0.01):
- exit()
- print "-- Balance --"
- print "BTC: %f USD: %f vwap: %f last: %f fee: %f interest: %f" % (btc, usd, vwap, last, fee, interest)
- order_type = 0
- order_id = '';
- best = last
- if abs(1 - last / vwap) > 0.1:
- best = vwap
- if btc * best < usd:
- print "-- Buying --"
- order_type = 0
- buy_coef = 1.0 - fee - interest
- price = best * buy_coef
- amount = usd / price
- print "placing order: buy %f @ %f (%f * %f) => %f" % (amount, price, best, buy_coef, amount * price)
- if ORDER:
- order_id = self.r.perform("buyBTC.php", {amount: amount, price: price})["oid"];
- else:
- print "-- Selling --"
- order_type = 1
- sell_coef = 1.0 + fee + interest
- price = best * sell_coef
- amount = btc
- print "placing order: sell %f @ %f (%f * %f) => %f" % (amount, price, best, sell_coef, amount * price)
- if ORDER:
- order_id = self.r.perform("sellBTC.php", {amount: amount, price: price})["oid"];
- return {"btc" : btc, "usd" : usd, "avg" : vwap,
- "amount" : amount, "price" : price, "type": order_type, "oid" : order_id }
- def main():
- bot = SimpleBot()
- bot.process()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement