mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 13:58:04 +00:00
python wrapper
This commit is contained in:
@@ -1,195 +0,0 @@
|
||||
using Xunit;
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class NormdistIndicatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void NormdistIndicator_Constructor_SetsDefaults()
|
||||
{
|
||||
var indicator = new NormdistIndicator();
|
||||
|
||||
Assert.Equal(SourceType.Close, indicator.Source);
|
||||
Assert.Equal(0.0, indicator.Mu);
|
||||
Assert.Equal(1.0, indicator.Sigma);
|
||||
Assert.Equal(14, indicator.Period);
|
||||
Assert.True(indicator.ShowColdValues);
|
||||
Assert.Equal("NORMDIST - Normal Distribution CDF", indicator.Name);
|
||||
Assert.True(indicator.SeparateWindow);
|
||||
Assert.True(indicator.OnBackGround);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormdistIndicator_MinHistoryDepths_EqualsPeriod()
|
||||
{
|
||||
var indicator = new NormdistIndicator { Period = 30 };
|
||||
Assert.Equal(30, indicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormdistIndicator_ShortName_IsCorrect()
|
||||
{
|
||||
var indicator = new NormdistIndicator { Mu = 0.5, Sigma = 2.0, Period = 20 };
|
||||
Assert.Equal("NORMDIST(0.50,2.00,20)", indicator.ShortName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormdistIndicator_Initialize_CreatesTwoLineSeries()
|
||||
{
|
||||
var indicator = new NormdistIndicator();
|
||||
indicator.Initialize();
|
||||
|
||||
Assert.Equal(2, indicator.LinesSeries.Count);
|
||||
Assert.Equal("NormDist", indicator.LinesSeries[0].Name);
|
||||
Assert.Equal("Mid", indicator.LinesSeries[1].Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormdistIndicator_ProcessUpdate_HistoricalBar_ComputesValue()
|
||||
{
|
||||
var indicator = new NormdistIndicator { Period = 5 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 0, 105 + i, 95 - i, 100 + i);
|
||||
var args = new UpdateArgs(UpdateReason.HistoricalBar);
|
||||
indicator.ProcessUpdate(args);
|
||||
}
|
||||
|
||||
// After 5 bars (= period), should have valid output
|
||||
double val = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(val), "Output must be finite after warmup");
|
||||
Assert.True(val >= 0.0 && val <= 1.0, $"Output {val} must be in [0,1]");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormdistIndicator_ProcessUpdate_NewBar_AddsNewValue()
|
||||
{
|
||||
var indicator = new NormdistIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
// Feed 3 historical bars
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 0, 105, 95, 100 + i);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
// Feed a new bar
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(3), 0, 106, 96, 103);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
|
||||
Assert.Equal(4, indicator.LinesSeries[0].Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormdistIndicator_ProcessUpdate_NewTick_ProcessesWithoutError()
|
||||
{
|
||||
var indicator = new NormdistIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
indicator.HistoricalData.AddBar(now, 0, 105, 95, 100);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.NewTick));
|
||||
|
||||
// 2 values: one historical, one intra-bar update
|
||||
Assert.Equal(2, indicator.LinesSeries[0].Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormdistIndicator_MidLine_IsAlwaysHalf()
|
||||
{
|
||||
var indicator = new NormdistIndicator { Period = 3 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 0, 105, 95, 100 + i);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
// Mid line should always be 0.5
|
||||
for (int i = 0; i < indicator.LinesSeries[1].Count; i++)
|
||||
{
|
||||
double mid = indicator.LinesSeries[1].GetValue(i);
|
||||
Assert.Equal(0.5, mid, 1e-10);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormdistIndicator_DifferentSourceType_Works()
|
||||
{
|
||||
var indicator = new NormdistIndicator { Period = 3, Source = SourceType.High };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
// High = 110+i, Low = 90, Close = 100
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 0, 110 + i, 90, 100);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double val = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(val));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormdistIndicator_OutputInRange_AfterManyBars()
|
||||
{
|
||||
var indicator = new NormdistIndicator { Period = 20 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 76001);
|
||||
var bars = gbm.Fetch(50, now.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Close.Count; i++)
|
||||
{
|
||||
double price = bars.Close[i].Value;
|
||||
indicator.HistoricalData.AddBar(
|
||||
new DateTime(bars.Close[i].Time, DateTimeKind.Utc),
|
||||
0, price * 1.01, price * 0.99, price);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
// Check all computed values are in [0, 1]
|
||||
for (int i = 0; i < indicator.LinesSeries[0].Count; i++)
|
||||
{
|
||||
double val = indicator.LinesSeries[0].GetValue(i);
|
||||
Assert.True(val >= 0.0 && val <= 1.0, $"Value {val} at index {i} out of range");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormdistIndicator_FlatPrices_OutputNearHalf()
|
||||
{
|
||||
// When all prices identical, z=0 → CDF = 0.5
|
||||
var indicator = new NormdistIndicator { Period = 5, Mu = 0.0, Sigma = 1.0 };
|
||||
indicator.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
indicator.HistoricalData.AddBar(now.AddMinutes(i), 0, 101, 99, 100);
|
||||
indicator.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double val = indicator.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(val));
|
||||
Assert.True(val >= 0.0 && val <= 1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormdistIndicator_CustomMuSigma_ShortNameReflects()
|
||||
{
|
||||
var indicator = new NormdistIndicator { Mu = -0.5, Sigma = 1.5, Period = 14 };
|
||||
Assert.Equal("NORMDIST(-0.50,1.50,14)", indicator.ShortName);
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
using System.Drawing;
|
||||
using TradingPlatform.BusinessLayer;
|
||||
using static QuanTAlib.IndicatorExtensions;
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// NORMDIST (Normal Distribution CDF) Quantower indicator.
|
||||
/// Computes Φ(z; μ, σ) applied to a z-score normalized price series
|
||||
/// over a rolling lookback window.
|
||||
/// </summary>
|
||||
public class NormdistIndicator : Indicator, IWatchlistIndicator
|
||||
{
|
||||
[DataSourceInput]
|
||||
public SourceType Source { get; set; } = SourceType.Close;
|
||||
|
||||
[InputParameter("Mean (μ)", sortIndex: 0, minimum: -100.0, maximum: 100.0, increment: 0.1, decimalPlaces: 3)]
|
||||
public double Mu { get; set; } = 0.0;
|
||||
|
||||
[InputParameter("Std Dev (σ)", sortIndex: 1, minimum: 0.001, maximum: 100.0, increment: 0.1, decimalPlaces: 3)]
|
||||
public double Sigma { get; set; } = 1.0;
|
||||
|
||||
[InputParameter("Period", sortIndex: 2, minimum: 2, maximum: 2000, increment: 1)]
|
||||
public int Period { get; set; } = 14;
|
||||
|
||||
[InputParameter("Show Cold Values", sortIndex: 100)]
|
||||
public bool ShowColdValues { get; set; } = true;
|
||||
|
||||
private Normdist? _normdist;
|
||||
private Func<IHistoryItem, double>? _selector;
|
||||
|
||||
public int MinHistoryDepths => Period;
|
||||
public override string ShortName => $"NORMDIST({Mu:F2},{Sigma:F2},{Period})";
|
||||
|
||||
public NormdistIndicator()
|
||||
{
|
||||
Name = "NORMDIST - Normal Distribution CDF";
|
||||
Description = "Applies the Gaussian CDF to a z-score normalized price series";
|
||||
SeparateWindow = true;
|
||||
OnBackGround = true;
|
||||
}
|
||||
|
||||
protected override void OnInit()
|
||||
{
|
||||
_normdist = new Normdist(Mu, Sigma, Period);
|
||||
_selector = Source.GetPriceSelector();
|
||||
|
||||
AddLineSeries(new LineSeries("NormDist", Color.Cyan, 2, LineStyle.Solid));
|
||||
// Reference level at 0.5 (midpoint / rolling mean)
|
||||
AddLineSeries(new LineSeries("Mid", Color.Gray, 1, LineStyle.Dash));
|
||||
}
|
||||
|
||||
protected override void OnUpdate(UpdateArgs args)
|
||||
{
|
||||
if (_normdist == null || _selector == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var item = HistoricalData[0, SeekOriginHistory.End];
|
||||
double value = _selector(item);
|
||||
bool isNew = args.IsNewBar();
|
||||
|
||||
TValue input = new(item.TimeLeft, value);
|
||||
_normdist.Update(input, isNew);
|
||||
|
||||
bool isHot = _normdist.IsHot;
|
||||
|
||||
LinesSeries[0].SetValue(_normdist.Last.Value, isHot, ShowColdValues);
|
||||
LinesSeries[1].SetValue(0.5, isHot, ShowColdValues);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user