Files
Avalonia/PortfolioManager/ViewModels/ScottPlotViewModel.cs
2025-06-20 20:13:27 -04:00

211 lines
8.2 KiB
C#

using System;
using Avalonia.Media;
using Eremex.AvaloniaUI.Charts;
using MarketData.DataAccess;
using MarketData.Generator;
using MarketData.MarketDataModel;
using PortfolioManager.DataSeriesViewModels;
using PortfolioManager.Models;
using ScottPlot;
using ScottPlot.Avalonia;
using ScottPlot.Plottables;
using ShimSkiaSharp;
using SkiaSharp;
namespace PortfolioManager.ViewModels
{
public class BollingerBandCompositeDataSourceGenerater
{
public BollingerBandCompositeDataSourceGenerater()
{
}
public void Render(AvaPlot plotter)
{
plotter.Plot.Axes.AutoScale();
plotter.Plot.HideLegend();
plotter.Refresh();
}
public void SetData(String selectedSymbol, int selectedDayCount, AvaPlot plotter)
{
Prices prices = PricingDA.GetPrices(selectedSymbol, selectedDayCount);
BollingerBands bollingerBands = BollingerBandGenerator.GenerateBollingerBands(prices);
// calculate the break even price on the open trades for this symbol
PortfolioTrades portfolioTrades = PortfolioDA.GetTradesSymbol(selectedSymbol);
PortfolioTrades openTrades = portfolioTrades.GetOpenTrades();
DateTime latestPricingDate = PricingDA.GetLatestDate(selectedSymbol);
Price latestPrice = PricingDA.GetPrice(selectedSymbol, latestPricingDate);
Price zeroPrice = ParityGenerator.GenerateGainLossValue(openTrades, latestPrice);
GenerateCompositeDataSources(plotter, bollingerBands);
GenerateZeroPoint(plotter, zeroPrice);
}
private void GenerateZeroPoint(AvaPlot plotter, Price zeroPrice)
{
ZeroPoint = GainLossModel.Price(zeroPrice);
(DateTime[] dates, double[] values) = ZeroPoint.ToXYData();
Scatter scatter = plotter.Plot.Add.Scatter(dates, values, ScottPlot.Color.FromSKColor(SKColors.Blue));
// Marker marker = plotter.Plot.Add.Marker(dates[0].ToOADate(), values[0] / 1.025);
// marker.MarkerFillColor = ScottPlot.Color.FromSKColor(SKColors.Blue);
// marker.MarkerStyle.Shape = MarkerShape.TriUp;
// marker.MarkerStyle.Size = 15;
Image image = TextMarkerImageGenerator.GenerateImage("Even $52.14 (+.25%)");
Coordinates coordinates = new Coordinates(dates[0].ToOADate(), values[0] / 1.0125);
ImageMarker marker = plotter.Plot.Add.ImageMarker(coordinates, image);
// Marker marker=plotter.Plot.Add.Marker(K.DataAdapter.ItemCount-1,values[0]);
// Marker marker=plotter.Plot.Add.Marker(0,values[0]);
// marker.MarkerFillColor = ScottPlot.Color.FromSKColor(SKColors.Blue);
// marker.MarkerStyle.Shape = MarkerShape.TriUp;
// marker.MarkerStyle.Size = 15;
}
private void GenerateCompositeDataSources(AvaPlot plotter, BollingerBands bollingerBands)
{
K = BollingerBandModel.K(bollingerBands);
KL1 = BollingerBandModel.KL1(bollingerBands);
L = BollingerBandModel.L(bollingerBands);
LP1 = BollingerBandModel.LP1(bollingerBands);
High = BollingerBandModel.High(bollingerBands);
Low = BollingerBandModel.Low(bollingerBands);
Close = BollingerBandModel.Close(bollingerBands);
SMAN = BollingerBandModel.SMAN(bollingerBands);
Volume = BollingerBandModel.Volume(bollingerBands);
LeastSquares = BollingerBandModel.LeastSquares(bollingerBands);
Scatter scatter = default;
{
(DateTime[] dates, double[] values) = K.ToXYData();
scatter = plotter.Plot.Add.ScatterLine(dates, values, ScottPlot.Color.FromSKColor(SKColors.Green));
scatter.LegendText = "K";
scatter.LineWidth = 3;
}
{
(DateTime[] dates, double[] values) = KL1.ToXYData();
scatter = plotter.Plot.Add.ScatterLine(dates, values, ScottPlot.Color.FromSKColor(SKColors.Green));
scatter.LegendText = "KL1";
scatter.LineWidth = 2;
}
{
(DateTime[] dates, double[] values) = L.ToXYData();
scatter = plotter.Plot.Add.ScatterLine(dates, values, ScottPlot.Color.FromSKColor(SKColors.Green));
scatter.LegendText = "L";
scatter.LineWidth = 3;
}
{
(DateTime[] dates, double[] values) = LP1.ToXYData();
scatter = plotter.Plot.Add.ScatterLine(dates, values, ScottPlot.Color.FromSKColor(SKColors.Green));
scatter.LegendText = "LP1";
scatter.LineWidth = 2;
}
{
(DateTime[] dates, double[] values) = High.ToXYData();
scatter = plotter.Plot.Add.ScatterLine(dates, values, ScottPlot.Color.FromSKColor(SKColors.Blue));
scatter.LegendText = "High";
scatter.LineWidth = 2;
}
{
(DateTime[] dates, double[] values) = Low.ToXYData();
scatter = plotter.Plot.Add.ScatterLine(dates, values, ScottPlot.Color.FromSKColor(SKColors.Red));
scatter.LegendText = "Low";
scatter.LineWidth = 2;
}
{
(DateTime[] dates, double[] values) = Close.ToXYData();
scatter = plotter.Plot.Add.ScatterLine(dates, values, ScottPlot.Color.FromSKColor(SKColors.Black));
scatter.LegendText = "Close";
scatter.LineWidth = 2;
}
{
(DateTime[] dates, double[] values) = LeastSquares.ToXYData();
scatter = plotter.Plot.Add.ScatterLine(dates, values, ScottPlot.Color.FromSKColor(SKColors.Orange));
scatter.LegendText = "LeastSquares";
scatter.LineWidth = 2;
}
{
(DateTime[] dates, double[] values) = SMAN.ToXYData();
scatter = plotter.Plot.Add.ScatterLine(dates, values, ScottPlot.Color.FromSKColor(SKColors.Purple));
scatter.LegendText = "SMAN";
scatter.LineWidth = 2;
}
}
public CompositeDataSource K { get; private set; } = Empty();
public CompositeDataSource KL1 { get; private set; } = Empty();
public CompositeDataSource L { get; private set; } = Empty();
public CompositeDataSource LP1 { get; private set; } = Empty();
public CompositeDataSource High { get; private set; } = Empty();
public CompositeDataSource Low { get; private set; } = Empty();
public CompositeDataSource Close { get; private set; } = Empty();
public CompositeDataSource SMAN { get; private set; } = Empty();
public CompositeDataSource Volume { get; private set; } = Empty();
public CompositeDataSource LeastSquares { get; private set; } = Empty();
public CompositeDataSource ZeroPoint { get; private set; } = Empty();
private static CompositeDataSource Empty()
{
return new CompositeDataSource()
{
DataAdapter = new SortedDateTimeDataAdapter()
};
}
}
public partial class ScottPlotViewModel : PlotterWorkspaceViewModel
{
private AvaPlot plotter = default;
public ScottPlotViewModel()
{
OnPlotterLoadedEventHandler += PlotterLoadedEvent;
}
public void PlotterLoadedEvent(object sender, PlotterLoadedEventArgs e)
{
String selectedSymbol = "CRS";
int selectedDayCount = 180;
plotter = e.AvaPlot; // we should store this somewhere
// Prices prices = PricingDA.GetPrices("SPY", 180);
// BollingerBands bollingerBands = BollingerBandGenerator.GenerateBollingerBands(prices);
BollingerBandCompositeDataSourceGenerater bollingerBandCompositeDataSourceGenerater = new BollingerBandCompositeDataSourceGenerater();
bollingerBandCompositeDataSourceGenerater.SetData(selectedSymbol, selectedDayCount, plotter);
//public void SetData(String selectedSymbol, int selectedDayCount, AvaPlot plotter)
// bollingerBandCompositeDataSourceGenerater.GenerateCompositeDataSources(plotter, bollingerBands);
bollingerBandCompositeDataSourceGenerater.Render(plotter);
// plotter.Plot.Axes.AutoScale();
// plotter.Refresh();
}
// ********************************************** P E R S I S T E N C E *************************
public override bool CanPersist()
{
return false;
}
public override SaveParameters GetSaveParameters()
{
return null;
}
public override void SetSaveParameters(SaveParameters saveParameters)
{
}
}
}