2018-05-22 10:11:50 +02:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using Core.Main ;
2024-01-15 15:12:46 +01:00
using System.Globalization ;
2018-05-22 10:11:50 +02:00
using Core.Main.DataObjects ;
using Core.Main.DataObjects.PTMagicData ;
2018-12-15 22:07:29 +01:00
namespace Monitor.Pages
{
public class SalesAnalyzer : _Internal . BasePageModelSecure
{
2018-05-22 10:11:50 +02:00
public ProfitTrailerData PTData = null ;
2024-01-15 15:12:46 +01:00
public MiscData MiscData { get ; set ; }
public PropertiesData PropertiesData { get ; set ; }
public StatsData StatsData { get ; set ; }
public List < DailyPNLData > DailyPNL { get ; set ; }
public List < MonthlyStatsData > MonthlyStats { get ; set ; }
2018-05-22 10:11:50 +02:00
public string TradesChartDataJSON = "" ;
public string ProfitChartDataJSON = "" ;
2019-03-19 17:13:41 +01:00
public string BalanceChartDataJSON = "" ;
2018-05-22 10:11:50 +02:00
public IEnumerable < KeyValuePair < string , double > > TopMarkets = null ;
public DateTime MinSellLogDate = Constants . confMinDate ;
public Dictionary < DateTime , double > DailyGains = new Dictionary < DateTime , double > ( ) ;
public Dictionary < DateTime , double > MonthlyGains = new Dictionary < DateTime , double > ( ) ;
public DateTimeOffset DateTimeNow = Constants . confMinDate ;
2020-07-22 09:48:40 +02:00
public double totalCurrentValue = 0 ;
2024-01-15 15:12:46 +01:00
2018-12-15 22:07:29 +01:00
public void OnGet ( )
{
2018-05-22 10:11:50 +02:00
base . Init ( ) ;
2018-12-15 22:07:29 +01:00
2018-05-22 10:11:50 +02:00
BindData ( ) ;
}
2024-01-15 15:12:46 +01:00
2018-12-15 22:07:29 +01:00
private void BindData ( )
{
2019-10-13 22:08:48 +02:00
PTData = this . PtDataObject ;
2024-01-15 15:12:46 +01:00
MiscData = this . PTData . Misc ;
PropertiesData = this . PTData . Properties ;
StatsData = this . PTData . Stats ;
MonthlyStats = this . PTData . MonthlyStats ;
DailyPNL = this . PTData . DailyPNL ;
//List<MonthlyStatsData> monthlyStatsData = this.PTData.MonthlyStats;
//List<DailyPNLData> dailyPNLData = this.PTData.DailyPNL;
2018-05-22 10:11:50 +02:00
// Convert local offset time to UTC
TimeSpan offsetTimeSpan = TimeSpan . Parse ( PTMagicConfiguration . GeneralSettings . Application . TimezoneOffset . Replace ( "+" , "" ) ) ;
DateTimeNow = DateTimeOffset . UtcNow . ToOffset ( offsetTimeSpan ) ;
BuildTopMarkets ( ) ;
BuildSalesChartData ( ) ;
2024-01-15 15:12:46 +01:00
BuildTCV ( ) ;
//MonthlyAverages(monthlyStatsData, PTData.Stats.FundingTotal);
}
public ( double totalMonths , DateTime startDate , DateTime endDate ) MonthlyAverages ( List < MonthlyStatsData > monthlyStats , List < DailyPNLData > dailyPNL )
{
double totalMonths = 0 ;
// Get the exact start and end dates of sales data
DateTime startDate = dailyPNL . Min ( d = > DateTime . ParseExact ( d . Date , "d-M-yyyy" , CultureInfo . InvariantCulture ) ) ;
DateTime endDate = dailyPNL . Max ( d = > DateTime . ParseExact ( d . Date , "d-M-yyyy" , CultureInfo . InvariantCulture ) ) ;
int daysInFirstMonth = DateTime . DaysInMonth ( startDate . Year , startDate . Month ) - startDate . Day + 1 ;
int daysInLastMonth = endDate . Day ;
//Console.WriteLine("Start Date: {0}, End Date: {1}, Days in first month: {2}, Days in last month: {3}", startDate, endDate, daysInFirstMonth, daysInLastMonth);
for ( int i = 0 ; i < monthlyStats . Count ; i + + )
{
var monthStat = monthlyStats [ i ] ;
double weight ;
// Parse the Month property into a DateTime object
DateTime monthDate = DateTime . ParseExact ( monthStat . Month , "M-yyyy" , CultureInfo . InvariantCulture ) ;
// If it's the first or last month in the dataset, calculate the weight based on the number of days
if ( i = = 0 )
{
// Calculate weight based on the number of days in the dataset for the first month
weight = daysInFirstMonth / 30.0 ;
}
else if ( i = = monthlyStats . Count - 1 )
{
// Calculate weight based on the number of days in the dataset for the last month
weight = ( daysInLastMonth / 30.0 ) ;
}
else
{
// Otherwise, assume it's a full month
weight = 1 ;
}
totalMonths + = weight ;
//Console.WriteLine("Month: {0}, Weight: {1}", monthStat.Month, weight);
}
return ( totalMonths , startDate , endDate ) ;
2018-05-22 10:11:50 +02:00
}
2018-12-15 22:07:29 +01:00
private void BuildTopMarkets ( )
{
2018-05-22 10:11:50 +02:00
var markets = PTData . SellLog . GroupBy ( m = > m . Market ) ;
Dictionary < string , double > topMarketsDic = new Dictionary < string , double > ( ) ;
2018-12-15 22:07:29 +01:00
foreach ( var market in markets )
{
2020-07-26 16:28:37 +02:00
double totalProfit = 0 ;
2021-08-24 05:48:50 +02:00
totalProfit = PTData . SellLog . FindAll ( m = > m . Market = = market . Key ) . Sum ( m = > m . Profit ) ;
2018-05-22 10:11:50 +02:00
topMarketsDic . Add ( market . Key , totalProfit ) ;
}
TopMarkets = new SortedDictionary < string , double > ( topMarketsDic ) . OrderByDescending ( m = > m . Value ) . Take ( PTMagicConfiguration . GeneralSettings . Monitor . MaxTopMarkets ) ;
}
2018-12-15 22:07:29 +01:00
private void BuildSalesChartData ( )
{
if ( PTData . SellLog . Count > 0 )
{
2018-05-22 10:11:50 +02:00
MinSellLogDate = PTData . SellLog . OrderBy ( sl = > sl . SoldDate ) . First ( ) . SoldDate . Date ;
2019-03-22 15:23:32 +01:00
DateTime graphStartDate = DateTimeNow . DateTime . Date . AddDays ( - 1850 ) ;
2018-05-22 10:11:50 +02:00
if ( MinSellLogDate > graphStartDate ) graphStartDate = MinSellLogDate ;
int tradeDayIndex = 0 ;
string tradesPerDayJSON = "" ;
string profitPerDayJSON = "" ;
2019-03-19 17:13:41 +01:00
string balancePerDayJSON = "" ;
double balance = 0.0 ;
2018-12-15 22:07:29 +01:00
for ( DateTime salesDate = graphStartDate ; salesDate < = DateTimeNow . DateTime . Date ; salesDate = salesDate . AddDays ( 1 ) )
{
if ( tradeDayIndex > 0 )
{
2018-05-22 10:11:50 +02:00
tradesPerDayJSON + = ",\n" ;
profitPerDayJSON + = ",\n" ;
2019-03-19 17:13:41 +01:00
balancePerDayJSON + = ",\n" ;
2018-05-22 10:11:50 +02:00
}
2020-07-26 16:28:37 +02:00
double profit = 0 ;
2018-05-22 10:11:50 +02:00
int trades = PTData . SellLog . FindAll ( t = > t . SoldDate . Date = = salesDate . Date ) . Count ;
2021-08-24 05:48:50 +02:00
profit = PTData . SellLog . FindAll ( t = > t . SoldDate . Date = = salesDate . Date ) . Sum ( t = > t . Profit ) ;
2018-05-22 10:11:50 +02:00
double profitFiat = Math . Round ( profit * Summary . MainMarketPrice , 2 ) ;
2019-03-19 17:13:41 +01:00
balance + = profitFiat ;
2018-05-22 10:11:50 +02:00
tradesPerDayJSON + = "{x: new Date('" + salesDate . Date . ToString ( "yyyy-MM-dd" ) + "'), y: " + trades + "}" ;
profitPerDayJSON + = "{x: new Date('" + salesDate . Date . ToString ( "yyyy-MM-dd" ) + "'), y: " + profitFiat . ToString ( "0.00" , new System . Globalization . CultureInfo ( "en-US" ) ) + "}" ;
2019-03-19 17:13:41 +01:00
balancePerDayJSON + = "{x: new Date('" + salesDate . Date . ToString ( "yyyy-MM-dd" ) + "'), y: " + balance . ToString ( "0.00" , new System . Globalization . CultureInfo ( "en-US" ) ) + "}" ;
2018-05-22 10:11:50 +02:00
tradeDayIndex + + ;
}
TradesChartDataJSON = "[" ;
TradesChartDataJSON + = "{" ;
TradesChartDataJSON + = "key: 'Sales'," ;
TradesChartDataJSON + = "color: '" + Constants . ChartLineColors [ 0 ] + "'," ;
TradesChartDataJSON + = "values: [" + tradesPerDayJSON + "]" ;
TradesChartDataJSON + = "}" ;
TradesChartDataJSON + = "]" ;
ProfitChartDataJSON = "[" ;
ProfitChartDataJSON + = "{" ;
ProfitChartDataJSON + = "key: 'Profit in " + Summary . MainFiatCurrency + "'," ;
ProfitChartDataJSON + = "color: '" + Constants . ChartLineColors [ 1 ] + "'," ;
ProfitChartDataJSON + = "values: [" + profitPerDayJSON + "]" ;
ProfitChartDataJSON + = "}" ;
ProfitChartDataJSON + = "]" ;
2019-03-19 17:13:41 +01:00
BalanceChartDataJSON = "[" ;
BalanceChartDataJSON + = "{" ;
BalanceChartDataJSON + = "key: 'Profit in " + Summary . MainFiatCurrency + "'," ;
BalanceChartDataJSON + = "color: '" + Constants . ChartLineColors [ 1 ] + "'," ;
BalanceChartDataJSON + = "values: [" + balancePerDayJSON + "]" ;
BalanceChartDataJSON + = "}" ;
BalanceChartDataJSON + = "]" ;
2020-07-26 16:28:37 +02:00
2018-12-15 22:07:29 +01:00
for ( DateTime salesDate = DateTimeNow . DateTime . Date ; salesDate > = MinSellLogDate ; salesDate = salesDate . AddDays ( - 1 ) )
{
2018-05-22 10:11:50 +02:00
List < SellLogData > salesDateSales = PTData . SellLog . FindAll ( sl = > sl . SoldDate . Date = = salesDate ) ;
2020-07-26 16:28:37 +02:00
double salesDateProfit ;
2021-08-24 05:48:50 +02:00
salesDateProfit = salesDateSales . Sum ( sl = > sl . Profit ) ;
2018-05-22 10:11:50 +02:00
double salesDateStartBalance = PTData . GetSnapshotBalance ( salesDate ) ;
double salesDateGain = Math . Round ( salesDateProfit / salesDateStartBalance * 100 , 2 ) ;
DailyGains . Add ( salesDate , salesDateGain ) ;
}
DateTime minSellLogMonthDate = new DateTime ( MinSellLogDate . Year , MinSellLogDate . Month , 1 ) . Date ;
DateTime salesMonthStartDate = new DateTime ( DateTimeNow . DateTime . Year , DateTimeNow . DateTime . Month , 1 ) . Date ;
2018-12-15 22:07:29 +01:00
for ( DateTime salesMonthDate = salesMonthStartDate . Date ; salesMonthDate > = minSellLogMonthDate ; salesMonthDate = salesMonthDate . AddMonths ( - 1 ) )
{
2018-05-22 10:11:50 +02:00
List < Core . Main . DataObjects . PTMagicData . SellLogData > salesMonthSales = PTData . SellLog . FindAll ( sl = > sl . SoldDate . Date . Month = = salesMonthDate . Month & & sl . SoldDate . Date . Year = = salesMonthDate . Year ) ;
2020-07-26 16:28:37 +02:00
double salesDateProfit ;
2021-08-24 05:48:50 +02:00
salesDateProfit = salesMonthSales . Sum ( sl = > sl . Profit ) ;
2018-05-22 10:11:50 +02:00
double salesDateStartBalance = PTData . GetSnapshotBalance ( salesMonthDate ) ;
double salesDateGain = Math . Round ( salesDateProfit / salesDateStartBalance * 100 , 2 ) ;
MonthlyGains . Add ( salesMonthDate , salesDateGain ) ;
}
}
}
2020-07-22 09:48:40 +02:00
private void BuildTCV ( )
{
double AvailableBalance = PTData . GetCurrentBalance ( ) ;
foreach ( Core . Main . DataObjects . PTMagicData . DCALogData dcaLogEntry in PTData . DCALog )
{
2021-02-18 10:02:26 +01:00
double leverage = dcaLogEntry . Leverage ;
if ( leverage = = 0 )
{
leverage = 1 ;
}
totalCurrentValue = totalCurrentValue + ( ( dcaLogEntry . Amount * dcaLogEntry . CurrentPrice ) / leverage ) ;
2020-07-22 09:48:40 +02:00
}
totalCurrentValue = totalCurrentValue + AvailableBalance ;
}
2018-05-22 10:11:50 +02:00
}
}