Forex_Strategy_Builder.Indicator_Compilation_Manager.LoadCompileSourceFile C# (CSharp) Method

LoadCompileSourceFile() public method

Load file, compile it and create/load the indicators into the CustomIndicatorsList.
public LoadCompileSourceFile ( string filePath, string &errorMessages ) : void
filePath string
errorMessages string
return void
        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 listCustomIndicators)
                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 sIndicatorName in Indicator_Store.OriginalIndicatorNames)
                if (sIndicatorName == newIndicator.IndicatorName)
                {
                    errorMessages = "The name '" + sIndicatorName + "' 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
            listCustomIndicators.Add(newIndicator);

            errorMessages = string.Empty;
            return;
        }

Usage Example

示例#1
0
        /// <summary>
        /// Load Source Files
        /// </summary>
        public static void LoadCustomIndicators()
        {
            _indicatorManager = new Indicator_Compilation_Manager();

            if (!Directory.Exists(Data.SourceFolder))
            {
                System.Windows.Forms.MessageBox.Show("Custom indicators folder does not exist!", Language.T("Custom Indicators"));
                Indicator_Store.ResetCustomIndicators(null);
                Indicator_Store.CombineAllIndicators();
                return;
            }

            string[] pathInputFiles = Directory.GetFiles(Data.SourceFolder, "*.cs");
            if (pathInputFiles.Length == 0)
            {
                System.Windows.Forms.MessageBox.Show("No custom indicator files found out!", Language.T("Custom Indicators"));
                Indicator_Store.ResetCustomIndicators(null);
                Indicator_Store.CombineAllIndicators();
                return;
            }

            StringBuilder errorReport = new StringBuilder();

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

            foreach (string filePath in pathInputFiles)
            {
                string errorMessages;
                _indicatorManager.LoadCompileSourceFile(filePath, out errorMessages);

                if (!string.IsNullOrEmpty(errorMessages))
                {
                    isError = true;

                    errorReport.AppendLine("<h2>File name: " + Path.GetFileName(filePath) + "</h2>");
                    string error = errorMessages.Replace(Environment.NewLine, "</br>");
                    error = error.Replace("\t", "&nbsp; &nbsp; &nbsp;");
                    errorReport.AppendLine("<p>" + error + "</p>");
                }
            }

            // Adds the custom indicators
            Indicator_Store.ResetCustomIndicators(_indicatorManager.CustomIndicatorsList);
            Indicator_Store.CombineAllIndicators();

            if (isError)
            {
                Fancy_Message_Box msgBox = new Fancy_Message_Box(errorReport.ToString(), Language.T("Custom Indicators"));
                msgBox.BoxWidth  = 550;
                msgBox.BoxHeight = 340;
                msgBox.TopMost   = true;
                msgBox.Show();
            }

            if (Configs.ShowCustomIndicators)
            {
                ShowLoadedCustomIndicators();
            }

            return;
        }
All Usage Examples Of Forex_Strategy_Builder.Indicator_Compilation_Manager::LoadCompileSourceFile