def __init__(self, genome, initValue):
     # print("NEW MODEL")
     self.__genome = genome
     self.__brain = []
     self.__layerMap = []
     # initValue refers to the amount of money we are willing to commit to a single trading strategy
     self.__initValue = initValue
     self.__equity = initValue
     self.__stocks = [0, 0, 0, 0, 0]
     self.__lastPrice = [0.0, 0.0, 0.0, 0.0, 0.0]
     # note that model networks now have their neurons divided into layers and that genes are translated into neurons
     self.__inputLayer = []
     self.__outputLayer = []
     for g in self.__genome.genes():
         if g.isEnabled() == True:
             newNeuronA = neatneuron.Neuron(g.inputNum())
             newNeuronB = neatneuron.Neuron(g.outputNum())
             if newNeuronA not in self.__brain:
                 newNeuronA.addNout(neatneuron.Connection(g.outputNum(), g.weight()))
                 self.__brain.append(newNeuronA)
                 if newNeuronA.node() in self.__genome.inputSet():
                     self.__inputLayer.append(newNeuronA)
                 if newNeuronA.node() in self.__genome.outputSet():
                     self.__outputLayer.append(newNeuronA)
             else:
                 self.__brain[self.__brain.index(newNeuronA)].addNout(neatneuron.Connection(g.outputNum(), g.weight()))
             if newNeuronB not in self.__brain:
                 self.__brain.append(newNeuronB)
                 if newNeuronB.node() in self.__genome.outputSet():
                     self.__outputLayer.append(newNeuronB)
                 if newNeuronB.node() in self.__genome.inputSet():
                     self.__inputLayer.append(newNeuronB)
     self.__layerMap.append(self.__inputLayer)
     self.separateIntoLayers()
     self.__layerMap.append(self.__outputLayer)
 def runTraced(self, data, stock, point, file):
     
     for d in data:
         position = point * 5 + data.index(d)
         # Data Point d - set of attributes that are deemed necessary for trading strategies
         for neuron in self.__brain:
             neuron.resetVal()
         self.__lastPrice[stock] = d[0]
         for i in range(0, len(d)):
             if i < len(self.__inputLayer):
                 self.__inputLayer[i].resetVal()
                 self.__inputLayer[i].passVal(d[i])
         for layer in self.__layerMap:
             for neuron in layer:
                 for output in neuron.nouts():
                     nextNeuron = self.__brain[self.__brain.index(neatneuron.Neuron(output.output))]
                     nextNeuron.passVal(activation_function(neuron.val() * output.weight))
         for neuronIndex in range(len(self.__outputLayer)):
             if self.__outputLayer[neuronIndex].val() > 1.0:
                 buyQuant = self.__stocks[stock]
                 self.buy(stock, self.__outputLayer[neuronIndex].val() - 1.0)
                 buyQuant -= self.__stocks[stock]
                 buyQuant *= -1
                 if buyQuant > 0:
                     file.write(str(position) + "," + str(buyQuant) + "," + str(stock) + "\n")
                 if buyQuant < 0:
                     file.write(str(position) + "," + str(buyQuant) + "," + str(stock) + "\n")
             if self.__outputLayer[neuronIndex].val() < -1.0:
                 #print("SELL FUNCTION CALLED")
                 sellQuant = self.__stocks[stock]
                 self.sell(stock, -1 * (self.__outputLayer[neuronIndex].val() + 1.0))
                 sellQuant -= self.__stocks[stock]
                 file.write(str(position) + "," + str(sellQuant) + "," + str(stock) + "\n")
 def separateIntoLayers(self):
     done = False
     while done == False:
         tentativeLayer = []
         for no in self.__layerMap[-1]:
             for out in no.nouts():
                 outn = self.__brain[self.__brain.index(neatneuron.Neuron(out.output))]
                 if outn not in self.__outputLayer and outn not in tentativeLayer:
                     if outn.layer() == -1 or outn.layer() > no.layer():
                         tentativeLayer.append(outn)
                         outn.setLayer(len(self.__layerMap))
         if len(tentativeLayer) > 0:
             self.__layerMap.append(tentativeLayer)
         else:
             done = True
 def runAlt(self, data, stock):
     for d in data:
         # Data Point d - set of attributes that are deemed necessary for trading strategies
         for neuron in self.__brain:
             neuron.resetVal()
         self.__lastPrice[stock] = d[0]
         for i in range(0, len(d)):
             if i < len(self.__inputLayer):
                 self.__inputLayer[i].resetVal()
                 self.__inputLayer[i].passVal(d[i])
         for layer in self.__layerMap:
             for neuron in layer:
                 for output in neuron.nouts():
                     nextNeuron = self.__brain[self.__brain.index(neatneuron.Neuron(output.output))]
                     nextNeuron.passVal(activation_function(neuron.val() * output.weight))
         for neuronIndex in range(len(self.__outputLayer)):
             if self.__outputLayer[neuronIndex].val() > 1.0:
                 
                 self.buy(stock, self.__outputLayer[neuronIndex].val() - 1.0)
             if self.__outputLayer[neuronIndex].val() < -1.0:
                 #print("SELL FUNCTION CALLED")
                 self.sell(stock, -1 * (self.__outputLayer[neuronIndex].val() + 1.0))