# Find first valid row for fills and orders via basic data validation fill_first_row = True order_first_row = True while order_first_row: try: order_row = od.order_basic_check(reader_orders.next()) order_first_row = False except: pass time_format = '%m/%d/%y %I:%M:%S %p' order_time = order_row['timestamp'] while fill_first_row: try: fill_row = od.fill_basic_check(reader_fills.next()) fill_time = fill_row['timestamp'] if order_time<fill_time: fill_first_row = False else: print "fill %s arrived before first order of the day" % fill_time except: pass # Function to process one day fills_continue = True orders_continue = True def process_day(reader_fills,reader_orders,fill_row,order_row,fills_continue,orders_continue): time_format = '%m/%d/%y %I:%M:%S %p' fill_time = fill_row['timestamp'] order_time = order_row['timestamp']
def process_day(reader_fills,reader_orders,fill_row,order_row,fills_continue,orders_continue): time_format = '%m/%d/%y %I:%M:%S %p' fill_time = fill_row['timestamp'] order_time = order_row['timestamp'] # Initialize today so that can check each day against it # Stop processing when the day is over today = fill_time.date() # Initialize exchange object exch = exchange.exchange() # Fills that occur before first order of day are not valid # Skip and print error while fill_time < order_time and fill_time.date() == today: print "fill %s arrived before first order of day" % fill_row['timestamp'] fill_row = reader_fills.next() fill_time = dt.datetime.strptime(fill_row['timestamp'],time_format) # While today and not end of csv first try to process to exchange # Then read next line in fill/order file that is chronologically behind the other # run a basic data check and loop # read until one file ends or begin new day while fills_continue and orders_continue and fill_time.date() == today and order_time.date() == today: if order_time <= fill_time: try: exch.process_order(order_row) except: pass try: order_row = reader_orders.next() try: order_row = od.order_basic_check(order_row) if order_time < order_row['timestamp']: order_time = order_row['timestamp'] except: pass except: print "end of orders" order_time = None orders_continue = False elif order_time > fill_time: try: exch.process_fill(fill_row) except: pass try: fill_row = reader_fills.next() try: fill_row = od.fill_basic_check(fill_row) if fill_time < fill_row['timestamp']: fill_time = fill_row['timestamp'] except: pass except: print "end of fills" fill_time = None fills_continue = False # If order file terminated first loop, finish fill file while fills_continue and fill_time.date() == today: try: exch.process_fill(fill_row) except: pass try: fill_row = reader_fills.next() try: fill_row = od.fill_basic_check(fill_row) if fill_time < fill_row['timestamp']: fill_time = fill_row['timestamp'] except: pass except: print "end of fills" fill_time = None fills_continue = False # If fill file terminated first loop, finish order file while orders_continue and order_time.date() == today: try: exch.process_order(order_row) except: pass try: order_row = reader_orders.next() try: order_row = od.order_basic_check(order_row) if order_time < order_row['timestamp']: order_time = order_row['timestamp'] except: pass except: print "end of orders" order_time = None orders_continue = False # Print ordres still open at the end of day exch.check_all_close_eod() # Return csv handler along with continuation conditions and next row as a tuple # Need to return the row because the the csvreader.next() returns row and increments return (reader_fills,reader_orders,fill_row,order_row,fills_continue,orders_continue)
def process_day(reader_fills,reader_orders,fill_row,order_row,fills_continue,orders_continue): time_format = '%m/%d/%y %I:%M:%S %p' fill_time = fill_row['timestamp'] order_time = order_row['timestamp'] today = fill_time.date() exch = exchange.exchange() # Fills that occur before first order of day are not valid while fill_time < order_time: print "fill %s arrived before first order of day" % fill_row['timestamp'] fill_row = reader_fills.next() fill_time = dt.datetime.strptime(fill_row['timestamp'],time_format) while fills_continue and orders_continue and fill_time.date() == today and order_time.date() == today: # read next row based on which row is behind the other if order_time <= fill_time: try: exch.process_order(order_row) except Exception as msg: print msg try: order_row = reader_orders.next() # print order_row['id'] try: order_row = od.order_basic_check(order_row) order_time = order_row['timestamp'] except Exception as msg: print msg # print order_row['id'] except: print "end of orders" order_time = None orders_continue = False elif order_time > fill_time: exch.process_fill(fill_row) try: fill_row = reader_fills.next() try: fill_row = od.fill_basic_check(fill_row) fill_time = fill_row['timestamp'] except Exception as msg: print msg # print fill_row['id'] except: print "end of fills" fill_time = None fills_continue = False # Now finish the longer list while fills_continue and fill_time.date() == today: exch.process_fill(fill_row) try: fill_row = reader_fills.next() try: fill_row = od.fill_basic_check(fill_row) fill_time = fill_row['timestamp'] except Exception as msg: print msg # print fill_row['id'] except: print "end of fills" fill_time = None fills_continue = False while orders_continue and order_time.date() == today: exch.process_order(order_row) try: order_row = reader_orders.next() try: order_row = od.order_basic_check(order_row) order_time = order_row['timestamp'] except Exception as msg: print msg # print order_row['id'] except: print "end of orders" order_time = None orders_continue = False # Check to see that the order log is esmpty at the end of a day exch.check_all_close_eod() return (reader_fills,reader_orders,fill_row,order_row,fills_continue,orders_continue)