def maketrade(pair, rate): # parse the pair to determine the starting coin and ending coin # the starting coin is the coin we're depositing to the exchange # the ending coin is the coin we're receiving from the exchange startingcoin = pair.split('_')[0] endingcoin = pair.split('_')[1] # initialize the crypto wrappers startingwallet = electrumapi(startingcoin) endingwallet = electrumapi(endingcoin) # get the balace of the starting coin startingcoinbalance = float(startingwallet.getbalance()) # only initiate a trade if funds are available and all of these funds are confirmed if not startingcoinbalance: print datetime.datetime.now(), ": No balance to trade!" return False # define withdrawal and return addresses # the withdrawal address will be denominated in the ending coin # the return address will be denominated in the starting coin withdrawaladdress = endingwallet.getrxaddress() returnaddress = startingwallet.getrxaddress() # get a deposit address to send funds to exchange = exchangeapi() depositaddress = exchange.getdepositaddress(withdrawaladdress, pair, returnaddress) # determine the amount to send # take the balance and multiply it by the rate for that coin # subtract fee amount txamount = (startingcoinbalance * startingwallet.rate) - float(startingwallet.fee) # log trade info print "--------------------------------------" print datetime.datetime.now() print "Trade Pair:", pair print "Withdrawal Address:", withdrawaladdress print "Return Address:", returnaddress print "Deposit Address:", depositaddress print "Amount:", txamount print "--------------------------------------" # make and broadcast transaction tx = startingwallet.mktx(depositaddress, txamount) if tx: jsontx = json.loads(tx) finalresult = startingwallet.broadcast(jsontx['hex']) # insert the trade into the database datastore = ratedata() datastore.tradeinsert(pair, rate, txamount, depositaddress, withdrawaladdress)
import time import requests from sqlite_class import ratedata # setup db datastore = ratedata() # define api variables base_url = "https://shapeshift.io" pair = "btc_ltc" apirate = "/rate/" + pair while True: try: response = requests.get(base_url + apirate) if response.status_code == 200: content = response.json() for key, value in content.iteritems(): if key == "rate": datastore.rateinsert(pair,value) time.sleep(59) except: pass datastore.dataclose