Exemplo n.º 1
0
function run()
{
	BarPeriod = 240;	// 4 hour bars
	
// calculate the buy/sell signal
	vars Price = series(price());
	vars DomPeriod = series(DominantPeriod(Price,30));
	var LowPeriod = LowPass(DomPeriod,500);
	vars HP = series(HighPass(Price,LowPeriod));
	vars Signal = series(Fisher(HP,500));
	var Threshold = 1.0;

// buy and sell
	Stop = 4*ATR(100);
	Trail = 4*ATR(100);
	if(crossUnder(Signal,-Threshold))
		enterLong(); 
	else if(crossOver(Signal,Threshold))
		enterShort();

// plot signals and thresholds
	plot("DominantPeriod",LowPeriod,NEW,BLUE);
	plot("Signal",Signal[0],NEW,RED);
	plot("Threshold1",Threshold,0,BLACK);
	plot("Threshold2",-Threshold,0,BLACK);
	PlotWidth = 600;
	PlotHeight1 = 300;
} 
Exemplo n.º 2
0
void GeneticAlgorithm::crossOver(vector<Army*>& selected)
{
    vector<Army*> indCross;

    for (unsigned int i = 0; i < selected.size(); ++i)
    {
        unsigned int other = rand()%selected.size();
        while ( other == i )
            other = rand()%selected.size();

        crossOver(selected[i], selected[other], indCross);
    }

    printf("CrossOvers: %d\n", indCross.size());

    // Deletar alguns individuos extras
    while (indCross.size() + selected.size() > INDIVIDUOS_GERACAO)
    {
        auto it = indCross.begin()+ (rand()%indCross.size());
        delete *it;
        indCross.erase( it );
    }

    printf("CrossOvers: %d\n", indCross.size());
    for (unsigned int i = 0; i < indCross.size(); ++i)
    {
        selected.push_back(indCross[i]);
    }
}
Exemplo n.º 3
0
Individual * Population::iterate (int iterations) {
    Individual * fittest = getFittest ();
    for (int i = 0; i < iterations; i++)
    {
        if (fittest->getFitness () == 0) {
            return fittest;
        } else {
            pop_stats.average_fitness.push_back (calcAvFitness (individuals, size));
            pop_stats.highest_fitness.push_back (fittest->getFitness ());
            
            Individual * winners = tournament ();
            mutation (winners);
            Individual * children = crossOver (winners);
            merge (winners, children);
            
            delete [] winners;
            delete [] children;
            
            calcFittest ();
            fittest = getFittest ();
        }
    }
    
    return fittest;
}
void multipleCrossOver() {
    int i, j, children_index=0;
    for (i=0; i<POPULATION_COUNT; i++) {
        for (j=i; j<POPULATION_COUNT; j++) {
            crossOver(i, j, children_index);
            children_index++;
        }
    }
}
Exemplo n.º 5
0
void Ga::execute() {
    for(int g=0; g<m_Generation; g++) {
        cout<<"gen="<<g<<endl;
        vector<Individual*> family;
        selectReproduction(family);
        crossOver(family);
        selectSurvive(family);
        printFitness();
    }
}
Exemplo n.º 6
0
// brute force optimization
function run(){  
	set(PARAMETERS);
  int PeriodsEMA1[4] = { 5, 10, 15, 20 };
  int PeriodsEMA2[3] = { 100, 150, 200 };  LookBack = 250;
  int Index = optimize(1,1,4*3,1) - 1;  int PeriodEMA1 = PeriodsEMA1[Index%4];
  int PeriodEMA2 = PeriodsEMA2[Index/4];    vars Price = series(price(0));
  vars EMA1 = series(EMA(Price,PeriodEMA1));
  vars EMA2 = series(EMA(Price,PeriodEMA2));    
  if(crossOver(EMA1,EMA2))
    enterLong();  
  else if(crossUnder(EMA1,EMA2))    
   enterShort();
 }
Exemplo n.º 7
0
function tradeCounterTrend()
{
	TimeFrame = 4;
	vars Price = series(price());
	vars Filtered = series(BandPass(Price,optimize(30,25,35),0.5));
	vars Signal = series(Fisher(Filtered,500));
	var Threshold = optimize(1,0.5,1.5,0.1);
	
	Stop = optimize(4,2,10) * ATR(100);
	Trail = 4*ATR(100);
	
	if(crossUnder(Signal,-Threshold)) 
		enterLong(); 
	else if(crossOver(Signal,Threshold)) 
		enterShort();
}
Exemplo n.º 8
0
void GeneticAlgorithm::run()
{
    if (armyType != 0) return;

    printf("Iterate 2 times\n");
    vector<Army*> selected, rejected;
    for (unsigned int i = 0; i < 2; i++)
    {
        // Completar exercito para INDIVIDUOS_GERACAO
        randomArmies( INDIVIDUOS_GERACAO - individuos.size() );
        // Adicionar 2 aleatorios sempre
        randomArmies( 2 );

        // Apply the selection - currently Tournament
        selectFromPop(20, selected, rejected);

        for (unsigned int r = 0; r < rejected.size(); ++r){
            delete rejected[r];
        }
        rejected.clear();

        // Apply CrossOver
        crossOver(selected); // Memory Leak

        // Apply Mutation
        mutate(selected);

        individuos.clear();
        printf("Iteration: %d\n", selected.size());
        for (unsigned int j = 0; j < selected.size(); ++j){
            individuos.push_back(selected[j]);
        }
        selected.clear();
    }

    char buffer[8];

    //Save the strongest armies
    printf("Salvando Individuos... %d\n", individuos.size());
    for (unsigned int i = 0; i < individuos.size(); i++)
    {
        sprintf(buffer, "r%d", i+1);
        individuos[i]->setArmyName(buffer);
        Army::saveArmy(individuos[i], directory.c_str());
        printf("army[%d]\n", i+1);
    }
}
Exemplo n.º 9
0
Individual<N> * Population<N>::iterate (int iterations) {
    for (int i = 0; i < iterations; i++) {
        Individual<N> * fittest = getFittest ();
        if (fittest) {
            return fittest;
        }
        else {
//            print ();
            if (OUTPUT) {
                std::cout << std::endl 
                          << "WHOLE POPULATION:" << std::endl;
            }
            getStats (individuals, size);
//            if (OUTPUT) print ();

            if (OUTPUT) std::cout << "WINNERS:" << std::endl;

            Individual<N> * winners = tournament ();

            getStats (winners, size/2);
//            if (OUTPUT) print (winners);

            mutation (winners);

            if (OUTPUT) std::cout << "WINNERS AFTER MUTATION:" << std::endl;
            getStats (winners, size/2);
//            if (OUTPUT) print (winners);
            
            if (OUTPUT) std::cout << "CHILDREN:" << std::endl;

            Individual<N> * children = crossOver (winners);

            getStats (children, size/2);
//            if (OUTPUT) print (children);
            
            merge (winners, children);

            return fittest;
        }
    }
    
    return nullptr;
}
Exemplo n.º 10
0
bool Population<N>::iterate (int iterations) {
    for (int i = 0; i < iterations; i++) {
        Individual<N> * fittest = getFittest ();
        if (fittest) {
            fittest->print ();
            return true;
        }
        else {
            print ();
            Individual<N> * winners = tournament ();
            mutation (winners);
            Individual<N> * children = crossOver (winners);
            merge (winners, children);
            return false;
        }
    }
    
    return false;
}
Exemplo n.º 11
0
function run() 
{
	BarPeriod	= 1440;
	//Detrend = 8; // does it also work with the inverse curve?
	asset("SPX500");
	
	vars Osc = series(StochEhlers(series(price()),20,10,10));
	
	if(crossOver(Osc,0.8)) 
		reverseShort(1);
	if(crossUnder(Osc,0.2))
		reverseLong(1);
	
	PlotWidth = 800;
	plot("StochEhlers",Osc[0],NEW,RED);
	plot("Threshold1",.2,0,BLACK);
	plot("Threshold2",.8,0,BLACK);
	set(PLOTNOW);
}
Exemplo n.º 12
0
Individual * Population::crossOver (Individual * individuals) {
    Individual * children = new Individual [size/2];
    
    for (int i = 0; i < size/2; i+=2) {
        Individual children_1;
        Individual children_2;
        
        crossOver (& individuals [i], & individuals [i + 1], 
                   children_1, children_2);
        
        children [i] = children_1;
        children [i + 1] = children_2;
    }
    
    pop_stats.average_children_fitness.push_back 
        (calcAvFitness (children, size/2));
    pop_stats.highest_children_fitness.push_back 
        (getFittest (children, size/2)->getFitness ());
    
    return children;
}
cinder::Surface ColorAlgoGen::operator()(const cinder::Surface& realImage)
{
    std::vector<SurfaceWrapper> nextGenPopulation;
    nextGenPopulation.reserve(m_popSize);


    std::for_each(m_population.begin(), m_population.end(), [this, &realImage](SurfaceWrapper& s)
    {
        s.fitness = getFitness(realImage, s.image);
    });

    std::sort(m_population.begin(), m_population.end(), [](const SurfaceWrapper& s1, const SurfaceWrapper& s2)
    {
        return s1.fitness < s2.fitness;
    });

    unsigned int copyRatio = tools::clamp<int>(
        static_cast<int>(getPercent(COPY) * m_popSize),
        0,
        m_popSize
        );

    if (copyRatio > 0)
    {
        std::transform(m_population.begin(), m_population.begin() + copyRatio, std::back_inserter(nextGenPopulation), [&realImage, this](const SurfaceWrapper& sw)
        {
            return sw;
        });
    }



    unsigned int mutateRatio = tools::clamp<int>(
        static_cast<int>(getPercent(MUTATE) * m_popSize),
        0,
        m_popSize - copyRatio
        );

    if (mutateRatio > 0)
    {
        std::transform(m_population.begin(), m_population.begin() + mutateRatio, std::back_inserter(nextGenPopulation), [&realImage, this](const SurfaceWrapper& sw)
        {
            cinder::Surface s = mutate(sw.image);
            
            return SurfaceWrapper(s, getFitness(realImage, s));
        });
    }

    unsigned int crossOverRatio = tools::clamp<int>(
        static_cast<int>(getPercent(CROSSOVER) * m_popSize),
        0,
        m_popSize - copyRatio - mutateRatio
        );

    if (crossOverRatio > 0)
    {
        std::transform(m_population.begin(), m_population.begin() + crossOverRatio, std::back_inserter(nextGenPopulation), [&realImage, this](const SurfaceWrapper& sw)
        {
            auto& pop = getPop();

            cinder::Surface s = crossOver(sw.image, pop[RANDOMIZER.nextUint(pop.size())].image);

            return SurfaceWrapper(s, getFitness(realImage, s));
        });
    }


    unsigned int randomRatio = tools::clamp<int>(
        static_cast<int>(getPercent(RANDOM) * m_popSize),
        0,
        m_popSize - copyRatio - mutateRatio - crossOverRatio
        );

    if (nextGenPopulation.size() + randomRatio < m_popSize)
    {
        randomRatio = m_popSize - nextGenPopulation.size();
    }

    if (randomRatio > 0)
    {
        std::transform(m_population.begin(), m_population.begin() + randomRatio, std::back_inserter(nextGenPopulation), [&realImage, this](const SurfaceWrapper& sw)
        {
            cinder::Surface s = getRandomSurface(sw.image.getWidth(), sw.image.getHeight());

            return SurfaceWrapper(s, getFitness(realImage, s));
        });
    }



    std::sort(nextGenPopulation.begin(), nextGenPopulation.end(), [](const SurfaceWrapper& s1, const SurfaceWrapper& s2)
    {
        return s1.fitness < s2.fitness;
    });


    m_population = nextGenPopulation;

    return m_population.front().image;
}
Exemplo n.º 14
0
bool unitTest_Factory()
{
    return unitTest_randomGen() && codeToTree() && randomIa() && mutate() && crossOver();
}