PTMagic/Core/ProfitTrailer/SettingsFiles.cs

225 lines
10 KiB
C#
Raw Normal View History

2018-05-22 10:11:50 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Core.Main;
using Core.Helper;
using Core.Main.DataObjects.PTMagicData;
using Newtonsoft.Json;
2018-12-03 19:45:06 +01:00
namespace Core.ProfitTrailer
{
public static class SettingsFiles
{
public static string GetActiveSetting(PTMagicConfiguration systemConfiguration, string pairsFileName, string dcaFileName, string indicatorsFileName, LogHelper log)
{
2018-05-22 10:11:50 +02:00
string pairsPropertiesPath = systemConfiguration.GeneralSettings.Application.ProfitTrailerPath + Constants.PTPathTrading + Path.DirectorySeparatorChar + pairsFileName;
string result = SettingsFiles.GetActiveSettingFromFile(pairsPropertiesPath, systemConfiguration, log);
2018-12-03 19:45:06 +01:00
if (result.Equals(""))
{
2018-05-22 10:11:50 +02:00
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;
}
2018-12-03 19:45:06 +01:00
public static void WriteHeaderLines(string filePath, string settingName, PTMagicConfiguration systemConfiguration)
{
2018-05-22 10:11:50 +02:00
// Writing Header lines
List<string> lines = File.ReadAllLines(filePath).ToList();
lines.Insert(0, "");
lines.Insert(0, "# ####################################");
lines.Insert(0, "# PTMagic_LastChanged = " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
lines.Insert(0, "# PTMagic_ActiveSetting = " + SystemHelper.StripBadCode(settingName, Constants.WhiteListProperties));
lines.Insert(0, "# ####### PTMagic Current Setting ########");
lines.Insert(0, "# ####################################");
if (!systemConfiguration.GeneralSettings.Application.TestMode) File.WriteAllLines(filePath, lines);
}
2018-12-03 19:45:06 +01:00
public static string GetActiveSettingFromFile(string filePath, PTMagicConfiguration systemConfiguration, LogHelper log)
{
2018-05-22 10:11:50 +02:00
string result = "";
2018-12-03 19:45:06 +01:00
if (File.Exists(filePath))
{
2018-05-22 10:11:50 +02:00
StreamReader sr = new StreamReader(filePath);
2018-12-03 19:45:06 +01:00
try
{
2018-05-22 10:11:50 +02:00
string line = sr.ReadLine();
2018-12-03 19:45:06 +01:00
while (line != null)
{
if (line.IndexOf("PTMagic_ActiveSetting", StringComparison.InvariantCultureIgnoreCase) > -1)
{
2018-05-22 10:11:50 +02:00
result = line.Replace("PTMagic_ActiveSetting", "", StringComparison.InvariantCultureIgnoreCase);
result = result.Replace("#", "");
result = result.Replace("=", "").Trim();
result = SystemHelper.StripBadCode(result, Constants.WhiteListProperties);
break;
}
line = sr.ReadLine();
}
2018-12-03 19:45:06 +01:00
}
catch { }
finally
{
2018-05-22 10:11:50 +02:00
sr.Close();
}
}
return result;
}
2018-12-03 19:45:06 +01:00
public static List<string> GetPresetFileLinesAsList(string settingName, string fileName, PTMagicConfiguration systemConfiguration)
{
2018-05-22 10:11:50 +02:00
return SettingsFiles.GetPresetFileLinesAsList(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar, settingName, fileName, systemConfiguration);
}
2018-12-03 19:45:06 +01:00
public static List<string> GetPresetFileLinesAsList(string baseFolderPath, string settingName, string fileName, PTMagicConfiguration systemConfiguration)
{
2018-05-22 10:11:50 +02:00
fileName = fileName.Replace(".PROPERTIES", ".properties");
string settingPropertiesPath = baseFolderPath + Constants.PTMagicPathPresets + Path.DirectorySeparatorChar + settingName + Path.DirectorySeparatorChar + fileName;
List<string> result = new List<string>();
2018-12-03 19:45:06 +01:00
if (File.Exists(settingPropertiesPath))
{
2018-05-22 10:11:50 +02:00
result = File.ReadAllLines(settingPropertiesPath).ToList();
}
return result;
}
2018-12-03 19:45:06 +01:00
public static bool CheckPresets(PTMagicConfiguration systemConfiguration, LogHelper log, bool forceCheck)
{
if (!forceCheck)
{
2018-05-22 10:11:50 +02:00
// If the check is not enforced, check for file changes
string[] presetFilePaths = Directory.GetFiles(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + Constants.PTMagicPathPresets, "*.*", SearchOption.AllDirectories);
2018-12-03 19:45:06 +01:00
foreach (string presetFilePath in presetFilePaths)
{
if (presetFilePath.IndexOf(".properties", StringComparison.InvariantCultureIgnoreCase) > -1)
{
2018-05-22 10:11:50 +02:00
FileInfo presetFile = new FileInfo(presetFilePath);
2018-12-03 19:45:06 +01:00
if (presetFile.LastWriteTime > DateTime.Now.AddMinutes(-systemConfiguration.AnalyzerSettings.MarketAnalyzer.IntervalMinutes).AddSeconds(2))
{
2018-05-22 10:11:50 +02:00
// File has changed recently, force preparation check
log.DoLogInfo("Preset files changed, enforcing preparation check...");
forceCheck = true;
break;
}
}
}
}
2018-12-03 19:45:06 +01:00
if (forceCheck)
{
2018-05-22 10:11:50 +02:00
log.DoLogInfo("Checking automated settings for presets...");
2018-12-03 19:45:06 +01:00
foreach (GlobalSetting setting in systemConfiguration.AnalyzerSettings.GlobalSettings)
{
if (setting.PairsProperties != null)
{
if (setting.PairsProperties.ContainsKey("File"))
{
2018-05-22 10:11:50 +02:00
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);
2018-12-03 19:45:06 +01:00
if (headerPairsSetting.Equals(""))
{
if (File.Exists(settingPairsPropertiesPath))
{
2018-05-22 10:11:50 +02:00
SettingsFiles.WriteHeaderLines(settingPairsPropertiesPath, setting.SettingName, systemConfiguration);
2018-12-03 19:45:06 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
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 + "'");
}
}
2018-12-03 19:45:06 +01:00
if (setting.DCAProperties != null)
{
if (setting.DCAProperties.ContainsKey("File"))
{
2018-05-22 10:11:50 +02:00
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);
2018-12-03 19:45:06 +01:00
if (headerDCASetting.Equals(""))
{
if (File.Exists(settingDCAPropertiesPath))
{
2018-05-22 10:11:50 +02:00
SettingsFiles.WriteHeaderLines(settingDCAPropertiesPath, setting.SettingName, systemConfiguration);
2018-12-03 19:45:06 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
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 + "'");
}
}
2018-12-03 19:45:06 +01:00
if (setting.IndicatorsProperties != null)
{
if (setting.IndicatorsProperties.ContainsKey("File"))
{
2018-05-22 10:11:50 +02:00
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);
2018-12-03 19:45:06 +01:00
if (headerIndicatorsSetting.Equals(""))
{
if (File.Exists(settingIndicatorsPropertiesPath))
{
2018-05-22 10:11:50 +02:00
SettingsFiles.WriteHeaderLines(settingIndicatorsPropertiesPath, setting.SettingName, systemConfiguration);
2018-12-03 19:45:06 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
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;
}
}
}