/*
 * 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.TradeAnalysisPanes;

import java.awt.Font;
import java.util.ArrayList;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryLabelPositions;

import com.strategyquant.lib.charts.common.SQBarChart;
import com.strategyquant.lib.charts.common.duration.TimeDuration;
import com.strategyquant.lib.language.L;
import com.strategyquant.lib.results.SQData;
import com.strategyquant.lib.results.SQOrder;
import com.strategyquant.lib.results.SQOrderList;
import com.strategyquant.lib.results.SQResultsGroup;
import com.strategyquant.lib.settings.SQConst;
import com.strategyquant.lib.snippets.TradeAnalysisChartPane;

public class PLbyTradeDurationChart extends TradeAnalysisChartPane {
	private ArrayList<TimeDuration> timeDurationList;
	
	public PLbyTradeDurationChart() {
		this.name = L.t("P/L by trade duration");
	}
	
	@Override
	public JFreeChart drawChart(SQResultsGroup strategyResult, SQData data) {
		SQBarChart chart = new SQBarChart();
		chart.xLegend = L.t("P/L / Time");
		
		if(strategyResult==null) {
			return chart.render();
		}
		
		double[] trades = computeData(strategyResult, data);
		
		for(int trade=0; trade<trades.length; trade++) {
			chart.setValue(trades[trade], L.t("P/L"), timeDurationList.get(trade).toString());
		}
		
		chart.setDomainAxisLabelPositions(CategoryLabelPositions.UP_90);
		chart.setDomainAxisLabelFont(new Font("Arial Unicode MS", Font.PLAIN, 10));
		chart.invertIfNegative(true);
		
		return chart.render();
	}

	private double[] computeData(SQResultsGroup strategyResult, SQData data) {
		String plType = data.getString("plType", SQConst.PL_IN_MONEY);
		
		SQOrderList orderList = filterOrders(strategyResult, data);

		timeDurationList = calculateTimeDurationScale(orderList);	

		double[] trades = new double[timeDurationList.size()];
		
		for(int trade=0; trade<trades.length; trade++) { 
			trades[trade]=0; 
		}
		
		int timeInTrade;
		
		for(SQOrder order : orderList) {
			timeInTrade = (int) (order.getTimeInTrade()/1000);
			
			for(int index=0; index<timeDurationList.size(); index++) {
				if(timeInTrade<timeDurationList.get(index).seconds) {
					trades[index]+=order.getPL(plType);  
					
					break;
				}
			}
		}
		
		return trades;
	}
}