package com.strategyquant.extend.DatabankColumns;

import com.strategyquant.lib.databank.DatabankColumn;
import com.strategyquant.lib.language.L;
import com.strategyquant.lib.results.SQResult;
import com.strategyquant.lib.results.SQResultsGroup;
import com.strategyquant.lib.results.SQStats;
import com.strategyquant.lib.results.StatsConst;
import com.strategyquant.lib.results.StatsMissingException;
import com.strategyquant.lib.settings.SQConst;

public class MaxOpenDrawdown extends DatabankColumn {
    
	public MaxOpenDrawdown() {
		super();
		
		setName(L.t("Max open drawdown"));		
		setTooltip(L.t("Max open drawdown"));
		setWidth(60);
	}

	@Override
	public Object getValue(SQResultsGroup strategyResults, String dataType, String direction, String sampleType, String plType) throws Exception {
		SQResult result = strategyResults.getResult(SQConst.SYMBOL_PORTFOLIO);
		
		SQStats stats = result.getStats(direction, getGlobalPlType(plType), sampleType);
		if(stats==null) throw new StatsMissingException();
		
		String dd_key = getGlobalPlType(plType).equals(SQConst.PL_IN_PCT) ? StatsConst.PCT_MAX_OPEN_DD : (getGlobalPlType(plType).equals(SQConst.PL_IN_TICKS) ? StatsConst.PIPS_MAX_OPEN_DD : StatsConst.MAX_OPEN_DD);
		
		return stats.getDouble(dd_key, 0);
	}
	
	@Override
	public String displayString(SQResultsGroup strategyResults, String dataType, String direction, String sampleType, String plType) throws Exception {
		Double dd = (Double) getValue(strategyResults, dataType, direction, sampleType, plType);
		
		return twoDecimalFormatWithPlType(dd, getGlobalPlType(plType));
	}
	
	@Override
	public String exportString(SQResultsGroup strategyResults, String dataType, String direction, String sampleType, String plType) throws Exception {
		Double dd = (Double) getValue(strategyResults, dataType, direction, sampleType, plType);
		
		return twoDecimalFormat(dd);		
	}
}