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

import com.strategyquant.lib.results.SQOrder;
import com.strategyquant.lib.results.SQOrderList;
import com.strategyquant.lib.snippets.WhatIf;

public class UseDifferentInitialBalance extends WhatIf {

	public UseDifferentInitialBalance() {
		setName("Use different initial balance");
        
      addDoubleParameter("Original balance", "Original balance", 10000d, 1d, 10000000d, 1000d);
		addDoubleParameter("New initial balance", "New initial balance", 20000d, 1d, 10000000d, 1000d);

		setFormatedName("Use {New initial balance} initial balance");
	}

	/**
	 * Function receives list of all orders sorted by open time and it could manipulate 
	 * the list and remove any order that matches certain filter from the list.     
	 * 
	 * 
	 * Order structure:
	 *   .Ticket - order ticket (integer number)
	 *   .Size - order size
	 *   .Type - order type: 1 - long trade, 2 - short trade     
	 *   .PL - trade resulting Profit/Loss in $
	 *   .PctPL - trade resulting Profit/Loss in %
	 *   .PctPL_TWR - trade resulting Profit/Loss in % computed as Time-Weighted Return
	 *   .PipsPL - trade resulting Profit/Loss in pips
	 *   .OpenPrice - opening price of an order
	 *   .OpenTime - opening time of an order (in seconds from 1.1.1970)
	 *   .StopLoss - stop loss level set on the trade
	 *   .TakeProfit - profit target level set on the trade
	 *   .ClosePrice - closing price of an order
	 *   .CloseTime - closing time of an order (in seconds from 1.1.1970)
	 *   .CloseType - closing type
	 *   .DD - drawdown during this trade in $                                            
	 *   .PctDD - drawdown during this trade in %                                            
	 *   .PipsDD - drawdown during this trade in pips
	 *   .AccountBalance - account balance during this trade                                                 
	 *   .Symbol - instrument symbol (for example "EURUSD")
	 *                    
	 * @param originalOrders - list of original orders that can be changed. Each order has the order properties specified above
	 */	
	@Override
	public void filter(SQOrderList originalOrders) throws Exception {
		double originalBalance = getDoubleParameterValue("Original balance");
      double newInitialBalance = getDoubleParameterValue("New initial balance");

      double ratio = newInitialBalance / originalBalance;
      
		for(SQOrder order : originalOrders) {
			if(order.isBalanceOrder()) continue;

         // recompute order size
         order.Size *= ratio;

			// recompute P/L for the new size
         order.PL *= ratio;

		}
	}
}