Пример #1
0
        /// <summary>
        /// Does the job
        /// </summary>
        private static void DoWorkTestCustomIndicators(object sender, DoWorkEventArgs e)
        {
            bool isErrors = false;

            var errorReport = new StringBuilder();

            errorReport.AppendLine("<h1>" + Language.T("Custom Indicators") + "</h1>");

            var okReport = new StringBuilder();

            okReport.AppendLine("<h1>" + Language.T("Custom Indicators") + "</h1>");
            okReport.AppendLine("<p>");

            foreach (string indicatorName in IndicatorStore.CustomIndicatorNames)
            {
                string errorList;
                if (!IndicatorTester.CustomIndicatorThoroughTest(indicatorName, out errorList))
                {
                    isErrors = true;
                    errorReport.AppendLine("<h2>" + indicatorName + "</h2>");
                    string error = errorList.Replace(Environment.NewLine, "</br>");
                    error = error.Replace("\t", "&nbsp; &nbsp; &nbsp;");
                    errorReport.AppendLine("<p>" + error + "</p>");
                }
                else
                {
                    okReport.AppendLine(indicatorName + " - OK" + "<br />");
                }
            }

            okReport.AppendLine("</p>");

            var result = new CustomIndicatorsTestResult
            {
                IsErrors = isErrors, ErrorReport = errorReport.ToString(), OKReport = okReport.ToString()
            };

            e.Result = result;
        }
        /// <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.
                var 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 IndicatorStore.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 (!IndicatorTester.CustomIndicatorFastTest(newIndicator, out errorTestIndicator))
            {
                // Testing the indicator failed.
                errorMessages = errorTestIndicator;
                return;
            }

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

            errorMessages = string.Empty;
        }