예제 #1
0
        public static string GetActiveSetting(PTMagicConfiguration systemConfiguration, string pairsFileName, string dcaFileName, string indicatorsFileName, LogHelper log)
        {
            string pairsPropertiesPath = systemConfiguration.GeneralSettings.Application.ProfitTrailerPath + Constants.PTPathTrading + Path.DirectorySeparatorChar + pairsFileName;

            string result = SettingsFiles.GetActiveSettingFromFile(pairsPropertiesPath, systemConfiguration, log);

            if (result.Equals(""))
            {
                SettingsFiles.WriteHeaderLines(pairsPropertiesPath, "Default", systemConfiguration);

                string dcaPropertiesPath = systemConfiguration.GeneralSettings.Application.ProfitTrailerPath + Constants.PTPathTrading + Path.DirectorySeparatorChar + dcaFileName;
                SettingsFiles.WriteHeaderLines(dcaPropertiesPath, "Default", systemConfiguration);

                string inditactorsPropertiesPath = systemConfiguration.GeneralSettings.Application.ProfitTrailerPath + Constants.PTPathTrading + Path.DirectorySeparatorChar + indicatorsFileName;
                SettingsFiles.WriteHeaderLines(inditactorsPropertiesPath, "Default", systemConfiguration);
            }


            return(result);
        }
예제 #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);
        }
예제 #3
0
        public static bool CheckPresets(PTMagicConfiguration systemConfiguration, LogHelper log, bool forceCheck)
        {
            if (!forceCheck)
            {
                // If the check is not enforced, check for file changes
                string[] presetFilePaths = Directory.GetFiles(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + Constants.PTMagicPathPresets, "*.*", SearchOption.AllDirectories);
                foreach (string presetFilePath in presetFilePaths)
                {
                    if (presetFilePath.IndexOf(".properties", StringComparison.InvariantCultureIgnoreCase) > -1)
                    {
                        FileInfo presetFile = new FileInfo(presetFilePath);
                        if (presetFile.LastWriteTimeUtc > DateTime.UtcNow.AddMinutes(-systemConfiguration.AnalyzerSettings.MarketAnalyzer.IntervalMinutes).AddSeconds(2))
                        {
                            // File has changed recently, force preparation check
                            log.DoLogInfo("Preset files changed, enforcing preparation check...");
                            forceCheck = true;
                            break;
                        }
                    }
                }
            }

            if (forceCheck)
            {
                log.DoLogInfo("Checking automated settings for presets...");
                foreach (GlobalSetting setting in systemConfiguration.AnalyzerSettings.GlobalSettings)
                {
                    if (setting.PairsProperties != null)
                    {
                        if (setting.PairsProperties.ContainsKey("File"))
                        {
                            setting.PairsProperties["File"] = SystemHelper.PropertyToString(setting.PairsProperties["File"]).Replace(".PROPERTIES", ".properties");

                            // Check preset PAIRS.PROPERTIES for header lines
                            string settingPairsPropertiesPath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + Constants.PTMagicPathPresets + Path.DirectorySeparatorChar + setting.SettingName + Path.DirectorySeparatorChar + setting.PairsProperties["File"];
                            string headerPairsSetting         = SettingsFiles.GetActiveSettingFromFile(settingPairsPropertiesPath, systemConfiguration, log);
                            if (headerPairsSetting.Equals(""))
                            {
                                if (!File.Exists(settingPairsPropertiesPath))
                                {
                                    Exception ex = new Exception("Not able to find preset file " + SystemHelper.PropertyToString(setting.PairsProperties["File"]) + " for '" + setting.SettingName + "'");
                                    log.DoLogCritical("Not able to find preset file " + SystemHelper.PropertyToString(setting.PairsProperties["File"]) + " for '" + setting.SettingName + "'", ex);
                                    throw ex;
                                }
                            }

                            log.DoLogInfo("Prepared " + SystemHelper.PropertyToString(setting.PairsProperties["File"]) + " for '" + setting.SettingName + "'");
                        }
                    }

                    if (setting.DCAProperties != null)
                    {
                        if (setting.DCAProperties.ContainsKey("File"))
                        {
                            setting.DCAProperties["File"] = SystemHelper.PropertyToString(setting.DCAProperties["File"]).Replace(".PROPERTIES", ".properties");

                            // Check preset DCA.PROPERTIES for header lines
                            string settingDCAPropertiesPath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + Constants.PTMagicPathPresets + Path.DirectorySeparatorChar + setting.SettingName + Path.DirectorySeparatorChar + setting.DCAProperties["File"];
                            string headerDCASetting         = SettingsFiles.GetActiveSettingFromFile(settingDCAPropertiesPath, systemConfiguration, log);
                            if (headerDCASetting.Equals(""))
                            {
                                if (!File.Exists(settingDCAPropertiesPath))
                                {
                                    Exception ex = new Exception("Not able to find preset file " + SystemHelper.PropertyToString(setting.DCAProperties["File"]) + " for '" + setting.SettingName + "'");
                                    log.DoLogCritical("Not able to find preset file " + SystemHelper.PropertyToString(setting.DCAProperties["File"]) + " for '" + setting.SettingName + "'", ex);
                                    throw ex;
                                }
                            }

                            log.DoLogInfo("Prepared " + SystemHelper.PropertyToString(setting.DCAProperties["File"]) + " for '" + setting.SettingName + "'");
                        }
                    }

                    if (setting.IndicatorsProperties != null)
                    {
                        if (setting.IndicatorsProperties.ContainsKey("File"))
                        {
                            setting.IndicatorsProperties["File"] = SystemHelper.PropertyToString(setting.IndicatorsProperties["File"]).Replace(".PROPERTIES", ".properties");

                            // Check preset INDICATORS.PROPERTIES for header lines
                            string settingIndicatorsPropertiesPath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + Constants.PTMagicPathPresets + Path.DirectorySeparatorChar + setting.SettingName + Path.DirectorySeparatorChar + setting.IndicatorsProperties["File"];
                            string headerIndicatorsSetting         = SettingsFiles.GetActiveSettingFromFile(settingIndicatorsPropertiesPath, systemConfiguration, log);
                            if (headerIndicatorsSetting.Equals(""))
                            {
                                if (!File.Exists(settingIndicatorsPropertiesPath))
                                {
                                    Exception ex = new Exception("Not able to find preset file " + SystemHelper.PropertyToString(setting.IndicatorsProperties["File"]) + " for '" + setting.SettingName + "'");
                                    log.DoLogCritical("Not able to find preset file " + SystemHelper.PropertyToString(setting.IndicatorsProperties["File"]) + " for '" + setting.SettingName + "'", ex);
                                    throw ex;
                                }
                            }

                            log.DoLogInfo("Prepared " + SystemHelper.PropertyToString(setting.IndicatorsProperties["File"]) + " for '" + setting.SettingName + "'");
                        }
                    }
                }
            }

            return(forceCheck);
        }
예제 #4
0
 public static List <string> GetPresetFileLinesAsList(string settingName, string fileName, PTMagicConfiguration systemConfiguration)
 {
     return(SettingsFiles.GetPresetFileLinesAsList(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar, settingName, fileName, systemConfiguration));
 }
예제 #5
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);
        }