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

import org.jfree.chart.JFreeChart;

import com.strategyquant.lib.charts.common.SQChart;
import com.strategyquant.lib.charts.common.SQScatterChart;
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 PLGrowthByDurationChart extends TradeAnalysisChartPane {
	private ArrayList<TimeDuration> timeDurationList;
	
	public PLGrowthByDurationChart() {
		this.name = L.t("P/L Growth by duration");
	}
	
	@Override
	public JFreeChart drawChart(SQResultsGroup strategyResult, SQData data) {
		SQScatterChart chart = new SQScatterChart();
		chart.xLegend = L.t("P/L Growth / Time");
		
		if(strategyResult==null) {
			return chart.render();
		}
		
		ArrayList<double[]> trades = computeData(strategyResult, data);
		
		for(int trade=0; trade<trades.size(); trade++) {
			double[] values = trades.get(trade);
			
			if(values[1]<0) {
				chart.add("losers", values[0], values[1]);
			} else {
				chart.add("winners", values[0], values[1]);
			}
		}

		chart.setDurationDomainAxis(true);
		chart.setSeriesColor("losers", SQChart.COLOR_RED);
		chart.setSeriesColor("winners", SQChart.COLOR_GREEN);
		
		return chart.render();
	}

	private ArrayList<double[]> computeData(SQResultsGroup strategyResult, SQData data) {
		ArrayList<double[]> trades = new ArrayList<double[]>();

		String plType = data.getString("plType", SQConst.PL_IN_MONEY);
		
		SQOrderList orderList = filterOrders(strategyResult, data);

		for(SQOrder order : orderList) {
			double[] values = new double[2];					
			values[0] = (int)(order.getTimeInTrade()/1000);
			values[1] = order.getPL(plType);
			
			trades.add(values);
		}
		
		return trades;
	}
}