/*
 * Copyright (c) 2015, StrategyQuant - All rights reserved.
 *
 * Code in this file was made in a good faith that it is correct and does what it should. 
 * If you found a bug in this code OR you have an improvement suggestion OR you want to include 
 * your own code snippet into our standard library please contact us at:
 * http://tasks.strategyquant.com/projects/snippets/
 *
 * This code can be used only within StrategyQuant products. 
 * Every owner of valid (free, trial or commercial) license of any StrategyQuant product 
 * is allowed to freely use, copy, modify or make derivative work of this code without limitations,
 * to be used in all StrategyQuant products and share his/her modifications or derivative work 
 * with the StrategyQuant community.
 *  
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES 
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *  
 */
package com.strategyquant.extend.StatValues;

import com.strategyquant.lib.results.SQOrder;
import com.strategyquant.lib.results.SQOrderList;
import com.strategyquant.lib.results.SQStats;
import com.strategyquant.lib.results.StatValue;
import com.strategyquant.lib.results.StatsConst;
import com.strategyquant.lib.results.StatsTypeCombination;
import com.strategyquant.lib.settings.SQConst;
import com.strategyquant.lib.settings.SQSettings;
import com.strategyquant.lib.utils.SQUtils;

public class SymmetryValues extends StatValue {

	@Override
	public String dependsOn() {
		return StatsConst.NET_PROFIT;
	}
	
	@Override
	public void initCompute(SQStats stats, StatsTypeCombination combination, SQOrderList ordersList, SQSettings settings, SQStats statsLong, SQStats statsShort) throws Exception {

	}

	@Override
	public void computeForOrder(SQStats stats, StatsTypeCombination combination, SQOrder order, SQSettings settings, SQStats statsLong, SQStats statsShort) throws Exception {

	}

	@Override
	public void endCompute(SQStats stats, StatsTypeCombination combination, SQOrderList ordersList, SQSettings settings, SQStats statsLong, SQStats statsShort) throws Exception {
		if(combination.getDirection().equals(SQConst.DIRECTION_ALL)) {
			double longPL = statsLong.getDouble(StatsConst.NET_PROFIT);
			double shortPL = statsShort.getDouble(StatsConst.NET_PROFIT);
			
			double symmetry = computeSymmetry(longPL, shortPL);
			
			stats.set(StatsConst.SYMMETRY, SQUtils.round2(symmetry));
		} else {
			stats.set(StatsConst.SYMMETRY, SQUtils.round2(0));
		}
	}

	private double computeSymmetry(double longPL, double shortPL) {
    	double maxProfit = 0;
    	double smallerProfit = 0;
    	
    	if(longPL == shortPL) {
    		return 1f;
    		
    	} else if((longPL > 0 && shortPL <= 0) || (longPL <= 0 && shortPL > 0)) {
    		return 0f;

    	} else if(Math.abs(longPL) > Math.abs(shortPL)) {
    		maxProfit = Math.abs(longPL);
    		smallerProfit = Math.abs(shortPL);
    	} else {
    		maxProfit = Math.abs(shortPL);
    		smallerProfit = Math.abs(longPL);
    	}

    	if(smallerProfit < 0 && maxProfit >= 0) {
    		return 0;
    	}
    	
    	// compute how much % is small profit from max profit
    	return smallerProfit / (maxProfit/100f);
	}
}