Ejemplo n.º 1
0
def save_trades(market,trades):    
    last_update = get_latest_trade_time(market) 
    print last_update
       
    for data in trades:
        if (market == 'MTgox'):
            time = int(data['time'])
            trade = MtgoxTrade()
            trade.tid = data['tid']
            #return 
        elif (market == '796futures'):  
            time = int(data['time'])
            trade = Futures796Trade()
        elif (market == '796stockpd'):
            time = int(data['time'])
            trade = Stockpd796Trade()          
        
        
        if (int(time)>int(last_update)):
            trade.time = time
            trade.price = float(data['price'])
            trade.amount = float(data['amount'])
            trade.type = data['type']
            trade.save()
Ejemplo n.º 2
0
def main(start, end):
    start = get_unixtime(start, input_dateformat)
    end = get_unixtime(end, input_dateformat)
    if end < start:
        print "End timestamp must be later than start timestamp. Exiting"
        sys.exit()
    print "Will get trades from ", start, "to", end
    """ read the output file and adjust the start date, if it exists
    """
    #    try:
    #        with open(outfile_name, "r") as in_file:
    #            goxdata = in_file.readlines()
    #            saved_start=get_unixtime(goxdata[0].split(",")[0], input_dateformat)
    #            saved_end=get_unixtime(goxdata[len(goxdata)-1].split(",")[0], input_dateformat)
    #
    #            print "File found, with start date:", saved_start, "and end date", saved_end
    #            if start < saved_end:
    #                print "Adjusted start time from ", start, "to ", saved_end
    #                start = saved_end
    #    except IOError:
    #        print "Output file not found. Will create a new one."
    """ get data from MtGox in chunks
    """
    try:
        currstart = start
        endreached = False
        while endreached == False:
            # populate the trades dictionary with the next batch of data
            data = fetch_data(currstart)
            print "Fetching data", currstart
            if (data == '[]'):
                print 'Empty data'
                break
            trades = [mtgox_trade(a) for a in json.loads(data)]
            currstart = trades[-1].timestamp

            if trades[-1].timestamp > end:
                endreached = True

            # place trades into the out_file before getting the next batch from MtGox
            # so that if the program gets interrupt you have saved the trades obtained so far
            with open(outfile_name, "a") as out_file:
                for item in trades:
                    # when you request data from a timestamp gox truncates your start time to seconds and then
                    # send you everything including the initial second. So you must filter here trades
                    # of the start_time second that are already in the database.
                    if item.timestamp > start and item.timestamp < end:
                        #out_file.write(item.trade_to_string()+"\n")
                        print item
                        from api.models import MtgoxTrade
                        mtgox = MtgoxTrade()
                        mtgox.amount = item.amount
                        mtgox.time = item.time
                        mtgox.price = item.price
                        mtgox.type = item.trade_type
                        mtgox.tid = item.timestamp
                        #print mtgox.tid
                        mtgox.save()

    except urllib2.HTTPError, e:
        print "Error:", str(e.code), str(e.reason)
        return