Forex_Strategy_Builder.Indicator_Tester.CustomIndicatorFastTest C# (CSharp) Method

CustomIndicatorFastTest() public static method

Tests general parameters of a custom indicator.
public static CustomIndicatorFastTest ( Indicator indicator, string &sErrorList ) : bool
indicator Indicator The indicator.
sErrorList string
return bool
        public static bool CustomIndicatorFastTest(Indicator indicator, out string sErrorList)
        {
            bool bIsOk = true;

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("ERROR: Indicator test failed for the '" + indicator.IndicatorName + "' indicator.");

            // Tests the IndicatorName property.
            if (string.IsNullOrEmpty(indicator.IndicatorName))
            {
                sb.AppendLine("\tThe property 'IndicatorName' is not set.");
                bIsOk = false;
            }

            // Tests the PossibleSlots property.
            if (!indicator.TestPossibleSlot(SlotTypes.Open)       &&
                !indicator.TestPossibleSlot(SlotTypes.OpenFilter) &&
                !indicator.TestPossibleSlot(SlotTypes.Close)      &&
                !indicator.TestPossibleSlot(SlotTypes.CloseFilter))
            {
                sb.AppendLine("\tThe property 'PossibleSlots' is not set.");
                bIsOk = false;
            }

            // Tests the CustomIndicator property.
            if (!indicator.CustomIndicator)
            {
                sb.AppendLine("\tThe indicator '" + indicator.IndicatorName + "' is not marked as custom. Set CustomIndicator = true;");
                bIsOk = false;
            }

            // Tests the SeparatedChartMaxValue properties.
            if (!indicator.SeparatedChart && indicator.SeparatedChartMaxValue != double.MinValue)
            {
                sb.AppendLine("\tSet SeparatedChart = true; or remove the property: SeparatedChartMaxValue = " + indicator.SeparatedChartMaxValue.ToString());
                bIsOk = false;
            }

            // Tests the SeparatedChartMinValue properties.
            if (!indicator.SeparatedChart && indicator.SeparatedChartMinValue != double.MaxValue)
            {
                sb.AppendLine("\tSet SeparatedChart = true; or remove the property: SeparatedChartMinValue = " + indicator.SeparatedChartMinValue.ToString());
                bIsOk = false;
            }

            // Tests the SpecialValues properties.
            if (!indicator.SeparatedChart && indicator.SpecialValues.Length > 0)
            {
                sb.AppendLine("\tSet SeparatedChart = true; or remove the property SpecialValues");
                bIsOk = false;
            }

            // Tests the IndParam properties.
            if (indicator.IndParam == null)
            {
                sb.AppendLine("\tThe property IndParam is not set. Set IndParam = new IndicatorParam();");
                bIsOk = false;
            }

            // Tests the IndParam.IndicatorName properties.
            if (indicator.IndParam.IndicatorName != indicator.IndicatorName)
            {
                sb.AppendLine("\tThe property IndParam.IndicatorName is not set. Set IndParam.IndicatorName = IndicatorName;");
                bIsOk = false;
            }

            // Tests the IndParam.SlotType properties.
            if (indicator.IndParam.SlotType != SlotTypes.NotDefined)
            {
                sb.AppendLine("\tThe property IndParam.SlotType is not set. Set IndParam.SlotType = slotType;");
                bIsOk = false;
            }

            // Tests the IndParam.ListParam properties.
            for (int iParam = 0; iParam < indicator.IndParam.ListParam.Length; iParam++)
            {
                ListParam listParam = indicator.IndParam.ListParam[iParam];
                if (!listParam.Enabled)
                    continue;

                if (string.IsNullOrEmpty(listParam.Caption))
                {
                    sb.AppendLine("\tThe property IndParam.ListParam[" + iParam + "].Caption is not set.");
                    bIsOk = false;
                }

                if (listParam.ItemList.Length == 0)
                {
                    sb.AppendLine("\tThe property IndParam.ListParam[" + iParam + "].ItemList is not set.");
                    bIsOk = false;
                }

                if (listParam.ItemList[listParam.Index] != listParam.Text)
                {
                    sb.AppendLine("\tThe property IndParam.ListParam[" + iParam + "].Text is wrong." +
                        " Set " + "IndParam.ListParam[" + iParam + "].Text = IndParam.ListParam[" + iParam +
                        "].ItemList[IndParam.ListParam[" + iParam + "].Index];");
                    bIsOk = false;
                }

                if (string.IsNullOrEmpty(listParam.ToolTip))
                {
                    sb.AppendLine("\tThe property IndParam.ListParam[" + iParam + "].ToolTip is not set.");
                    bIsOk = false;
                }
            }

            // Tests the IndParam.NumParam properties.
            for (int iParam = 0; iParam < indicator.IndParam.NumParam.Length; iParam++)
            {
                NumericParam numParam = indicator.IndParam.NumParam[iParam];
                if (!numParam.Enabled)
                    continue;

                if (string.IsNullOrEmpty(numParam.Caption))
                {
                    sb.AppendLine("\tThe property IndParam.NumParam[" + iParam + "].Caption is not set.");
                    bIsOk = false;
                }

                double dValue = numParam.Value;
                double dMin   = numParam.Min;
                double dMax   = numParam.Max;
                double dPoint = numParam.Point;

                if (dMin > dMax)
                {
                    sb.AppendLine("\tIndParam.NumParam[" + iParam + "].Min > IndParam.NumParam[" + iParam + "].Max.");
                    bIsOk = false;
                }

                if (dValue > dMax)
                {
                    sb.AppendLine("\tIndParam.NumParam[" + iParam + "].Value > IndParam.NumParam[" + iParam + "].Max.");
                    bIsOk = false;
                }

                if (dValue < dMin)
                {
                    sb.AppendLine("\tIndParam.NumParam[" + iParam + "].Value < IndParam.NumParam[" + iParam + "].Min.");
                    bIsOk = false;
                }

                if (dPoint < 0)
                {
                    sb.AppendLine("\tIndParam.NumParam[" + iParam + "].Point cannot be < 0");
                    bIsOk = false;
                }

                if (dPoint > 6)
                {
                    sb.AppendLine("\tIndParam.NumParam[" + iParam + "].Point cannot be > 6");
                    bIsOk = false;
                }

                if (string.IsNullOrEmpty(numParam.ToolTip))
                {
                    sb.AppendLine("\tThe property IndParam.NumParam[" + iParam + "].ToolTip is not set.");
                    bIsOk = false;
                }
            }

            // Tests the IndParam.CheckParam properties.
            for (int iParam = 0; iParam < indicator.IndParam.CheckParam.Length; iParam++)
            {
                CheckParam checkParam = indicator.IndParam.CheckParam[iParam];
                if (!checkParam.Enabled)
                    continue;

                if (string.IsNullOrEmpty(checkParam.Caption))
                {
                    sb.AppendLine("\tThe property IndParam.CheckParam[" + iParam + "].Caption is not set.");
                    bIsOk = false;
                }

                if (string.IsNullOrEmpty(checkParam.ToolTip))
                {
                    sb.AppendLine("\tThe property IndParam.CheckParam[" + iParam + "].ToolTip is not set.");
                    bIsOk = false;
                }
            }

            try
            {
                indicator.Calculate(SlotTypes.NotDefined);
            }
            catch (System.Exception exc)
            {
                sb.AppendLine("\tError when executing Calculate(SlotTypes.NotDefined). " + exc.Message);
                bIsOk = false;
            }

            try
            {
                indicator.SetDescription(SlotTypes.NotDefined);
            }
            catch (System.Exception exc)
            {
                sb.AppendLine("\tError when executing SetDescription(SlotTypes.NotDefined). " + exc.Message);
                bIsOk = false;
            }

            try
            {
                indicator.ToString();
            }
            catch (System.Exception exc)
            {
                sb.AppendLine("\tError when executing ToString(). " + exc.Message);
                bIsOk = false;
            }

            if (bIsOk)
                sErrorList = string.Empty;
            else
                sErrorList = sb.ToString();

            return bIsOk;
        }

Usage Example

Esempio n. 1
0
        /// <summary>
        /// Load file, compile it and create/load the indicators into the CustomIndicatorsList.
        /// </summary>
        /// <param name="filePath">Path to the source file</param>
        /// <param name="errorMessages">Resulting error messages, if any.</param>
        public void LoadCompileSourceFile(string filePath, out string errorMessages)
        {
            string errorLoadSourceFile;
            string source = LoadSourceFile(filePath, out errorLoadSourceFile);

            if (string.IsNullOrEmpty(source))
            {   // Source file loading failed.
                errorMessages = errorLoadSourceFile;
                return;
            }

            Dictionary <string, int> dictCompilationErrors;
            Assembly assembly = _compiler.CompileSource(source, out dictCompilationErrors);

            if (assembly == null)
            {   // Assembly compilation failed.
                StringBuilder sbCompilationError = new StringBuilder();
                sbCompilationError.AppendLine("ERROR: Indicator compilation failed in file [" + Path.GetFileName(filePath) + "]");

                foreach (string error in dictCompilationErrors.Keys)
                {
                    sbCompilationError.AppendLine('\t' + error);
                }

                errorMessages = sbCompilationError.ToString();
                return;
            }

            string    errorGetIndicator;
            string    indicatorFileName = Path.GetFileNameWithoutExtension(filePath);
            Indicator newIndicator      = GetIndicatorInstanceFromAssembly(assembly, indicatorFileName, out errorGetIndicator);

            if (newIndicator == null)
            {   // Getting an indicator instance failed.
                errorMessages = errorGetIndicator;
                return;
            }

            // Check for a repeated indicator name among the custom indicators
            foreach (Indicator indicator in _customIndicatorsList)
            {
                if (indicator.IndicatorName == newIndicator.IndicatorName)
                {
                    errorMessages = "The name '" + newIndicator.IndicatorName + "' found out in [" + Path.GetFileName(filePath) + "] is already in use.";
                    return;
                }
            }

            // Check for a repeated indicator name among the original indicators
            foreach (string indicatorName in Indicator_Store.OriginalIndicatorNames)
            {
                if (indicatorName == newIndicator.IndicatorName)
                {
                    errorMessages = "The name '" + indicatorName + "' found out in [" + Path.GetFileName(filePath) + "] is already in use.";
                    return;
                }
            }

            // Test the new custom indicator
            string errorTestIndicator;

            if (!Indicator_Tester.CustomIndicatorFastTest(newIndicator, out errorTestIndicator))
            {   // Testing the indicator failed.
                errorMessages = errorTestIndicator;
                return;
            }

            // Adds the custom indicator to the list
            _customIndicatorsList.Add(newIndicator);

            errorMessages = string.Empty;
            return;
        }