package ch.epfl.bigdata.ts.genalg;

import java.util.Random;

/**
 * Created by dorwi on 05.04.14.
 */
public class Individual{
    /*
        Genes:
            0. the time interval for tracking the average
                - [600..6000s]
            1. the percentage using on average to generate the buy
                - [50..150%]
            2. percentage for protecting loose
                - [50..150%]
            3. percentage for protecting gain
                - [50..150%]
    */

    static int NUMBER_OF_GENES = 4;
    private double[] genes = new double[NUMBER_OF_GENES];
    double amount = 10000;


    /*
        variables important for the trading strategy
     */

    boolean openedPosition = false;
    boolean finishedInterval = false;
    boolean calculatedInterval = false;
    double lastPrice;
    double priceBuy;
    long intervalBegin;
    long intervalEnd;
    double avg;
    long count;
    double sum;
    double sellLoss;
    double sellGain;
    long numberOfShares=0;

    static Random r = new Random(System.currentTimeMillis());
    //Create a random individual
    public void generateIndividual(){
        genes[0] = Constants.INTERVAL_MIN + (Constants.INTERVAL_MAX-Constants.INTERVAL_MIN)*r.nextDouble();
        genes[1] = Constants.AVG_PERCENTAGE_MIN + (Constants.AVG_PERCENTAGE_MAX-Constants.AVG_PERCENTAGE_MIN)*r.nextDouble();
        genes[2] = Constants.GAIN_PERCENTAGE_MIN + (Constants.GAIN_PERCENTAGE_MAX-Constants.GAIN_PERCENTAGE_MIN)*r.nextDouble();
        genes[3] = Constants.LOSS_PERCENTAGE_MIN + (Constants.LOSS_PERCENTAGE_MAX-Constants.LOSS_PERCENTAGE_MIN)*r.nextDouble();
        //System.out.println(toString());
        //initially 10.000$
        amount = 10000;
    }

    public void reset(){
        amount = 10000;
        numberOfShares = 0;
        openedPosition = false;
        finishedInterval = false;
        calculatedInterval = false;
    }

    /*Getters and setters*/
    public static void setNumberOfGenes(int length){
        NUMBER_OF_GENES = length;
    }

    public double getGene(int index){
        return genes[index];
    }

    public double getAmount(){return amount;}

    public long getNumberOfShares(){return numberOfShares;}

    public double getLastPrice(){return lastPrice;}

    public void setAmount(double value){
        amount = value;
    }

    public void setNumberOfShares(int value){
        numberOfShares = value;
    }

    public void setGene(int index, double value){
        genes[index] = value;
    }

    /*Public methods*/
    public int size(){
        return genes.length;
    }

    public double getFitness(){
        return amount;
    }

    /*
    * The implemented strategy:
     * for genes[0] time listen to prices, then calculate the avg
     * if the price is below genes[1]*avg, then buy with the last price
     * if the price is below genes[2]*avg, then sell
     * if the price is above genes[3]*avg, then sell
    * */
    public void trade(long time,double price){
        lastPrice = price;
        if (openedPosition){
            if (price<sellLoss){
                sell(price);
            }
            if (price>sellGain){
                sell(price);
            }
        } else{
            if (!calculatedInterval){
                calculatedInterval = true;
                finishedInterval = false;
                intervalBegin = time;
                intervalEnd = time + (int)Math.floor(genes[0]);
                count = 1;
                sum = price;
            } else if (finishedInterval){
                if (price<priceBuy){
                    buy(price);
                }
            } else if (time > intervalEnd){
                finishedInterval = true;
                avg = sum/count;
                priceBuy = genes[1]*avg;
            } else {
                count++;
                sum+=price;
            }
        }
    }

    public void buy(double price){
        openedPosition = true;
        sellLoss = genes[2]*avg;
        sellGain = genes[3]*avg;
        numberOfShares = (int)Math.floor(amount/price);
        amount -= numberOfShares*price;
    }

    public void sell(double price){
        openedPosition = false;
        calculatedInterval = false;
        amount += numberOfShares*price;
        numberOfShares = 0;
    }

    @Override
    public String toString(){
        String geneString = "";
        for (int i=0; i<size();i++){
            geneString += getGene(i);
            geneString += " ";
        }
        return geneString;
    }
}

@@ -59,49 +59,8 @@
 m;%0A%0A
-/**%0A * Created by dorwi on 05.04.14.%0A */%0A
 publ
@@ -164,39 +164,8 @@
 age%0A
-                - %5B600..6000s%5D%0A
     
@@ -231,37 +231,8 @@
 buy%0A
-                - %5B50..150%25%5D%0A
     
@@ -278,37 +278,8 @@
 ose%0A
-                - %5B50..150%25%5D%0A
     
@@ -328,76 +328,11 @@
     
-            - %5B50..150%25%5D%0A    */%0A%0A    static int NUMBER_OF_GENES = 4;
+*/%0A
 %0A   
@@ -368,16 +368,26 @@
  double%5B
+Constants.
 NUMBER_O
@@ -416,20 +416,66 @@
 mount = 
-1000
+Constants.STARTING_MONEY;%0A    long numberOfShares=
 0;%0A%0A%0A   
@@ -535,25 +535,24 @@
 egy%0A     */%0A
-%0A
     boolean 
@@ -836,34 +836,8 @@
 in;%0A
-    long numberOfShares=0;
 %0A%0A  
@@ -970,24 +970,210 @@
 l()%7B%0A       
+  for (int i=0; i%3CConstants.NUMBER_OF_GENES; i++)%7B%0A             generate_gene(i);%0A         %7D%0A    %7D%0A%0A    public void generate_gene(int index)%7B%0A        switch (index) %7B%0A            case 0:
  genes%5B0%5D = 
@@ -1264,24 +1264,59 @@
 e();%0A       
+         break;%0A            case 1:
  genes%5B1%5D = 
@@ -1425,24 +1425,59 @@
 e();%0A       
+         break;%0A            case 2:
  genes%5B2%5D = 
@@ -1589,24 +1589,59 @@
 e();%0A       
+         break;%0A            case 3:
  genes%5B3%5D = 
@@ -1762,93 +1762,32 @@
     
-//System.out.println(toString());%0A        //initially 10.000$%0A        amount = 10000;
+        break;%0A        %7D
 %0A   
@@ -1836,13 +1836,32 @@
 t = 
-10000
+Constants.STARTING_MONEY
 ;%0A  
@@ -2027,102 +2027,8 @@
 s*/%0A
-    public static void setNumberOfGenes(int length)%7B%0A        NUMBER_OF_GENES = length;%0A    %7D%0A%0A
     

clean code