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 ;
2019-03-02 18:03:29 +01:00
using System.Text.RegularExpressions ;
2018-05-22 10:11:50 +02:00
using System.Threading.Tasks ;
using Core.Main ;
using Core.Helper ;
using Core.Main.DataObjects.PTMagicData ;
using Newtonsoft.Json ;
2018-12-15 22:07:29 +01:00
namespace Core.ProfitTrailer
{
public static class SettingsHandler
{
2019-03-02 18:03:29 +01:00
#region "Private methods"
private static bool IsPropertyLine ( string line )
{
Regex lineRegex = new Regex ( @"^[^#][^=]+=.*$" ) ;
return lineRegex . IsMatch ( line ) ;
}
private static string CalculatePropertyValue ( string settingProperty , string oldValueString , string newValueString , out string configPropertyKey )
{
int valueMode = Constants . ValueModeDefault ;
2019-03-04 23:37:05 +01:00
configPropertyKey = settingProperty . Trim ( ) ;
2019-03-02 18:03:29 +01:00
string result = null ;
// Determine the mode for changing the value
if ( configPropertyKey . IndexOf ( "_OFFSETPERCENT" ) > - 1 )
{
valueMode = Constants . ValueModeOffsetPercent ;
configPropertyKey = configPropertyKey . Replace ( "_OFFSETPERCENT" , "" ) ;
}
else if ( configPropertyKey . IndexOf ( "_OFFSET" ) > - 1 )
{
valueMode = Constants . ValueModeOffset ;
configPropertyKey = configPropertyKey . Replace ( "_OFFSET" , "" ) ;
}
// Boolean value, fix case
if ( newValueString . ToLower ( ) . Equals ( "true" ) | | newValueString . ToLower ( ) . Equals ( "false" ) )
{
result = newValueString . ToLower ( ) ;
}
else
{
// Value, calculate new value
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 ( oldValueString , 0 , "en-US" ) ;
result = 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 ( oldValueString , 0 , "en-US" ) ;
if ( oldValue < 0 ) offsetValuePercent = offsetValuePercent * - 1 ;
double oldValueOffset = ( oldValue * ( offsetValuePercent / 100 ) ) ;
// Use integers for timeout and pairs properties, otherwise double
if ( configPropertyKey . Contains ( "rebuy_timeout" , StringComparison . InvariantCultureIgnoreCase ) | | configPropertyKey . Contains ( "trading_pairs" , StringComparison . InvariantCultureIgnoreCase ) )
{
// Ensure some values are rounded up to integers for PT comaptability
result = ( ( int ) ( Math . Round ( ( oldValue + oldValueOffset ) , MidpointRounding . AwayFromZero ) + . 5 ) ) . ToString ( new System . Globalization . CultureInfo ( "en-US" ) ) ;
}
else
{
// Use double to calculate
result = Math . Round ( ( oldValue + oldValueOffset ) , 8 ) . ToString ( new System . Globalization . CultureInfo ( "en-US" ) ) ;
}
}
break ;
default :
2019-03-05 21:33:37 +01:00
// Raw value no processing required
result = newValueString ;
2019-03-02 18:03:29 +01:00
break ;
}
}
return result ;
}
#endregion
#region "Public interface"
2018-12-15 22:07:29 +01:00
public static string GetMainMarket ( PTMagicConfiguration systemConfiguration , List < string > pairsLines , LogHelper log )
{
2018-05-22 10:11:50 +02:00
string result = "" ;
2018-12-15 22:07:29 +01:00
foreach ( string line in pairsLines )
{
if ( line . Replace ( " " , "" ) . StartsWith ( "MARKET" , StringComparison . InvariantCultureIgnoreCase ) )
{
2018-05-22 10:11:50 +02:00
result = line . Replace ( "MARKET" , "" , StringComparison . InvariantCultureIgnoreCase ) ;
result = result . Replace ( "#" , "" ) ;
result = result . Replace ( "=" , "" ) . Trim ( ) ;
break ;
}
}
return result ;
}
2018-12-15 22:07:29 +01:00
public static string GetMarketPairs ( PTMagicConfiguration systemConfiguration , List < string > pairsLines , LogHelper log )
{
2018-05-22 10:11:50 +02:00
string result = "" ;
2018-12-15 22:07:29 +01:00
foreach ( string line in pairsLines )
{
if ( line . Replace ( " " , "" ) . StartsWith ( "ALL_enabled_pairs" , StringComparison . InvariantCultureIgnoreCase ) | | line . Replace ( " " , "" ) . StartsWith ( "enabled_pairs" , StringComparison . InvariantCultureIgnoreCase ) )
{
2018-05-22 10:11:50 +02:00
result = line . Replace ( "ALL_enabled_pairs" , "" , StringComparison . InvariantCultureIgnoreCase ) ;
result = result . Replace ( "enabled_pairs" , "" , StringComparison . InvariantCultureIgnoreCase ) ;
result = result . Replace ( "#" , "" ) ;
result = result . Replace ( "=" , "" ) . Trim ( ) ;
break ;
}
}
return result ;
}
2018-12-15 22:07:29 +01:00
public static string GetActiveSetting ( PTMagic ptmagicInstance , ref bool headerLinesAdded )
{
2018-05-22 10:11:50 +02:00
string result = "" ;
2019-03-05 23:55:46 +01:00
if ( ( ptmagicInstance . PairsLines = = null ) | | ptmagicInstance . PTMagicConfiguration . GeneralSettings . Application . TestMode )
2018-12-15 22:07:29 +01:00
{
2019-03-05 23:55:46 +01:00
// Return current active setting
result = ptmagicInstance . ActiveSetting ;
}
else
{
// Determine from file lines
foreach ( string line in ptmagicInstance . PairsLines )
2018-12-15 22:07:29 +01:00
{
2019-03-05 23:55:46 +01:00
if ( line . IndexOf ( "PTMagic_ActiveSetting" , StringComparison . InvariantCultureIgnoreCase ) > - 1 )
{
result = line . Replace ( "PTMagic_ActiveSetting" , "" , StringComparison . InvariantCultureIgnoreCase ) ;
result = result . Replace ( "#" , "" ) ;
result = result . Replace ( "=" , "" ) . Trim ( ) ;
result = SystemHelper . StripBadCode ( result , Constants . WhiteListProperties ) ;
break ;
}
}
if ( result . Equals ( "" ) )
{
SettingsHandler . WriteHeaderLines ( "Pairs" , ptmagicInstance ) ;
SettingsHandler . WriteHeaderLines ( "DCA" , ptmagicInstance ) ;
SettingsHandler . WriteHeaderLines ( "Indicators" , ptmagicInstance ) ;
headerLinesAdded = true ;
2018-05-22 10:11:50 +02:00
}
}
return result ;
}
2018-12-15 22:07:29 +01:00
public static void WriteHeaderLines ( string fileType , PTMagic ptmagicInstance )
{
2018-05-23 06:23:10 +02:00
List < string > fileLines = ( List < string > ) ptmagicInstance . GetType ( ) . GetProperty ( fileType + "Lines" ) . GetValue ( ptmagicInstance , null ) ;
2018-05-22 10:11:50 +02:00
// Writing Header lines
2019-03-02 18:03:29 +01:00
fileLines . Insert ( 0 , "#" ) ;
2018-05-23 06:23:10 +02:00
fileLines . Insert ( 0 , "# ####################################" ) ;
2019-03-02 18:03:29 +01:00
fileLines . Insert ( 0 , "# PTMagic_LastChanged = " + DateTime . Now . ToShortDateString ( ) + " " + DateTime . Now . ToShortTimeString ( ) ) ;
2018-05-23 06:23:10 +02:00
fileLines . Insert ( 0 , "# PTMagic_ActiveSetting = " + SystemHelper . StripBadCode ( ptmagicInstance . DefaultSettingName , Constants . WhiteListProperties ) ) ;
fileLines . Insert ( 0 , "# ####################################" ) ;
2018-05-23 07:44:31 +02:00
ptmagicInstance . GetType ( ) . GetProperty ( fileType + "Lines" ) . SetValue ( ptmagicInstance , fileLines ) ;
2018-05-22 10:11:50 +02:00
}
2018-12-15 22:07:29 +01:00
public static Dictionary < string , string > GetPropertiesAsDictionary ( List < string > propertyLines )
{
2018-05-22 10:11:50 +02:00
Dictionary < string , string > result = new Dictionary < string , string > ( ) ;
2018-12-15 22:07:29 +01:00
foreach ( string line in propertyLines )
{
if ( ! line . StartsWith ( "#" , StringComparison . InvariantCultureIgnoreCase ) )
{
2018-05-22 10:11:50 +02:00
string [ ] lineContentArray = line . Split ( "=" ) ;
2018-12-15 22:07:29 +01:00
if ( lineContentArray . Length = = 2 )
{
if ( ! result . ContainsKey ( lineContentArray [ 0 ] . Trim ( ) ) )
{
2018-05-22 10:11:50 +02:00
result . Add ( lineContentArray [ 0 ] . Trim ( ) , lineContentArray [ 1 ] . Trim ( ) ) ;
2018-12-15 22:07:29 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
result [ lineContentArray [ 0 ] . Trim ( ) ] = lineContentArray [ 1 ] . Trim ( ) ;
}
}
}
}
return result ;
}
2018-12-15 22:07:29 +01:00
public static string GetCurrentPropertyValue ( Dictionary < string , string > properties , string propertyKey , string fallbackPropertyKey )
{
2018-05-22 10:11:50 +02:00
string result = "" ;
2018-12-15 22:07:29 +01:00
if ( properties . ContainsKey ( propertyKey ) )
{
2018-05-22 10:11:50 +02:00
result = properties [ propertyKey ] ;
2018-12-15 22:07:29 +01:00
}
else if ( ! fallbackPropertyKey . Equals ( "" ) & & properties . ContainsKey ( fallbackPropertyKey ) )
{
2018-05-22 10:11:50 +02:00
result = properties [ fallbackPropertyKey ] ;
}
return result ;
}
2018-12-15 22:07:29 +01:00
public static void CompileProperties ( PTMagic ptmagicInstance , GlobalSetting setting )
{
2018-05-23 07:44:31 +02:00
SettingsHandler . BuildPropertyLines ( "Pairs" , ptmagicInstance , setting ) ;
SettingsHandler . BuildPropertyLines ( "DCA" , ptmagicInstance , setting ) ;
SettingsHandler . BuildPropertyLines ( "Indicators" , ptmagicInstance , setting ) ;
2018-05-22 10:11:50 +02:00
}
2018-12-15 22:07:29 +01:00
public static void BuildPropertyLines ( string fileType , PTMagic ptmagicInstance , GlobalSetting setting )
{
2018-05-22 10:11:50 +02:00
List < string > result = new List < string > ( ) ;
2018-05-23 07:44:31 +02:00
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 ) ;
2018-12-15 22:07:29 +01:00
if ( properties ! = null )
{
2018-05-22 10:11:50 +02:00
// Building Properties
2018-12-15 22:07:29 +01:00
if ( ! setting . SettingName . Equals ( ptmagicInstance . DefaultSettingName , StringComparison . InvariantCultureIgnoreCase ) & & ptmagicInstance . PTMagicConfiguration . GeneralSettings . Application . AlwaysLoadDefaultBeforeSwitch & & ! properties . ContainsKey ( "File" ) )
{
2018-05-22 10:11:50 +02:00
// Load default settings as basis for the switch
2018-05-23 07:44:31 +02:00
GlobalSetting defaultSetting = ptmagicInstance . PTMagicConfiguration . AnalyzerSettings . GlobalSettings . Find ( a = > a . SettingName . Equals ( ptmagicInstance . DefaultSettingName , StringComparison . InvariantCultureIgnoreCase ) ) ;
2018-12-15 22:07:29 +01:00
if ( defaultSetting ! = null )
{
2018-05-22 10:11:50 +02:00
Dictionary < string , object > defaultProperties = new Dictionary < string , object > ( ) ;
2018-12-15 22:07:29 +01:00
switch ( fileType . ToLower ( ) )
{
2018-05-22 10:11:50 +02:00
case "pairs" :
defaultProperties = defaultSetting . PairsProperties ;
break ;
case "dca" :
defaultProperties = defaultSetting . DCAProperties ;
break ;
case "inidcators" :
defaultProperties = defaultSetting . IndicatorsProperties ;
break ;
}
2018-12-15 22:07:29 +01:00
if ( defaultProperties . ContainsKey ( "File" ) )
{
2018-05-23 07:44:31 +02:00
fileLines = SettingsFiles . GetPresetFileLinesAsList ( defaultSetting . SettingName , defaultProperties [ "File" ] . ToString ( ) , ptmagicInstance . PTMagicConfiguration ) ;
2018-05-22 10:11:50 +02:00
}
}
2018-12-15 22:07:29 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
// Check if settings are configured in a seperate file
2018-12-15 22:07:29 +01:00
if ( properties . ContainsKey ( "File" ) )
{
2018-05-23 07:44:31 +02:00
fileLines = SettingsFiles . GetPresetFileLinesAsList ( setting . SettingName , properties [ "File" ] . ToString ( ) , ptmagicInstance . PTMagicConfiguration ) ;
2018-05-22 10:11:50 +02:00
}
}
2019-03-02 18:03:29 +01:00
// Loop through config line by line reprocessing where required.
2018-12-15 22:07:29 +01:00
foreach ( string line in fileLines )
{
if ( line . IndexOf ( "PTMagic_ActiveSetting" , StringComparison . InvariantCultureIgnoreCase ) > - 1 )
{
2018-05-22 10:11:50 +02:00
// Setting current active setting
result . Add ( "# PTMagic_ActiveSetting = " + setting . SettingName ) ;
2018-12-15 22:07:29 +01:00
}
else if ( line . IndexOf ( "PTMagic_LastChanged" , StringComparison . InvariantCultureIgnoreCase ) > - 1 )
{
2018-05-22 10:11:50 +02:00
// Setting last change datetime
2019-03-02 18:03:29 +01:00
result . Add ( "# PTMagic_LastChanged = " + DateTime . Now . ToShortDateString ( ) + " " + DateTime . Now . ToShortTimeString ( ) ) ;
2018-05-22 10:11:50 +02:00
2018-12-15 22:07:29 +01:00
}
else if ( line . IndexOf ( "PTMagic_SingleMarketSettings" , StringComparison . InvariantCultureIgnoreCase ) > - 1 )
{
2018-05-22 10:11:50 +02:00
// Single Market Settings will get overwritten every single run => crop the lines
break ;
2018-12-15 22:07:29 +01:00
}
2019-03-02 18:03:29 +01:00
else if ( IsPropertyLine ( line ) )
2018-12-15 22:07:29 +01:00
{
2019-03-02 18:03:29 +01:00
// We have got a property line
2018-12-15 22:07:29 +01:00
if ( properties ! = null )
{
2019-03-02 18:03:29 +01:00
bool madeSubstitution = false ;
2018-12-15 22:07:29 +01:00
foreach ( string settingProperty in properties . Keys )
{
2019-03-02 18:03:29 +01:00
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 ) ;
2018-05-22 10:11:50 +02:00
}
}
2019-03-02 18:03:29 +01:00
}
else
{
// Non property line, just copy it
result . Add ( line ) ;
2018-05-22 10:11:50 +02:00
}
}
}
2018-05-23 07:44:31 +02:00
ptmagicInstance . GetType ( ) . GetProperty ( fileType + "Lines" ) . SetValue ( ptmagicInstance , result ) ;
2018-05-22 10:11:50 +02:00
}
2019-03-02 18:03:29 +01:00
public static bool BuildPropertyLine ( List < string > result , string settingName , string line , Dictionary < string , object > properties , string settingProperty )
2018-12-15 22:07:29 +01:00
{
2019-03-02 18:03:29 +01:00
bool madeSubstitutions = false ;
2018-05-22 10:11:50 +02:00
2019-03-02 18:03:29 +01:00
string propertyKey ;
2018-05-22 10:11:50 +02:00
2019-03-04 23:37:05 +01:00
var lineParts = line . Trim ( ) . Split ( "=" ) ;
string linePropertyName = lineParts [ 0 ] . Trim ( ) ;
2019-03-02 18:03:29 +01:00
string newValueString = SystemHelper . PropertyToString ( properties [ settingProperty ] ) ;
2019-03-04 23:37:05 +01:00
string oldValueString = lineParts [ 1 ] . Trim ( ) ;
2018-05-22 10:11:50 +02:00
2019-03-02 18:03:29 +01:00
newValueString = CalculatePropertyValue ( settingProperty , oldValueString , newValueString , out propertyKey ) ;
2018-05-22 10:11:50 +02:00
2019-03-04 23:37:05 +01:00
if ( linePropertyName . Equals ( propertyKey , StringComparison . InvariantCultureIgnoreCase ) )
2019-03-02 18:03:29 +01:00
{
madeSubstitutions = true ;
2018-05-22 10:11:50 +02:00
line = propertyKey + " = " + newValueString ;
string previousLine = result . Last ( ) ;
2019-03-04 23:37:05 +01:00
if ( previousLine . IndexOf ( "PTMagic changed line" , StringComparison . InvariantCultureIgnoreCase ) > - 1 )
2018-12-15 22:07:29 +01:00
{
2018-05-22 10:11:50 +02:00
result . RemoveAt ( result . Count - 1 ) ;
2018-12-15 22:07:29 +01:00
}
else
{
2019-03-04 23:37:05 +01:00
result . Add ( String . Format ( "# PTMagic changed {5} for setting '{0}' from value '{1}' to '{2}' on {3} {4}" , settingName , oldValueString , newValueString , DateTime . Now . ToShortDateString ( ) , DateTime . Now . ToShortTimeString ( ) , linePropertyName ) ) ;
2018-05-22 10:11:50 +02:00
}
result . Add ( line ) ;
}
2019-03-02 18:03:29 +01:00
return madeSubstitutions ;
2018-05-22 10:11:50 +02:00
}
2018-12-15 22:07:29 +01:00
public static void CompileSingleMarketProperties ( PTMagic ptmagicInstance , Dictionary < string , List < string > > matchedTriggers )
{
try
{
2018-05-22 10:11:50 +02:00
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 > ( ) ;
2018-12-15 22:07:29 +01:00
foreach ( string pairsLine in ptmagicInstance . PairsLines )
{
if ( pairsLine . IndexOf ( "PTMagic_SingleMarketSettings" , StringComparison . InvariantCultureIgnoreCase ) > - 1 )
{
2018-05-22 10:11:50 +02:00
// Single Market Settings will get overwritten every single run => crop the lines
break ;
2018-12-15 22:07:29 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
string globalPairsLine = pairsLine ;
globalPairsLines . Add ( globalPairsLine ) ;
}
}
2019-03-02 19:27:19 +01:00
2019-03-02 09:10:33 +01:00
newPairsLines . Add ( "# PTMagic_SingleMarketSettings - Written on " + DateTime . Now . ToString ( ) ) ;
2018-05-22 10:11:50 +02:00
newPairsLines . Add ( "# ########################################################################" ) ;
2019-03-02 18:03:29 +01:00
newPairsLines . Add ( "#" ) ;
2018-05-22 10:11:50 +02:00
2018-12-15 22:07:29 +01:00
foreach ( string dcaLine in ptmagicInstance . DCALines )
{
if ( dcaLine . IndexOf ( "PTMagic_SingleMarketSettings" , StringComparison . InvariantCultureIgnoreCase ) > - 1 )
{
2018-05-22 10:11:50 +02:00
// Single Market Settings will get overwritten every single run => crop the lines
break ;
2018-12-15 22:07:29 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
string globalDCALine = dcaLine ;
globalDCALines . Add ( globalDCALine ) ;
}
}
2019-03-02 19:27:19 +01:00
2019-03-02 09:10:33 +01:00
newDCALines . Add ( "# PTMagic_SingleMarketSettings - Written on " + DateTime . Now . ToString ( ) ) ;
2018-05-22 10:11:50 +02:00
newDCALines . Add ( "# ########################################################################" ) ;
2019-03-02 18:03:29 +01:00
newDCALines . Add ( "#" ) ;
2018-05-22 10:11:50 +02:00
2018-12-15 22:07:29 +01:00
foreach ( string indicatorsLine in ptmagicInstance . IndicatorsLines )
{
if ( indicatorsLine . IndexOf ( "PTMagic_SingleMarketSettings" , StringComparison . InvariantCultureIgnoreCase ) > - 1 )
{
2018-05-22 10:11:50 +02:00
// Single Market Settings will get overwritten every single run => crop the lines
break ;
2018-12-15 22:07:29 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
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 ) ;
2019-03-02 19:27:19 +01:00
2019-03-02 09:10:33 +01:00
newIndicatorsLines . Add ( "# PTMagic_SingleMarketSettings - Written on " + DateTime . Now . ToString ( ) ) ;
2018-05-22 10:11:50 +02:00
newIndicatorsLines . Add ( "# ########################################################################" ) ;
2019-03-02 18:03:29 +01:00
newIndicatorsLines . Add ( "#" ) ;
2018-05-22 10:11:50 +02:00
2018-12-15 22:07:29 +01:00
foreach ( string marketPair in ptmagicInstance . TriggeredSingleMarketSettings . Keys . OrderBy ( k = > k ) )
{
2018-05-22 10:11:50 +02:00
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
2018-12-15 22:07:29 +01:00
foreach ( SingleMarketSetting setting in ptmagicInstance . TriggeredSingleMarketSettings [ marketPair ] )
{
2018-05-22 13:04:53 +02:00
ptmagicInstance . Log . DoLogInfo ( "Building single market settings '" + setting . SettingName + "' for '" + marketPair + "'..." ) ;
2018-05-22 10:11:50 +02:00
2018-12-15 22:07:29 +01:00
foreach ( string settingPairsProperty in setting . PairsProperties . Keys )
{
if ( ! pairsPropertiesToApply . ContainsKey ( settingPairsProperty ) )
{
2018-05-22 10:11:50 +02:00
pairsPropertiesToApply . Add ( settingPairsProperty , setting . PairsProperties [ settingPairsProperty ] ) ;
2018-12-15 22:07:29 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
pairsPropertiesToApply [ settingPairsProperty ] = setting . PairsProperties [ settingPairsProperty ] ;
}
}
2018-12-15 22:07:29 +01:00
foreach ( string settingDCAProperty in setting . DCAProperties . Keys )
{
if ( ! dcaPropertiesToApply . ContainsKey ( settingDCAProperty ) )
{
2018-05-22 10:11:50 +02:00
dcaPropertiesToApply . Add ( settingDCAProperty , setting . DCAProperties [ settingDCAProperty ] ) ;
2018-12-15 22:07:29 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
dcaPropertiesToApply [ settingDCAProperty ] = setting . DCAProperties [ settingDCAProperty ] ;
}
}
2018-12-15 22:07:29 +01:00
foreach ( string settingIndicatorsProperty in setting . IndicatorsProperties . Keys )
{
if ( ! indicatorsPropertiesToApply . ContainsKey ( settingIndicatorsProperty ) )
{
2018-05-22 10:11:50 +02:00
indicatorsPropertiesToApply . Add ( settingIndicatorsProperty , setting . IndicatorsProperties [ settingIndicatorsProperty ] ) ;
2018-12-15 22:07:29 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
indicatorsPropertiesToApply [ settingIndicatorsProperty ] = setting . IndicatorsProperties [ settingIndicatorsProperty ] ;
}
}
2018-05-22 13:04:53 +02:00
ptmagicInstance . Log . DoLogInfo ( "Built single market settings '" + setting . SettingName + "' for '" + marketPair + "'." ) ;
2018-05-22 10:11:50 +02:00
}
2019-01-07 15:33:02 +01:00
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 ) ;
2018-05-22 10:11:50 +02:00
}
// Combine global settings lines with single market settings lines
globalPairsLines . AddRange ( newPairsLines ) ;
globalDCALines . AddRange ( newDCALines ) ;
globalIndicatorsLines . AddRange ( newIndicatorsLines ) ;
2018-05-22 13:04:53 +02:00
ptmagicInstance . PairsLines = globalPairsLines ;
ptmagicInstance . DCALines = globalDCALines ;
ptmagicInstance . IndicatorsLines = globalIndicatorsLines ;
2018-12-15 22:07:29 +01:00
}
catch ( Exception ex )
{
2018-05-22 13:04:53 +02:00
ptmagicInstance . Log . DoLogCritical ( "Critical error while writing settings!" , ex ) ;
2018-05-22 10:11:50 +02:00
throw ( ex ) ;
}
}
2019-01-07 15:33:02 +01:00
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 )
2018-12-15 22:07:29 +01:00
{
if ( properties . Keys . Count > 0 )
{
2018-05-22 10:11:50 +02:00
string appliedSettingsStringList = "" ;
2018-12-15 22:07:29 +01:00
foreach ( SingleMarketSetting sms in appliedSettings )
{
2018-05-22 10:11:50 +02:00
if ( ! appliedSettingsStringList . Equals ( "" ) ) appliedSettingsStringList + = ", " ;
appliedSettingsStringList + = sms . SettingName ;
}
newPropertyLines . Add ( "# " + marketPair + " - Current active settings: " + appliedSettingsStringList ) ;
2018-12-15 22:07:29 +01:00
foreach ( string settingProperty in properties . Keys )
{
2018-05-22 10:11:50 +02:00
string propertyKey = settingProperty ;
2019-03-02 18:03:29 +01:00
string propertyKeyName = propertyKey . Replace ( "_OFFSETPERCENT" , "" ) ;
propertyKeyName = propertyKeyName . Replace ( "_OFFSET" , "" ) ;
2018-05-22 10:11:50 +02:00
string newValueString = SystemHelper . PropertyToString ( properties [ settingProperty ] ) ;
2019-03-02 18:03:29 +01:00
string oldValueString = SettingsHandler . GetCurrentPropertyValue ( fullProperties , propertyKeyName , propertyKeyName . Replace ( "ALL_" , "DEFAULT_" ) ) ;
newValueString = CalculatePropertyValue ( settingProperty , oldValueString , newValueString , out propertyKey ) ;
2018-05-22 10:11:50 +02:00
string propertyMarketName = marketPair ;
2019-01-07 15:33:02 +01:00
// Adjust market pair name
propertyMarketName = propertyMarketName . Replace ( mainMarket , "" ) . Replace ( "_" , "" ) . Replace ( "-" , "" ) ;
2018-05-22 10:11:50 +02:00
string propertyKeyString = "" ;
2018-12-15 22:07:29 +01:00
if ( propertyKey . StartsWith ( "ALL" , StringComparison . InvariantCultureIgnoreCase ) )
{
2018-05-22 10:11:50 +02:00
propertyKeyString = propertyKey . Replace ( "ALL" , propertyMarketName , StringComparison . InvariantCultureIgnoreCase ) ;
2018-12-15 22:07:29 +01:00
}
else if ( propertyKey . StartsWith ( "DEFAULT" , StringComparison . InvariantCultureIgnoreCase ) )
{
2018-05-22 10:11:50 +02:00
propertyKeyString = propertyKey . Replace ( "DEFAULT" , propertyMarketName , StringComparison . InvariantCultureIgnoreCase ) ;
2018-12-15 22:07:29 +01:00
}
else
{
if ( propertyKey . StartsWith ( "_" , StringComparison . InvariantCultureIgnoreCase ) )
{
2018-05-22 10:11:50 +02:00
propertyKeyString = propertyMarketName + propertyKey ;
2018-12-15 22:07:29 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
propertyKeyString = propertyMarketName + "_" + propertyKey ;
}
}
newPropertyLines . Add ( propertyKeyString + " = " + newValueString ) ;
}
}
return newPropertyLines ;
}
2018-12-15 22:07:29 +01:00
public static bool RemoveSingleMarketSettings ( PTMagic ptmagicInstance )
{
2018-05-22 10:11:50 +02:00
bool result = false ;
2018-12-15 22:07:29 +01:00
try
{
2018-05-22 10:11:50 +02:00
List < string > cleanedUpPairsLines = new List < string > ( ) ;
List < string > cleanedUpDCALines = new List < string > ( ) ;
List < string > cleanedUpIndicatorsLines = new List < string > ( ) ;
bool removedPairsSingleMarketSettings = false ;
2018-12-15 22:07:29 +01:00
foreach ( string pairsLine in ptmagicInstance . PairsLines )
{
if ( pairsLine . IndexOf ( "PTMagic_SingleMarketSettings" , StringComparison . InvariantCultureIgnoreCase ) > - 1 )
{
2018-05-22 10:11:50 +02:00
// Single Market Settings will get overwritten every single run => crop the lines
removedPairsSingleMarketSettings = true ;
break ;
2018-12-15 22:07:29 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
string newPairsLine = pairsLine ;
cleanedUpPairsLines . Add ( newPairsLine ) ;
}
}
bool removedDCASingleMarketSettings = false ;
2018-12-15 22:07:29 +01:00
foreach ( string dcaLine in ptmagicInstance . DCALines )
{
if ( dcaLine . IndexOf ( "PTMagic_SingleMarketSettings" , StringComparison . InvariantCultureIgnoreCase ) > - 1 )
{
2018-05-22 10:11:50 +02:00
// Single Market Settings will get overwritten every single run => crop the lines
removedDCASingleMarketSettings = true ;
break ;
2018-12-15 22:07:29 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
string newDCALine = dcaLine ;
cleanedUpDCALines . Add ( newDCALine ) ;
}
}
bool removedIndicatorsSingleMarketSettings = false ;
2018-12-15 22:07:29 +01:00
foreach ( string indicatorsLine in ptmagicInstance . IndicatorsLines )
{
if ( indicatorsLine . IndexOf ( "PTMagic_SingleMarketSettings" , StringComparison . InvariantCultureIgnoreCase ) > - 1 )
{
2018-05-22 10:11:50 +02:00
// Single Market Settings will get overwritten every single run => crop the lines
removedIndicatorsSingleMarketSettings = true ;
break ;
2018-12-15 22:07:29 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
string newIndicatorsLine = indicatorsLine ;
cleanedUpIndicatorsLines . Add ( newIndicatorsLine ) ;
}
}
2018-05-22 13:04:53 +02:00
ptmagicInstance . PairsLines = cleanedUpPairsLines ;
ptmagicInstance . DCALines = cleanedUpDCALines ;
ptmagicInstance . IndicatorsLines = cleanedUpIndicatorsLines ;
2018-05-22 10:11:50 +02:00
result = removedPairsSingleMarketSettings & & removedDCASingleMarketSettings & & removedIndicatorsSingleMarketSettings ;
2018-12-15 22:07:29 +01:00
}
catch ( Exception ex )
{
2018-05-22 13:04:53 +02:00
ptmagicInstance . Log . DoLogCritical ( "Critical error while writing settings!" , ex ) ;
2018-05-22 10:11:50 +02:00
}
return result ;
}
2019-03-02 18:03:29 +01:00
#endregion
2018-05-22 10:11:50 +02:00
}
}