Commission by Instrument

#region Namespaces
using System;
using System.IO;
using System.Linq;
#endregion

namespace ScriptCode
{
    /// <summary>
    /// Commission scripts are used for calculating the commission paid to a broker for each executed order,
    /// based on the broker's exact commission schedule.
    /// </summary>
    public partial class MyCommission : CommissionScriptBase // NEVER CHANGE THE CLASS NAME
    {
        #region Variables
        // The commission amount paid for each executed stock order. 
		private double _commissionPerStockOrder;
		// The commission amount paid for each share of the executed order.
		private double _commissionPerStockShare;
		// The commission amount paid for each executed futures order.
		private double _commissionPerFuturesOrder;
		// The commission amount paid for each futures contract of the executed order.
		private double _commissionPerFuturesContract;
		// The commission amount paid for each executed forex order.
		private double _commissionPerForexOrder;
		// The commission amount paid for each forex unit of the executed order.
		private double _commissionPerForexUnit;
        #endregion

        #region OnInitialize
        /// <summary>
        /// This function is used for accepting the script parameters and for initializing the script prior to all other function calls. 
        /// Once the script is assigned to a desktop, its parameter values can be specified by the user and can be selected for optimization. 
        /// </summary>
        /// --------------------------------------------------------------------------------------------------
        /// PLEASE USE THE SCRIPT WIZARD (CTRL+W) TO ADD, EDIT AND REMOVE THE SCRIPT PARAMETERS
        /// --------------------------------------------------------------------------------------------------
        /// YOU MUST SET A PARAM TAG FOR EACH PARAMETER ACCEPTED BY THIS FUNCTION.
        /// ALL PARAM TAGS SHOULD BE SET IN THE 'OnInitialize' REGION, RIGHT ABOVE THE 'OnInitialize' FUNCTION.
        /// THE ORDER OF THE TAGS MUST MATCH THE ORDER OF THE ACTUAL PARAMETERS.

        /// REQUIRED ATTRIBUTES:
        /// (1) name: The exact parameter name.
        /// (2) type: The type of data to collect from the user: 
        /// Set to "Integer" when the data type is 'int'
        /// Set to "IntegerArray" when the data type is 'int[]'
        /// Set to "DateTime" when the data type is 'long'  
        /// Set to "DateTimeArray" when the data type is 'long[]'  
        /// Set to "Boolean" when the data type is 'bool'
        /// Set to "BooleanArray" when the data type is 'bool[]'
        /// Set to "Double" when the data type is 'double'
        /// Set to "DoubleArray" when the data type is 'double[]'
        /// Set to "String" when the data type is 'string'
        /// Set to "StringArray" when the data type is 'string[]'

        /// OPTIONAL ATTRIBUTES:
        /// (3) default: The default parameter value is only valid when the type is Integer, Boolean, Double, String or an API Type. 
        /// (4) min: The minimum parameter value is only valid when the type is Integer or Double.
        /// (5) max: The maximum parameter value is only valid when the type is Integer or Double.

        /// EXAMPLE: <param name="" type="" default="" min="" max="">Enter the parameter description here.</param>
        /// --------------------------------------------------------------------------------------------------
		/// <param name="commissionPerStockOrder" type="Double" default="10">The commission amount paid for each executed stock or ETF order.</param>
		/// <param name="commissionPerStockShare" type="Double" default="0.01">The commission amount paid for each share of the executed order.</param>
		/// <param name="commissionPerFuturesOrder" type="Double" default="0.25">The commission amount paid for each executed futures or CFD order.</param>
		/// <param name="commissionPerFuturesContract" type="Double" default="1.5">The commission amount paid for each futures or CFD contract of the executed order.</param>
		/// <param name="commissionPerForexOrder" type="Double" default="1.0">The commission amount paid for each executed forex order.</param>
		/// <param name="commissionPerForexUnit" type="Double" default="0.00002">The commission amount paid for each forex unit of the executed order.</param>
        public void OnInitialize(
			double commissionPerStockOrder,
			double commissionPerStockShare,
			double commissionPerFuturesOrder,
			double commissionPerFuturesContract,
			double commissionPerForexOrder,
			double commissionPerForexUnit)
        {
		    // Set the parameters to script variables. 
			_commissionPerStockOrder = commissionPerStockOrder;
			_commissionPerStockShare = commissionPerStockShare;
			_commissionPerFuturesOrder = commissionPerFuturesOrder;
			_commissionPerFuturesContract = commissionPerFuturesContract;
			_commissionPerForexOrder = commissionPerForexOrder;
			_commissionPerForexUnit = commissionPerForexUnit;
        }
        #endregion

        #region OnCommission
        /// <summary>
        /// This function is called to calculate the commission paid to a broker for a specified order fill.
	    /// Note that the commission is denominated in the currency of the specified desktop strategy.
        /// </summary>
        /// <param name="strategyNumber" type="Integer">The strategy number to which the order belongs</param>
        /// <param name="orderIndex" type="Integer">The order index in the orders table to which the order fill belongs</param>
        /// <param name="orderFillIndex" type="Integer">The order fill index (0 being the first fill for the order, 1 being the next fill, etc.)</param>
        /// <param name="fillQuantity" type="Double">The quantity of the new order fill for the specified order</param>
        /// <param name="fillPrice" type="Double">The price of the new order fill for the specified order</param>
        /// <returns type="Double">The commission paid for the specified order fill, in the desktop strategy currency.</returns>
        public override double OnCommission(
            int strategyNumber,
            int orderIndex,
            int orderFillIndex,
            double fillQuantity,
            double fillPrice)
        {
		    // Get the symbol instrument.
			C_Instrument symbolInstrument = SymbolInstrument(strategyNumber, OrderSymbolIndex(strategyNumber, orderIndex));
			// Check whether this is the first order fill.
			if (orderFillIndex == 0) {
				// Check whether the order was for a stock or ETF symbol.
				if (symbolInstrument == C_Instrument.STOCK || symbolInstrument == C_Instrument.ETF) {
					return _commissionPerStockOrder + (_commissionPerStockShare * fillQuantity);
				}
				// Check whether the order was for a futures or CFD symbol.
				if (symbolInstrument == C_Instrument.FUTURE || symbolInstrument == C_Instrument.CFD) {
					return _commissionPerFuturesOrder + (_commissionPerFuturesContract * fillQuantity);
				}
				// Check whether the order was for a FOREX symbol.
				if (symbolInstrument == C_Instrument.FOREX) {
					return _commissionPerForexOrder + (_commissionPerForexUnit * fillQuantity);
				}
			}
			// This isn't the first order fill.
			else {
				// Check whether the order was for a stock or ETF symbol.
				if (symbolInstrument == C_Instrument.STOCK || symbolInstrument == C_Instrument.ETF) {
					return _commissionPerStockShare * fillQuantity;
				}
				// Check whether the order was for a futures or CFD symbol.
				if (symbolInstrument == C_Instrument.FUTURE || symbolInstrument == C_Instrument.CFD) {
					return _commissionPerFuturesContract * fillQuantity;
				}
				// Check whether the order was for a FOREX symbol.
				if (symbolInstrument == C_Instrument.FOREX) {
					return _commissionPerForexUnit * fillQuantity;
				}
			}
			return 0;
        }
        #endregion

        #region OnShutdown
        /// <summary>
        /// This function is called when the script is shutdown.
        /// </summary>
        public override void OnShutdown() 
        { 
		    // OnShutdown Content
        }
        #endregion
    }
}