package com.strategyquant.extend.MonteCarloValues;

import com.strategyquant.lib.fitness.FitnessFunction;
import com.strategyquant.lib.language.L;
import com.strategyquant.lib.results.StatsConst;
import com.strategyquant.lib.snippets.MonteCarloSim.MCStatsConst;
import com.strategyquant.lib.snippets.MonteCarloSim.MCValueComputer;
import com.strategyquant.lib.snippets.MonteCarloSim.MonteCarloStatValues;
import com.strategyquant.lib.utils.SQUtils;

public class ConsecutiveLosingOrders extends MCValueComputer {

   public ConsecutiveLosingOrders() {
      super("ConsecutiveLosingOrders", FitnessFunction.MINIMIZE);

      setColumnName(L.t("Consecutive Losing Orders"));
      setColumnWidth(100);
      setTooltip(L.t("Consecutive Losing Orders"));
   }

	/**
	 * compute your value here.
	 * P/L for every trade computed by current Monte Carlo run is stored in orders array.
	 */
	@Override
	protected double compute(MonteCarloStatValues statValues, double initialCapital, double[] orders, double avgTradesPerMonth) throws Exception {
      int maxConsecLosingOrders = 0;
      int consecLosingOrders = 0;

      for(int i=0; i<orders.length; i++) {
         if(orders[i] > 0) {
            // it is winning order, reset the counter
            consecLosingOrders = 0;
            
         } else {
            // it is losing order
            consecLosingOrders++;
            if(consecLosingOrders > maxConsecLosingOrders) {
               maxConsecLosingOrders = consecLosingOrders; 
            }
         }
      }

      return maxConsecLosingOrders;
	}

	/**
	 * convert double value into string with the proper formating
	 */
	@Override
	public String formatValue(double value) {
      return String.valueOf((int) value);
	}

	/**
	 * if your value depends on some values that were already computed add teh dependency here.
	 * For example Return / DD value depends on Max DD and Net Profit
	 */
	@Override
	public String dependsOn() {
    	return null;
		//return StatsConst.join(MCStatsConst.MAX_DD, MCStatsConst.PCT_NET_PROFIT);
	}
}