public static List <string> BuildPropertyLinesForSingleMarketSetting(string mainMarket, string marketPair, List <SingleMarketSetting> appliedSettings, Dictionary <string, object> properties, Dictionary <string, List <string> > matchedTriggers, Dictionary <string, string> fullProperties, List <string> newPropertyLines, PTMagicConfiguration systemConfiguration, LogHelper log) { if (properties.Keys.Count > 0) { string appliedSettingsStringList = ""; foreach (SingleMarketSetting sms in appliedSettings) { if (!appliedSettingsStringList.Equals("")) { appliedSettingsStringList += ", "; } appliedSettingsStringList += sms.SettingName; } newPropertyLines.Add("# " + marketPair + " - Current active settings: " + appliedSettingsStringList); foreach (string settingProperty in properties.Keys) { int valueMode = Constants.ValueModeDefault; string propertyKey = settingProperty; // Check for offset values if (propertyKey.IndexOf("_OFFSETPERCENT") > -1) { valueMode = Constants.ValueModeOffsetPercent; propertyKey = propertyKey.Replace("_OFFSETPERCENT", ""); } else if (propertyKey.IndexOf("_OFFSET") > -1) { valueMode = Constants.ValueModeOffset; propertyKey = propertyKey.Replace("_OFFSET", ""); } string newValueString = SystemHelper.PropertyToString(properties[settingProperty]); if (newValueString.ToLower().Equals("true") || newValueString.ToLower().Equals("false")) { newValueString = newValueString.ToLower(); } string propertyMarketName = marketPair; // Adjust market pair name propertyMarketName = propertyMarketName.Replace(mainMarket, "").Replace("_", "").Replace("-", ""); string propertyKeyString = ""; if (propertyKey.StartsWith("ALL", StringComparison.InvariantCultureIgnoreCase)) { propertyKeyString = propertyKey.Replace("ALL", propertyMarketName, StringComparison.InvariantCultureIgnoreCase); } else if (propertyKey.StartsWith("DEFAULT", StringComparison.InvariantCultureIgnoreCase)) { propertyKeyString = propertyKey.Replace("DEFAULT", propertyMarketName, StringComparison.InvariantCultureIgnoreCase); } else { if (propertyKey.StartsWith("_", StringComparison.InvariantCultureIgnoreCase)) { propertyKeyString = propertyMarketName + propertyKey; } else { propertyKeyString = propertyMarketName + "_" + propertyKey; } } switch (valueMode) { case Constants.ValueModeOffset: // Offset value by a fixed amount double offsetValue = SystemHelper.TextToDouble(newValueString, 0, "en-US"); if (offsetValue != 0) { double oldValue = SystemHelper.TextToDouble(SettingsHandler.GetCurrentPropertyValue(fullProperties, propertyKey, propertyKey.Replace("ALL_", "DEFAULT_")), 0, "en-US"); newValueString = Math.Round((oldValue + offsetValue), 8).ToString(new System.Globalization.CultureInfo("en-US")); } break; case Constants.ValueModeOffsetPercent: // Offset value by percentage double offsetValuePercent = SystemHelper.TextToDouble(newValueString, 0, "en-US"); if (offsetValuePercent != 0) { double oldValue = SystemHelper.TextToDouble(SettingsHandler.GetCurrentPropertyValue(fullProperties, propertyKey, propertyKey.Replace("ALL_", "DEFAULT_")), 0, "en-US"); if (oldValue < 0) { offsetValuePercent = offsetValuePercent * -1; } double oldValueOffset = (oldValue * (offsetValuePercent / 100)); newValueString = Math.Round((oldValue + oldValueOffset), 8).ToString(new System.Globalization.CultureInfo("en-US")); } break; default: break; } newPropertyLines.Add(propertyKeyString + " = " + newValueString); } newPropertyLines.Add(""); } return(newPropertyLines); }
public static void CompileSingleMarketProperties(PTMagic ptmagicInstance, Dictionary <string, List <string> > matchedTriggers) { try { List <string> globalPairsLines = new List <string>(); List <string> globalDCALines = new List <string>(); List <string> globalIndicatorsLines = new List <string>(); List <string> newPairsLines = new List <string>(); List <string> newDCALines = new List <string>(); List <string> newIndicatorsLines = new List <string>(); foreach (string pairsLine in ptmagicInstance.PairsLines) { if (pairsLine.IndexOf("PTMagic_SingleMarketSettings", StringComparison.InvariantCultureIgnoreCase) > -1) { // Single Market Settings will get overwritten every single run => crop the lines break; } else { string globalPairsLine = pairsLine; globalPairsLines.Add(globalPairsLine); } } newPairsLines.Add("# PTMagic_SingleMarketSettings - Written on " + DateTime.Now.ToString()); newPairsLines.Add("# ########################################################################"); newPairsLines.Add(""); foreach (string dcaLine in ptmagicInstance.DCALines) { if (dcaLine.IndexOf("PTMagic_SingleMarketSettings", StringComparison.InvariantCultureIgnoreCase) > -1) { // Single Market Settings will get overwritten every single run => crop the lines break; } else { string globalDCALine = dcaLine; globalDCALines.Add(globalDCALine); } } newDCALines.Add("# PTMagic_SingleMarketSettings - Written on " + DateTime.Now.ToString()); newDCALines.Add("# ########################################################################"); newDCALines.Add(""); foreach (string indicatorsLine in ptmagicInstance.IndicatorsLines) { if (indicatorsLine.IndexOf("PTMagic_SingleMarketSettings", StringComparison.InvariantCultureIgnoreCase) > -1) { // Single Market Settings will get overwritten every single run => crop the lines break; } else { string globalIndicatorsLine = indicatorsLine; globalIndicatorsLines.Add(globalIndicatorsLine); } } Dictionary <string, string> globalPairsProperties = SettingsHandler.GetPropertiesAsDictionary(globalPairsLines); Dictionary <string, string> globalDCAProperties = SettingsHandler.GetPropertiesAsDictionary(globalDCALines); Dictionary <string, string> globalIndicatorsProperties = SettingsHandler.GetPropertiesAsDictionary(globalIndicatorsLines); newIndicatorsLines.Add("# PTMagic_SingleMarketSettings - Written on " + DateTime.Now.ToString()); newIndicatorsLines.Add("# ########################################################################"); newIndicatorsLines.Add(""); foreach (string marketPair in ptmagicInstance.TriggeredSingleMarketSettings.Keys.OrderBy(k => k)) { Dictionary <string, object> pairsPropertiesToApply = new Dictionary <string, object>(); Dictionary <string, object> dcaPropertiesToApply = new Dictionary <string, object>(); Dictionary <string, object> indicatorsPropertiesToApply = new Dictionary <string, object>(); // Build Properties as a whole list so that a single coin also has only one block with single market settings applied to it foreach (SingleMarketSetting setting in ptmagicInstance.TriggeredSingleMarketSettings[marketPair]) { ptmagicInstance.Log.DoLogInfo("Building single market settings '" + setting.SettingName + "' for '" + marketPair + "'..."); foreach (string settingPairsProperty in setting.PairsProperties.Keys) { if (!pairsPropertiesToApply.ContainsKey(settingPairsProperty)) { pairsPropertiesToApply.Add(settingPairsProperty, setting.PairsProperties[settingPairsProperty]); } else { pairsPropertiesToApply[settingPairsProperty] = setting.PairsProperties[settingPairsProperty]; } } foreach (string settingDCAProperty in setting.DCAProperties.Keys) { if (!dcaPropertiesToApply.ContainsKey(settingDCAProperty)) { dcaPropertiesToApply.Add(settingDCAProperty, setting.DCAProperties[settingDCAProperty]); } else { dcaPropertiesToApply[settingDCAProperty] = setting.DCAProperties[settingDCAProperty]; } } foreach (string settingIndicatorsProperty in setting.IndicatorsProperties.Keys) { if (!indicatorsPropertiesToApply.ContainsKey(settingIndicatorsProperty)) { indicatorsPropertiesToApply.Add(settingIndicatorsProperty, setting.IndicatorsProperties[settingIndicatorsProperty]); } else { indicatorsPropertiesToApply[settingIndicatorsProperty] = setting.IndicatorsProperties[settingIndicatorsProperty]; } } ptmagicInstance.Log.DoLogInfo("Built single market settings '" + setting.SettingName + "' for '" + marketPair + "'."); } newPairsLines = SettingsHandler.BuildPropertyLinesForSingleMarketSetting(ptmagicInstance.LastRuntimeSummary.MainMarket, marketPair, ptmagicInstance.TriggeredSingleMarketSettings[marketPair], pairsPropertiesToApply, matchedTriggers, globalPairsProperties, newPairsLines, ptmagicInstance.PTMagicConfiguration, ptmagicInstance.Log); newDCALines = SettingsHandler.BuildPropertyLinesForSingleMarketSetting(ptmagicInstance.LastRuntimeSummary.MainMarket, marketPair, ptmagicInstance.TriggeredSingleMarketSettings[marketPair], dcaPropertiesToApply, matchedTriggers, globalDCAProperties, newDCALines, ptmagicInstance.PTMagicConfiguration, ptmagicInstance.Log); newIndicatorsLines = SettingsHandler.BuildPropertyLinesForSingleMarketSetting(ptmagicInstance.LastRuntimeSummary.MainMarket, marketPair, ptmagicInstance.TriggeredSingleMarketSettings[marketPair], indicatorsPropertiesToApply, matchedTriggers, globalIndicatorsProperties, newIndicatorsLines, ptmagicInstance.PTMagicConfiguration, ptmagicInstance.Log); } // Combine global settings lines with single market settings lines globalPairsLines.AddRange(newPairsLines); globalDCALines.AddRange(newDCALines); globalIndicatorsLines.AddRange(newIndicatorsLines); ptmagicInstance.PairsLines = globalPairsLines; ptmagicInstance.DCALines = globalDCALines; ptmagicInstance.IndicatorsLines = globalIndicatorsLines; } catch (Exception ex) { ptmagicInstance.Log.DoLogCritical("Critical error while writing settings!", ex); throw (ex); } }
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); }
public static void CompileProperties(PTMagic ptmagicInstance, GlobalSetting setting) { SettingsHandler.BuildPropertyLines("Pairs", ptmagicInstance, setting); SettingsHandler.BuildPropertyLines("DCA", ptmagicInstance, setting); SettingsHandler.BuildPropertyLines("Indicators", ptmagicInstance, setting); }
public static List <string> BuildPropertyLinesForSingleMarketSetting(string mainMarket, string marketPair, List <SingleMarketSetting> appliedSettings, Dictionary <string, object> properties, Dictionary <string, List <string> > matchedTriggers, Dictionary <string, string> fullProperties, List <string> newPropertyLines, PTMagicConfiguration systemConfiguration, LogHelper log) { if (properties.Keys.Count > 0) { string appliedSettingsStringList = ""; foreach (SingleMarketSetting sms in appliedSettings) { if (!appliedSettingsStringList.Equals("")) { appliedSettingsStringList += ", "; } appliedSettingsStringList += sms.SettingName; } newPropertyLines.Add("# " + marketPair + " - Current active settings: " + appliedSettingsStringList); foreach (string settingProperty in properties.Keys) { string propertyKey = settingProperty; string propertyKeyName = propertyKey.Replace("_OFFSETPERCENT", ""); propertyKeyName = propertyKeyName.Replace("_OFFSET", ""); string newValueString = SystemHelper.PropertyToString(properties[settingProperty]); string oldValueString = SettingsHandler.GetCurrentPropertyValue(fullProperties, propertyKeyName, propertyKeyName.Replace("ALL_", "DEFAULT_")); newValueString = CalculatePropertyValue(settingProperty, oldValueString, newValueString, out propertyKey); string propertyMarketName = marketPair; // Adjust market pair name propertyMarketName = propertyMarketName.Replace(mainMarket, "").Replace("_", "").Replace("-", ""); string propertyKeyString = ""; if (propertyKey.StartsWith("ALL", StringComparison.InvariantCultureIgnoreCase)) { propertyKeyString = propertyKey.Replace("ALL", propertyMarketName, StringComparison.InvariantCultureIgnoreCase); } else if (propertyKey.StartsWith("DEFAULT", StringComparison.InvariantCultureIgnoreCase)) { propertyKeyString = propertyKey.Replace("DEFAULT", propertyMarketName, StringComparison.InvariantCultureIgnoreCase); } else { if (propertyKey.StartsWith("_", StringComparison.InvariantCultureIgnoreCase)) { propertyKeyString = propertyMarketName + propertyKey; } else { propertyKeyString = propertyMarketName + "_" + propertyKey; } } newPropertyLines.Add(propertyKeyString + " = " + newValueString); } } return(newPropertyLines); }
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); }