####################################
# Basic Mt.Gox Trader Bot #
# ----------------------- #
# Feel free to change whatever! #
# #
# Always bet on red #
# Mt.Red #
####################################
import urllib
import urllib2
import time
USER = '***CHANGE_ME***' # username
PASS = '***CHANGE_ME_ALSO***' # password
BASEURL = 'https://mtgox.com/code/' # base URL (don't change this)
SLEEPTIME = 3 # sleep time (seconds) between looping
SELLTHRESHOLD = 20 # if the sell goes above this, then sell bitcoins (greater than buy threshold!)
BUYTHRESHOLD = 18 # if the buy goes below this, buy bitcoins (less than sell threshold!)
BUYDIFF = 0.01 # add this to my buy rate when buying
SELLDIFF = -0.01 # add this to my sell rate when selling
def main():
while True:
sell()
buy()
time.sleep(SLEEPTIME)
def buy():
if canBuy():
print "Buying " + str(getBuyBTC()) + " BTC for " + str(getBuyUSD()) + " USD"
#buyBTC(getBuyBTC(),getBuyUSD())
def canBuy():
return noOrders() and haveUSD() and canBuyOne() and belowBuyThreshold()
def haveUSD():
return getUSD() > 0
def belowBuyThreshold():
return getBuy() < BUYTHRESHOLD
def getBuyUSD():
return getBuy() + BUYDIFF
def getBuyBTC():
return int(getUSD()/getBuyUSD())
def sell():
if canSell():
print "Selling " + str(getSellBTC()) + " BTC for " + str(getSellUSD()) + " USD"
#sellBTC(getSellBTC(),getSellUSD())
def getSellUSD():
return getSell() + SELLDIFF
def getSellBTC():
return int(getBTC())
def canSell():
return noOrders() and haveBTC() and aboveSellThreshold()
def canBuyOne():
return getUSD() > getBuyUSD()
def aboveSellThreshold():
return getSell() > SELLTHRESHOLD
def haveBTC():
return getBTC() > 0
def noOrders():
return getNumOrders() == 0
def printInfo():
print "##############"
print "# BTC: " + str(getBTC())
print "# USD: " + str(getUSD())
print "# High: " + str(getHigh())
print "# Low: " + str(getLow())
print "# Vol: " + str(getVol())
print "# Buy: " + str(getBuy())
print "# Sell: " + str(getSell())
print "##############"
def getLast():
return getTickerData()['last']
def getSell():
return getTickerData()['sell']
def getBuy():
return getTickerData()['buy']
def getVol():
return getTickerData()['vol']
def getLow():
return getTickerData()['low']
def getHigh():
return getTickerData()['high']
def getTickerData():
return getPlainURL('data/ticker.php')['ticker']
def getPlainURL(url):
url = BASEURL + url
f = urllib2.urlopen(url)
return eval(f.read())
def buyBTC(btc, usd):
return getURLWithParams('buyBTC.php', {'amount':btc,'price':usd})
def sellBTC(btc, usd):
return getURLWithParams('sellBTC.php', {'amount':btc,'price':usd})
def cancelOrder(oid):
order = getOrder(oid)
if order == None:
return
return getURLWithParams('cancelOrder.php', {'oid':oid,'type':order['type']})
def getOrder(oid):
for order in getOrders():
if order['oid'] == oid:
return order
return None
def getNumOrders():
return len(getOrders())
def getOrders():
return getURL('getOrders.php')['orders']
def getBTC():
return getFunds()['btcs']
def getUSD():
return getFunds()['usds']
def getFunds():
return getURL('getFunds.php')
def getURL(url):
url = BASEURL + url
f = urllib2.urlopen(url,getCreds())
return eval(f.read())
def getURLWithParams(url, dict):
url = BASEURL + url
dict.update(getCredsDict())
f = urllib2.urlopen(url,getParams(dict))
return eval(f.read())
def getCreds():
return getParams(getCredsDict())
def getParams(dict):
return urllib.urlencode(dict)
def getCredsDict():
return {'name':USER, 'pass':PASS}
if __name__ == '__main__':
main()