Summary:
This script is used for optimizing the portfolio script parameters for the best average price change %.

                    
#region Namespaces using System; using System.IO; using System.Linq; #endregion namespace ScriptCode { /// <summary> /// Performance metric scripts are used for calculating performance statistics. /// /// This script can be used in several ways: /// (1) It can be displayed as part of a desktop's performance results. /// (2) It can be used as an optimization score when optimizing a desktop. /// </summary> public partial class MyPerformanceMetric : PerformanceMetricScriptBase // NEVER CHANGE THE CLASS NAME { #region Variables // Use for indicating whether to find the maximum or minimum value. private bool _findMaximum; #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. /// </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="findMaximum" type="Boolean" default="True">Use for indicating whether to find the maximum or minimum value.</param> public void OnInitialize( bool findMaximum) { // Set the find maximum flag. _findMaximum = findMaximum; } #endregion #region OnCalculateMetric /// <summary> /// This function calculates the performance metric for the specified desktop strategies. /// </summary> /// <param name="startUtcDateTime" type="DateTime">The start UTC date/time from which to calculate the metric</param> /// <param name="endUtcDateTime" type="DateTime">The end UTC date/time up to which to calculate the metric</param> /// <param name="strategyNumbers" type="IntegerArray">The desktop strategy numbers on which to calculate the metric</param> /// <param name="useTrades" type="Boolean">Indicates whether to use trades (true) or positions (false) for the calculation</param> /// <returns type="Double">The performance metric based on the specified settings.</returns> public override double OnCalculateMetric( long startUtcDateTime, long endUtcDateTime, int[] strategyNumbers, bool useTrades) { // The statistics request that is used to calculate the performance metric. StatsRequest(strategyNumbers, startUtcDateTime, endUtcDateTime, true, true, true, true, useTrades); // Check whether to calculate the maximum value. if (_findMaximum) { return StatsAveragePricePercentChange(); } else { return - 1 * StatsAveragePricePercentChange(); } } #endregion #region OnShutdown /// <summary> /// This function is called when the script is shutdown. /// </summary> public override void OnShutdown() { } #endregion } }

The Algorithmic Trading Software for Hedge Funds and Quants