Files
TradeBlotter/Model/BollingerBandModel.cs
2024-02-23 06:58:53 -05:00

157 lines
7.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using MarketData.MarketDataModel;
using MarketData.Numerical;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using MarketData.Utils;
namespace TradeBlotter.Model
{
public class BollingerBandModel
{
private BollingerBandModel()
{
}
public static CompositeDataSource SMAN(BollingerBands bollingerBands)
{
if (null == bollingerBands) return null;
CompositeDataSource compositeDataSource;
var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
xData.SetXMapping(x => (x.Ticks / 10000000000.0));
var yData = new EnumerableDataSource<double>(bollingerBands.Select(y => y.SMAN));
yData.SetYMapping(y => y);
compositeDataSource = xData.Join(yData);
return compositeDataSource;
}
public static CompositeDataSource K(BollingerBands bollingerBands)
{
if (null == bollingerBands) return null;
CompositeDataSource compositeDataSource;
var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
xData.SetXMapping(x => (x.Ticks/ 10000000000.0));
var yData = new EnumerableDataSource<double>(bollingerBands.Select(y => y.K));
yData.SetYMapping(y => y);
compositeDataSource = xData.Join(yData);
return compositeDataSource;
}
public static CompositeDataSource KL1(BollingerBands bollingerBands)
{
if (null == bollingerBands) return null;
CompositeDataSource compositeDataSource;
var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
xData.SetXMapping(x => (x.Ticks / 10000000000.0));
var yData = new EnumerableDataSource<double>(bollingerBands.Select(y => y.KL1));
yData.SetYMapping(y => y);
compositeDataSource = xData.Join(yData);
return compositeDataSource;
}
public static CompositeDataSource L(BollingerBands bollingerBands)
{
if (null == bollingerBands) return null;
CompositeDataSource compositeDataSource;
var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
xData.SetXMapping(x => (x.Ticks / 10000000000.0));
var yData = new EnumerableDataSource<double>(bollingerBands.Select(y => y.L));
yData.SetYMapping(y => y);
compositeDataSource = xData.Join(yData);
return compositeDataSource;
}
public static CompositeDataSource LP1(BollingerBands bollingerBands)
{
if (null == bollingerBands) return null;
CompositeDataSource compositeDataSource;
var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
xData.SetXMapping(x => (x.Ticks / 10000000000.0));
var yData = new EnumerableDataSource<double>(bollingerBands.Select(y => y.LP1));
yData.SetYMapping(y => y);
compositeDataSource = xData.Join(yData);
return compositeDataSource;
}
public static CompositeDataSource High(BollingerBands bollingerBands)
{
if (null == bollingerBands) return null;
CompositeDataSource compositeDataSource;
var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
xData.SetXMapping(x => (x.Ticks / 10000000000.0));
var yData = new EnumerableDataSource<double>(bollingerBands.Select(y => y.High));
yData.SetYMapping(y => y);
compositeDataSource = xData.Join(yData);
return compositeDataSource;
}
public static CompositeDataSource Low(BollingerBands bollingerBands)
{
if (null == bollingerBands) return null;
CompositeDataSource compositeDataSource;
var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
xData.SetXMapping(x => (x.Ticks / 10000000000.0));
var yData = new EnumerableDataSource<double>(bollingerBands.Select(y => y.Low));
yData.SetYMapping(y => y);
compositeDataSource = xData.Join(yData);
return compositeDataSource;
}
public static CompositeDataSource Close(BollingerBands bollingerBands)
{
if (null == bollingerBands) return null;
CompositeDataSource compositeDataSource;
var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
xData.SetXMapping(x => (x.Ticks / 10000000000.0));
var yData = new EnumerableDataSource<double>(bollingerBands.Select(y => y.Close));
yData.SetYMapping(y => y);
compositeDataSource = xData.Join(yData);
return compositeDataSource;
}
public static CompositeDataSource LeastSquares(BollingerBands bollingerBands)
{
if (null == bollingerBands) return null;
CompositeDataSource compositeDataSource;
var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
xData.SetXMapping(x => (x.Ticks / 10000000000.0));
LeastSquaresResult leastSquaresResult=bollingerBands.LeastSquaresFitClose();
var yData = new EnumerableDataSource<double>(leastSquaresResult.LeastSquares);
yData.SetYMapping(y => y);
compositeDataSource = xData.Join(yData);
return compositeDataSource;
}
public static CompositeDataSource LeastSquaresExtend(BollingerBands bollingerBands,DateTime extendToDate)
{
if (null == bollingerBands) return null;
CompositeDataSource compositeDataSource;
LeastSquaresResult leastSquaresResult=bollingerBands.LeastSquaresFitClose();
DateTime maxDateInBand=bollingerBands.Select(x=>x.Date).Max();
if(maxDateInBand<extendToDate)
{
DateGenerator dateGenerator=new DateGenerator();
List<DateTime> extendedDates=dateGenerator.GenerateHistoricalDates(dateGenerator.FindNextBusinessDay(maxDateInBand),extendToDate);
var xData=new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date).Union(extendedDates.Select(x=>x)).OrderByDescending(x=>x.Date));
leastSquaresResult.Extend(extendedDates.Count,leastSquaresResult.Slope);
xData.SetXMapping(x => (x.Ticks / 10000000000.0));
var yData = new EnumerableDataSource<double>(leastSquaresResult.LeastSquares);
yData.SetYMapping(y => y);
compositeDataSource = xData.Join(yData);
}
else
{
var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
xData.SetXMapping(x => (x.Ticks / 10000000000.0));
var yData = new EnumerableDataSource<double>(leastSquaresResult.LeastSquares);
yData.SetYMapping(y => y);
compositeDataSource = xData.Join(yData);
}
return compositeDataSource;
}
public static CompositeDataSource Volume(BollingerBands bollingerBands)
{
if (null == bollingerBands) return null;
CompositeDataSource compositeDataSource;
var xData = new EnumerableDataSource<DateTime>(bollingerBands.Select(x => x.Date.Date));
xData.SetXMapping(x => (x.Ticks / 10000000000.0));
var yData = new EnumerableDataSource<long>(bollingerBands.Select(y => y.Volume));
yData.SetYMapping(y => y);
compositeDataSource = xData.Join(yData);
return compositeDataSource;
}
}
}