Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.26 KB | None | 0 0
  1. import pytrader
  2. import urllib
  3. import urllib2
  4. import simplejson as json
  5.  
  6. class Market:
  7.     """ Represents the current state of the mtgox market. """
  8.     def __init__(self, upencoded, user, password):
  9.         self.ask = 0 #sell
  10.         self.bid = 0 #buy
  11.         self.last = 0
  12.         self.vol = 0
  13.         self.dayhigh = 0
  14.         self.daylow = 0
  15.         self.bids = None
  16.         self.asks = None
  17.         self.orders = []
  18.         self.upencoded = upencoded
  19.         self.user = user
  20.         self.password = password
  21.  
  22.     def refresh(self):
  23.         """ Update the market with current data. Can be slow due to network
  24.        latency """
  25.         f = urllib2.urlopen(pytrader.mtgoxticker)
  26.         parsed = json.load(f)['ticker']
  27.  
  28.         self.ask = parsed['sell']
  29.         self.bid = parsed['buy']
  30.         self.last = parsed['last']
  31.         self.vol = parsed['vol']
  32.         self.dayhigh = parsed['high']
  33.         self.daylow = parsed['low']
  34.  
  35.         #refresh all our orders
  36.         f = urllib2.urlopen(pytrader.mtgoxorders, self.upencoded)
  37.         parsed = json.load(f)['orders']
  38.  
  39.         upoids = []
  40.         for o in parsed:
  41.             self.add_update_order(o)
  42.             upoids.append(int(o['oid']))
  43.  
  44.         #now check if there is an oid that wasn't updated
  45.  
  46.         for o in self.orders:
  47.             if o.oid not in upoids:
  48.                 o.complete()
  49.                 self.orders.remove(o)
  50.                 print "AN ORDER HAS BEEN COMPLETED"
  51.  
  52.     def add_update_order(self, order):
  53.         new = 1
  54.         oid = int(order['oid'])
  55.  
  56.         for o in self.orders:
  57.             #order already exists
  58.             if oid == o.oid:
  59.                     new = 0
  60.                     #update the actual order
  61.                     o.update(order)
  62.  
  63.             #order is cancelled
  64.             if o.oid == -1:
  65.                 self.orders.remove(o)
  66.  
  67.         if new == 1:
  68.             o = Order(order)
  69.             self.orders.append(o)
  70.             return o
  71.  
  72.  
  73.     def refresh_depth(self):
  74.         """ Refreshes the depth of the market. We don't refresh this every tick
  75.        because it takes a while to load. """
  76.         f = urllib2.urlopen(pytrader.mtgoxdepth)
  77.         parsed = json.load(f)
  78.         self.bids = parsed['bids']
  79.         self.asks = parsed['asks']
  80.  
  81.     def buy(self, amount=0, price=0):
  82.         """ Put in an order for amount bitcoins at a certain price. """
  83.         orders = []
  84.  
  85.         encoded = urllib.urlencode({'name':self.user, 'pass':self.password,
  86.             'amount':amount, 'price':price})
  87.  
  88.         f = urllib2.urlopen(pytrader.mtgoxbuy, encoded)
  89.         parsed = json.load(f)
  90.  
  91.         pytrader.status("Placed a buy order for " + str(amount) + "BTC @ $" + str(price) + ".")
  92.  
  93.         order = None
  94.         for o in parsed['orders']:
  95.             order = self.add_update_order(o)
  96.             if order:
  97.                 orders.append(order)
  98.  
  99.         try:
  100.             for s in parsed['trades']:
  101.                 pytrader.status(s)
  102.         except KeyError:
  103.             pass
  104.  
  105.         return orders
  106.  
  107.  
  108.     def find_order(self, amount, price, type):
  109.         for o in self.orders:
  110.             # it is possible for multiple orders to have the same amount &
  111.             # price, but they are equivalent for our purposes
  112.  
  113.             if (o.amount == amount) and (o.price == price) and (o.type ==
  114.                     type):
  115.                 return o
  116.  
  117.         return None
  118.  
  119.     def spread(self):
  120.         """ Compute the current market spread(bid-ask)"""
  121.         return abs(self.ask - self.bid)
  122.  
  123.     def pprint(self):
  124.         print "Market Stats:"
  125.         print "Ask: " + str(self.ask)
  126.         print "Bid: " + str(self.bid)
  127.         print "Last: " + str(self.last)
  128.         print "Volume: " + str(self.vol)
  129.         print "High: " + str(self.dayhigh)
  130.         print "Low: " + str(self.daylow)
  131.  
  132.     def pprint_spread(self):
  133.         pytrader.status("Spread: " + str(self.spread()))
  134.  
  135.     def pprint_last(self):
  136.         pytrader.status("Last trade: " + "$" + str(self.last) + "/BTC")
  137.  
  138. class Order:
  139.     """ Represents an order on the Mt Gox exchange. """
  140.     def __init__(self, order):
  141.         self.completed = False
  142.         self.update(order)
  143.  
  144.     def update(self, order):
  145.         self.status = order['status']
  146.         self.price = order['price']
  147.         self.oid = int(order['oid'])
  148.         self.dark = int(order['dark'])
  149.         self.amount = order['amount']
  150.         self.date = order['date']
  151.         self.type = order['type']
  152.  
  153.     def cancel(self):
  154.         encoded = urllib.urlencode({'name':self.user, 'pass':self.password,
  155.             'oid':self.oid, 'type':self.type})
  156.  
  157.         f = urllib2.urlopen(pytrader/mtgoxcancel, encoded)
  158.  
  159.         pytrader.status("Order " + str(self.oid) + "canceled")
  160.         #ensure any double-cancel will not work
  161.         self.oid = -1
  162.  
  163.     def value(self):
  164.         return self.amount * self.price
  165.  
  166.     def pprint(self):
  167.         print "Order " + str(self.oid) + ":"
  168.         print "Amount: " + str(self.amount)
  169.         print "Price: " + str(self.price)
  170.         print "Type: " + str(self.type)
  171.         print "Value:",
  172.  
  173.         if self.type == 2:
  174.             print "$" + str(self.value())
  175.         else:
  176.             print str(self.value()) + "BTC"
  177.  
  178.     def complete(self):
  179.         self.completed = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement