Don't like ads? PRO users don't see any ads ;-)
Guest

Basic Mt.Gox Trader

By: a guest on Jun 14th, 2011  |  syntax: Python  |  size: 4.14 KB  |  hits: 274  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ####################################
  2. #     Basic Mt.Gox Trader Bot      #
  3. #     -----------------------      #
  4. #   Feel free to change whatever!  #
  5. #                                  #
  6. #       Always bet on red          #
  7. #             Mt.Red               #
  8. ####################################
  9.  
  10. import urllib
  11. import urllib2
  12. import time
  13.  
  14. USER = '***CHANGE_ME***'               # username
  15. PASS = '***CHANGE_ME_ALSO***'          # password
  16. BASEURL = 'https://mtgox.com/code/'    # base URL (don't change this)
  17. SLEEPTIME = 3                          # sleep time (seconds) between looping
  18. SELLTHRESHOLD = 20                     # if the sell goes above this, then sell bitcoins (greater than buy threshold!)
  19. BUYTHRESHOLD = 18                      # if the buy goes below this, buy bitcoins (less than sell threshold!)
  20. BUYDIFF = 0.01                         # add this to my buy rate when buying
  21. SELLDIFF = -0.01                       # add this to my sell rate when selling
  22.  
  23.  
  24. def main():
  25.   while True:
  26.     sell()
  27.     buy()
  28.     time.sleep(SLEEPTIME)
  29.  
  30. def buy():
  31.   if canBuy():
  32.     print "Buying " + str(getBuyBTC()) + " BTC for " + str(getBuyUSD()) + " USD"                                                                                                                                                            
  33.     #buyBTC(getBuyBTC(),getBuyUSD())
  34.  
  35. def canBuy():
  36.   return noOrders() and haveUSD() and canBuyOne() and belowBuyThreshold()
  37.  
  38. def haveUSD():
  39.   return getUSD() > 0
  40.  
  41. def belowBuyThreshold():
  42.   return getBuy() < BUYTHRESHOLD
  43.  
  44. def getBuyUSD():
  45.   return getBuy() + BUYDIFF
  46.  
  47. def getBuyBTC():
  48.   return int(getUSD()/getBuyUSD())
  49.  
  50. def sell():
  51.   if canSell():
  52.     print "Selling " + str(getSellBTC()) + " BTC for " + str(getSellUSD()) + " USD"
  53.     #sellBTC(getSellBTC(),getSellUSD())
  54.  
  55. def getSellUSD():
  56.   return getSell() + SELLDIFF
  57.  
  58. def getSellBTC():
  59.   return int(getBTC())
  60.  
  61. def canSell():
  62.   return noOrders() and haveBTC() and aboveSellThreshold()
  63.  
  64. def canBuyOne():
  65.   return getUSD() > getBuyUSD()
  66.  
  67. def aboveSellThreshold():
  68.   return getSell() > SELLTHRESHOLD
  69.  
  70. def haveBTC():
  71.   return getBTC() > 0
  72.  
  73. def noOrders():
  74.   return getNumOrders() == 0
  75.  
  76. def printInfo():
  77.   print "##############"
  78.   print "# BTC:  " + str(getBTC())
  79.   print "# USD:  " + str(getUSD())
  80.   print "# High: " + str(getHigh())
  81.   print "# Low:  " + str(getLow())
  82.   print "# Vol:  " + str(getVol())
  83.   print "# Buy:  " + str(getBuy())
  84.   print "# Sell: " + str(getSell())
  85.   print "##############"
  86.  
  87. def getLast():
  88.   return getTickerData()['last']
  89.  
  90. def getSell():
  91.   return getTickerData()['sell']
  92.  
  93. def getBuy():
  94.   return getTickerData()['buy']
  95.  
  96. def getVol():
  97.   return getTickerData()['vol']
  98.  
  99. def getLow():
  100.   return getTickerData()['low']
  101.  
  102. def getHigh():
  103.   return getTickerData()['high']
  104.  
  105. def getTickerData():
  106.   return getPlainURL('data/ticker.php')['ticker']
  107.  
  108. def getPlainURL(url):
  109.   url = BASEURL + url
  110.   f = urllib2.urlopen(url)
  111.   return eval(f.read())
  112.  
  113. def buyBTC(btc, usd):
  114.   return getURLWithParams('buyBTC.php', {'amount':btc,'price':usd})
  115.  
  116. def sellBTC(btc, usd):
  117.   return getURLWithParams('sellBTC.php', {'amount':btc,'price':usd})
  118.  
  119. def cancelOrder(oid):
  120.   order = getOrder(oid)
  121.   if order == None:
  122.     return
  123.   return getURLWithParams('cancelOrder.php', {'oid':oid,'type':order['type']})
  124.  
  125. def getOrder(oid):
  126.   for order in getOrders():
  127.     if order['oid'] == oid:
  128.       return order
  129.   return None
  130.  
  131. def getNumOrders():
  132.   return len(getOrders())
  133.  
  134. def getOrders():
  135.   return getURL('getOrders.php')['orders']
  136.  
  137. def getBTC():
  138.   return getFunds()['btcs']
  139.  
  140. def getUSD():
  141.   return getFunds()['usds']
  142.  
  143. def getFunds():
  144.   return getURL('getFunds.php')
  145.  
  146. def getURL(url):
  147.   url = BASEURL + url
  148.   f = urllib2.urlopen(url,getCreds())
  149.   return eval(f.read())
  150.  
  151. def getURLWithParams(url, dict):
  152.   url = BASEURL + url
  153.   dict.update(getCredsDict())
  154.   f = urllib2.urlopen(url,getParams(dict))
  155.   return eval(f.read())
  156.  
  157. def getCreds():
  158.   return getParams(getCredsDict())
  159.  
  160. def getParams(dict):
  161.   return urllib.urlencode(dict)
  162.  
  163. def getCredsDict():
  164.   return {'name':USER, 'pass':PASS}
  165.  
  166. if __name__ == '__main__':
  167.   main()