Skip to content

Commit

Permalink
position sizing
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanoPenazzi committed Mar 23, 2021
1 parent 091101a commit 278c965
Show file tree
Hide file tree
Showing 26 changed files with 792 additions and 207 deletions.
5 changes: 0 additions & 5 deletions src/main/java/or/model/ModelI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
*/
package or.model;

import java.util.List;

/**
* @author stefanopenazzi
*
*/
public interface ModelI {

public List<Double> getObjectiveFunctionValues(Object...objects);


}
85 changes: 85 additions & 0 deletions src/main/java/or/model/SMASJeneticModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
*
*/
package or.model;

import java.lang.reflect.InvocationTargetException;
import java.util.function.Function;

import data.source.internal.timeseries.TimeSeriesI;
import io.jenetics.Chromosome;
import io.jenetics.Genotype;
import io.jenetics.IntegerChromosome;
import io.jenetics.IntegerGene;
import io.jenetics.Phenotype;
import io.jenetics.engine.Constraint;
import strategies.SingleMovingAverageCrossoverStrategy;

/**
* @author stefanopenazzi
*
*/
public abstract class SMASJeneticModel<T extends SingleMovingAverageCrossoverStrategy> extends StrategyJeneticModelAbstract<T> {

private TimeSeriesI ts;

public SMASJeneticModel(Class<T> strategyC,TimeSeriesI ts) {
super(strategyC);
this.ts = ts;
}

@SuppressWarnings("rawtypes")
@Override
public Function<Genotype,Double> getFitnessFunction() {

Function<Genotype,Double> res = gt -> {
IntegerChromosome length = (IntegerChromosome)gt.get(0);
T strategy = null;
try {
strategy = (T) this.strategyC.getConstructor(TimeSeriesI.class,String.class,Integer.TYPE,Integer.TYPE).newInstance(this.ts,"close",length.intValue(),0);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
strategy.run();
return fitnessFunctionDesign(strategy);

};
return res;
}

@SuppressWarnings("rawtypes")
public abstract Boolean isValid(final Genotype gt);

@SuppressWarnings("rawtypes")
public abstract Genotype repairGenotype(final Genotype gt);

public abstract double fitnessFunctionDesign(T smacs);

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Genotype getEncoding() {
return Genotype.of((Chromosome)IntegerChromosome.of(1, 150, 1));
}

@SuppressWarnings("unchecked")
@Override
public Constraint getConstraint(){
return new RepairingConstraint();
}

@SuppressWarnings("rawtypes")
private class RepairingConstraint implements Constraint{

@Override
public boolean test(Phenotype pt) {
return isValid(pt.genotype());
}

@Override
public Phenotype repair(Phenotype pt, long generation) {
return Phenotype.of(repairGenotype(pt.genotype()),generation);
}

}
}
43 changes: 43 additions & 0 deletions src/main/java/or/model/SSMASJeneticModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
*
*/
package or.model;

import data.source.internal.timeseries.TimeSeriesI;
import io.jenetics.Genotype;
import io.jenetics.IntegerChromosome;
import io.jenetics.IntegerGene;
import strategies.SingleMovingAverageCrossoverStrategy;

/**
* @author stefanopenazzi
*
*/
public class SSMASJeneticModel extends SMASJeneticModel {

/**
* @param strategyC
* @param ts
*/
public SSMASJeneticModel(Class strategyC, TimeSeriesI ts) {
super(strategyC, ts);
}

@Override
public double fitnessFunctionDesign(SingleMovingAverageCrossoverStrategy smacs) {
return smacs.getTotNetProfit();
}

@Override
public Boolean isValid(Genotype gt) {
return ((IntegerChromosome)gt.get(0)).intValue() > 0? true : false;
}

@Override
public Genotype<IntegerGene> repairGenotype(Genotype gt) {
return Genotype.of(IntegerChromosome.of(1, 200, 1));
}



}
32 changes: 32 additions & 0 deletions src/main/java/or/model/StrategyJeneticModelAbstract.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
*
*/
package or.model;

import java.util.function.Function;

import io.jenetics.Genotype;
import io.jenetics.engine.Constraint;
import strategies.StrategyI;

/**
* @author stefanopenazzi
*
*/
public abstract class StrategyJeneticModelAbstract <T extends StrategyI> implements ModelI {

protected final Class<? extends StrategyI> strategyC;

public StrategyJeneticModelAbstract(Class<? extends StrategyI> strategyC) {
this.strategyC = strategyC;
}

@SuppressWarnings("rawtypes")
public abstract Function<Genotype,Double> getFitnessFunction();

@SuppressWarnings("rawtypes")
public abstract Genotype getEncoding();

public abstract Constraint <?,?> getConstraint();

}
48 changes: 0 additions & 48 deletions src/main/java/or/model/StrategyModel.java

This file was deleted.

80 changes: 80 additions & 0 deletions src/main/java/or/model/TestJeneticsSolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
*
*/
package or.model;

/**
* @author stefanopenazzi
*
*/

import io.jenetics.BitChromosome;
import io.jenetics.BitGene;
import io.jenetics.Genotype;
import io.jenetics.RouletteWheelSelector;
import io.jenetics.TournamentSelector;
import io.jenetics.engine.Engine;
import io.jenetics.engine.EvolutionResult;
import io.jenetics.util.Factory;

public class TestJeneticsSolver {

// 2.) Definition of the fitness function.
private static Integer eval(Genotype<BitGene> gt) {
return gt.chromosome()
.as(BitChromosome.class)
.bitCount();
}

/**
* @param args
*/
public static void main(String[] args) {
// 1.) Define the genotype (factory) suitable
// for the problem.

/* A chromosome is an array of Genes. A Genotype is an array of chromosome
* the chromosomes in the Genotype do not necessarily have the same size.
* Still unclear how this works considering that the genes are the variables of
* the model(a vector not a matrix). All the chromosomes must have the same genes' type */

Factory<Genotype<BitGene>> gtf =
Genotype.of(BitChromosome.of(50, 0.5));

// 3.) Create the execution environment.
Engine<BitGene, Integer> engine = Engine
.builder(TestJeneticsSolver::eval, gtf)
//Selection for crossover and mutation follows
.survivorsSelector(new RouletteWheelSelector<>()) //Strongest individual are selected and avoid the crossover
.offspringSelector(new TournamentSelector<>()) //Crossover
.offspringFraction(0.7)
.populationSize(50)
//when the candidates are selected it is necessary specify how they crossover and how they mutate
.build();

// 4.) Start the execution (evolution) and
// collect the result.
Genotype<BitGene> result = engine.stream()
.limit(100)
.collect(EvolutionResult.toBestGenotype());

System.out.println("Hello World:\n" + result);
}

}



/*
* CROSSOVER
* The crossover operator is the process in which individuals from the
* population trade genetic information, hopefully to create a new individual
* which contains the best parts from its parents’ genomes. During crossover
* each individual in the population is considered for crossover; this is where
* the crossover rate parameter is used. By comparing the crossover rate to a
* random number, we can decide whether the individual should have crossover
* applied to it, or whether it should be added straight into the next
* population unaffected by crossover. If an individual is selected for
* crossover then a second parent needs be found. To find the second parent, we
* need to pick one of many possible selection methods.
*/
14 changes: 0 additions & 14 deletions src/main/java/or/solver/GridSearch.java

This file was deleted.

Loading

0 comments on commit 278c965

Please sign in to comment.