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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.strategyquant.lib.results.SQResult;
import com.strategyquant.lib.results.SQResultsGroup;
import com.strategyquant.lib.results.SQStats;
import com.strategyquant.lib.scripter.Program;
import com.strategyquant.lib.settings.SQConst;
import com.strategyquant.lib.tests.TestOutput;
import com.strategyquant.lib.tests.TestScript;


public class EAALoaderMyfxbook extends TestScript {

	public EAALoaderMyfxbook() {
		setCategory("EAA Loaders");
		setName("EAA Loader Myfxbook");
	}


	@Override
	public void runTest() {
		
		test1();
		test2();
		test3();
		test4();
		test5();
	}
	
	public void test1() {
		
		try {
			SQResultsGroup strategyResults = (SQResultsGroup) Program.get("Loader").call("loadFile", "./tests/data/loaders/test_statement.csv");

			SQResult result;
			SQStats stats;

			// verify portfolio
			result = strategyResults.getResult(SQConst.SYMBOL_PORTFOLIO);
			stats = result.getStats(SQConst.DIRECTION_ALL, SQConst.PL_IN_PCT, SQConst.KEY_SAMPLE_ALL);
		
			assertEquals("Test 1 NumberOfTrades", 445, stats.getInt("NumberOfTrades"));
			assertEquals("Test 1 NetProfit", 619.86, stats.getDouble("NetProfit"));
		} catch (Exception e) {
			fail("Exception "+ e.getMessage());
		}
	}
	





	public void test2() {
		
		try {
			SQResultsGroup strategyResults = (SQResultsGroup) Program.get("Loader").call("loadFile", "./tests/data/loaders/myfxbook_CONTEST IRF 4N.csv");

			SQResult result;
			SQStats stats;

			// verify portfolio
			result = strategyResults.getResult(SQConst.SYMBOL_PORTFOLIO);
			stats = result.getStats(SQConst.DIRECTION_ALL, SQConst.PL_IN_PCT, SQConst.KEY_SAMPLE_ALL);
		
			assertEquals("Test 2 NumberOfTrades", 71, stats.getInt("NumberOfTrades"));

			//in this report for some reason AdvancedLoadSettings.AddCommSwapToPL is set to true, although it should be false by default
			// assertEquals(14248.38, stats.getDouble("NetProfit"));
			assertEquals("Test 2 NetProfit", 13986.34, stats.getDouble("NetProfit"));
			
		} catch (Exception e) {
			fail("Exception "+ e.getMessage());
		}
	}
	
	public void test3() {
		
		try {
			SQResultsGroup strategyResults = (SQResultsGroup) Program.get("Loader").call("loadFile", "./tests/data/loaders/myfxbook_statement %288%29.csv");
			SQResult result;
			SQStats stats;

			// verify portfolio
			result = strategyResults.getResult(SQConst.SYMBOL_PORTFOLIO);
			stats = result.getStats(SQConst.DIRECTION_ALL, SQConst.PL_IN_PCT, SQConst.KEY_SAMPLE_ALL);
		
			assertEquals("Test 3 NumberOfTrades", 170, stats.getInt("NumberOfTrades"));
			assertEquals("Test 3 NetProfit", 1139.09, stats.getDouble("NetProfit"));
		} catch (Exception e) {
			fail("Exception "+ e.getMessage());
		}
	}
	
	public void test4() {
		
		try {
			SQResultsGroup strategyResults = (SQResultsGroup) Program.get("Loader").call("loadFile", "./tests/data/loaders/myfxbook_statement.csv");
			SQResult result;
			SQStats stats;

			// verify portfolio
			result = strategyResults.getResult(SQConst.SYMBOL_PORTFOLIO);
			stats = result.getStats(SQConst.DIRECTION_ALL, SQConst.PL_IN_PCT, SQConst.KEY_SAMPLE_ALL);
		
			assertEquals("Test 4 NumberOfTrades", 100, stats.getInt("NumberOfTrades"));
			assertEquals("Test 4 NetProfit", 61.15, stats.getDouble("NetProfit"));
		} catch (Exception e) {
			fail("Exception "+ e.getMessage());
		}
	}

	public void test5() {
		
		try {
			SQResultsGroup strategyResults = (SQResultsGroup) Program.get("Loader").call("loadFile", "./tests/data/loaders/myfxbook_statement2.csv");
			SQResult result;
			SQStats stats;


			result = strategyResults.getResult(SQConst.SYMBOL_PORTFOLIO);
			stats = result.getStats(SQConst.DIRECTION_ALL, SQConst.PL_IN_MONEY, SQConst.KEY_SAMPLE_ALL);
		
			assertEquals("Test 5 NumberOfTrades", 499, stats.getInt("NumberOfTrades"));
			assertEquals("Test 5 NetProfit", 284873.49, stats.getDouble("NetProfit"));
		} catch (Exception e) {
			fail("Exception "+ e.getMessage());
		}
	}	
}