2018-05-22 10:11:50 +02:00
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Net ;
2019-12-14 14:13:47 +01:00
using System.Net.Http ;
using System.Net.Http.Headers ;
using System.Threading.Tasks ;
2018-05-22 10:11:50 +02:00
using Newtonsoft.Json ;
using Core.Main ;
using Core.Helper ;
using Core.Main.DataObjects.PTMagicData ;
2018-12-15 22:07:29 +01:00
namespace Core.MarketAnalyzer
{
public class BaseAnalyzer
{
2019-12-14 14:13:47 +01:00
public static string GetJsonStringFromURL ( string url , LogHelper log , ( string header , string value ) [ ] headers = null )
2018-12-15 22:07:29 +01:00
{
2019-12-14 14:13:47 +01:00
HttpClient webClient = null ;
if ( webClient = = null )
2018-12-15 22:07:29 +01:00
{
2019-12-14 14:13:47 +01:00
webClient = new HttpClient ( ) ;
// Setup the one time conneciton characteristics
webClient . Timeout = new TimeSpan ( 0 , 0 , 30 ) ; // 30 second call timeout
webClient . DefaultRequestHeaders . ConnectionClose = false ; // Keep alives
2018-12-01 15:05:35 +01:00
}
2019-12-14 14:13:47 +01:00
else
{
webClient . DefaultRequestHeaders . Clear ( ) ;
}
// Accept JSON and Text
webClient . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/json" ) ) ;
webClient . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "text/plain" ) ) ;
2018-12-01 15:05:35 +01:00
2019-12-14 14:13:47 +01:00
// Setup the keep alive timeout
ServicePointManager . FindServicePoint ( new Uri ( url ) ) . ConnectionLeaseTimeout = 300000 ; // 5 mins for keep alives
2018-05-22 10:11:50 +02:00
2019-12-14 14:13:47 +01:00
// Add any custom headers
if ( headers ! = null )
{
foreach ( var header in headers )
{
webClient . DefaultRequestHeaders . Add ( header . header , header . value ) ;
}
}
2019-02-18 00:08:11 +01:00
2018-12-15 22:07:29 +01:00
try
{
2019-12-30 16:13:28 +01:00
// log.DoLogInfo("Calling URL: " + url);
2019-12-14 14:13:47 +01:00
var response = webClient . GetAsync ( url ) . Result ;
string repsonseString = response . Content . ReadAsStringAsync ( ) . Result ;
if ( response . IsSuccessStatusCode )
2019-09-07 00:55:41 +02:00
{
2019-12-14 14:13:47 +01:00
return repsonseString ;
2019-09-07 00:55:41 +02:00
}
2019-12-14 14:13:47 +01:00
else
{
// Error
var message = string . Format ( "Error whilst calling {0} - {1}" , url , repsonseString ) ;
log . DoLogError ( message ) ;
throw new Exception ( message ) ;
}
2018-12-15 22:07:29 +01:00
}
2019-12-14 14:13:47 +01:00
catch ( TaskCanceledException tcEx )
2018-12-15 22:07:29 +01:00
{
2019-12-14 14:13:47 +01:00
// Conneciton timeout
log . DoLogError ( string . Format ( "Timeout whilst calling {0} - {1}" , url , tcEx . Message ) ) ;
2019-09-22 00:43:13 +02:00
throw ;
2018-12-15 22:07:29 +01:00
}
catch ( Exception ex )
{
2019-12-14 14:13:47 +01:00
log . DoLogError ( string . Format ( "Error whilst calling {0} \nError: {1}" , url , ex . Message ) ) ;
2019-09-22 00:43:13 +02:00
throw ;
}
2018-05-22 10:11:50 +02:00
}
2019-12-14 14:13:47 +01:00
public static Dictionary < string , dynamic > GetJsonFromURL ( string url , LogHelper log , ( string header , string value ) [ ] headers = null )
2018-12-15 22:07:29 +01:00
{
2019-12-14 14:13:47 +01:00
Dictionary < string , dynamic > jsonObject = null ;
string jsonString = GetJsonStringFromURL ( url , log , headers ) ;
2018-05-22 10:11:50 +02:00
2019-12-14 14:13:47 +01:00
// Convert the response to JSON
jsonObject = JsonConvert . DeserializeObject < Dictionary < string , dynamic > > ( jsonString ) ;
return jsonObject ;
}
2018-05-22 10:11:50 +02:00
2019-12-14 14:13:47 +01:00
public static Newtonsoft . Json . Linq . JObject GetSimpleJsonObjectFromURL ( string url , LogHelper log , ( string header , string value ) [ ] headers = null )
{
Newtonsoft . Json . Linq . JObject jsonObject = null ;
string jsonString = GetJsonStringFromURL ( url , log , headers ) ;
jsonObject = JsonConvert . DeserializeObject < Newtonsoft . Json . Linq . JObject > ( jsonString ) ;
2018-05-22 10:11:50 +02:00
return jsonObject ;
}
2018-12-15 22:07:29 +01:00
public static List < dynamic > GetSimpleJsonListFromURL ( string url , LogHelper log )
{
2018-05-22 10:11:50 +02:00
List < dynamic > jsonObject = null ;
2019-12-14 14:13:47 +01:00
string jsonString = GetJsonStringFromURL ( url , log , null ) ;
jsonObject = JsonConvert . DeserializeObject < List < dynamic > > ( jsonString ) ;
2018-05-22 10:11:50 +02:00
return jsonObject ;
}
2018-12-15 22:07:29 +01:00
public static Newtonsoft . Json . Linq . JArray GetSimpleJsonArrayFromURL ( string url , LogHelper log )
{
2018-05-22 10:11:50 +02:00
Newtonsoft . Json . Linq . JArray jsonObject = null ;
2019-12-14 14:13:47 +01:00
string jsonString = GetJsonStringFromURL ( url , log , null ) ;
jsonObject = JsonConvert . DeserializeObject < Newtonsoft . Json . Linq . JArray > ( jsonString ) ;
2018-05-22 10:11:50 +02:00
return jsonObject ;
}
2018-12-15 22:07:29 +01:00
public static string GetLatestGitHubRelease ( LogHelper log , string defaultVersion )
{
2018-05-22 10:11:50 +02:00
string result = defaultVersion ;
2018-12-15 22:07:29 +01:00
try
{
2018-12-19 15:40:44 +01:00
string baseUrl = "https://api.github.com/repos/PTMagicians/PTMagic/releases/latest" ;
2018-05-22 10:11:50 +02:00
2019-12-14 14:13:47 +01:00
Newtonsoft . Json . Linq . JObject jsonObject = GetSimpleJsonObjectFromURL ( baseUrl , log , new ( string header , string value ) [ ] { ( "User-Agent" , "PTMagic.Import" ) } ) ;
2018-12-15 22:07:29 +01:00
if ( jsonObject ! = null )
{
2018-05-22 10:11:50 +02:00
result = jsonObject . GetValue ( "tag_name" ) . ToString ( ) ;
}
2018-12-15 22:07:29 +01:00
}
catch ( WebException ex )
{
2018-05-22 10:11:50 +02:00
log . DoLogDebug ( "GitHub version check error: " + ex . Message ) ;
2018-12-15 22:07:29 +01:00
}
catch ( Exception ex )
{
2018-05-22 10:11:50 +02:00
log . DoLogDebug ( "GitHub version check error: " + ex . Message ) ;
}
return result ;
}
2019-02-17 15:45:00 +01:00
public static double GetMainFiatCurrencyRate ( string currency , string FreeCurrencyAPI , LogHelper log )
2018-12-15 22:07:29 +01:00
{
2018-05-22 10:11:50 +02:00
double result = 1 ;
2019-02-18 00:33:02 +01:00
string baseUrl = "http://free.currencyconverterapi.com/api/v5/convert?q=USD_" + currency + "&compact=y&apiKey=" + FreeCurrencyAPI ;
2018-05-22 10:11:50 +02:00
log . DoLogDebug ( "http://free.currencyconverterapi.com - Getting latest exchange rates..." ) ;
2019-12-14 14:13:47 +01:00
Newtonsoft . Json . Linq . JObject jsonObject = GetSimpleJsonObjectFromURL ( baseUrl , log , null ) ;
2018-12-15 22:07:29 +01:00
if ( jsonObject ! = null )
{
2018-05-22 10:11:50 +02:00
log . DoLogDebug ( "http://free.currencyconverterapi.com - Received latest exchange rates." ) ;
result = ( double ) jsonObject [ "USD_" + currency ] [ "val" ] ;
log . DoLogInfo ( "http://free.currencyconverterapi.com - Latest exchange rate for USD to " + currency + " is " + result ) ;
}
return result ;
}
2018-12-15 22:07:29 +01:00
public static Dictionary < string , MarketInfo > GetMarketInfosFromFile ( PTMagicConfiguration systemConfiguration , LogHelper log )
{
2018-05-22 10:11:50 +02:00
Dictionary < string , MarketInfo > result = new Dictionary < string , MarketInfo > ( ) ;
string marketInfoFilePath = Directory . GetCurrentDirectory ( ) + Path . DirectorySeparatorChar + Constants . PTMagicPathData + Path . DirectorySeparatorChar + Constants . PTMagicPathExchange + Path . DirectorySeparatorChar + "MarketInfo.json" ;
2018-12-15 22:07:29 +01:00
if ( File . Exists ( marketInfoFilePath ) )
{
try
{
2018-05-22 10:11:50 +02:00
result = JsonConvert . DeserializeObject < Dictionary < string , MarketInfo > > ( System . IO . File . ReadAllText ( marketInfoFilePath ) ) ;
2018-12-15 22:07:29 +01:00
}
catch ( Exception ex )
{
2018-05-22 10:11:50 +02:00
log . DoLogDebug ( ex . Message ) ;
}
}
2018-12-15 22:07:29 +01:00
if ( result = = null )
{
2018-05-22 10:11:50 +02:00
result = new Dictionary < string , MarketInfo > ( ) ;
}
return result ;
}
2018-12-15 22:07:29 +01:00
public static void SaveMarketInfosToFile ( Dictionary < string , MarketInfo > marketInfos , PTMagicConfiguration systemConfiguration , LogHelper log )
{
2018-05-22 10:11:50 +02:00
FileHelper . WriteTextToFile ( Directory . GetCurrentDirectory ( ) + Path . DirectorySeparatorChar + Constants . PTMagicPathData + Path . DirectorySeparatorChar + Constants . PTMagicPathExchange + Path . DirectorySeparatorChar , "MarketInfo.json" , JsonConvert . SerializeObject ( marketInfos ) ) ;
}
2018-12-15 22:07:29 +01:00
public static D ictionary < string , Market > GetMarketDataFromFile ( PTMagicConfiguration systemConfiguration , LogHelper log , string platform , DateTime maxDateTime , string marketCaption )
{
2018-05-22 10:11:50 +02:00
Dictionary < string , Market > result = new Dictionary < string , Market > ( ) ;
DirectoryInfo dataDirectory = new DirectoryInfo ( Directory . GetCurrentDirectory ( ) + Path . DirectorySeparatorChar + Constants . PTMagicPathData + Path . DirectorySeparatorChar + platform + Path . DirectorySeparatorChar ) ;
// Get market files older than max datetime in descending order (newest file up top)
List < FileInfo > marketFiles = dataDirectory . EnumerateFiles ( "MarketData*" )
. Select ( x = > { x . Refresh ( ) ; return x ; } )
. Where ( x = > x . LastWriteTimeUtc < = maxDateTime )
. ToArray ( ) . OrderByDescending ( f = > f . LastWriteTimeUtc ) . ToList ( ) ;
FileInfo marketFile = null ;
2018-12-15 22:07:29 +01:00
if ( marketFiles . Count > 0 )
{
2018-05-22 10:11:50 +02:00
marketFile = marketFiles . First ( ) ;
log . DoLogDebug ( platform + " - " + marketCaption + " market data loaded (" + marketFile . LastWriteTimeUtc . ToString ( ) + ")" ) ;
2018-12-15 22:07:29 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
log . DoLogDebug ( platform + " - Not able to load " + marketCaption + " market data. Loading next youngest market file instead." ) ;
// Get market files younger than max datetime in ascending order (oldest file up top)
marketFiles = dataDirectory . EnumerateFiles ( "MarketData*" )
. Select ( x = > { x . Refresh ( ) ; return x ; } )
. Where ( x = > x . LastWriteTimeUtc > = maxDateTime )
. ToArray ( ) . OrderBy ( f = > f . LastWriteTimeUtc ) . ToList ( ) ;
2018-12-15 22:07:29 +01:00
if ( marketFiles . Count > 0 )
{
2018-05-22 10:11:50 +02:00
marketFile = marketFiles . First ( ) ;
log . DoLogDebug ( platform + " - " + marketCaption + " market data loaded (" + marketFile . LastWriteTimeUtc . ToString ( ) + ")" ) ;
}
}
2018-12-15 22:07:29 +01:00
try
{
2018-05-22 10:11:50 +02:00
// Get JSON object
result = JsonConvert . DeserializeObject < Dictionary < string , Market > > ( marketFile . OpenText ( ) . ReadToEnd ( ) ) ;
2018-12-15 22:07:29 +01:00
}
catch ( Exception ex )
{
2018-05-22 10:11:50 +02:00
log . DoLogCritical ( ex . Message , ex ) ;
}
return result ;
}
2018-12-15 22:07:29 +01:00
public static Dictionary < string , List < MarketTrendChange > > BuildMarketTrends ( string platform , string mainMarket , List < string > marketList , string sortBy , bool isGlobal , Dictionary < string , List < MarketTrendChange > > output , PTMagicConfiguration systemConfiguration , LogHelper log )
{
2018-05-22 10:11:50 +02:00
2018-12-15 22:07:29 +01:00
try
{
2018-05-22 10:11:50 +02:00
List < MarketTrend > marketTrends = systemConfiguration . AnalyzerSettings . MarketAnalyzer . MarketTrends . FindAll ( mt = > mt . Platform . Equals ( platform , StringComparison . InvariantCultureIgnoreCase ) ) ;
2018-12-15 22:07:29 +01:00
if ( marketTrends . Count > 0 )
{
2019-02-04 01:17:38 +01:00
Dictionary < string , Market > recentMarkets = BaseAnalyzer . GetMarketDataFromFile ( systemConfiguration , log , platform , DateTime . UtcNow , "Recent" ) ;
2018-05-22 10:11:50 +02:00
2018-12-15 22:07:29 +01:00
foreach ( MarketTrend marketTrend in marketTrends )
{
if ( platform . Equals ( "Exchange" , StringComparison . InvariantCultureIgnoreCase ) )
{
2018-05-22 10:11:50 +02:00
log . DoLogInfo ( platform + " - Building market trend changes for '" + marketTrend . Name + "' on main market '" + mainMarket + "' with " + marketList . Count . ToString ( ) + " markets..." ) ;
2018-12-15 22:07:29 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
log . DoLogInfo ( platform + " - Building market trend changes for '" + marketTrend . Name + "'..." ) ;
}
2019-02-04 01:17:38 +01:00
Dictionary < string , Market > trendMarkets = BaseAnalyzer . GetMarketDataFromFile ( systemConfiguration , log , platform , DateTime . UtcNow . AddMinutes ( - marketTrend . TrendMinutes ) , marketTrend . Name ) ;
2018-05-22 10:11:50 +02:00
List < MarketTrendChange > marketTrendChanges = BaseAnalyzer . GetMarketTrendChanges ( platform , mainMarket , marketTrend , marketList , recentMarkets , trendMarkets , sortBy , isGlobal , systemConfiguration , log ) ;
output . Add ( marketTrend . Name , marketTrendChanges ) ;
log . DoLogInfo ( platform + " - " + marketTrendChanges . Count . ToString ( ) + " Market trend changes built for '" + marketTrend . Name + "'." ) ;
}
}
2018-12-15 22:07:29 +01:00
}
catch ( Exception ex )
{
2018-05-22 10:11:50 +02:00
log . DoLogCritical ( ex . Message , ex ) ;
}
return output ;
}
2019-03-09 09:38:46 +01:00
public static List < MarketTrendChange > GetMarketTrendChanges (
2019-09-22 00:43:13 +02:00
string platform ,
string mainMarket ,
MarketTrend marketTrend ,
List < string > marketList ,
Dictionary < string , Market > recentMarkets ,
Dictionary < string , Market > trendMarkets ,
string sortBy ,
bool isGlobal ,
PTMagicConfiguration systemConfiguration ,
2019-03-09 09:38:46 +01:00
LogHelper log )
2018-12-15 22:07:29 +01:00
{
2018-05-22 10:11:50 +02:00
List < MarketTrendChange > result = new List < MarketTrendChange > ( ) ;
var sortedMarkets = new SortedDictionary < string , Market > ( recentMarkets ) . OrderBy ( m = > m . Value . Position ) ;
2018-12-15 22:07:29 +01:00
if ( sortBy . Equals ( "Volume" ) )
{
2018-05-22 10:11:50 +02:00
sortedMarkets = new SortedDictionary < string , Market > ( recentMarkets ) . OrderByDescending ( m = > m . Value . Volume24h ) ;
}
int marketCount = 1 ;
2018-12-15 22:07:29 +01:00
foreach ( KeyValuePair < string , Market > recentMarketPair in sortedMarkets )
{
if ( marketList . Count = = 0 | | marketList . Contains ( recentMarketPair . Key ) )
{
2018-05-22 10:11:50 +02:00
bool excludeMainCurrency = systemConfiguration . AnalyzerSettings . MarketAnalyzer . ExcludeMainCurrency ;
2018-12-15 22:07:29 +01:00
if ( ! marketTrend . ExcludeMainCurrency )
{
2018-05-22 10:11:50 +02:00
excludeMainCurrency = marketTrend . ExcludeMainCurrency ;
}
2018-12-15 22:07:29 +01:00
if ( platform . Equals ( "CoinMarketCap" , StringComparison . InvariantCulture ) & & excludeMainCurrency )
{
2018-05-22 10:11:50 +02:00
// Check if this is the main currency (only for CoinMarketCap)
2018-12-15 22:07:29 +01:00
if ( recentMarketPair . Value . Symbol . Equals ( mainMarket , StringComparison . InvariantCultureIgnoreCase ) )
{
2018-05-22 10:11:50 +02:00
// If the current market is the main currency, skip it
continue ;
}
}
2019-03-09 09:38:46 +01:00
2020-11-08 13:45:31 +01:00
Market recentMarket ;
if ( recentMarkets . TryGetValue ( recentMarketPair . Key , out recentMarket ) )
2018-12-15 22:07:29 +01:00
{
2020-11-08 13:45:31 +01:00
List < string > ignoredMarkets = SystemHelper . ConvertTokenStringToList ( marketTrend . IgnoredMarkets , "," ) ;
if ( ignoredMarkets . Contains ( recentMarketPair . Value . Symbol ) )
{
log . DoLogDebug ( platform + " - Market trend '" + marketTrend . Name + "' for '" + recentMarketPair . Key + "' is ignored in this trend." ) ;
continue ;
}
List < string > allowedMarkets = SystemHelper . ConvertTokenStringToList ( marketTrend . AllowedMarkets , "," ) ;
if ( allowedMarkets . Count > 0 & & ! allowedMarkets . Contains ( recentMarketPair . Value . Symbol ) )
{
log . DoLogDebug ( platform + " - Market trend '" + marketTrend . Name + "' for '" + recentMarketPair . Key + "' is not allowed in this trend." ) ;
continue ;
}
}
else
2020-08-20 08:16:48 +02:00
{
2020-11-08 13:45:31 +01:00
// No recent market data
log . DoLogDebug ( platform + " - Market trend '" + marketTrend . Name + "' for '" + recentMarketPair . Key + "' has no recent market trend data." ) ;
2020-08-20 08:16:48 +02:00
continue ;
}
2018-05-22 10:11:50 +02:00
2020-11-08 13:45:31 +01:00
Market trendMarket ;
if ( trendMarkets . TryGetValue ( recentMarketPair . Key , out trendMarket ) )
2020-08-20 08:16:48 +02:00
{
double recentMarketPrice = recentMarket . Price ;
double trendMarketPrice = trendMarket . Price ;
if ( ! platform . Equals ( "CoinMarketCap" , StringComparison . InvariantCulture ) & & marketTrend . TrendCurrency . Equals ( "Fiat" , StringComparison . InvariantCultureIgnoreCase ) )
{
if ( recentMarket . MainCurrencyPriceUSD > 0 & & trendMarket . MainCurrencyPriceUSD > 0 )
2018-12-15 22:07:29 +01:00
{
2020-08-20 08:16:48 +02:00
recentMarketPrice = recentMarketPrice * recentMarket . MainCurrencyPriceUSD ;
trendMarketPrice = trendMarketPrice * trendMarket . MainCurrencyPriceUSD ;
2018-05-22 10:11:50 +02:00
}
2020-08-20 08:16:48 +02:00
}
2018-05-22 10:11:50 +02:00
2020-08-20 08:16:48 +02:00
double trendMarketChange = ( recentMarketPrice - trendMarketPrice ) / trendMarketPrice * 100 ;
2018-05-22 10:11:50 +02:00
2020-08-20 08:16:48 +02:00
MarketTrendChange mtc = new MarketTrendChange ( ) ;
mtc . MarketTrendName = marketTrend . Name ;
mtc . TrendMinutes = marketTrend . TrendMinutes ;
mtc . TrendChange = trendMarketChange ;
mtc . Market = recentMarket . Name ;
mtc . LastPrice = recentMarket . Price ;
mtc . Volume24h = recentMarket . Volume24h ;
mtc . TrendDateTime = DateTime . UtcNow ;
2018-05-22 10:11:50 +02:00
2020-08-20 08:16:48 +02:00
result . Add ( mtc ) ;
log . DoLogDebug ( platform + " - Market trend '" + marketTrend . Name + "' for '" + recentMarketPair . Key + "' (Vol. " + recentMarket . Volume24h . ToString ( "#,#0.00" ) + ") is " + trendMarketChange . ToString ( "#,#0.00" ) + "% in " + SystemHelper . GetProperDurationTime ( marketTrend . TrendMinutes * 60 ) . ToLower ( ) + "." ) ;
marketCount + + ;
2020-11-08 13:45:31 +01:00
}
else
{
// No data market trend data
log . DoLogDebug ( platform + " - Market trend '" + marketTrend . Name + "' for '" + recentMarketPair . Key + "' has no market trend data." ) ;
continue ;
2018-05-22 10:11:50 +02:00
}
}
}
2018-12-15 22:07:29 +01:00
if ( marketTrend . MaxMarkets > 0 & & isGlobal )
{
2018-05-22 10:11:50 +02:00
int maxMarkets = ( marketTrend . MaxMarkets < = result . Count ) ? marketTrend . MaxMarkets : result . Count ;
result = result . GetRange ( 0 , maxMarkets ) ;
}
return result ;
}
2018-12-15 22:07:29 +01:00
public static Dictionary < string , double > BuildGlobalMarketTrends ( Dictionary < string , List < MarketTrendChange > > globalMarketTrendChanges , PTMagicConfiguration systemConfiguration , LogHelper log )
{
2018-05-22 10:11:50 +02:00
Dictionary < string , double > result = new Dictionary < string , double > ( ) ;
List < MarketTrend > marketTrends = systemConfiguration . AnalyzerSettings . MarketAnalyzer . MarketTrends ;
2018-12-15 22:07:29 +01:00
if ( marketTrends . Count > 0 )
{
foreach ( MarketTrend marketTrend in marketTrends )
{
2018-05-22 10:11:50 +02:00
log . DoLogInfo ( "Building market trend average for '" + marketTrend . Name + "'" ) ;
2018-12-15 22:07:29 +01:00
if ( globalMarketTrendChanges . ContainsKey ( marketTrend . Name ) )
{
2018-05-22 10:11:50 +02:00
List < MarketTrendChange > marketTrendChanges = globalMarketTrendChanges [ marketTrend . Name ] ;
2018-12-15 22:07:29 +01:00
if ( marketTrendChanges ! = null & & marketTrendChanges . Count > 0 )
{
2021-01-30 17:32:36 +01:00
double totalTrendChange = 0 ;
2021-02-05 16:22:01 +01:00
int trendsCount = marketTrendChanges . Count ;
2021-01-30 17:32:36 +01:00
foreach ( MarketTrendChange marketTrendChange in marketTrendChanges )
{
2021-01-31 18:29:15 +01:00
if ( marketTrend . TrendThreshold ! = 0 )
2021-01-30 17:32:36 +01:00
{
2021-01-31 18:29:15 +01:00
if ( ( marketTrendChange . TrendChange > marketTrend . TrendThreshold ) | | ( marketTrendChange . TrendChange < ( marketTrend . TrendThreshold * - 1 ) ) )
2021-01-30 17:32:36 +01:00
{
2021-01-31 14:35:11 +01:00
log . DoLogInfo ( "Market trend '" + marketTrend . Name + "' is ignoring " + marketTrendChange . Market + " for exceeding TrendThreshold." ) ;
2021-02-05 16:22:01 +01:00
trendsCount + = - 1 ;
2021-01-30 17:32:36 +01:00
}
else
{
totalTrendChange + = marketTrendChange . TrendChange ;
}
}
else
{
totalTrendChange + = marketTrendChange . TrendChange ;
}
}
2021-02-05 16:22:01 +01:00
double averageTrendChange = totalTrendChange / trendsCount ;
2018-05-22 10:11:50 +02:00
result . Add ( marketTrend . Name , averageTrendChange ) ;
log . DoLogInfo ( "Built average market trend change '" + marketTrend . Name + "' (" + averageTrendChange . ToString ( "#,#0.00" ) + "% in " + marketTrend . TrendMinutes . ToString ( ) + " minutes) for " + marketTrendChanges . Count . ToString ( ) + " markets." ) ;
2018-12-15 22:07:29 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
result . Add ( marketTrend . Name , 0 ) ;
log . DoLogWarn ( "No market trend changes found for '" + marketTrend . Name + "' - returning 0%" ) ;
}
2018-12-15 22:07:29 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
result . Add ( marketTrend . Name , 0 ) ;
log . DoLogWarn ( "Market trend '" + marketTrend . Name + "' not found in globalMarketTrendChanges[] - returning 0%" ) ;
}
}
}
return result ;
}
}
}