Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pytrader
- import urllib
- import urllib2
- import simplejson as json
- class Market:
- """ Represents the current state of the mtgox market. """
- def __init__(self, upencoded, user, password):
- self.ask = 0 #sell
- self.bid = 0 #buy
- self.last = 0
- self.vol = 0
- self.dayhigh = 0
- self.daylow = 0
- self.bids = None
- self.asks = None
- self.orders = []
- self.upencoded = upencoded
- self.user = user
- self.password = password
- def refresh(self):
- """ Update the market with current data. Can be slow due to network
- latency """
- f = urllib2.urlopen(pytrader.mtgoxticker)
- parsed = json.load(f)['ticker']
- self.ask = parsed['sell']
- self.bid = parsed['buy']
- self.last = parsed['last']
- self.vol = parsed['vol']
- self.dayhigh = parsed['high']
- self.daylow = parsed['low']
- #refresh all our orders
- f = urllib2.urlopen(pytrader.mtgoxorders, self.upencoded)
- parsed = json.load(f)['orders']
- upoids = []
- for o in parsed:
- self.add_update_order(o)
- upoids.append(int(o['oid']))
- #now check if there is an oid that wasn't updated
- for o in self.orders:
- if o.oid not in upoids:
- o.complete()
- self.orders.remove(o)
- print "AN ORDER HAS BEEN COMPLETED"
- def add_update_order(self, order):
- new = 1
- oid = int(order['oid'])
- for o in self.orders:
- #order already exists
- if oid == o.oid:
- new = 0
- #update the actual order
- o.update(order)
- #order is cancelled
- if o.oid == -1:
- self.orders.remove(o)
- if new == 1:
- o = Order(order)
- self.orders.append(o)
- return o
- def refresh_depth(self):
- """ Refreshes the depth of the market. We don't refresh this every tick
- because it takes a while to load. """
- f = urllib2.urlopen(pytrader.mtgoxdepth)
- parsed = json.load(f)
- self.bids = parsed['bids']
- self.asks = parsed['asks']
- def buy(self, amount=0, price=0):
- """ Put in an order for amount bitcoins at a certain price. """
- orders = []
- encoded = urllib.urlencode({'name':self.user, 'pass':self.password,
- 'amount':amount, 'price':price})
- f = urllib2.urlopen(pytrader.mtgoxbuy, encoded)
- parsed = json.load(f)
- pytrader.status("Placed a buy order for " + str(amount) + "BTC @ $" + str(price) + ".")
- order = None
- for o in parsed['orders']:
- order = self.add_update_order(o)
- if order:
- orders.append(order)
- try:
- for s in parsed['trades']:
- pytrader.status(s)
- except KeyError:
- pass
- return orders
- def find_order(self, amount, price, type):
- for o in self.orders:
- # it is possible for multiple orders to have the same amount &
- # price, but they are equivalent for our purposes
- if (o.amount == amount) and (o.price == price) and (o.type ==
- type):
- return o
- return None
- def spread(self):
- """ Compute the current market spread(bid-ask)"""
- return abs(self.ask - self.bid)
- def pprint(self):
- print "Market Stats:"
- print "Ask: " + str(self.ask)
- print "Bid: " + str(self.bid)
- print "Last: " + str(self.last)
- print "Volume: " + str(self.vol)
- print "High: " + str(self.dayhigh)
- print "Low: " + str(self.daylow)
- def pprint_spread(self):
- pytrader.status("Spread: " + str(self.spread()))
- def pprint_last(self):
- pytrader.status("Last trade: " + "$" + str(self.last) + "/BTC")
- class Order:
- """ Represents an order on the Mt Gox exchange. """
- def __init__(self, order):
- self.completed = False
- self.update(order)
- def update(self, order):
- self.status = order['status']
- self.price = order['price']
- self.oid = int(order['oid'])
- self.dark = int(order['dark'])
- self.amount = order['amount']
- self.date = order['date']
- self.type = order['type']
- def cancel(self):
- encoded = urllib.urlencode({'name':self.user, 'pass':self.password,
- 'oid':self.oid, 'type':self.type})
- f = urllib2.urlopen(pytrader/mtgoxcancel, encoded)
- pytrader.status("Order " + str(self.oid) + "canceled")
- #ensure any double-cancel will not work
- self.oid = -1
- def value(self):
- return self.amount * self.price
- def pprint(self):
- print "Order " + str(self.oid) + ":"
- print "Amount: " + str(self.amount)
- print "Price: " + str(self.price)
- print "Type: " + str(self.type)
- print "Value:",
- if self.type == 2:
- print "$" + str(self.value())
- else:
- print str(self.value()) + "BTC"
- def complete(self):
- self.completed = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement