def getTrades(filepath): header = True file = open(filepath, 'rb') for row in csv.reader(file.read().splitlines(), delimiter=','): if not header: try: date_str = row[10].split("/") today = date(int(date_str[2]), int(date_str[0]), int(date_str[1])) trade = Trade() trade.account = row[0] trade.symbol = row[1] trade.securityType = row[2] trade.side = row[3] trade.quantity = row[4] trade.price = row[5] trade.route = row[6] trade.destination = row[7] trade.liqFlag = row[9] trade.tradeDate = today trade.executionId = row[11] trade.save() # save into database except Exception, e: print str(e.message) continue else: header = False
def upload(request): if request.POST: try: file = request.FILES['content'] count = 1 for row in csv.reader(file.read().splitlines(), delimiter=','): if count != 1: # Ignore the header row try: Trade.objects.get( executionId = row[8] ) # ignore existed trade, identify by execution id except Trade.DoesNotExist: trade = Trade() trade.account = row[0] trade.symbol = row[1] trade.side= row[2] trade.quantity = row[3] trade.price = row[4] trade.broker = row[5] trade.tradeDate = row[6] trade.exchange = row[7] trade.executionId = row[8] trade.save() else: count += 1 except Exception, e: print e.message return HttpResponseRedirect("/trade/")
def getTradesByDir(path): print "Getting trades record from files..." filelist = os.listdir(path) filelist.sort() log = open(ERROR_LOG, "a") for filename in filelist: # each file represent one day if string.find(filename, ".DS_Store"): continue filepath = os.path.join(path, filename) print filepath file = open(filepath, 'rb') header = True for row in csv.reader(file.read().splitlines(), delimiter=','): # all marks in this file if not header: try: date_str = row[10].split("/") today = date(int(date_str[2]), int(date_str[0]), int(date_str[1])) trade = Trade() trade.account = row[0] trade.symbol = row[1] trade.securityType = row[2] trade.side = row[3] trade.quantity = row[4] trade.price = row[5] trade.route = row[6] trade.destination = row[7] trade.liqFlag = row[9] trade.tradeDate = today trade.executionId = row[11] trade.save() # save into database except Exception, e: print str(e.message) log.write( strftime("%Y-%m-%d %H:%M:%S", time.localtime()) ) log.write( "\tGet trades file from %s failed: %s\n" % (filename, str(e.message)) ) continue else: header = False file.close()
def getReport(today): # filepath = getTradeFile(today) # if filepath == None: # return False print "Getting reports..." filepath = './temp/WBPT_LiquidEOD_2013_01_07.csv' file = open(filepath, 'rb') log = open(ERROR_LOG, "a") refreshReports(today) # create header = True for row in csv.reader(file.read().splitlines(), delimiter=','): if not header: try: date_str = row[10].split("/") today = date(int(date_str[2]), int(date_str[0]), int(date_str[1])) trade = Trade() trade.account = row[0] trade.symbol = row[1] trade.securityType = row[2] trade.side = row[3] trade.quantity = row[4] trade.price = row[5] trade.route = row[6] trade.destination = row[7] trade.liqFlag = row[9] trade.tradeDate = today trade.executionId = row[11] new_report = newReport(trade.account, trade.symbol, today) # get report # update report if trade.side == "BUY": total = new_report.buys * new_report.buyAve total += trade.quantity * trade.price # new total new_report.buys += trade.quantity # new buys new_report.buyAve = total / new_report.buys # new buy ave elif trade.side == "SEL" or trade.side == "SS": total = new_report.sells * new_report.sellAve total += trade.quantity * trade.price # new total new_report.sells += trade.quantity # new sells new_report.sellAve = total / new_report.sells # new sell ave else: print "Error: Invalid Side." continue trade.save() # save into database new_report.save() # save result except Exception, e: print str(e.message) log.write( strftime("%Y-%m-%d %H:%M:%S", time.localtime()) ) log.write("\tGet report Failed: %s\n" % str(e.message)) continue else: header = False