Example #1
0
        public static void WriteHeaderLines(string fileType, PTMagic ptmagicInstance)
        {
            List <string> fileLines = (List <string>)ptmagicInstance.GetType().GetProperty(fileType + "Lines").GetValue(ptmagicInstance, null);

            // Writing Header lines
            fileLines.Insert(0, "");
            fileLines.Insert(0, "# ####################################");
            fileLines.Insert(0, "# PTMagic_LastChanged = " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
            fileLines.Insert(0, "# PTMagic_ActiveSetting = " + SystemHelper.StripBadCode(ptmagicInstance.DefaultSettingName, Constants.WhiteListProperties));
            fileLines.Insert(0, "# ####################################");

            ptmagicInstance.GetType().GetProperty(fileType + "Lines").SetValue(ptmagicInstance, fileLines);
        }
Example #2
0
        public static void BuildPropertyLines(string fileType, PTMagic ptmagicInstance, GlobalSetting setting)
        {
            List <string> result = new List <string>();

            List <string> fileLines = (List <string>)ptmagicInstance.GetType().GetProperty(fileType + "Lines").GetValue(ptmagicInstance, null);

            Dictionary <string, object> properties = (Dictionary <string, object>)setting.GetType().GetProperty(fileType + "Properties").GetValue(setting, null);

            if (properties != null)
            {
                // Building Properties
                if (!setting.SettingName.Equals(ptmagicInstance.DefaultSettingName, StringComparison.InvariantCultureIgnoreCase) && ptmagicInstance.PTMagicConfiguration.GeneralSettings.Application.AlwaysLoadDefaultBeforeSwitch && !properties.ContainsKey("File"))
                {
                    // Load default settings as basis for the switch
                    GlobalSetting defaultSetting = ptmagicInstance.PTMagicConfiguration.AnalyzerSettings.GlobalSettings.Find(a => a.SettingName.Equals(ptmagicInstance.DefaultSettingName, StringComparison.InvariantCultureIgnoreCase));
                    if (defaultSetting != null)
                    {
                        Dictionary <string, object> defaultProperties = new Dictionary <string, object>();
                        switch (fileType.ToLower())
                        {
                        case "pairs":
                            defaultProperties = defaultSetting.PairsProperties;
                            break;

                        case "dca":
                            defaultProperties = defaultSetting.DCAProperties;
                            break;

                        case "inidcators":
                            defaultProperties = defaultSetting.IndicatorsProperties;
                            break;
                        }

                        if (defaultProperties.ContainsKey("File"))
                        {
                            fileLines = SettingsFiles.GetPresetFileLinesAsList(defaultSetting.SettingName, defaultProperties["File"].ToString(), ptmagicInstance.PTMagicConfiguration);
                        }
                    }
                }
                else
                {
                    // Check if settings are configured in a seperate file
                    if (properties.ContainsKey("File"))
                    {
                        fileLines = SettingsFiles.GetPresetFileLinesAsList(setting.SettingName, properties["File"].ToString(), ptmagicInstance.PTMagicConfiguration);
                    }
                }

                foreach (string line in fileLines)
                {
                    if (line.IndexOf("PTMagic_ActiveSetting", StringComparison.InvariantCultureIgnoreCase) > -1)
                    {
                        // Setting current active setting
                        result.Add("# PTMagic_ActiveSetting = " + setting.SettingName);
                    }
                    else if (line.IndexOf("PTMagic_LastChanged", StringComparison.InvariantCultureIgnoreCase) > -1)
                    {
                        // Setting last change datetime
                        result.Add("# PTMagic_LastChanged = " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
                    }
                    else if (line.IndexOf("PTMagic_SingleMarketSettings", StringComparison.InvariantCultureIgnoreCase) > -1)
                    {
                        // Single Market Settings will get overwritten every single run => crop the lines
                        break;
                    }
                    else
                    {
                        // Writing property items
                        int oldResultCount = result.Count;
                        if (properties != null)
                        {
                            foreach (string settingProperty in properties.Keys)
                            {
                                result = SettingsHandler.BuildPropertyLine(result, setting.SettingName, line, properties, settingProperty);
                            }
                        }
                        if (oldResultCount == result.Count)
                        {
                            result.Add(line);
                        }
                    }
                }
            }

            ptmagicInstance.GetType().GetProperty(fileType + "Lines").SetValue(ptmagicInstance, result);
        }
Example #3
0
        public static void BuildPropertyLines(string fileType, PTMagic ptmagicInstance, GlobalSetting setting, DateTime settingLastChanged)
        {
            bool          headerLinesExist = false;
            List <string> result           = new List <string>();
            List <string> fileLines        = null;

            // Analsye the properties for the setting and apply
            Dictionary <string, object> properties = (Dictionary <string, object>)setting.GetType().GetProperty(fileType + "Properties").GetValue(setting, null);

            // Building Properties
            if (properties == null || !properties.ContainsKey("File"))
            {
                // Load default settings as basis for the switch
                GlobalSetting defaultSetting = ptmagicInstance.PTMagicConfiguration.AnalyzerSettings.GlobalSettings.Find(a => a.SettingName.Equals(ptmagicInstance.DefaultSettingName, StringComparison.InvariantCultureIgnoreCase));
                Dictionary <string, object> defaultProperties = (Dictionary <string, object>)defaultSetting.GetType().GetProperty(fileType + "Properties").GetValue(defaultSetting, null);

                if (defaultProperties.ContainsKey("File"))
                {
                    // Load the default settings file lines
                    fileLines = SettingsFiles.GetPresetFileLinesAsList(defaultSetting.SettingName, defaultProperties["File"].ToString(), ptmagicInstance.PTMagicConfiguration);
                }
                else
                {
                    // No preset file defined, this is a bad settings file!
                    throw new ApplicationException(string.Format("No 'File' setting found in the '{0}Properties' of the 'Default' setting section in the 'settings.analyzer.json' file; this must be defined!", fileType));
                }
            }
            else
            {
                // Settings are configured in a seperate file
                fileLines = SettingsFiles.GetPresetFileLinesAsList(setting.SettingName, properties["File"].ToString(), ptmagicInstance.PTMagicConfiguration);
            }

            // Check for PTM header in preset file
            // Loop through config line by line reprocessing where required.
            foreach (string line in fileLines)
            {
                if (line.IndexOf("PTMagic_ActiveSetting", StringComparison.InvariantCultureIgnoreCase) > -1)
                {
                    // Setting current active setting
                    result.Add("# PTMagic_ActiveSetting = " + setting.SettingName);
                    headerLinesExist = true;
                }
                else if (line.IndexOf("PTMagic_LastChanged", StringComparison.InvariantCultureIgnoreCase) > -1)
                {
                    // Setting last change datetime
                    result.Add("# PTMagic_LastChanged = " + settingLastChanged.ToShortDateString() + " " + settingLastChanged.ToShortTimeString());
                }
                else if (line.IndexOf("PTMagic_SingleMarketSettings", StringComparison.InvariantCultureIgnoreCase) > -1)
                {
                    // Single Market Settings will get overwritten every single run => crop the lines
                    break;
                }
                else if (IsPropertyLine(line))
                {
                    // We have got a property line
                    if (properties != null)
                    {
                        bool madeSubstitution = false;

                        foreach (string settingProperty in properties.Keys)
                        {
                            if (madeSubstitution)
                            {
                                // We've made a substitution so no need to process the rest of the properties
                                break;
                            }
                            else
                            {
                                madeSubstitution = SettingsHandler.BuildPropertyLine(result, setting.SettingName, line, properties, settingProperty);
                            }
                        }

                        if (!madeSubstitution)
                        {
                            // No substitution made, so simply copy the line
                            result.Add(line);
                        }
                    }
                }
                else
                {
                    // Non property line, just copy it
                    result.Add(line);
                }
            }

            // Write header lines if required
            if (!headerLinesExist)
            {
                WriteHeaderLines(setting.SettingName, settingLastChanged, result);
            }

            // Save lines to current context for the file type
            ptmagicInstance.GetType().GetProperty(fileType + "Lines").SetValue(ptmagicInstance, result);
        }