PTMagic/Core/DataObjects/ProfitTrailerData.cs

368 lines
15 KiB
C#
Raw Normal View History

2018-05-22 10:11:50 +02:00
using System;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Core.Main.DataObjects.PTMagicData;
2018-12-03 19:45:06 +01:00
namespace Core.Main.DataObjects
{
2018-05-22 10:11:50 +02:00
2018-12-03 19:45:06 +01:00
public class ProfitTrailerData
{
2018-05-22 10:11:50 +02:00
private List<SellLogData> _sellLog = new List<SellLogData>();
private List<DCALogData> _dcaLog = new List<DCALogData>();
private List<BuyLogData> _buyLog = new List<BuyLogData>();
private string _ptmBasePath = "";
private PTMagicConfiguration _systemConfiguration = null;
private TransactionData _transactionData = null;
private DateTimeOffset _dateTimeNow = Constants.confMinDate;
2018-12-03 19:45:06 +01:00
public ProfitTrailerData(string ptmBasePath, PTMagicConfiguration systemConfiguration)
{
2018-05-22 10:11:50 +02:00
_ptmBasePath = ptmBasePath;
_systemConfiguration = systemConfiguration;
// Find the path to the Profit Trailer data file
string ptDataFilePath = Path.Combine(systemConfiguration.GeneralSettings.Application.ProfitTrailerPath, "data", "ProfitTrailerData.json");
if (!File.Exists(ptDataFilePath))
{
// Try the older location for PT 1.x and PT 2.0.x
ptDataFilePath = Path.Combine(systemConfiguration.GeneralSettings.Application.ProfitTrailerPath, "ProfitTrailerData.json");
if (!File.Exists(ptDataFilePath))
{
// Can't find the Profit Trailer Data
throw new Exception("Unable to load Profit Trailer data file at: " + ptDataFilePath);
}
}
2018-12-03 19:45:06 +01:00
PTData rawPTData = JsonConvert.DeserializeObject<PTData>(File.ReadAllText(ptDataFilePath));
2018-12-03 19:45:06 +01:00
if (rawPTData.SellLogData != null)
{
2018-05-22 10:11:50 +02:00
this.BuildSellLogData(rawPTData.SellLogData, _systemConfiguration);
}
2018-12-03 19:45:06 +01:00
if (rawPTData.bbBuyLogData != null)
{
2018-05-22 10:11:50 +02:00
this.BuildBuyLogData(rawPTData.bbBuyLogData, _systemConfiguration);
}
2018-12-03 19:45:06 +01:00
if (rawPTData.DCALogData != null)
{
2018-05-22 10:11:50 +02:00
this.BuildDCALogData(rawPTData.DCALogData, rawPTData.GainLogData, _systemConfiguration);
}
// Convert local offset time to UTC
TimeSpan offsetTimeSpan = TimeSpan.Parse(systemConfiguration.GeneralSettings.Application.TimezoneOffset.Replace("+", ""));
_dateTimeNow = DateTimeOffset.UtcNow.ToOffset(offsetTimeSpan);
}
2018-12-03 19:45:06 +01:00
public List<SellLogData> SellLog
{
get
{
2018-05-22 10:11:50 +02:00
return _sellLog;
}
}
2018-12-03 19:45:06 +01:00
public List<SellLogData> SellLogToday
{
get
{
2018-05-22 10:11:50 +02:00
return _sellLog.FindAll(sl => sl.SoldDate.Date == _dateTimeNow.DateTime.Date);
}
}
2018-12-03 19:45:06 +01:00
public List<SellLogData> SellLogYesterday
{
get
{
2018-05-22 10:11:50 +02:00
return _sellLog.FindAll(sl => sl.SoldDate.Date == _dateTimeNow.DateTime.AddDays(-1).Date);
}
}
2018-12-03 19:45:06 +01:00
public List<SellLogData> SellLogLast7Days
{
get
{
2018-05-22 10:11:50 +02:00
return _sellLog.FindAll(sl => sl.SoldDate.Date >= _dateTimeNow.DateTime.AddDays(-7).Date);
}
}
2018-12-03 19:45:06 +01:00
public List<DCALogData> DCALog
{
get
{
2018-05-22 10:11:50 +02:00
return _dcaLog;
}
}
2018-12-03 19:45:06 +01:00
public List<BuyLogData> BuyLog
{
get
{
2018-05-22 10:11:50 +02:00
return _buyLog;
}
}
2018-12-03 19:45:06 +01:00
public TransactionData TransactionData
{
get
{
2018-05-22 10:11:50 +02:00
if (_transactionData == null) _transactionData = new TransactionData(_ptmBasePath);
return _transactionData;
}
}
2018-12-03 19:45:06 +01:00
public double GetCurrentBalance()
{
2018-05-22 10:11:50 +02:00
return this.GetSnapshotBalance(DateTime.Now.ToUniversalTime());
}
2018-12-03 19:45:06 +01:00
public double GetSnapshotBalance(DateTime snapshotDateTime)
{
2018-05-22 10:11:50 +02:00
double result = _systemConfiguration.GeneralSettings.Application.StartBalance;
result += this.SellLog.FindAll(sl => sl.SoldDate.Date < snapshotDateTime.Date).Sum(sl => sl.Profit);
result += this.TransactionData.Transactions.FindAll(t => t.GetLocalDateTime(_systemConfiguration.GeneralSettings.Application.TimezoneOffset) < snapshotDateTime).Sum(t => t.Amount);
return result;
}
2018-12-03 19:45:06 +01:00
private void BuildSellLogData(List<sellLogData> rawSellLogData, PTMagicConfiguration systemConfiguration)
{
foreach (sellLogData rsld in rawSellLogData)
{
2018-05-22 10:11:50 +02:00
SellLogData sellLogData = new SellLogData();
sellLogData.SoldAmount = rsld.soldAmount;
sellLogData.BoughtTimes = rsld.boughtTimes;
sellLogData.Market = rsld.market;
sellLogData.ProfitPercent = rsld.profit;
sellLogData.SoldPrice = rsld.currentPrice;
sellLogData.AverageBuyPrice = rsld.averageCalculator.avgPrice;
sellLogData.TotalCost = sellLogData.SoldAmount * sellLogData.AverageBuyPrice;
double soldValueRaw = (sellLogData.SoldAmount * sellLogData.SoldPrice);
double soldValueAfterFees = soldValueRaw - (soldValueRaw * (rsld.averageCalculator.fee / 100));
sellLogData.SoldValue = soldValueAfterFees;
sellLogData.Profit = Math.Round(sellLogData.SoldValue - sellLogData.TotalCost, 8);
// Profit Trailer sales are saved in UTC
DateTimeOffset ptSoldDate = DateTimeOffset.Parse(rsld.soldDate.date.year.ToString() + "-" + rsld.soldDate.date.month.ToString("00") + "-" + rsld.soldDate.date.day.ToString("00") + "T" + rsld.soldDate.time.hour.ToString("00") + ":" + rsld.soldDate.time.minute.ToString("00") + ":" + rsld.soldDate.time.second.ToString("00"), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
// Convert UTC sales time to local offset time
TimeSpan offsetTimeSpan = TimeSpan.Parse(systemConfiguration.GeneralSettings.Application.TimezoneOffset.Replace("+", ""));
ptSoldDate = ptSoldDate.ToOffset(offsetTimeSpan);
sellLogData.SoldDate = ptSoldDate.DateTime;
_sellLog.Add(sellLogData);
}
}
2018-12-03 19:45:06 +01:00
private void BuildDCALogData(List<dcaLogData> rawDCALogData, List<dcaLogData> rawPairsLogData, PTMagicConfiguration systemConfiguration)
{
foreach (dcaLogData rdld in rawDCALogData)
{
2018-05-22 10:11:50 +02:00
DCALogData dcaLogData = new DCALogData();
dcaLogData.Amount = rdld.averageCalculator.totalAmount;
dcaLogData.BoughtTimes = rdld.boughtTimes;
dcaLogData.Market = rdld.market;
dcaLogData.ProfitPercent = rdld.profit;
dcaLogData.AverageBuyPrice = rdld.averageCalculator.avgPrice;
dcaLogData.TotalCost = rdld.averageCalculator.totalCost;
dcaLogData.BuyTriggerPercent = rdld.buyProfit;
dcaLogData.CurrentLowBBValue = rdld.BBLow;
dcaLogData.CurrentHighBBValue = rdld.highbb;
dcaLogData.BBTrigger = rdld.BBTrigger;
dcaLogData.CurrentPrice = rdld.currentPrice;
dcaLogData.SellTrigger = rdld.triggerValue;
dcaLogData.PercChange = rdld.percChange;
dcaLogData.BuyStrategy = rdld.buyStrategy;
if (dcaLogData.BuyStrategy == null) dcaLogData.BuyStrategy = "";
dcaLogData.SellStrategy = rdld.sellStrategy;
if (dcaLogData.SellStrategy == null) dcaLogData.SellStrategy = "";
2018-12-03 19:45:06 +01:00
if (rdld.positive != null)
{
dcaLogData.IsTrailing = rdld.positive.IndexOf("trailing", StringComparison.InvariantCultureIgnoreCase) > -1;
dcaLogData.IsTrue = rdld.positive.IndexOf("true", StringComparison.InvariantCultureIgnoreCase) > -1;
2018-12-03 19:45:06 +01:00
}
else
{
if (rdld.buyStrategies != null)
{
foreach (PTStrategy bs in rdld.buyStrategies)
{
2018-05-22 10:11:50 +02:00
Strategy buyStrategy = new Strategy();
buyStrategy.Type = bs.type;
buyStrategy.Name = bs.name;
buyStrategy.EntryValue = bs.entryValue;
buyStrategy.EntryValueLimit = bs.entryValueLimit;
buyStrategy.TriggerValue = bs.triggerValue;
buyStrategy.CurrentValue = bs.currentValue;
buyStrategy.CurrentValuePercentage = bs.currentValuePercentage;
buyStrategy.Decimals = bs.decimals;
buyStrategy.IsTrailing = bs.positive.IndexOf("trailing", StringComparison.InvariantCultureIgnoreCase) > -1;
buyStrategy.IsTrue = bs.positive.IndexOf("true", StringComparison.InvariantCultureIgnoreCase) > -1;
2018-05-22 10:11:50 +02:00
dcaLogData.BuyStrategies.Add(buyStrategy);
}
}
2018-12-03 19:45:06 +01:00
if (rdld.sellStrategies != null)
{
foreach (PTStrategy ss in rdld.sellStrategies)
{
2018-05-22 10:11:50 +02:00
Strategy sellStrategy = new Strategy();
sellStrategy.Type = ss.type;
sellStrategy.Name = ss.name;
sellStrategy.EntryValue = ss.entryValue;
sellStrategy.EntryValueLimit = ss.entryValueLimit;
sellStrategy.TriggerValue = ss.triggerValue;
sellStrategy.CurrentValue = ss.currentValue;
sellStrategy.CurrentValuePercentage = ss.currentValuePercentage;
sellStrategy.Decimals = ss.decimals;
sellStrategy.IsTrailing = ss.positive.IndexOf("trailing", StringComparison.InvariantCultureIgnoreCase) > -1;
sellStrategy.IsTrue = ss.positive.IndexOf("true", StringComparison.InvariantCultureIgnoreCase) > -1;
2018-05-22 10:11:50 +02:00
dcaLogData.SellStrategies.Add(sellStrategy);
}
}
}
// Profit Trailer bought times are saved in UTC
2018-12-03 19:45:06 +01:00
if (rdld.averageCalculator.firstBoughtDate != null)
{
2018-05-22 10:11:50 +02:00
DateTimeOffset ptFirstBoughtDate = DateTimeOffset.Parse(rdld.averageCalculator.firstBoughtDate.date.year.ToString() + "-" + rdld.averageCalculator.firstBoughtDate.date.month.ToString("00") + "-" + rdld.averageCalculator.firstBoughtDate.date.day.ToString("00") + "T" + rdld.averageCalculator.firstBoughtDate.time.hour.ToString("00") + ":" + rdld.averageCalculator.firstBoughtDate.time.minute.ToString("00") + ":" + rdld.averageCalculator.firstBoughtDate.time.second.ToString("00"), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
// Convert UTC bought time to local offset time
TimeSpan offsetTimeSpan = TimeSpan.Parse(systemConfiguration.GeneralSettings.Application.TimezoneOffset.Replace("+", ""));
ptFirstBoughtDate = ptFirstBoughtDate.ToOffset(offsetTimeSpan);
dcaLogData.FirstBoughtDate = ptFirstBoughtDate.DateTime;
2018-12-03 19:45:06 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
dcaLogData.FirstBoughtDate = Constants.confMinDate;
}
_dcaLog.Add(dcaLogData);
}
2018-12-03 19:45:06 +01:00
foreach (dcaLogData rpld in rawPairsLogData)
{
2018-05-22 10:11:50 +02:00
DCALogData dcaLogData = new DCALogData();
dcaLogData.Amount = rpld.averageCalculator.totalAmount;
dcaLogData.BoughtTimes = 0;
dcaLogData.Market = rpld.market;
dcaLogData.ProfitPercent = rpld.profit;
dcaLogData.AverageBuyPrice = rpld.averageCalculator.avgPrice;
dcaLogData.TotalCost = rpld.averageCalculator.totalCost;
dcaLogData.BuyTriggerPercent = rpld.buyProfit;
dcaLogData.CurrentPrice = rpld.currentPrice;
dcaLogData.SellTrigger = rpld.triggerValue;
dcaLogData.PercChange = rpld.percChange;
dcaLogData.BuyStrategy = rpld.buyStrategy;
if (dcaLogData.BuyStrategy == null) dcaLogData.BuyStrategy = "";
dcaLogData.SellStrategy = rpld.sellStrategy;
if (dcaLogData.SellStrategy == null) dcaLogData.SellStrategy = "";
dcaLogData.IsTrailing = false;
2018-12-03 19:45:06 +01:00
if (rpld.sellStrategies != null)
{
foreach (PTStrategy ss in rpld.sellStrategies)
{
2018-05-22 10:11:50 +02:00
Strategy sellStrategy = new Strategy();
sellStrategy.Type = ss.type;
sellStrategy.Name = ss.name;
sellStrategy.EntryValue = ss.entryValue;
sellStrategy.EntryValueLimit = ss.entryValueLimit;
sellStrategy.TriggerValue = ss.triggerValue;
sellStrategy.CurrentValue = ss.currentValue;
sellStrategy.CurrentValuePercentage = ss.currentValuePercentage;
sellStrategy.Decimals = ss.decimals;
sellStrategy.IsTrailing = ss.positive.IndexOf("trailing", StringComparison.InvariantCultureIgnoreCase) > -1;
sellStrategy.IsTrue = ss.positive.IndexOf("true", StringComparison.InvariantCultureIgnoreCase) > -1;
2018-05-22 10:11:50 +02:00
dcaLogData.SellStrategies.Add(sellStrategy);
}
}
// Profit Trailer bought times are saved in UTC
2018-12-03 19:45:06 +01:00
if (rpld.averageCalculator.firstBoughtDate != null)
{
2018-05-22 10:11:50 +02:00
DateTimeOffset ptFirstBoughtDate = DateTimeOffset.Parse(rpld.averageCalculator.firstBoughtDate.date.year.ToString() + "-" + rpld.averageCalculator.firstBoughtDate.date.month.ToString("00") + "-" + rpld.averageCalculator.firstBoughtDate.date.day.ToString("00") + "T" + rpld.averageCalculator.firstBoughtDate.time.hour.ToString("00") + ":" + rpld.averageCalculator.firstBoughtDate.time.minute.ToString("00") + ":" + rpld.averageCalculator.firstBoughtDate.time.second.ToString("00"), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
// Convert UTC bought time to local offset time
TimeSpan offsetTimeSpan = TimeSpan.Parse(systemConfiguration.GeneralSettings.Application.TimezoneOffset.Replace("+", ""));
ptFirstBoughtDate = ptFirstBoughtDate.ToOffset(offsetTimeSpan);
dcaLogData.FirstBoughtDate = ptFirstBoughtDate.DateTime;
2018-12-03 19:45:06 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
dcaLogData.FirstBoughtDate = Constants.confMinDate;
}
_dcaLog.Add(dcaLogData);
}
}
2018-12-03 19:45:06 +01:00
private void BuildBuyLogData(List<buyLogData> rawBuyLogData, PTMagicConfiguration systemConfiguration)
{
foreach (buyLogData rbld in rawBuyLogData)
{
2018-05-22 10:11:50 +02:00
BuyLogData buyLogData = new BuyLogData();
buyLogData.Market = rbld.market;
buyLogData.ProfitPercent = rbld.profit;
buyLogData.TriggerValue = rbld.triggerValue;
buyLogData.CurrentValue = rbld.currentValue;
buyLogData.CurrentPrice = rbld.currentPrice;
buyLogData.PercChange = rbld.percChange;
buyLogData.BuyStrategy = rbld.buyStrategy;
buyLogData.CurrentLowBBValue = rbld.BBLow;
buyLogData.CurrentHighBBValue = rbld.BBHigh;
buyLogData.BBTrigger = rbld.BBTrigger;
2018-12-03 19:45:06 +01:00
if (buyLogData.BuyStrategy == null) buyLogData.BuyStrategy = "";
2018-05-22 10:11:50 +02:00
2018-12-03 19:45:06 +01:00
if (rbld.positive != null)
{
buyLogData.IsTrailing = rbld.positive.IndexOf("trailing", StringComparison.InvariantCultureIgnoreCase) > -1;
buyLogData.IsTrue = rbld.positive.IndexOf("true", StringComparison.InvariantCultureIgnoreCase) > -1;
2018-12-03 19:45:06 +01:00
}
else
{
if (rbld.buyStrategies != null)
{
foreach (PTStrategy bs in rbld.buyStrategies)
{
2018-05-22 10:11:50 +02:00
Strategy buyStrategy = new Strategy();
buyStrategy.Type = bs.type;
buyStrategy.Name = bs.name;
buyStrategy.EntryValue = bs.entryValue;
buyStrategy.EntryValueLimit = bs.entryValueLimit;
buyStrategy.TriggerValue = bs.triggerValue;
buyStrategy.CurrentValue = bs.currentValue;
buyStrategy.CurrentValuePercentage = bs.currentValuePercentage;
buyStrategy.Decimals = bs.decimals;
buyStrategy.IsTrailing = bs.positive.IndexOf("trailing", StringComparison.InvariantCultureIgnoreCase) > -1;
buyStrategy.IsTrue = bs.positive.IndexOf("true", StringComparison.InvariantCultureIgnoreCase) > -1;
2018-05-22 10:11:50 +02:00
buyLogData.BuyStrategies.Add(buyStrategy);
}
}
}
_buyLog.Add(buyLogData);
}
}
}
}