2018-05-22 10:11:50 +02:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using Microsoft.AspNetCore.Http ;
using Core.Main ;
using Core.Helper ;
using Core.Main.DataObjects ;
using Core.Main.DataObjects.PTMagicData ;
using Core.MarketAnalyzer ;
2020-07-21 23:29:07 +02:00
namespace Monitor.Pages
{
public class DashboardBottomModel : _Internal . BasePageModelSecureAJAX
{
2018-05-22 10:11:50 +02:00
public ProfitTrailerData PTData = null ;
public List < MarketTrend > MarketTrends { get ; set ; } = new List < MarketTrend > ( ) ;
public string TrendChartDataJSON = "" ;
public string ProfitChartDataJSON = "" ;
public string LastGlobalSetting = "Default" ;
public DateTimeOffset DateTimeNow = Constants . confMinDate ;
2019-04-30 17:36:27 +02:00
public string AssetDistributionData = "" ;
2020-07-17 13:35:43 +02:00
public double totalCurrentValue = 0 ;
2020-07-21 23:29:07 +02:00
public void OnGet ( )
{
2018-05-22 10:11:50 +02:00
// Initialize Config
base . Init ( ) ;
BindData ( ) ;
2019-04-30 17:36:27 +02:00
BuildAssetDistributionData ( ) ;
2018-05-22 10:11:50 +02:00
}
2020-07-21 23:29:07 +02:00
private void BindData ( )
{
2019-10-13 22:08:48 +02:00
PTData = this . PtDataObject ;
2018-05-22 10:11:50 +02:00
// Cleanup temp files
FileHelper . CleanupFilesMinutes ( PTMagicMonitorBasePath + "wwwroot" + System . IO . Path . DirectorySeparatorChar + "assets" + System . IO . Path . DirectorySeparatorChar + "tmp" + System . IO . Path . DirectorySeparatorChar , 5 ) ;
// Convert local offset time to UTC
TimeSpan offsetTimeSpan = TimeSpan . Parse ( PTMagicConfiguration . GeneralSettings . Application . TimezoneOffset . Replace ( "+" , "" ) ) ;
DateTimeNow = DateTimeOffset . UtcNow . ToOffset ( offsetTimeSpan ) ;
// Get last and current active setting
2020-07-21 23:29:07 +02:00
if ( ! String . IsNullOrEmpty ( HttpContext . Session . GetString ( "LastGlobalSetting" ) ) )
{
2018-05-22 10:11:50 +02:00
LastGlobalSetting = HttpContext . Session . GetString ( "LastGlobalSetting" ) ;
}
HttpContext . Session . SetString ( "LastGlobalSetting" , Summary . CurrentGlobalSetting . SettingName ) ;
// Get market trends
MarketTrends = PTMagicConfiguration . AnalyzerSettings . MarketAnalyzer . MarketTrends . OrderBy ( mt = > mt . TrendMinutes ) . ThenByDescending ( mt = > mt . Platform ) . ToList ( ) ;
BuildMarketTrendChartData ( ) ;
BuildProfitChartData ( ) ;
}
2020-07-21 23:29:07 +02:00
private void BuildMarketTrendChartData ( )
{
if ( MarketTrends . Count > 0 )
{
2018-05-22 10:11:50 +02:00
TrendChartDataJSON = "[" ;
int mtIndex = 0 ;
2020-07-21 23:29:07 +02:00
foreach ( MarketTrend mt in MarketTrends )
{
if ( mt . DisplayGraph )
{
2018-05-22 10:11:50 +02:00
string lineColor = "" ;
2020-07-21 23:29:07 +02:00
if ( mtIndex < Constants . ChartLineColors . Length )
{
2018-05-22 10:11:50 +02:00
lineColor = Constants . ChartLineColors [ mtIndex ] ;
2020-07-21 23:29:07 +02:00
}
else
{
2018-05-22 10:11:50 +02:00
lineColor = Constants . ChartLineColors [ mtIndex - 20 ] ;
}
2020-07-21 23:29:07 +02:00
if ( Summary . MarketTrendChanges . ContainsKey ( mt . Name ) )
{
2018-05-22 10:11:50 +02:00
List < MarketTrendChange > marketTrendChangeSummaries = Summary . MarketTrendChanges [ mt . Name ] ;
2020-07-21 23:29:07 +02:00
if ( marketTrendChangeSummaries . Count > 0 )
{
2018-05-22 10:11:50 +02:00
if ( ! TrendChartDataJSON . Equals ( "[" ) ) TrendChartDataJSON + = "," ;
TrendChartDataJSON + = "{" ;
TrendChartDataJSON + = "key: '" + SystemHelper . SplitCamelCase ( mt . Name ) + "'," ;
TrendChartDataJSON + = "color: '" + lineColor + "'," ;
TrendChartDataJSON + = "values: [" ;
// Get trend ticks for chart
2019-02-04 01:17:38 +01:00
DateTime currentDateTime = new DateTime ( DateTime . UtcNow . Year , DateTime . UtcNow . Month , DateTime . UtcNow . Day , DateTime . UtcNow . Hour , 0 , 0 ) ;
2018-05-22 10:11:50 +02:00
DateTime startDateTime = currentDateTime . AddHours ( - PTMagicConfiguration . GeneralSettings . Monitor . GraphMaxTimeframeHours ) ;
DateTime endDateTime = currentDateTime ;
int trendChartTicks = 0 ;
2020-07-21 23:29:07 +02:00
for ( DateTime tickTime = startDateTime ; tickTime < = endDateTime ; tickTime = tickTime . AddMinutes ( PTMagicConfiguration . GeneralSettings . Monitor . GraphIntervalMinutes ) )
{
2018-05-22 10:11:50 +02:00
List < MarketTrendChange > tickRange = marketTrendChangeSummaries . FindAll ( m = > m . TrendDateTime > = tickTime ) . OrderBy ( m = > m . TrendDateTime ) . ToList ( ) ;
2020-07-21 23:29:07 +02:00
if ( tickRange . Count > 0 )
{
2018-05-22 10:11:50 +02:00
MarketTrendChange mtc = tickRange . First ( ) ;
if ( tickTime ! = startDateTime ) TrendChartDataJSON + = ",\n" ;
if ( Double . IsInfinity ( mtc . TrendChange ) ) mtc . TrendChange = 0 ;
TrendChartDataJSON + = "{ x: new Date('" + tickTime . ToString ( "yyyy-MM-ddTHH:mm:ss" ) . Replace ( "." , ":" ) + "'), y: " + mtc . TrendChange . ToString ( "0.00" , new System . Globalization . CultureInfo ( "en-US" ) ) + "}" ;
trendChartTicks + + ;
}
}
// Add most recent tick
List < MarketTrendChange > latestTickRange = marketTrendChangeSummaries . OrderByDescending ( m = > m . TrendDateTime ) . ToList ( ) ;
2020-07-21 23:29:07 +02:00
if ( latestTickRange . Count > 0 )
{
2018-05-22 10:11:50 +02:00
MarketTrendChange mtc = latestTickRange . First ( ) ;
if ( trendChartTicks > 0 ) TrendChartDataJSON + = ",\n" ;
if ( Double . IsInfinity ( mtc . TrendChange ) ) mtc . TrendChange = 0 ;
TrendChartDataJSON + = "{ x: new Date('" + mtc . TrendDateTime . ToString ( "yyyy-MM-ddTHH:mm:ss" ) . Replace ( "." , ":" ) + "'), y: " + mtc . TrendChange . ToString ( "0.00" , new System . Globalization . CultureInfo ( "en-US" ) ) + "}" ;
}
TrendChartDataJSON + = "]" ;
TrendChartDataJSON + = "}" ;
mtIndex + + ;
}
}
}
}
TrendChartDataJSON + = "]" ;
}
}
2020-07-21 23:29:07 +02:00
private void BuildProfitChartData ( )
{
2018-05-22 10:11:50 +02:00
int tradeDayIndex = 0 ;
string profitPerDayJSON = "" ;
2020-07-23 20:25:43 +02:00
2020-07-21 23:29:07 +02:00
if ( PTData . SellLog . Count > 0 )
{
2018-05-22 10:11:50 +02:00
DateTime minSellLogDate = PTData . SellLog . OrderBy ( sl = > sl . SoldDate ) . First ( ) . SoldDate . Date ;
2019-05-01 05:16:37 +02:00
DateTime graphStartDate = DateTime . UtcNow . Date . AddDays ( - 30 ) ;
2020-07-26 16:28:37 +02:00
if ( minSellLogDate > graphStartDate )
{
graphStartDate = minSellLogDate ;
}
2020-07-21 23:29:07 +02:00
for ( DateTime salesDate = graphStartDate ; salesDate < = DateTime . UtcNow . Date ; salesDate = salesDate . AddDays ( 1 ) )
{
if ( tradeDayIndex > 0 )
{
2018-05-22 10:11:50 +02:00
profitPerDayJSON + = ",\n" ;
}
int trades = PTData . SellLog . FindAll ( t = > t . SoldDate . Date = = salesDate ) . Count ;
double profit = PTData . SellLog . FindAll ( t = > t . SoldDate . Date = = salesDate ) . Sum ( t = > t . Profit ) ;
2020-07-26 16:28:37 +02:00
if ( PTData . Properties . Shorting )
{
profit = profit * ( - 1 ) ;
}
2018-05-22 10:11:50 +02:00
double profitFiat = Math . Round ( profit * Summary . MainMarketPrice , 2 ) ;
profitPerDayJSON + = "{x: new Date('" + salesDate . ToString ( "yyyy-MM-dd" ) + "'), y: " + profitFiat . ToString ( "0.00" , new System . Globalization . CultureInfo ( "en-US" ) ) + "}" ;
tradeDayIndex + + ;
}
ProfitChartDataJSON = "[" ;
ProfitChartDataJSON + = "{" ;
ProfitChartDataJSON + = "key: 'Profit in " + Summary . MainFiatCurrency + "'," ;
ProfitChartDataJSON + = "color: '" + Constants . ChartLineColors [ 1 ] + "'," ;
ProfitChartDataJSON + = "values: [" + profitPerDayJSON + "]" ;
ProfitChartDataJSON + = "}" ;
ProfitChartDataJSON + = "]" ;
}
}
2019-04-30 17:36:27 +02:00
private void BuildAssetDistributionData ( )
{
2020-07-17 14:34:07 +02:00
// the per PT-Eelroy, the PT API doesn't provide these values when using leverage, so they are calculated here to cover either case.
2020-07-17 07:02:48 +02:00
double PairsBalance = 0.0 ;
double DCABalance = 0.0 ;
double PendingBalance = 0.0 ;
double AvailableBalance = PTData . GetCurrentBalance ( ) ;
2020-07-21 23:29:07 +02:00
bool isSellStrategyTrue = false ;
bool isTrailingSellActive = false ;
foreach ( Core . Main . DataObjects . PTMagicData . DCALogData dcaLogEntry in PTData . DCALog )
2020-07-17 07:02:48 +02:00
{
string sellStrategyText = Core . ProfitTrailer . StrategyHelper . GetStrategyText ( Summary , dcaLogEntry . SellStrategies , dcaLogEntry . SellStrategy , isSellStrategyTrue , isTrailingSellActive ) ;
2020-07-17 14:34:07 +02:00
2020-07-17 07:02:48 +02:00
// Aggregate totals
2020-07-17 13:35:43 +02:00
if ( dcaLogEntry . Leverage = = 0 )
2020-07-17 07:02:48 +02:00
{
2020-07-17 13:35:43 +02:00
if ( sellStrategyText . Contains ( "PENDING" ) )
{
2020-07-21 23:29:07 +02:00
PendingBalance = PendingBalance + ( dcaLogEntry . Amount * dcaLogEntry . CurrentPrice ) ;
2020-07-17 13:35:43 +02:00
}
2020-07-21 23:29:07 +02:00
else if ( dcaLogEntry . BuyStrategies . Count > 0 )
2020-07-17 13:35:43 +02:00
{
2020-07-21 23:29:07 +02:00
DCABalance = DCABalance + ( dcaLogEntry . Amount * dcaLogEntry . CurrentPrice ) ;
2020-07-17 13:35:43 +02:00
}
else
{
2020-07-21 23:29:07 +02:00
PairsBalance = PairsBalance + ( dcaLogEntry . Amount * dcaLogEntry . CurrentPrice ) ;
2020-07-17 13:35:43 +02:00
}
2020-07-17 07:02:48 +02:00
}
else
{
2020-07-17 13:35:43 +02:00
if ( sellStrategyText . Contains ( "PENDING" ) )
{
2021-02-17 16:35:23 +01:00
PendingBalance = PendingBalance + ( ( dcaLogEntry . Amount * dcaLogEntry . CurrentPrice ) / dcaLogEntry . Leverage ! = 0 ? dcaLogEntry . Leverage : 1 ) ;
2020-07-17 13:35:43 +02:00
}
2020-07-21 23:29:07 +02:00
else if ( dcaLogEntry . BuyStrategies . Count > 0 )
2020-07-17 13:35:43 +02:00
{
2021-02-17 16:35:23 +01:00
DCABalance = DCABalance + ( ( dcaLogEntry . Amount * dcaLogEntry . CurrentPrice ) / dcaLogEntry . Leverage ! = 0 ? dcaLogEntry . Leverage : 1 ) ;
2020-07-17 13:35:43 +02:00
}
else
{
2021-02-17 16:35:23 +01:00
PairsBalance = PairsBalance + ( ( dcaLogEntry . Amount * dcaLogEntry . CurrentPrice ) / dcaLogEntry . Leverage ! = 0 ? dcaLogEntry . Leverage : 1 ) ;
2020-07-17 13:35:43 +02:00
}
2020-07-17 07:02:48 +02:00
}
}
2020-07-17 13:35:43 +02:00
totalCurrentValue = PendingBalance + DCABalance + PairsBalance + AvailableBalance ;
2019-04-30 17:36:27 +02:00
AssetDistributionData = "[" ;
2019-06-12 02:41:35 +02:00
AssetDistributionData + = "{label: 'Pairs',color: '#82E0AA',value: '" + PairsBalance . ToString ( "0.00" , new System . Globalization . CultureInfo ( "en-US" ) ) + "'}," ;
AssetDistributionData + = "{label: 'DCA',color: '#D98880',value: '" + DCABalance . ToString ( "0.00" , new System . Globalization . CultureInfo ( "en-US" ) ) + "'}," ;
AssetDistributionData + = "{label: 'Pending',color: '#F5B041',value: '" + PendingBalance . ToString ( "0.00" , new System . Globalization . CultureInfo ( "en-US" ) ) + "'}," ;
AssetDistributionData + = "{label: 'Balance',color: '#85C1E9',value: '" + AvailableBalance . ToString ( "0.00" , new System . Globalization . CultureInfo ( "en-US" ) ) + "'}]" ;
2019-04-30 17:36:27 +02:00
}
2018-05-22 10:11:50 +02:00
}
}