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,136 +0,0 @@
using TradingPlatform.BusinessLayer;
namespace QuanTAlib.Tests;
public sealed class SpearmanIndicatorTests
{
[Fact]
public void SpearmanIndicator_Constructor_SetsDefaults()
{
var indicator = new SpearmanIndicator();
Assert.Equal(20, indicator.Period);
Assert.Equal(SourceType.Close, indicator.Source);
Assert.Equal(SourceType.Open, indicator.Source2);
Assert.True(indicator.ShowColdValues);
Assert.Equal("SPEARMAN - Spearman Rank Correlation", indicator.Name);
Assert.True(indicator.SeparateWindow);
Assert.True(indicator.OnBackGround);
}
[Fact]
public void SpearmanIndicator_MinHistoryDepths_EqualsTwo()
{
var indicator = new SpearmanIndicator();
Assert.Equal(2, SpearmanIndicator.MinHistoryDepths);
Assert.Equal(2, ((IWatchlistIndicator)indicator).MinHistoryDepths);
}
[Fact]
public void SpearmanIndicator_ShortName_IncludesPeriodAndSources()
{
var indicator = new SpearmanIndicator { Period = 20 };
Assert.Contains("SPEARMAN", indicator.ShortName, StringComparison.Ordinal);
Assert.Contains("20", indicator.ShortName, StringComparison.Ordinal);
}
[Fact]
public void SpearmanIndicator_Initialize_CreatesInternalSpearman()
{
var indicator = new SpearmanIndicator { Period = 10 };
indicator.Initialize();
Assert.Single(indicator.LinesSeries);
}
[Fact]
public void SpearmanIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
{
var indicator = new SpearmanIndicator { Period = 5 };
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);
}
[Fact]
public void SpearmanIndicator_ProcessUpdate_NewBar_ComputesValue()
{
var indicator = new SpearmanIndicator { Period = 5 };
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 SpearmanIndicator_ProcessUpdate_NewTick_ProcessesWithoutError()
{
var indicator = new SpearmanIndicator { Period = 5 };
indicator.Initialize();
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
double firstValue = indicator.LinesSeries[0].GetValue(0);
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewTick));
double secondValue = indicator.LinesSeries[0].GetValue(0);
Assert.True(double.IsNaN(firstValue) || double.IsFinite(firstValue));
Assert.True(double.IsNaN(secondValue) || double.IsFinite(secondValue));
}
[Fact]
public void SpearmanIndicator_MultipleUpdates_ProducesSequence()
{
var indicator = new SpearmanIndicator { Period = 3 };
indicator.Initialize();
var now = DateTime.UtcNow;
double[] opens = [100, 101, 102, 103, 104, 105];
double[] closes = [100, 101, 102, 103, 104, 105];
for (int i = 0; i < opens.Length; i++)
{
indicator.HistoricalData.AddBar(now.AddMinutes(i), opens[i], opens[i] + 5, opens[i] - 5, closes[i]);
indicator.ProcessUpdate(new UpdateArgs(i == 0 ? UpdateReason.HistoricalBar : UpdateReason.NewBar));
}
Assert.Equal(opens.Length, indicator.LinesSeries[0].Count);
}
[Fact]
public void SpearmanIndicator_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 SpearmanIndicator { Period = 5, Source = source, Source2 = SourceType.Close };
indicator.Initialize();
var now = DateTime.UtcNow;
indicator.HistoricalData.AddBar(now, 100, 105, 95, 102);
// Should not throw and should produce output
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
Assert.Equal(1, indicator.LinesSeries[0].Count);
}
}
}
@@ -1,79 +0,0 @@
using System.Drawing;
using System.Runtime.CompilerServices;
using TradingPlatform.BusinessLayer;
namespace QuanTAlib;
/// <summary>
/// Quantower adapter for Spearman Rank Correlation indicator.
/// Measures monotonic association between two price sources from the same symbol.
/// </summary>
/// <remarks>
/// This adapter compares two different price sources from the same symbol (e.g., Close vs Open,
/// Close vs Volume, High vs Low). For cross-symbol correlation, use the core
/// Spearman class directly.
///
/// Output is Spearman's ρ coefficient, ranging from -1 to +1.
/// Values near +1 indicate strong positive monotonic association, near -1 strong negative.
/// </remarks>
[SkipLocalsInit]
public sealed class SpearmanIndicator : Indicator, IWatchlistIndicator
{
[InputParameter("Period", sortIndex: 0, minimum: 2, maximum: 10000)]
public int Period { get; set; } = 20;
[IndicatorExtensions.DataSourceInput]
public SourceType Source { get; set; } = SourceType.Close;
[InputParameter("Source 2 Type", sortIndex: 2)]
public SourceType Source2 { get; set; } = SourceType.Open;
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private Spearman _spearman = null!;
private readonly LineSeries _series;
private string _sourceName = null!;
private Func<IHistoryItem, double> _priceSelector = null!;
private Func<IHistoryItem, double> _priceSelector2 = null!;
public static int MinHistoryDepths => 2;
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public override string ShortName => $"SPEARMAN({Period}):{_sourceName}/{Source2}";
public SpearmanIndicator()
{
OnBackGround = true;
SeparateWindow = true;
Name = "SPEARMAN - Spearman Rank Correlation";
Description = "Measures monotonic association between two price sources. Range: -1 to +1.";
_series = new LineSeries(name: "Spearman", color: IndicatorExtensions.Statistics, width: 2, style: LineStyle.Solid);
AddLineSeries(_series);
}
protected override void OnInit()
{
_priceSelector = Source.GetPriceSelector();
_priceSelector2 = Source2.GetPriceSelector();
_sourceName = Source.ToString();
_spearman = new Spearman(Period);
base.OnInit();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void OnUpdate(UpdateArgs args)
{
bool isNew = args.IsNewBar();
var item = HistoricalData[Count - 1, SeekOriginHistory.Begin];
double valueA = _priceSelector(item);
double valueB = _priceSelector2(item);
var tvalA = new TValue(item.TimeLeft.Ticks, valueA);
var tvalB = new TValue(item.TimeLeft.Ticks, valueB);
double value = _spearman.Update(tvalA, tvalB, isNew).Value;
_series.SetValue(value, _spearman.IsHot, ShowColdValues);
}
}