Commit Latest

This commit is contained in:
2025-06-13 21:13:11 -04:00
parent 12b9c3cd72
commit 9bbfc9831f
7 changed files with 534 additions and 84 deletions

View File

@@ -0,0 +1,189 @@
using System.Collections.Generic;
using System.Linq;
using Eremex.AvaloniaUI.Charts;
using MarketData.MarketDataModel;
using MarketData.Numerical;
using PortfolioManager.DataSeriesViewModels;
namespace PortfolioManager.Models
{
public class BollingerBandModel
{
private BollingerBandModel()
{
}
public static CompositeDataSource Empty()
{
CompositeDataSource compositeDataSource = new CompositeDataSource()
{
DataAdapter = new SortedDateTimeDataAdapter()
};
return compositeDataSource;
}
public static CompositeDataSource SMAN(BollingerBands bollingerBands)
{
if (null == bollingerBands || 0 == bollingerBands.Count) return Empty();
SortedDateTimeDataAdapter sortedDateTimeDataAdapter = new SortedDateTimeDataAdapter();
List<BollingerBandElement> sortedBollingerBands = bollingerBands.OrderBy(x => x.Date).ToList();
foreach (BollingerBandElement bollingerBandElement in sortedBollingerBands)
{
sortedDateTimeDataAdapter.Add(bollingerBandElement.Date, bollingerBandElement.SMAN);
}
CompositeDataSource compositeDataSource = new CompositeDataSource()
{
DataAdapter = sortedDateTimeDataAdapter
};
return compositeDataSource;
}
public static CompositeDataSource K(BollingerBands bollingerBands)
{
if (null == bollingerBands || 0 == bollingerBands.Count) return Empty();
SortedDateTimeDataAdapter sortedDateTimeDataAdapter = new SortedDateTimeDataAdapter();
List<BollingerBandElement> sortedBollingerBands = bollingerBands.OrderBy(x => x.Date).ToList();
foreach (BollingerBandElement bollingerBandElement in sortedBollingerBands)
{
sortedDateTimeDataAdapter.Add(bollingerBandElement.Date, bollingerBandElement.K);
}
CompositeDataSource compositeDataSource = new CompositeDataSource()
{
DataAdapter = sortedDateTimeDataAdapter
};
return compositeDataSource;
}
public static CompositeDataSource KL1(BollingerBands bollingerBands)
{
if (null == bollingerBands || 0 == bollingerBands.Count) return Empty();
SortedDateTimeDataAdapter sortedDateTimeDataAdapter = new SortedDateTimeDataAdapter();
List<BollingerBandElement> sortedBollingerBands = bollingerBands.OrderBy(x => x.Date).ToList();
foreach (BollingerBandElement bollingerBandElement in sortedBollingerBands)
{
sortedDateTimeDataAdapter.Add(bollingerBandElement.Date, bollingerBandElement.KL1);
}
CompositeDataSource compositeDataSource = new CompositeDataSource()
{
DataAdapter = sortedDateTimeDataAdapter
};
return compositeDataSource;
}
public static CompositeDataSource L(BollingerBands bollingerBands)
{
if (null == bollingerBands || 0 == bollingerBands.Count) return Empty();
SortedDateTimeDataAdapter sortedDateTimeDataAdapter = new SortedDateTimeDataAdapter();
List<BollingerBandElement> sortedBollingerBands = bollingerBands.OrderBy(x => x.Date).ToList();
foreach (BollingerBandElement bollingerBandElement in sortedBollingerBands)
{
sortedDateTimeDataAdapter.Add(bollingerBandElement.Date, bollingerBandElement.L);
}
CompositeDataSource compositeDataSource = new CompositeDataSource()
{
DataAdapter = sortedDateTimeDataAdapter
};
return compositeDataSource;
}
public static CompositeDataSource LP1(BollingerBands bollingerBands)
{
if (null == bollingerBands || 0 == bollingerBands.Count) return Empty();
SortedDateTimeDataAdapter sortedDateTimeDataAdapter = new SortedDateTimeDataAdapter();
List<BollingerBandElement> sortedBollingerBands = bollingerBands.OrderBy(x => x.Date).ToList();
foreach (BollingerBandElement bollingerBandElement in sortedBollingerBands)
{
sortedDateTimeDataAdapter.Add(bollingerBandElement.Date, bollingerBandElement.LP1);
}
CompositeDataSource compositeDataSource = new CompositeDataSource()
{
DataAdapter = sortedDateTimeDataAdapter
};
return compositeDataSource;
}
public static CompositeDataSource High(BollingerBands bollingerBands)
{
if (null == bollingerBands || 0 == bollingerBands.Count) return Empty();
SortedDateTimeDataAdapter sortedDateTimeDataAdapter = new SortedDateTimeDataAdapter();
List<BollingerBandElement> sortedBollingerBands = bollingerBands.OrderBy(x => x.Date).ToList();
foreach (BollingerBandElement bollingerBandElement in sortedBollingerBands)
{
sortedDateTimeDataAdapter.Add(bollingerBandElement.Date, bollingerBandElement.High);
}
CompositeDataSource compositeDataSource = new CompositeDataSource()
{
DataAdapter = sortedDateTimeDataAdapter
};
return compositeDataSource;
}
public static CompositeDataSource Low(BollingerBands bollingerBands)
{
if (null == bollingerBands || 0 == bollingerBands.Count) return Empty();
SortedDateTimeDataAdapter sortedDateTimeDataAdapter = new SortedDateTimeDataAdapter();
List<BollingerBandElement> sortedBollingerBands = bollingerBands.OrderBy(x => x.Date).ToList();
foreach (BollingerBandElement bollingerBandElement in sortedBollingerBands)
{
sortedDateTimeDataAdapter.Add(bollingerBandElement.Date, bollingerBandElement.Low);
}
CompositeDataSource compositeDataSource = new CompositeDataSource()
{
DataAdapter = sortedDateTimeDataAdapter
};
return compositeDataSource;
}
public static CompositeDataSource Close(BollingerBands bollingerBands)
{
if (null == bollingerBands || 0 == bollingerBands.Count) return Empty();
SortedDateTimeDataAdapter sortedDateTimeDataAdapter = new SortedDateTimeDataAdapter();
List<BollingerBandElement> sortedBollingerBands = bollingerBands.OrderBy(x => x.Date).ToList();
foreach (BollingerBandElement bollingerBandElement in sortedBollingerBands)
{
sortedDateTimeDataAdapter.Add(bollingerBandElement.Date, bollingerBandElement.Close);
}
CompositeDataSource compositeDataSource = new CompositeDataSource()
{
DataAdapter = sortedDateTimeDataAdapter
};
return compositeDataSource;
}
public static CompositeDataSource Volume(BollingerBands bollingerBands)
{
if (null == bollingerBands || 0 == bollingerBands.Count) return Empty();
SortedDateTimeDataAdapter sortedDateTimeDataAdapter = new SortedDateTimeDataAdapter();
List<BollingerBandElement> sortedBollingerBands = bollingerBands.OrderBy(x => x.Date).ToList();
foreach (BollingerBandElement bollingerBandElement in sortedBollingerBands)
{
sortedDateTimeDataAdapter.Add(bollingerBandElement.Date, bollingerBandElement.Volume);
}
CompositeDataSource compositeDataSource = new CompositeDataSource()
{
DataAdapter = sortedDateTimeDataAdapter
};
return compositeDataSource;
}
// The least squares might be in the wrong order if the Bollinger band was already sorted by date
public static CompositeDataSource LeastSquares(BollingerBands bollingerBands)
{
if (null == bollingerBands || 0 == bollingerBands.Count) return null;
LeastSquaresResult leastSquaresResult = bollingerBands.LeastSquaresFitClose();
SortedDateTimeDataAdapter sortedDateTimeDataAdapter = new SortedDateTimeDataAdapter();
List<BollingerBandElement> sortedBollingerBands = bollingerBands.OrderBy(x => x.Date).ToList();
for (int index = 0; index < bollingerBands.Count; index++)
{
BollingerBandElement element = bollingerBands[index];
int leastSquaresIndex = (leastSquaresResult.LeastSquares.Length - 1) - index;
sortedDateTimeDataAdapter.Add(element.Date, leastSquaresResult.LeastSquares[leastSquaresIndex]);
}
CompositeDataSource compositeDataSource = new CompositeDataSource()
{
DataAdapter = sortedDateTimeDataAdapter
};
return compositeDataSource;
}
}
}

View File

@@ -4,6 +4,7 @@ using Eremex.AvaloniaUI.Charts;
using MarketData.MarketDataModel;
using MarketData.MarketDataModel.GainLoss;
using MarketData.Numerical;
using MarketData.Utils;
using PortfolioManager.DataSeriesViewModels;
namespace PortfolioManager.Models
@@ -27,16 +28,24 @@ namespace PortfolioManager.Models
{
if (null == price) return Empty();
SortedDateTimeDataAdapter sortedDateTimeDataAdapter = new SortedDateTimeDataAdapter();
sortedDateTimeDataAdapter.Add(price.Date, price.Close);
CompositeDataSource compositeDataSource = new CompositeDataSource()
{
DataAdapter = sortedDateTimeDataAdapter
};
return compositeDataSource;
}
// CompositeDataSource compositeDataSource;
// var xData = new EnumerableDataSource<DateTime>(new DateTime[]{price.Date});
// xData.SetXMapping(x => (x.Ticks / 10000000000.0));
// var yData = new EnumerableDataSource<double>(new double[]{price.Close});
// yData.SetYMapping(y => y);
// compositeDataSource = xData.Join(yData);
// return compositeDataSource;
return Empty();
public static CompositeDataSource CreateCompositeDataSource(DateTime xSource, double ySource)
{
if (Utility.IsEpoch(xSource)) return Empty();
SortedDateTimeDataAdapter sortedDateTimeDataAdapter = new SortedDateTimeDataAdapter();
sortedDateTimeDataAdapter.Add(xSource, ySource);
CompositeDataSource compositeDataSource = new CompositeDataSource()
{
DataAdapter = sortedDateTimeDataAdapter
};
return compositeDataSource;
}
// This is the active gain/loss as number or percent.

View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Eremex.AvaloniaUI.Charts;
using MarketData.MarketDataModel;
using PortfolioManager.DataSeriesViewModels;
namespace PortfolioManager.Models
{
public class PortfolioTradeModel
{
private PortfolioTradeModel()
{
}
public static CompositeDataSource PortfolioTrades(PortfolioTrades portfolioTrades)
{
if (null == portfolioTrades || 0 == portfolioTrades.Count) return null;
List<PortfolioTrade> sortedPortfolioTrades = portfolioTrades.OrderBy(x => x.TradeDate).ToList();
SortedDateTimeDataAdapter sortedDateTimeDataAdapter = new SortedDateTimeDataAdapter();
foreach (PortfolioTrade portfolioTrade in sortedPortfolioTrades)
{
sortedDateTimeDataAdapter.Add(portfolioTrade.TradeDate, portfolioTrade.Price);
}
CompositeDataSource compositeDataSource = new CompositeDataSource()
{
DataAdapter = sortedDateTimeDataAdapter
};
return compositeDataSource;
}
}
}

View File

@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Eremex.AvaloniaUI.Charts;
using MarketData.MarketDataModel;
using PortfolioManager.DataSeriesViewModels;
namespace PortfolioManager.Models
{
public class StopLimitCompositeModel
{
private StopLimitCompositeModel()
{
}
public static CompositeDataSource Empty()
{
CompositeDataSource compositeDataSource = new CompositeDataSource()
{
DataAdapter = new SortedDateTimeDataAdapter()
};
return compositeDataSource;
}
public static CompositeDataSource CreateCompositeDataSource(StopLimits stopLimits)
{
if (null == stopLimits || 0 == stopLimits.Count) return Empty();
SortedDateTimeDataAdapter sortedDateTimeDataAdapter = new SortedDateTimeDataAdapter();
List<StopLimit> sortedStopLimits = stopLimits.OrderBy(x => x.EffectiveDate).ToList();
foreach (StopLimit stopLimit in sortedStopLimits)
{
sortedDateTimeDataAdapter.Add(stopLimit.EffectiveDate, stopLimit.StopPrice);
}
CompositeDataSource compositeDataSource = new CompositeDataSource()
{
DataAdapter = sortedDateTimeDataAdapter
};
return compositeDataSource;
}
}
}