11using System . Text . Json ;
2+ using CryptoExchange . Net . CommonObjects ;
23using Hjson ;
34using Microsoft . Extensions . Logging ;
45using PbLayla . Model . Dori ;
@@ -15,6 +16,95 @@ public static string GenerateNormalConfig(this IPbMultiConfig template)
1516 return serializedConfig ;
1617 }
1718
19+ public static string GenerateNormalAdaptiveTrendConfig ( this IPbMultiConfig template ,
20+ string normalConfig ,
21+ string unstuckConfig ,
22+ double pbStuckThreshold ,
23+ double pbLossAllowance )
24+ {
25+ var config = template . Clone ( ) ;
26+ config . LossAllowancePct = pbLossAllowance ;
27+ config . StuckThreshold = pbStuckThreshold ;
28+ var symbols = config . ParseSymbols ( ) ;
29+ foreach ( var symbol in symbols )
30+ {
31+ if ( symbol . LongMode == TradeMode . Normal )
32+ {
33+ // continue trading with normal config
34+ symbol . LiveConfigPath = FormattableString . Invariant ( $ "configs/{ normalConfig } ") ;
35+ }
36+ else if ( symbol . LongMode == TradeMode . GracefulStop )
37+ {
38+ // Dori wants to exit this symbol
39+ symbol . LiveConfigPath = FormattableString . Invariant ( $ "configs/{ unstuckConfig } ") ;
40+ }
41+ }
42+ config . UpdateSymbols ( symbols ) ;
43+
44+ var serializedConfig = config . SerializeConfig ( ) ;
45+ return serializedConfig ;
46+ }
47+
48+ public static string GenerateUnstuckAdaptiveTrendConfig ( this IPbMultiConfig template ,
49+ HashSet < string > symbolsToUnstuck ,
50+ string normalConfig ,
51+ string unstuckConfig ,
52+ double pbStuckThreshold ,
53+ double pbLossAllowance ,
54+ double unstuckExposure ,
55+ bool disableOthers )
56+ {
57+ var config = template . Clone ( ) ;
58+ config . LossAllowancePct = pbLossAllowance ;
59+ config . StuckThreshold = pbStuckThreshold ;
60+ var symbols = config . ParseSymbols ( ) ;
61+ var foundSymbols = new HashSet < string > ( StringComparer . InvariantCultureIgnoreCase ) ;
62+ foreach ( var symbolConfig in symbols )
63+ {
64+ if ( symbolsToUnstuck . Contains ( symbolConfig . Symbol ) )
65+ {
66+ foundSymbols . Add ( symbolConfig . Symbol ) ;
67+ symbolConfig . LongMode = TradeMode . GracefulStop ;
68+ symbolConfig . LiveConfigPath = FormattableString . Invariant ( $ "configs/{ unstuckConfig } ") ;
69+ symbolConfig . WalletExposureLimitLong = unstuckExposure ;
70+ }
71+ else
72+ {
73+ if ( disableOthers && symbolConfig . LongMode is TradeMode . GracefulStop or TradeMode . Normal )
74+ {
75+ symbolConfig . LongMode = TradeMode . TakeProfitOnly ;
76+ symbolConfig . LiveConfigPath = FormattableString . Invariant ( $ "configs/{ unstuckConfig } ") ;
77+ }
78+ else if ( symbolConfig . LongMode == TradeMode . Normal )
79+ {
80+ symbolConfig . LiveConfigPath = FormattableString . Invariant ( $ "configs/{ normalConfig } ") ;
81+ }
82+ }
83+ }
84+
85+ // add stuck symbols that we have not found in config
86+ foreach ( var symbolToUnstuck in symbolsToUnstuck )
87+ {
88+ if ( ! foundSymbols . Contains ( symbolToUnstuck ) )
89+ {
90+ var newSymbol = new SymbolOptions
91+ {
92+ LongMode = TradeMode . GracefulStop ,
93+ LiveConfigPath = FormattableString . Invariant ( $ "configs/{ unstuckConfig } ") ,
94+ Symbol = symbolToUnstuck ,
95+ WalletExposureLimitLong = unstuckExposure ,
96+ ShortMode = TradeMode . Manual ,
97+ } ;
98+ symbols = symbols . Append ( newSymbol ) . ToArray ( ) ;
99+ }
100+ }
101+
102+ config . UpdateSymbols ( symbols ) ;
103+
104+ var serializedConfig = config . SerializeConfig ( ) ;
105+ return serializedConfig ;
106+ }
107+
18108 public static string GenerateUnstuckConfig ( this IPbMultiConfig template , HashSet < string > symbolsToUnstuck , string unstuckConfig , double unstuckExposure , bool disableOthers )
19109 {
20110 var config = template . Clone ( ) ;
0 commit comments