def assessFitness(self): trade_at_open = True fitnessess = [] for (d,date) in enumerate(trading_days): # Make up some VWAP vol profiles buying = buyings[d] volProfiles = [] if trade_at_open: filename = os.getcwd() + '/tickData/' + date.strftime('%Y%m%d') + ticker + 'open.csv' startTrading = datetime.datetime.combine(date, datetime.time(13, 30, 0)) endTrading = datetime.datetime.combine(date, datetime.time(14, 0, 0)) volProfiles.append(VolWindow(datetime.time(13, 30, 0), datetime.time(13, 35, 0))) volProfiles[0].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 35, 0), datetime.time(13, 40, 0))) volProfiles[1].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 40, 0), datetime.time(13, 45, 0))) volProfiles[2].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 45, 0), datetime.time(13, 50, 0))) volProfiles[3].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 50, 0), datetime.time(13, 55, 0))) volProfiles[4].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 55, 0), datetime.time(14, 00, 0))) volProfiles[5].volProfile = 5 else: filename = os.getcwd() + '/tickData/' + date.strftime('%Y%m%d') + ticker + 'close.csv' startTrading = datetime.datetime.combine(date, datetime.time(13, 30, 0)) endTrading = datetime.datetime.combine(date, datetime.time(14, 0, 0)) volProfiles.append(VolWindow(datetime.time(13, 30, 0), datetime.time(13, 35, 0))) volProfiles[0].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 35, 0), datetime.time(13, 40, 0))) volProfiles[1].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 40, 0), datetime.time(13, 45, 0))) volProfiles[2].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 45, 0), datetime.time(13, 50, 0))) volProfiles[3].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 50, 0), datetime.time(13, 55, 0))) volProfiles[4].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 55, 0), datetime.time(14, 00, 0))) volProfiles[5].volProfile = 5 theVWAP = VWAP(startTrading,endTrading,BINSIZE) trading_session = Trading(buying, date, startTrading, endTrading, volProfiles, filename, open_prices[d], self.genotype) trading_session.trade() trades = trading_session.trades trade_prices = [x[1] for x in trades] theVWAP.addDatapointFile(filename, False) the_vwaps = theVWAP.getBinVWAPS() for (i,price) in enumerate(trade_prices): if price==None: fitnessess.append(0.0) else: if trading_session.buying: f = (the_vwaps[i]-price)/float(the_vwaps[i]) else: f = (price-the_vwaps[i])/float(the_vwaps[i]) fitnessess.append(1+f) self.fitness = (sum(fitnessess)/float(len(fitnessess)))
if __name__ == "__main__": import sys args = sys.argv[1:] if len(args) != 1: print( "This script requires exactly one command-line argument. Killing") exit(1) import Trading exit_code = Trading.run(args[0]) or 0 exit(exit_code) else: raise ImportError("Run this file directly, don't import it!")
if len(sys.argv) >= 2: startPoint = int(sys.argv[1]) if len(sys.argv) == 3: endPoint = int(sys.argv[2]) print("startPoint = %d, endPoint = %d" % (startPoint, endPoint)) historyData = loadData(startPoint, endPoint) scene = ChartScene() view = SceneView(scene) mainWindow = MainWindow(view) mainWindow.show() mainWindow.setKeyPressAction(keyPress) trading = Trading(scene) def setStatusCursorCoordinates(point): x = -1 for row in historyData: if row['num'] == int(point.x()): x = row['time'] break if x == -1: mainWindow.displayCoordinate(0, 0) return mainWindow.displayCoordinate(x, point.y())
trading.push(row['time'], row['price']) pos += 1 startPoint = 0 endPoint = 0 if len(sys.argv) > 1: if len(sys.argv) >= 2: startPoint = int(sys.argv[1]) if len(sys.argv) == 3: endPoint = int(sys.argv[2]) print("startPoint = %d, endPoint = %d" % (startPoint, endPoint)) historyData = loadData(startPoint, endPoint) scene = ChartScene() view = SceneView(scene) mainWindow = MainWindow(view) mainWindow.show() mainWindow.setKeyPressAction(keyPress) trading = Trading(scene) pos = 0 for row in historyData: trading.push(row['time'], row['price']) pos += 1 if pos > 1000: break
startTrading = datetime.datetime.combine(date, datetime.time(13, 30, 0)) endTrading = datetime.datetime.combine(date, datetime.time(14, 0, 0)) volProfiles.append(VolWindow(datetime.time(13, 30, 0), datetime.time(13, 35, 0))) volProfiles[0].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 35, 0), datetime.time(13, 40, 0))) volProfiles[1].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 40, 0), datetime.time(13, 45, 0))) volProfiles[2].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 45, 0), datetime.time(13, 50, 0))) volProfiles[3].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 50, 0), datetime.time(13, 55, 0))) volProfiles[4].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 55, 0), datetime.time(14, 00, 0))) volProfiles[5].volProfile = 5 trading_session = Trading(buying, date, startTrading, endTrading, volProfiles, filename, open_prices[d], elite_genome) trading_session.trade() trades = trading_session.trades trade_times = [x[0] for x in trades] trade_prices = [x[1] for x in trades] crappyvolProfiles = [] ################ For crappy crappyvolProfiles.append(VolWindow(datetime.time(13, 30, 0), datetime.time(13, 35, 0))) crappyvolProfiles[0].volProfile = 5 crappyvolProfiles.append(VolWindow(datetime.time(13, 35, 0), datetime.time(13, 40, 0))) crappyvolProfiles[1].volProfile = 5 crappyvolProfiles.append(VolWindow(datetime.time(13, 40, 0), datetime.time(13, 45, 0))) crappyvolProfiles[2].volProfile = 5
volProfiles.append(VolWindow(datetime.time(13, 30, 0), datetime.time(13, 35, 0))) volProfiles[0].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 35, 0), datetime.time(13, 40, 0))) volProfiles[1].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 40, 0), datetime.time(13, 45, 0))) volProfiles[2].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 45, 0), datetime.time(13, 50, 0))) volProfiles[3].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 50, 0), datetime.time(13, 55, 0))) volProfiles[4].volProfile = 5 volProfiles.append(VolWindow(datetime.time(13, 55, 0), datetime.time(14, 00, 0))) volProfiles[5].volProfile = 5 # trade mornTrading = Trading(True, date, startTrading, endTrading, volProfiles, filename, oPrices[i]) mornTrading.trade() # skim through file to work out period VWAP pqSum = 0 qSum = 0 try: reader = open(filename, 'rU') for line in reader: split = line.split(',') if (split[1] == 'TRADE'): pqSum += int(split[3]) * float(split[2]) qSum += int(split[3]) reader.close() expVwap = (pqSum / float(qSum)) except IOError: