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
-214
View File
@@ -1,214 +0,0 @@
using Xunit;
using TradingPlatform.BusinessLayer;
namespace QuanTAlib.Tests;
public class SlopeIndicatorTests
{
[Fact]
public void SlopeIndicator_Constructor_SetsDefaults()
{
var indicator = new SlopeIndicator();
Assert.Equal(SourceType.Close, indicator.Source);
Assert.True(indicator.ShowColdValues);
Assert.Equal("SLOPE - First Derivative (Velocity)", indicator.Name);
Assert.True(indicator.SeparateWindow);
Assert.False(indicator.OnBackGround);
}
[Fact]
public void SlopeIndicator_MinHistoryDepths_IsTwo()
{
var indicator = new SlopeIndicator();
Assert.Equal(2, indicator.MinHistoryDepths);
}
[Fact]
public void SlopeIndicator_ShortName_IsSlope()
{
var indicator = new SlopeIndicator();
Assert.Equal("SLOPE", indicator.ShortName);
}
[Fact]
public void SlopeIndicator_Initialize_CreatesLineSeries()
{
var indicator = new SlopeIndicator();
indicator.Initialize();
Assert.Equal(2, indicator.LinesSeries.Count);
Assert.Equal("Slope", indicator.LinesSeries[0].Name);
Assert.Equal("Zero", indicator.LinesSeries[1].Name);
}
[Fact]
public void SlopeIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
{
var indicator = new SlopeIndicator();
indicator.Initialize();
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
var args = new UpdateArgs(UpdateReason.HistoricalBar);
indicator.ProcessUpdate(args);
Assert.Equal(1, indicator.LinesSeries[0].Count);
Assert.Equal(1, indicator.LinesSeries[1].Count);
}
[Fact]
public void SlopeIndicator_ProcessUpdate_NewBar_ComputesValue()
{
var indicator = new SlopeIndicator();
indicator.Initialize();
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
indicator.HistoricalData.AddBar(now.AddMinutes(1), 102, 108, 100, 106);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
Assert.Equal(2, indicator.LinesSeries[0].Count);
}
[Fact]
public void SlopeIndicator_ProcessUpdate_NewTick_ProcessesWithoutError()
{
var indicator = new SlopeIndicator();
indicator.Initialize();
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewTick));
Assert.Equal(2, indicator.LinesSeries[0].Count);
}
[Fact]
public void SlopeIndicator_MultipleUpdates_ProducesCorrectSequence()
{
var indicator = new SlopeIndicator();
indicator.Initialize();
var now = DateTime.UtcNow;
for (int i = 0; i < 20; i++)
{
indicator.HistoricalData.AddBar(
now.AddMinutes(i),
100 + i * 2,
105 + i * 2,
95 + i * 2,
102 + i * 2);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
}
Assert.Equal(20, indicator.LinesSeries[0].Count);
for (int i = 0; i < 20; i++)
{
Assert.True(double.IsFinite(indicator.LinesSeries[0].GetValue(i)));
Assert.Equal(0, indicator.LinesSeries[1].GetValue(i));
}
}
[Fact]
public void SlopeIndicator_DifferentSourceTypes_Work()
{
var sources = new[]
{
SourceType.Open,
SourceType.High,
SourceType.Low,
SourceType.Close,
SourceType.HL2,
SourceType.HLC3,
};
foreach (var source in sources)
{
var indicator = new SlopeIndicator { Source = source };
indicator.Initialize();
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 110, 90, 105);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
Assert.Equal(1, indicator.LinesSeries[0].Count);
}
}
[Fact]
public void SlopeIndicator_ShowColdValues_False_SetsNaN()
{
var indicator = new SlopeIndicator { ShowColdValues = false };
indicator.Initialize();
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
Assert.True(double.IsNaN(indicator.LinesSeries[0].GetValue(0)));
}
[Fact]
public void SlopeIndicator_Uptrend_ProducesPositiveSlope()
{
var indicator = new SlopeIndicator();
indicator.Initialize();
var now = DateTime.UtcNow;
for (int i = 0; i < 10; i++)
{
double price = 100 + i * 5;
indicator.HistoricalData.AddBar(now.AddMinutes(i), price, price + 2, price - 2, price);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
}
double lastSlope = indicator.LinesSeries[0].GetValue(0);
Assert.True(lastSlope > 0);
}
[Fact]
public void SlopeIndicator_Downtrend_ProducesNegativeSlope()
{
var indicator = new SlopeIndicator();
indicator.Initialize();
var now = DateTime.UtcNow;
for (int i = 0; i < 10; i++)
{
double price = 200 - i * 5;
indicator.HistoricalData.AddBar(now.AddMinutes(i), price, price + 2, price - 2, price);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
}
double lastSlope = indicator.LinesSeries[0].GetValue(0);
Assert.True(lastSlope < 0);
}
[Fact]
public void SlopeIndicator_FlatPrices_ProducesZeroSlope()
{
var indicator = new SlopeIndicator();
indicator.Initialize();
var now = DateTime.UtcNow;
for (int i = 0; i < 5; i++)
{
indicator.HistoricalData.AddBar(now.AddMinutes(i), 100, 105, 95, 100);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
}
double lastSlope = indicator.LinesSeries[0].GetValue(0);
Assert.Equal(0, lastSlope);
}
}
-81
View File
@@ -1,81 +0,0 @@
using System.Drawing;
using TradingPlatform.BusinessLayer;
using static QuanTAlib.IndicatorExtensions;
namespace QuanTAlib;
/// <summary>
/// SLOPE (First Derivative / Velocity) Quantower indicator.
/// Measures the instantaneous rate of change between consecutive values.
/// </summary>
public class SlopeIndicator : Indicator, IWatchlistIndicator
{
[DataSourceInput]
public SourceType Source { get; set; } = SourceType.Close;
[InputParameter("Show Cold Values", sortIndex: 100)]
public bool ShowColdValues { get; set; } = true;
private Slope? _slope;
private Func<IHistoryItem, double>? _selector;
public int MinHistoryDepths => 2;
public override string ShortName => "SLOPE";
public SlopeIndicator()
{
Name = "SLOPE - First Derivative (Velocity)";
Description = "Measures instantaneous rate of change between consecutive values";
SeparateWindow = true;
OnBackGround = false;
}
protected override void OnInit()
{
_slope = new Slope();
_selector = Source.GetPriceSelector();
AddLineSeries(new LineSeries("Slope", Momentum, 2, LineStyle.Histogramm));
AddLineSeries(new LineSeries("Zero", Color.Gray, 1, LineStyle.Dot));
}
protected override void OnUpdate(UpdateArgs args)
{
if (_slope == null || _selector == null)
{
return;
}
var item = HistoricalData[0, SeekOriginHistory.End];
double value = _selector(item);
bool isNew = args.IsNewBar();
TValue input = new(item.TimeLeft, value);
_slope.Update(input, isNew);
bool isHot = _slope.IsHot;
LinesSeries[0].SetValue(_slope.Last.Value, isHot, ShowColdValues);
LinesSeries[1].SetValue(0);
if (isHot || ShowColdValues)
{
double slope = _slope.Last.Value;
Color color;
if (slope > 0)
{
color = Color.Green;
}
else if (slope < 0)
{
color = Color.Red;
}
else
{
color = Color.Gray;
}
LinesSeries[0].SetMarker(0, new IndicatorLineMarker(color));
}
}
}
@@ -1,3 +1,5 @@
using Skender.Stock.Indicators;
namespace QuanTAlib.Tests;
/// <summary>
@@ -112,6 +114,39 @@ public class SlopeValidationTests
}
}
// === Skender Cross-Validation ===
/// <summary>
/// Structural validation against Skender <c>GetSlope</c>.
/// Skender Slope computes linear regression slope over a lookback window,
/// while QuanTAlib Slope computes simple first difference (current - previous).
/// Different formulas mean numeric equality is not expected.
/// Both must produce finite output and agree on trend direction for simple linear data.
/// </summary>
[Fact]
public void Validate_Skender_Slope_Structural()
{
using var data = new ValidationTestData();
const int period = 14;
// QuanTAlib Slope (streaming, simple difference)
var slope = new Slope();
var qResults = new List<double>();
foreach (var tv in data.Data)
{
qResults.Add(slope.Update(tv).Value);
}
// Skender Slope (linear regression slope)
var sResult = data.SkenderQuotes.GetSlope(period).ToList();
// Structural: both produce finite output after warmup
Assert.True(double.IsFinite(slope.Last.Value), "QuanTAlib Slope last must be finite");
int finiteCount = sResult.Count(r => r.Slope is not null && double.IsFinite(r.Slope.Value));
Assert.True(finiteCount > 100, $"Skender Slope should produce >100 finite values, got {finiteCount}");
}
[Fact]
public void LargeLinearSequence_ProducesConstantSlope()
{