def Test(TopSpecies, testStart, testEnd): for x in range(1): try: with open('bList.csv', 'rb') as csvfile: balance = [] for row in csvfile: balance.append(float(row)) Investment = balance[-1] except(IndexError): print "yikes" Investment = 100000 stockList=["LUV"] avg=[] mx=[] graph={} g=[] TOPV=0 Graph = [] Environment = Strategy(stockList) numSpecies=1 pInvest=Investment genCount=1 startDay= testStart DAYS= testEnd inputList={} startPriceList={} endPriceList={} #Make Portfolios Environment.addPortfolio("p0",pInvest) Environment.getPortfolios()["p0"] = TopSpecies Environment.getPortfolios()["p0"].reset(Investment) #Set inputList try: inputList = pickle.load( open( "inputListA.p", "rb" ) ) startPriceList = pickle.load( open( "startPriceListA.p", "rb" ) ) endPriceList = pickle.load( open( "endPriceListA.p", "rb" ) ) except(IOError): print "Writing New Pickle" for d in range(startDay,DAYS): if d in inputList: print "Day", d, "Ready" else: print "Adding Day", d Environment.setInputs("LUVBBF3", d, d+1) inputList[d]=(Environment.getInputsF().copy()) startPriceList[d]=Environment.getRecentPrice() endPriceList[d]=Environment.getEndPrice() pickle.dump( inputList, open( "inputListA.p", "wb" ) ) pickle.dump( startPriceList, open( "startPriceListA.p", "wb" ) ) pickle.dump( endPriceList, open( "endPriceListA.p", "wb" ) ) #Make lists for randomization of initial population inputs=inputList[startDay] variables=inputs.keys() right=[[],[]] weight=[] fitness={} delimeters=["<",">"] for valTop in np.arange(-1,1.1,.1): right[0].append(round(valTop,2)) for y in variables: right[1].append(y) for z in np.arange(0,1,.01): weight.append(round(z,3)) #Construct test population # for portfolio in Environment.getPortfolios().keys(): # p = Environment.getPortfolios()[portfolio] # p.addSpecies(inputs) # for gene in TopSpecies: # Environment.getPortfolios()['p0'].getSpecies().addGene(TopSpecies[gene].left,TopSpecies[gene].right, TopSpecies[gene].delimeter, TopSpecies[gene].weight) # Start loop of days and generations for day in range(startDay, DAYS): inputs=inputList[day] startPrice=startPriceList[day] endPrice=endPriceList[day] # Get boolean statements for the day for portfolio in Environment.getPortfolios().keys(): for gene in Environment.getPortfolios()[portfolio].getSpecies().getGenes().keys(): Environment.getPortfolios()[portfolio].getSpecies().getGenes()[gene].makeBoolean(inputs) # Get species output and add corresponding behavior portfolio = "p0" print Environment.getPortfolios()[portfolio].getSpecies().getRightThresh(),"right" Environment.getPortfolios()[portfolio].addBehavior(Environment.getPortfolios()[portfolio].getSpecies().getOutput() > Environment.getPortfolios()[portfolio].getSpecies().getRightThresh() , ("Buy", "LUV",int(Environment.getPortfolios()[portfolio].getShareFactor()*(Environment.getPortfolios()[portfolio].getBalance()/(0.0+startPrice))),startPrice)) Environment.getPortfolios()[portfolio].addBehavior(Environment.getPortfolios()[portfolio].getSpecies().getOutput() < Environment.getPortfolios()[portfolio].getSpecies().getLeftThresh(), ("Short", "LUV",int(Environment.getPortfolios()[portfolio].getShareFactor()*(Environment.getPortfolios()[portfolio].getBalance()/(0.0+startPrice))),startPrice)) Environment.getPortfolios()[portfolio].addGeneCorrect(endPrice-startPrice) #Go through with actions, sellBack at end of day and add balance to balanceList Environment.getPortfolios()[portfolio].makeActions(startPrice) Environment.getPortfolios()[portfolio].sellBack(stockList,endPrice) Environment.getPortfolios()[portfolio].addBalance(Environment.getPortfolios()[portfolio].balance) # for gene in Environment.getPortfolios()["p0"].getGeneCorrect(): # Environment.getPortfolios()["p0"].getGeneCorrect()[gene]=average(Environment.getPortfolios()["p0"].getGeneCorrect()[gene]) # plt.bar(range(len(Environment.getPortfolios()["p0"].getGeneCorrect())), Environment.getPortfolios()["p0"].getGeneCorrect().values(), align='center') # plt.xticks(range(len(Environment.getPortfolios()["p0"].getGeneCorrect())), Environment.getPortfolios()["p0"].getGeneCorrect().keys()) # plt.show() # lsta=[] # lstb=[] # for gene in Environment.getPortfolios()["p0"].getGeneCorrect(): # lsta.append(Environment.getPortfolios()["p0"].getGeneCorrect()[gene]) # lstb.append(Environment.getPortfolios()["p0"].getSpecies().genes[gene].getWeight()) # plt.plot(lsta,lstb,"go") # print(np.corrcoef(lsta,lstb)) # plt.show() #Add balanceList to graph for portfolio in Environment.getPortfolios().keys(): fitness[portfolio]=Environment.getPortfolios()[portfolio].balanceList[-1] sorted_fitness = sorted(fitness.items(), key=operator.itemgetter(1)) if(sorted_fitness[-1][-1]>TOPV): TOP=sorted_fitness[-1][0] TOPV=sorted_fitness[-1][1] print "TEST DAY : ", DAYS print "TEST CORRECT : ", (Environment.getPortfolios()["p0"].getCorrectList().count(1)*1.0)/len(Environment.getPortfolios()["p0"].getCorrectList()) print "TEST INCORRECT : ", (Environment.getPortfolios()["p0"].getCorrectList().count(-1)*1.0)/len(Environment.getPortfolios()["p0"].getCorrectList()) print "TEST HELD : ", (Environment.getPortfolios()["p0"].getCorrectList().count(0)*1.0)/len(Environment.getPortfolios()["p0"].getCorrectList()) print "TEST GUESSED : ", (Environment.getPortfolios()["p0"].getCorrectList().count(1)+Environment.getPortfolios()["p0"].getCorrectList().count(-1)*1.0)/len(Environment.getPortfolios()["p0"].getCorrectList()) print "LEFT THRESH : ", Environment.getPortfolios()[portfolio].getSpecies().getLeftThresh() print "RIGHT THRESH : ", Environment.getPortfolios()[portfolio].getSpecies().getRightThresh() print "SHARE FACTOR : ", Environment.getPortfolios()[portfolio].getShareFactor() print "OUTPUT : ", Environment.getPortfolios()[portfolio].getSpecies().getOutput() print "INVESTMENT : ", Investment print "TEST BALANCE : ", Environment.getPortfolios()["p0"].getBalanceList()[-1] print "DAY CHANGE : ", endPrice-startPrice print "DAY PROFIT : ", Environment.getPortfolios()["p0"].getBalanceList()[-1]-Investment return [(Environment.getPortfolios()["p0"].getCorrectList().count(1)*1.0)/len(Environment.getPortfolios()["p0"].getCorrectList()), Environment.getPortfolios()["p0"].getBalanceList()[-1]]
def Enviro(stockList, Investment, numSpecies, genCount, shareCount, trainStart, trainEnd, testStart, testEnd, SaveNew, geneTest, evolutionPlot): avg = [] mx = [] graph = {} g = [] TOPV = 0 TOP = 0 qg = [] Environment = Strategy(stockList) pInvest = Investment startDay = trainStart DAYS = trainEnd inputList = {} startPriceList = {} endPriceList = {} #Make Portfolios for individual in range(numSpecies): Environment.addPortfolio("p" + str(individual), pInvest) #Set inputList try: inputList = pickle.load(open("inputListA.p", "rb")) startPriceList = pickle.load(open("startPriceListA.p", "rb")) endPriceList = pickle.load(open("endPriceListA.p", "rb")) except (IOError): print "Writing New Pickle" for d in range(startDay, DAYS): if d in inputList: pass else: print "Adding Day", d Environment.setInputs("LUVBBF3", d, d + 1) inputList[d] = (Environment.getInputsF().copy()) startPriceList[d] = Environment.getRecentPrice() endPriceList[d] = Environment.getEndPrice() pickle.dump(inputList, open("inputListA.p", "wb")) pickle.dump(startPriceList, open("startPriceListA.p", "wb")) pickle.dump(endPriceList, open("endPriceListA.p", "wb")) #Make lists for randomization of initial population inputs = inputList[startDay] variables = inputs.keys() right = [[], []] weight = [] fitness = {} delimeters = ["<", ">"] for valTop in np.arange(-1, 1.1, .1): right[0].append(round(valTop, 2)) for y in variables: right[1].append(y) for z in np.arange(0, 1, .01): weight.append(round(z, 3)) #Construct initial population for portfolio in Environment.getPortfolios().keys(): p = Environment.getPortfolios()[portfolio] p.addSpecies(inputs) for variable in Environment.getPortfolios()[portfolio].getSpecies( ).getGenes().keys(): a = random.randint(0, 1) b = random.randint(0, len(right[a]) - 1) c = delimeters[random.randint(0, 1)] d = weight[random.randint(0, 99)] Environment.getPortfolios()[portfolio].getSpecies().addGene( variable, right[a][b], c, d) Environment.getPortfolios()[portfolio].getSpecies().setThresh( random.randrange( -len(Environment.getPortfolios() [portfolio].getSpecies().getGenes()), 1) + 0.0, random.randrange( 0, len(Environment.getPortfolios() [portfolio].getSpecies().getGenes()) + 1) + 0.0) Environment.getPortfolios()[portfolio].getSpecies().setShareFactor( (random.randrange(1, 100))) # Start loop of days and generations manager = mp.Manager() portDict = manager.dict() for gen in range(genCount): portDict.clear() t1 = time.time() print "Generation : ", gen + 1 pList = [] for portfolio in Environment.getPortfolios().keys(): proc = mp.Process(target=MultiProcess1, args=(portDict, Environment, startDay, DAYS, inputList, startPriceList, endPriceList, stockList, portfolio)) pList.append(proc) proc.start() for proc in pList: if proc.is_alive(): proc.join() for port in portDict.keys(): Environment.getPortfolios()[port] = portDict[port] #Get fitness of species for portfolio in Environment.getPortfolios().keys(): #fitness[portfolio]=(Environment.getPortfolios()[portfolio].getCorrectList().count(1))*1.0/len(Environment.getPortfolios()[portfolio].getCorrectList()) fitness[portfolio] = (Environment.getPortfolios( )[portfolio].getCorrectList().count(1)) * 1.0 / len( Environment.getPortfolios()[portfolio].getCorrectList()) # Environment.getPortfolios()[portfolio].balanceList[-1] #Find top 50% sorted_fitness = sorted(fitness.items(), key=operator.itemgetter(1)) if (sorted_fitness[-1][-1] > TOPV): TOP = sorted_fitness[-1][0] TOPV = sorted_fitness[-1][1] #Get bottom 50% bottom = sorted_fitness[0:(numSpecies / 2)] #Choose 50 pairs of top 50 top = sorted_fitness[numSpecies / 2:numSpecies] topPair = [] # for valTop in range(len(top)): # pair=valTop # while top[pair][0]==top[valTop][0]: # pair=random.randint(0,len(top)-1) # topPair.append((top[valTop][0],top[pair][0])) pairs = [] for valTop in range(len(top)): for val2 in range(valTop + 1, len(top)): pairs.append((top[valTop][0], top[val2][0])) np.random.shuffle(pairs) topPair = pairs[:numSpecies] #For portfolio in 50 worst, set it equal to the breeding of a pair of top 50 for valBot in range(len(bottom)): a = Environment.getPortfolios()[topPair[valBot] [0]].getSpecies().copy() b = Environment.getPortfolios()[topPair[valBot] [1]].getSpecies().copy() Environment.getPortfolios()[bottom[valBot][0]].resetAll(pInvest) Environment.getPortfolios()[bottom[valBot][0]].updateSpecies( a.breed(b).copy()) q = [] for portfolio in range(len(Environment.getPortfolios().keys())): for port2 in range(portfolio + 1, len(Environment.getPortfolios().keys())): if (portfolio != port2): q.append( Environment.getPortfolios()[Environment.getPortfolios( ).keys()[portfolio]].getSpecies().numEquals( Environment.getPortfolios()[ Environment.getPortfolios().keys() [port2]].getSpecies())) print np.round( 100.0 * average(q) / len(Environment.getPortfolios()[Environment.getPortfolios().keys() [0]].getSpecies().getGenes()), 2), "%" qg.append(100.0 * average(q) / len(Environment.getPortfolios()[ Environment.getPortfolios().keys()[0]].getSpecies().getGenes())) print np.round(time.time() - t1, 2), "seconds" print print if (gen != genCount - 1): for valTop in range(len(top)): Environment.getPortfolios()[top[valTop][0]].reset(pInvest) print TOPV, "Fitness" if (len(Environment.getPortfolios()[TOP].getCorrectList()) == 0): print "bad things happend there was nothing there, uh hello" else: print "TRAIN CORRECT : ", ( Environment.getPortfolios()[TOP].getCorrectList().count(1) * 1.0) / len(Environment.getPortfolios()[TOP].getCorrectList()) print "TRAIN INCORRECT : ", ( Environment.getPortfolios()[TOP].getCorrectList().count(-1) * 1.0) / len(Environment.getPortfolios()[TOP].getCorrectList()) print "TRAIN HELD : ", ( Environment.getPortfolios()[TOP].getCorrectList().count(0) * 1.0) / len(Environment.getPortfolios()[TOP].getCorrectList()) print "TRAIN GUESSED : ", ( Environment.getPortfolios()[TOP].getCorrectList().count(1) + Environment.getPortfolios()[TOP].getCorrectList().count(-1) * 1.0) / len(Environment.getPortfolios()[TOP].getCorrectList()) print "TRAIN BALANCE : ", Environment.getPortfolios( )[TOP].getBalanceList()[-1] pToO = [] for day in range(startDay, DAYS): inputs = inputList[day] startPrice = startPriceList[day] endPrice = endPriceList[day] # Get boolean statements for the day for gene in Environment.getPortfolios()[TOP].getSpecies().getGenes( ).keys(): Environment.getPortfolios()[TOP].getSpecies().getGenes( )[gene].makeBoolean(inputs) # Get species output and add corresponding behavior # print Environment.getPortfolios()[portfolio].getBalance()/(0.0+startPrice),Environment.getPortfolios()[portfolio].getShareFactor() Environment.getPortfolios()[TOP].addBehavior( Environment.getPortfolios()[TOP].getSpecies().getOutput() > Environment.getPortfolios()[TOP].getSpecies().getRightThresh(), ("Buy", "LUV", int(Environment.getPortfolios()[TOP].getShareFactor() * (Environment.getPortfolios()[TOP].getBalance() / (0.0 + startPrice))), startPrice)) Environment.getPortfolios()[TOP].addBehavior( Environment.getPortfolios()[TOP].getSpecies().getOutput() < Environment.getPortfolios()[TOP].getSpecies().getLeftThresh(), ("Short", "LUV", int(Environment.getPortfolios()[TOP].getShareFactor() * (Environment.getPortfolios()[TOP].getBalance() / (0.0 + startPrice))), startPrice)) Environment.getPortfolios()[TOP].addGeneCorrect(endPrice - startPrice) #Go through with actions, sellBack at end of day and add balance to balanceList Environment.getPortfolios()[TOP].makeActions(startPrice) Environment.getPortfolios()[TOP].sellBack(stockList, endPrice) Environment.getPortfolios()[TOP].addBalance( Environment.getPortfolios()[TOP].balance) pToO.append((Environment.getPortfolios()[TOP].getSpecies().getOutput(), (Environment.getPortfolios()[TOP].balanceList[-1] - Environment.getPortfolios()[TOP].balanceList[-2]) / abs( (Environment.getPortfolios()[TOP].balanceList[-1] - Environment.getPortfolios()[TOP].balanceList[-2])))) np.savetxt("pCorrectToOutput.csv", pToO, delimiter=",") # if evolutionPlot == True: # plt.plot(Environment.getPortfolios()[TOP].getBalanceList()) # plt.show() if geneTest == True: for gene in Environment.getPortfolios()[TOP].getGeneCorrect(): Environment.getPortfolios()[TOP].getGeneCorrect()[gene] = average( Environment.getPortfolios()[TOP].getGeneCorrect()[gene]) # plt.bar(range(len(Environment.getPortfolios()[TOP].getGeneCorrect())), Environment.getPortfolios()[TOP].getGeneCorrect().values(), align='center') # plt.xticks(range(len(Environment.getPortfolios()[TOP].getGeneCorrect())), Environment.getPortfolios()[TOP].getGeneCorrect().keys()) # plt.show() lsta = [] lstb = [] for gene in Environment.getPortfolios()[TOP].getGeneCorrect(): lsta.append( Environment.getPortfolios()[TOP].getGeneCorrect()[gene]) lstb.append(Environment.getPortfolios() [TOP].getSpecies().genes[gene].getWeight()) # plt.plot(lsta,lstb,"go") print(np.corrcoef(lsta, lstb)) # plt.show() #plt.plot(Environment.getPortfolios()[TOP].getShareCountList(),Environment.getPortfolios()[TOP].getOutputList(),"bo") # return IndividualTester.Test(Environment.getPortfolios()[TOP].getSpecies().getGenes(), testStart, testEnd, shareCount, Investment),(Environment.getPortfolios()[TOP].getCorrectList().count(1)*1.0)/len(Environment.getPortfolios()[TOP].getCorrectList()) return IndividualTester.Test(Environment.getPortfolios()[TOP], testStart, testEnd)
DAYS = 5 inputList = {} startPriceList = {} endPriceList = {} #Make Portfolios for individual in range(numSpecies): Environment.addPortfolio("p" + str(individual), pInvest) #Set inputList for d in range(1, DAYS): print "preparing for day", d Environment.setInputs("LUVBBF3", d, d + 1) inputList[d] = (Environment.getInputsF().copy()) startPriceList[d] = Environment.getRecentPrice() endPriceList[d] = Environment.getEndPrice() #Make lists for randomization of initial population inputs = inputList[1] variables = inputs.keys() right = [[], []] weight = [] fitness = {} delimeters = ["<", ">"] for valTop in np.arange(-1, 1.1, .1): right[0].append(round(valTop, 2)) for y in variables:
# for portfolio in Environment.getPortfolios(): # Environment.getPortfolios()[portfolio].addSpecies({}) # for d in range(1,DAYS): # print "preparing for day",d # Environment.setInputs("LUVBBF3", d,d+1 ) # print Environment.getInputsF() # inputList[str(d)]=Environment.getInputsF() # print inputList[str(d)] for gen in range(genCount): for day in range(1, DAYS): print "day " + str(day) + " year " + str(gen + 1) Environment.setInputs("LUVBBF3", day, day + 1) inputs = Environment.getInputsF() # inputs=inputList[str(day)] # print inputs # print inputList[str(day)] # print Environment.endPrice variables = inputs.keys() delimeters = ["<", ">"] right = [[], []] weight = [] fitness = {} for x in np.arange(-1, 1.1, .1): right[0].append(round(x, 2)) for y in variables: right[1].append(y) for z in np.arange(0, 1, .01):
def Enviro(stockList, Investment, numSpecies, genCount, shareCount, trainStart, trainEnd, testStart, testEnd, SaveNew, geneTest, evolutionPlot): avg = [] mx = [] graph = {} g = [] TOPV = 0 TOP = 0 Environment = Strategy(stockList) pInvest = Investment startDay = trainStart DAYS = trainEnd inputList = {} startPriceList = {} endPriceList = {} #Make Portfolios for individual in range(numSpecies): Environment.addPortfolio("p" + str(individual), pInvest) #Set inputList try: inputList = pickle.load(open("inputListA.p", "rb")) startPriceList = pickle.load(open("startPriceListA.p", "rb")) endPriceList = pickle.load(open("endPriceListA.p", "rb")) except (IOError): print "Writing New Pickle" for d in range(startDay, DAYS): if d in inputList: print "Day", d, "Ready" else: print "Adding Day", d Environment.setInputs("LUVBBF3", d, d + 1) inputList[d] = (Environment.getInputsF().copy()) startPriceList[d] = Environment.getRecentPrice() endPriceList[d] = Environment.getEndPrice() pickle.dump(inputList, open("inputListA.p", "wb")) pickle.dump(startPriceList, open("startPriceListA.p", "wb")) pickle.dump(endPriceList, open("endPriceListA.p", "wb")) #Make lists for randomization of initial population inputs = inputList[startDay] variables = inputs.keys() right = [[], []] weight = [] fitness = {} delimeters = ["<", ">"] for valTop in np.arange(-1, 1.1, .1): right[0].append(round(valTop, 2)) for y in variables: right[1].append(y) for z in np.arange(0, 1, .01): weight.append(round(z, 3)) #Construct initial population for portfolio in Environment.getPortfolios().keys(): p = Environment.getPortfolios()[portfolio] p.addSpecies(inputs) for variable in Environment.getPortfolios()[portfolio].getSpecies( ).getGenes().keys(): a = random.randint(0, 1) b = random.randint(0, len(right[a]) - 1) c = delimeters[random.randint(0, 1)] d = weight[random.randint(0, 99)] Environment.getPortfolios()[portfolio].getSpecies().addGene( variable, right[a][b], c, d) Environment.getPortfolios()[portfolio].getSpecies().setThresh( random.randrange( -len(Environment.getPortfolios() [portfolio].getSpecies().getGenes()), 1) + 0.0, random.randrange( 0, len(Environment.getPortfolios() [portfolio].getSpecies().getGenes()) + 1) + 0.0) Environment.getPortfolios()[portfolio].getSpecies().setShareFactor( (random.randrange(1, 100))) # Start loop of days and generations for gen in range(genCount): pool = mp.Pool(100) partial_day_func = partial(MultiProcess1, Environment, inputList, startPriceList, endPriceList, stockList) pool.map(partial_day_func, [day for day in range(startDay, DAYS)]) pool.close() pool.join() #Get fitness of species for portfolio in Environment.getPortfolios().keys(): # fitness[portfolio]=Environment.getPortfolios()[portfolio].balanceList[-1] fitness[portfolio] = (Environment.getPortfolios( )[portfolio].getCorrectList().count(1)) * 1.0 / len( Environment.getPortfolios()[portfolio].getCorrectList()) #Find top 50% sorted_fitness = sorted(fitness.items(), key=operator.itemgetter(1)) if (sorted_fitness[-1][-1] > TOPV): TOP = sorted_fitness[-1][0] TOPV = sorted_fitness[-1][1] #Get bottom 50% bottom = sorted_fitness[0:(numSpecies / 2)] #Choose 50 pairs of top 50 top = sorted_fitness[numSpecies / 2:numSpecies] topPair = [] for valTop in range(len(top)): pair = valTop while top[pair][0] == top[valTop][0]: pair = random.randint(0, len(top) - 1) topPair.append((top[valTop][0], top[pair][0])) #For portfolio in 50 worst, set it equal to the breeding of a pair of top 50 for valBot in range(len(bottom)): a = Environment.getPortfolios()[topPair[valBot] [0]].getSpecies().copy() b = Environment.getPortfolios()[topPair[valBot] [1]].getSpecies().copy() Environment.getPortfolios()[bottom[valBot][0]].resetAll(pInvest) Environment.getPortfolios()[bottom[valBot][0]].updateSpecies( a.breed(b).copy()) for valTop in range(len(top)): Environment.getPortfolios()[top[valTop][0]].reset(pInvest) print TOPV, "Fitness" if (len(Environment.getPortfolios()[TOP].getCorrectList()) == 0): print "bad things happend there was nothing there, uh hello" else: print "TRAIN CORRECT : ", ( Environment.getPortfolios()[TOP].getCorrectList().count(1) * 1.0) / len(Environment.getPortfolios()[TOP].getCorrectList()) print "TRAIN INCORRECT : ", ( Environment.getPortfolios()[TOP].getCorrectList().count(-1) * 1.0) / len(Environment.getPortfolios()[TOP].getCorrectList()) print "TRAIN HELD : ", ( Environment.getPortfolios()[TOP].getCorrectList().count(0) * 1.0) / len(Environment.getPortfolios()[TOP].getCorrectList()) print "TRAIN GUESSED : ", ( Environment.getPortfolios()[TOP].getCorrectList().count(1) + Environment.getPortfolios()[TOP].getCorrectList().count(-1) * 1.0) / len(Environment.getPortfolios()[TOP].getCorrectList()) print "TRAIN BALANCE : ", Environment.getPortfolios( )[TOP].getBalanceList()[-2] # if evolutionPlot == True: # plt.plot(Environment.getPortfolios()[TOP].getBalanceList()) # plt.show() if geneTest == True: for gene in Environment.getPortfolios()[TOP].getGeneCorrect(): Environment.getPortfolios()[TOP].getGeneCorrect()[gene] = average( Environment.getPortfolios()[TOP].getGeneCorrect()[gene]) # plt.bar(range(len(Environment.getPortfolios()[TOP].getGeneCorrect())), Environment.getPortfolios()[TOP].getGeneCorrect().values(), align='center') # plt.xticks(range(len(Environment.getPortfolios()[TOP].getGeneCorrect())), Environment.getPortfolios()[TOP].getGeneCorrect().keys()) # plt.show() lsta = [] lstb = [] for gene in Environment.getPortfolios()[TOP].getGeneCorrect(): lsta.append( Environment.getPortfolios()[TOP].getGeneCorrect()[gene]) lstb.append(Environment.getPortfolios() [TOP].getSpecies().genes[gene].getWeight()) # plt.plot(lsta,lstb,"go") print(np.corrcoef(lsta, lstb)) # plt.show() #plt.plot(Environment.getPortfolios()[TOP].getShareCountList(),Environment.getPortfolios()[TOP].getOutputList(),"bo") # return IndividualTester.Test(Environment.getPortfolios()[TOP].getSpecies().getGenes(), testStart, testEnd, shareCount, Investment),(Environment.getPortfolios()[TOP].getCorrectList().count(1)*1.0)/len(Environment.getPortfolios()[TOP].getCorrectList()) return IndividualTester.Test( Environment.getPortfolios()[TOP].getSpecies().getGenes(), testStart, testEnd, shareCount, Investment), ( Environment.getPortfolios()[TOP].getCorrectList().count(1) * 1.0) / len(Environment.getPortfolios()[TOP].getCorrectList())
def EnviroInitializer(stockList, Investment, numSpecies, genCount, shareCount, startDay, endDay, testStart, testEnd, SaveNew, geneTest, evolutionPlot, geneChange, oldStart, oldEnd): avg = [] mx = [] graph = {} g = [] TOPV = 0 TOP = 0 Environment = Strategy(stockList) pInvest = Investment startDay = startDay DAYS = endDay inputList = {} startPriceList = {} endPriceList = {} inputListW = {} startPriceListW = {} endPriceListW = {} #Make Portfolios for individual in range(numSpecies): Environment.addPortfolio("p" + str(individual), pInvest) #Set Womb Battle days and womb change try: inputListW = pickle.load(open("inputListW1.p", "rb")) startPriceListW = pickle.load(open("startPriceListW1.p", "rb")) endPriceListW = pickle.load(open("endPriceListW1.p", "rb")) except (IOError): print "Writing New Pickle" for d in range(oldStart, oldEnd): if d in inputListW: print "Old Day", d, "Ready" else: print "Adding Old Day", d Environment.setInputs("LUVBBF3", d, d + 1) inputListW[d] = (Environment.getInputsF().copy()) startPriceListW[d] = Environment.getRecentPrice() endPriceListW[d] = Environment.getEndPrice() pickle.dump(inputListW, open("inputListW1.p", "wb")) pickle.dump(startPriceListW, open("startPriceListW1.p", "wb")) pickle.dump(endPriceListW, open("endPriceListW1.p", "wb")) #Set inputList try: inputList = pickle.load(open("inputListF1.p", "rb")) startPriceList = pickle.load(open("startPriceListF1.p", "rb")) endPriceList = pickle.load(open("endPriceListF1.p", "rb")) except (IOError): print "Writing New Pickle" for d in range(startDay, DAYS): if d in inputList: print "Day", d, "Ready" else: print "Adding Day", d Environment.setInputs("LUVBBF3", d, d + 1) inputList[d] = (Environment.getInputsF().copy()) startPriceList[d] = Environment.getRecentPrice() endPriceList[d] = Environment.getEndPrice() pickle.dump(inputList, open("inputListF1.p", "wb")) pickle.dump(startPriceList, open("startPriceListF1.p", "wb")) pickle.dump(endPriceList, open("endPriceListF1.p", "wb")) #Make lists for randomization of initial population inputs = inputList[startDay] variables = inputs.keys() right = [[], []] weight = [] fitness = {} delimeters = ["<", ">"] for valTop in np.arange(-1, 1.1, .1): right[0].append(round(valTop, 2)) for y in variables: right[1].append(y) for z in np.arange(0, 1, .01): weight.append(round(z, 3)) #Construct initial population for portfolio in Environment.getPortfolios().keys(): p = Environment.getPortfolios()[portfolio] p.addSpecies(inputs) for variable in Environment.getPortfolios()[portfolio].getSpecies( ).getGenes().keys(): a = random.randint(0, 1) b = random.randint(0, len(right[a]) - 1) c = delimeters[random.randint(0, 1)] d = weight[random.randint(0, 99)] Environment.getPortfolios()[portfolio].getSpecies().addGene( variable, right[a][b], c, d) Environment.getPortfolios()[portfolio].getSpecies().setThresh( random.randrange( -len(Environment.getPortfolios() [portfolio].getSpecies().getGenes()), 1) + 0.0, random.randrange( 0, len(Environment.getPortfolios() [portfolio].getSpecies().getGenes()) + 1) + 0.0) Environment.getPortfolios()[portfolio].getSpecies().setShareFactor( (random.randrange(1, 100))) return inputList, startPriceList, endPriceList, Environment
def Enviro(stockList,Investment,numSpecies, genCount, shareCount, startDay, endDay, testStart, testEnd, SaveNew, geneTest, evolutionPlot, geneChange, oldStart,oldEnd): avg=[] mx=[] graph={} g=[] TOPV=0 TOP=0 Environment = Strategy(stockList) pInvest=Investment startDay = startDay DAYS=endDay inputList={} startPriceList={} endPriceList={} inputListW = {} startPriceListW = {} endPriceListW = {} #Make Portfolios for individual in range(numSpecies): Environment.addPortfolio("p"+str(individual),pInvest) #Set Womb Battle days and womb change try: inputListW = pickle.load( open( "inputListW1.p", "rb" ) ) startPriceListW = pickle.load( open( "startPriceListW1.p", "rb" ) ) endPriceListW = pickle.load( open( "endPriceListW1.p", "rb" ) ) except(IOError): print "Writing New Pickle" for d in range(oldStart,oldEnd): if d in inputListW: print "Old Day", d, "Ready" else: print "Adding Old Day", d Environment.setInputs("LUVBBF3", d, d+1) inputListW[d]=(Environment.getInputsF().copy()) startPriceListW[d]=Environment.getRecentPrice() endPriceListW[d]=Environment.getEndPrice() pickle.dump( inputListW, open( "inputListW1.p", "wb" ) ) pickle.dump( startPriceListW, open( "startPriceListW1.p", "wb" ) ) pickle.dump( endPriceListW, open( "endPriceListW1.p", "wb" ) ) #Set inputList try: inputList = pickle.load( open( "inputListF1.p", "rb" ) ) startPriceList = pickle.load( open( "startPriceListF1.p", "rb" ) ) endPriceList = pickle.load( open( "endPriceListF1.p", "rb" ) ) except(IOError): print "Writing New Pickle" for d in range(startDay,DAYS): if d in inputList: print "Day", d, "Ready" else: print "Adding Day", d Environment.setInputs("LUVBBF3", d, d+1) inputList[d]=(Environment.getInputsF().copy()) startPriceList[d]=Environment.getRecentPrice() endPriceList[d]=Environment.getEndPrice() pickle.dump( inputList, open( "inputListF1.p", "wb" ) ) pickle.dump( startPriceList, open( "startPriceListF1.p", "wb" ) ) pickle.dump( endPriceList, open( "endPriceListF1.p", "wb" ) ) #Make lists for randomization of initial population inputs=inputList[startDay] variables=inputs.keys() right=[[],[]] weight=[] fitness={} delimeters=["<",">"] for valTop in np.arange(-1,1.1,.1): right[0].append(round(valTop,2)) for y in variables: right[1].append(y) for z in np.arange(0,1,.01): weight.append(round(z,3)) #Construct initial population for portfolio in Environment.getPortfolios().keys(): p = Environment.getPortfolios()[portfolio] p.addSpecies(inputs) for variable in Environment.getPortfolios()[portfolio].getSpecies().getGenes().keys(): a=random.randint(0,1) b=random.randint(0,len(right[a])-1) c=delimeters[random.randint(0,1)] d=weight[random.randint(0,99)] Environment.getPortfolios()[portfolio].getSpecies().addGene(variable,right[a][b],c,d) Environment.getPortfolios()[portfolio].getSpecies().setThresh(random.randrange(-len(Environment.getPortfolios()[portfolio].getSpecies().getGenes()),1)+0.0,random.randrange(0,len(Environment.getPortfolios()[portfolio].getSpecies().getGenes())+1)+0.0) Environment.getPortfolios()[portfolio].getSpecies().setShareFactor((random.randrange(1,100))) # Start loop of days and generations Corro = [] for gen in range(genCount): for day in range(startDay,DAYS): print "day " + str(day) +" year "+ str(gen+1) inputs=inputList[day] startPrice=startPriceList[day] endPrice=endPriceList[day] # Get boolean statements for the day for portfolio in Environment.getPortfolios().keys(): for gene in Environment.getPortfolios()[portfolio].getSpecies().getGenes().keys(): Environment.getPortfolios()[portfolio].getSpecies().getGenes()[gene].makeBoolean(inputs) # Get species output and add corresponding behavior for portfolio in Environment.getPortfolios().keys(): Environment.getPortfolios()[portfolio].addBehavior(Environment.getPortfolios()[portfolio].getSpecies().getOutput() > Environment.getPortfolios()[portfolio].getSpecies().getRightThresh() , ("Buy", "LUV",int(Environment.getPortfolios()[portfolio].getShareFactor()*(Environment.getPortfolios()[portfolio].getBalance()/(0.0+startPrice))),startPrice)) Environment.getPortfolios()[portfolio].addBehavior(Environment.getPortfolios()[portfolio].getSpecies().getOutput() < Environment.getPortfolios()[portfolio].getSpecies().getLeftThresh(), ("Short", "LUV",int(Environment.getPortfolios()[portfolio].getShareFactor()*(Environment.getPortfolios()[portfolio].getBalance()/(0.0+startPrice))),startPrice)) Environment.getPortfolios()[portfolio].addGeneCorrect(endPrice-startPrice) #Go through with actions, sellBack at end of day and add balance to balanceList for portfolio in Environment.getPortfolios().keys(): Environment.getPortfolios()[portfolio].makeActions(startPrice) Environment.getPortfolios()[portfolio].sellBack(stockList,endPrice) Environment.getPortfolios()[portfolio].addBalance(Environment.getPortfolios()[portfolio].balance) #Get fitness of species for portfolio in Environment.getPortfolios().keys(): # fitness[portfolio]=Environment.getPortfolios()[portfolio].balanceList[-1] fitness[portfolio]=(Environment.getPortfolios()[portfolio].getCorrectList().count(1))*1.0/len(Environment.getPortfolios()[portfolio].getCorrectList()) #Find top 50% sorted_fitness = sorted(fitness.items(), key=operator.itemgetter(1)) if(sorted_fitness[-1][-1]>TOPV): TOP=sorted_fitness[-1][0] TOPV=sorted_fitness[-1][1] #Get bottom 50% bottom=sorted_fitness[0:(numSpecies/2)] #Choose 50 pairs of top 50 top=sorted_fitness[numSpecies/2:numSpecies] topPair=[] for valTop in range(len(top)): pair=valTop while top[pair][0]==top[valTop][0]: pair=random.randint(0,len(top)-1) topPair.append((top[valTop][0],top[pair][0])) #For portfolio in 50 worst, set it equal to the breeding of a pair of top 50 for valBot in range(len(bottom)): a = Environment.getPortfolios()[topPair[valBot][0]].getSpecies().copy() b=Environment.getPortfolios()[topPair[valBot][1]].getSpecies().copy() Environment.getPortfolios()[bottom[valBot][0]].resetAll(pInvest) Environment.getPortfolios()[bottom[valBot][0]].updateSpecies(a.breed(b).copy()) for valTop in range(len(top)): Environment.getPortfolios()[top[valTop][0]].reset(pInvest) plt.plot(Environment.getPortfolios()[TOP].getBalanceList()) if geneChange == True: lsta=[] lstb=[] for gene in Environment.getPortfolios()[TOP].getGeneCorrect(): lsta.append(average(Environment.getPortfolios()[TOP].getGeneCorrect()[gene])) lstb.append(Environment.getPortfolios()[TOP].getSpecies().genes[gene].getWeight()) print(np.corrcoef(lsta,lstb)[0][1]) Corro.append(np.corrcoef(lsta,lstb)[0][1]) print TOPV, "Fitness" print "SPECIES : ", Environment.getPortfolios()[TOP].getSpecies().printSpecies() if len(Environment.getPortfolios()[TOP].getCorrectList()) == 0: print "IT HAPPENED" else: print "CORRECT : ", (Environment.getPortfolios()[TOP].getCorrectList().count(1)*1.0)/len(Environment.getPortfolios()[TOP].getCorrectList()) print "INCORRECT : ", (Environment.getPortfolios()[TOP].getCorrectList().count(-1)*1.0)/len(Environment.getPortfolios()[TOP].getCorrectList()) print "HELD : " , (Environment.getPortfolios()[TOP].getCorrectList().count(0)*1.0)/len(Environment.getPortfolios()[TOP].getCorrectList()) print "GUESSED : " , (Environment.getPortfolios()[TOP].getCorrectList().count(1)+Environment.getPortfolios()[TOP].getCorrectList().count(-1)*1.0)/len(Environment.getPortfolios()[TOP].getCorrectList()) print "BALANCE : ", Environment.getPortfolios()[TOP].getBalanceList()[-2] plt.show() if geneChange == True: plt.plot(range(genCount), Corro, 'go') plt.show() if evolutionPlot == True: plt.plot(Environment.getPortfolios()[TOP].getBalanceList()) plt.show() if geneTest == True: for gene in Environment.getPortfolios()[TOP].getGeneCorrect(): Environment.getPortfolios()[TOP].getGeneCorrect()[gene]=average(Environment.getPortfolios()[TOP].getGeneCorrect()[gene]) plt.bar(range(len(Environment.getPortfolios()[TOP].getGeneCorrect())), Environment.getPortfolios()[TOP].getGeneCorrect().values(), align='center') plt.xticks(range(len(Environment.getPortfolios()[TOP].getGeneCorrect())), Environment.getPortfolios()[TOP].getGeneCorrect().keys()) locs, labels = plt.xticks() plt.setp(labels, rotation=90) plt.show() lsta=[] lstb=[] for gene in Environment.getPortfolios()[TOP].getGeneCorrect(): lsta.append(Environment.getPortfolios()[TOP].getGeneCorrect()[gene]) lstb.append(Environment.getPortfolios()[TOP].getSpecies().genes[gene].getWeight()) print "CORRELATION :", np.corrcoef(lsta,lstb)[0][1] plt.plot(lsta,lstb,"go") plt.show() print "Testing Robustness..." IndividualTester.Test(Environment.getPortfolios()[TOP].getSpecies().getGenes(), testStart, testEnd, shareCount, Investment, SaveNewTest)