python wrapper

This commit is contained in:
Miha Kralj
2026-02-28 14:14:35 -08:00
parent 82e0248eb0
commit 83e9511261
521 changed files with 62395 additions and 15669 deletions
@@ -1,257 +0,0 @@
using TradingPlatform.BusinessLayer;
using QuanTAlib;
namespace QuanTAlib.Tests;
public class LinRegIndicatorTests
{
[Fact]
public void LinRegIndicator_Constructor_SetsDefaults()
{
var indicator = new LinRegIndicator();
Assert.Equal(14, indicator.Period);
Assert.Equal(0, indicator.Offset);
Assert.True(indicator.ShowColdValues);
Assert.Equal("LinReg - Linear Regression Curve", indicator.Name);
Assert.False(indicator.SeparateWindow);
Assert.True(indicator.OnBackGround);
Assert.Equal(SourceType.Close, indicator.Source);
}
[Fact]
public void LinRegIndicator_MinHistoryDepths_EqualsZero()
{
var indicator = new LinRegIndicator { Period = 20 };
Assert.Equal(0, LinRegIndicator.MinHistoryDepths);
IWatchlistIndicator watchlistIndicator = indicator;
Assert.Equal(0, watchlistIndicator.MinHistoryDepths);
}
[Fact]
public void LinRegIndicator_Initialize_CreatesInternalLinReg()
{
var indicator = new LinRegIndicator { Period = 10 };
// Initialize should not throw
indicator.Initialize();
// After init, line series should exist
Assert.Single(indicator.LinesSeries);
Assert.Equal("LinReg", indicator.LinesSeries[0].Name);
}
[Fact]
public void LinRegIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
{
var indicator = new LinRegIndicator { Period = 5 };
indicator.Initialize();
// Add historical data
var now = DateTime.UtcNow;
// Need enough bars for Period
for (int i = 0; i < 20; i++)
{
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
// Process update for each bar to simulate history loading
var args = new UpdateArgs(UpdateReason.HistoricalBar);
indicator.ProcessUpdate(args);
}
// Line series should have a value
double linreg = indicator.LinesSeries[0].GetValue(0);
Assert.True(double.IsFinite(linreg));
}
}
public class LinRegSlopeIndicatorTests
{
[Fact]
public void LinRegSlopeIndicator_Constructor_SetsDefaults()
{
var indicator = new LinRegSlopeIndicator();
Assert.Equal(14, indicator.Period);
Assert.True(indicator.ShowColdValues);
Assert.Equal("LinReg Slope", indicator.Name);
Assert.True(indicator.SeparateWindow);
Assert.True(indicator.OnBackGround);
Assert.Equal(SourceType.Close, indicator.Source);
}
[Fact]
public void LinRegSlopeIndicator_MinHistoryDepths_EqualsZero()
{
var indicator = new LinRegSlopeIndicator { Period = 20 };
Assert.Equal(0, LinRegSlopeIndicator.MinHistoryDepths);
IWatchlistIndicator watchlistIndicator = indicator;
Assert.Equal(0, watchlistIndicator.MinHistoryDepths);
}
[Fact]
public void LinRegSlopeIndicator_Initialize_CreatesInternalLinReg()
{
var indicator = new LinRegSlopeIndicator { Period = 10 };
// Initialize should not throw
indicator.Initialize();
// After init, line series should exist
Assert.Single(indicator.LinesSeries);
Assert.Equal("Slope", indicator.LinesSeries[0].Name);
}
[Fact]
public void LinRegSlopeIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
{
var indicator = new LinRegSlopeIndicator { Period = 5 };
indicator.Initialize();
// Add historical data
var now = DateTime.UtcNow;
// Need enough bars for Period
for (int i = 0; i < 20; i++)
{
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
// Process update for each bar to simulate history loading
var args = new UpdateArgs(UpdateReason.HistoricalBar);
indicator.ProcessUpdate(args);
}
// Line series should have a value
double slope = indicator.LinesSeries[0].GetValue(0);
Assert.True(double.IsFinite(slope));
}
}
public class LinRegInterceptIndicatorTests
{
[Fact]
public void LinRegInterceptIndicator_Constructor_SetsDefaults()
{
var indicator = new LinRegInterceptIndicator();
Assert.Equal(14, indicator.Period);
Assert.True(indicator.ShowColdValues);
Assert.Equal("LinReg Intercept", indicator.Name);
Assert.True(indicator.SeparateWindow);
Assert.True(indicator.OnBackGround);
Assert.Equal(SourceType.Close, indicator.Source);
}
[Fact]
public void LinRegInterceptIndicator_MinHistoryDepths_EqualsZero()
{
var indicator = new LinRegInterceptIndicator { Period = 20 };
Assert.Equal(0, LinRegInterceptIndicator.MinHistoryDepths);
IWatchlistIndicator watchlistIndicator = indicator;
Assert.Equal(0, watchlistIndicator.MinHistoryDepths);
}
[Fact]
public void LinRegInterceptIndicator_Initialize_CreatesInternalLinReg()
{
var indicator = new LinRegInterceptIndicator { Period = 10 };
// Initialize should not throw
indicator.Initialize();
// After init, line series should exist
Assert.Single(indicator.LinesSeries);
Assert.Equal("Intercept", indicator.LinesSeries[0].Name);
}
[Fact]
public void LinRegInterceptIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
{
var indicator = new LinRegInterceptIndicator { Period = 5 };
indicator.Initialize();
// Add historical data
var now = DateTime.UtcNow;
// Need enough bars for Period
for (int i = 0; i < 20; i++)
{
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
// Process update for each bar to simulate history loading
var args = new UpdateArgs(UpdateReason.HistoricalBar);
indicator.ProcessUpdate(args);
}
// Line series should have a value
double intercept = indicator.LinesSeries[0].GetValue(0);
Assert.True(double.IsFinite(intercept));
}
}
public class LinRegRSquaredIndicatorTests
{
[Fact]
public void LinRegRSquaredIndicator_Constructor_SetsDefaults()
{
var indicator = new LinRegRSquaredIndicator();
Assert.Equal(14, indicator.Period);
Assert.True(indicator.ShowColdValues);
Assert.Equal("LinReg R-Squared", indicator.Name);
Assert.True(indicator.SeparateWindow);
Assert.True(indicator.OnBackGround);
Assert.Equal(SourceType.Close, indicator.Source);
}
[Fact]
public void LinRegRSquaredIndicator_MinHistoryDepths_EqualsZero()
{
var indicator = new LinRegRSquaredIndicator { Period = 20 };
Assert.Equal(0, LinRegRSquaredIndicator.MinHistoryDepths);
IWatchlistIndicator watchlistIndicator = indicator;
Assert.Equal(0, watchlistIndicator.MinHistoryDepths);
}
[Fact]
public void LinRegRSquaredIndicator_Initialize_CreatesInternalLinReg()
{
var indicator = new LinRegRSquaredIndicator { Period = 10 };
// Initialize should not throw
indicator.Initialize();
// After init, line series should exist
Assert.Single(indicator.LinesSeries);
Assert.Equal("RSquared", indicator.LinesSeries[0].Name);
}
[Fact]
public void LinRegRSquaredIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
{
var indicator = new LinRegRSquaredIndicator { Period = 5 };
indicator.Initialize();
// Add historical data
var now = DateTime.UtcNow;
// Need enough bars for Period
for (int i = 0; i < 20; i++)
{
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
// Process update for each bar to simulate history loading
var args = new UpdateArgs(UpdateReason.HistoricalBar);
indicator.ProcessUpdate(args);
}
// Line series should have a value
double r2 = indicator.LinesSeries[0].GetValue(0);
Assert.True(double.IsFinite(r2));
}
}
-228
View File
@@ -1,228 +0,0 @@
using System.Drawing;
using System.Runtime.CompilerServices;
using TradingPlatform.BusinessLayer;
namespace QuanTAlib;
[SkipLocalsInit]
public sealed class LinRegIndicator : Indicator, IWatchlistIndicator
{
[InputParameter("Period", sortIndex: 1, 1, 2000, 1, 0)]
public int Period { get; set; } = 14;
[InputParameter("Offset", sortIndex: 2, -2000, 2000, 1, 0)]
public int Offset { get; set; } = 0;
[IndicatorExtensions.DataSourceInput]
public SourceType Source { get; set; } = SourceType.Close;
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private LinReg _linreg = null!;
private readonly LineSeries _series;
private Func<IHistoryItem, double> _priceSelector = null!;
public static int MinHistoryDepths => 0;
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public override string ShortName => $"LinReg({Period})";
public override string SourceCodeLink => "https://github.com/mihakralj/QuanTAlib/blob/main/lib/statistics/linreg/LinReg.Quantower.cs";
public LinRegIndicator()
{
OnBackGround = true;
SeparateWindow = false;
Name = "LinReg - Linear Regression Curve";
Description = "Plots the end point of the linear regression line for each bar.";
_series = new LineSeries(name: "LinReg", color: IndicatorExtensions.Statistics, width: 2, style: LineStyle.Solid);
AddLineSeries(_series);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void OnInit()
{
_linreg = new LinReg(Period, Offset);
_priceSelector = Source.GetPriceSelector();
base.OnInit();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void OnUpdate(UpdateArgs args)
{
var item = this.HistoricalData[this.Count - 1, SeekOriginHistory.Begin];
double value = _priceSelector(item);
var time = this.HistoricalData.Time();
var input = new TValue(time, value);
TValue result = _linreg.Update(input, args.IsNewBar());
_series.SetValue(result.Value, _linreg.IsHot, ShowColdValues);
}
}
[SkipLocalsInit]
public sealed class LinRegSlopeIndicator : Indicator, IWatchlistIndicator
{
[InputParameter("Period", sortIndex: 1, 1, 2000, 1, 0)]
public int Period { get; set; } = 14;
[IndicatorExtensions.DataSourceInput]
public SourceType Source { get; set; } = SourceType.Close;
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private LinReg _linreg = null!;
private readonly LineSeries _series;
private Func<IHistoryItem, double> _priceSelector = null!;
public static int MinHistoryDepths => 0;
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public override string ShortName => $"LinRegSlope({Period})";
public override string SourceCodeLink => "https://github.com/mihakralj/QuanTAlib/blob/main/lib/statistics/linreg/LinReg.Quantower.cs";
public LinRegSlopeIndicator()
{
OnBackGround = true;
SeparateWindow = true;
Name = "LinReg Slope";
Description = "Plots the slope of the linear regression line.";
_series = new LineSeries(name: "Slope", color: IndicatorExtensions.Momentum, width: 2, style: LineStyle.Solid);
AddLineSeries(_series);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void OnInit()
{
_linreg = new LinReg(Period);
_priceSelector = Source.GetPriceSelector();
base.OnInit();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void OnUpdate(UpdateArgs args)
{
var item = this.HistoricalData[this.Count - 1, SeekOriginHistory.Begin];
double value = _priceSelector(item);
var time = this.HistoricalData.Time();
var input = new TValue(time, value);
_linreg.Update(input, args.IsNewBar());
_series.SetValue(_linreg.Slope, _linreg.IsHot, ShowColdValues);
}
}
[SkipLocalsInit]
public sealed class LinRegInterceptIndicator : Indicator, IWatchlistIndicator
{
[InputParameter("Period", sortIndex: 1, 1, 2000, 1, 0)]
public int Period { get; set; } = 14;
[IndicatorExtensions.DataSourceInput]
public SourceType Source { get; set; } = SourceType.Close;
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private LinReg _linreg = null!;
private readonly LineSeries _series;
private Func<IHistoryItem, double> _priceSelector = null!;
public static int MinHistoryDepths => 0;
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public override string ShortName => $"LinRegIntercept({Period})";
public override string SourceCodeLink => "https://github.com/mihakralj/QuanTAlib/blob/main/lib/statistics/linreg/LinReg.Quantower.cs";
public LinRegInterceptIndicator()
{
OnBackGround = true;
SeparateWindow = true;
Name = "LinReg Intercept";
Description = "Plots the intercept of the linear regression line.";
_series = new LineSeries(name: "Intercept", color: IndicatorExtensions.Experiments, width: 2, style: LineStyle.Solid);
AddLineSeries(_series);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void OnInit()
{
_linreg = new LinReg(Period);
_priceSelector = Source.GetPriceSelector();
base.OnInit();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void OnUpdate(UpdateArgs args)
{
var item = this.HistoricalData[this.Count - 1, SeekOriginHistory.Begin];
double value = _priceSelector(item);
var time = this.HistoricalData.Time();
var input = new TValue(time, value);
_linreg.Update(input, args.IsNewBar());
_series.SetValue(_linreg.Intercept, _linreg.IsHot, ShowColdValues);
}
}
[SkipLocalsInit]
public sealed class LinRegRSquaredIndicator : Indicator, IWatchlistIndicator
{
[InputParameter("Period", sortIndex: 1, 1, 2000, 1, 0)]
public int Period { get; set; } = 14;
[IndicatorExtensions.DataSourceInput]
public SourceType Source { get; set; } = SourceType.Close;
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private LinReg _linreg = null!;
private readonly LineSeries _series;
private Func<IHistoryItem, double> _priceSelector = null!;
public static int MinHistoryDepths => 0;
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public override string ShortName => $"LinRegR2({Period})";
public override string SourceCodeLink => "https://github.com/mihakralj/QuanTAlib/blob/main/lib/statistics/linreg/LinReg.Quantower.cs";
public LinRegRSquaredIndicator()
{
OnBackGround = true;
SeparateWindow = true;
Name = "LinReg R-Squared";
Description = "Plots the R-Squared (coefficient of determination) of the linear regression line.";
_series = new LineSeries(name: "RSquared", color: IndicatorExtensions.Oscillators, width: 2, style: LineStyle.Solid);
AddLineSeries(_series);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void OnInit()
{
_linreg = new LinReg(Period);
_priceSelector = Source.GetPriceSelector();
base.OnInit();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void OnUpdate(UpdateArgs args)
{
var item = this.HistoricalData[this.Count - 1, SeekOriginHistory.Begin];
double value = _priceSelector(item);
var time = this.HistoricalData.Time();
var input = new TValue(time, value);
_linreg.Update(input, args.IsNewBar());
_series.SetValue(_linreg.RSquared, _linreg.IsHot, ShowColdValues);
}
}