mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-22 12:38:06 +00:00
normalization of methods
This commit is contained in:
@@ -241,7 +241,7 @@ public class TwapTests
|
||||
}
|
||||
|
||||
// Batch
|
||||
var batchResult = Twap.Calculate(bars, period: 10);
|
||||
var batchResult = Twap.Batch(bars, period: 10);
|
||||
|
||||
Assert.Equal(bars.Count, batchResult.Count);
|
||||
for (int i = 0; i < bars.Count; i++)
|
||||
@@ -272,7 +272,7 @@ public class TwapTests
|
||||
|
||||
// Span
|
||||
var output = new double[prices.Length];
|
||||
Twap.Calculate(prices, output, period: 10);
|
||||
Twap.Batch(prices, output, period: 10);
|
||||
|
||||
for (int i = 0; i < prices.Length; i++)
|
||||
{
|
||||
@@ -286,7 +286,7 @@ public class TwapTests
|
||||
var price = new double[100];
|
||||
var output = new double[99]; // Different length
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Twap.Calculate(price, output));
|
||||
Assert.Throws<ArgumentException>(() => Twap.Batch(price, output));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -295,7 +295,7 @@ public class TwapTests
|
||||
var price = new double[100];
|
||||
var output = new double[100];
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Twap.Calculate(price, output, period: -1));
|
||||
Assert.Throws<ArgumentException>(() => Twap.Batch(price, output, period: -1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -304,7 +304,7 @@ public class TwapTests
|
||||
var price = Array.Empty<double>();
|
||||
var output = Array.Empty<double>();
|
||||
|
||||
Twap.Calculate(price, output);
|
||||
Twap.Batch(price, output);
|
||||
|
||||
Assert.Empty(output);
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public class TwapValidationTests
|
||||
}
|
||||
|
||||
// Batch
|
||||
var batchResult = Twap.Calculate(_data.Bars, period);
|
||||
var batchResult = Twap.Batch(_data.Bars, period);
|
||||
var batchValues = batchResult.Values.ToArray();
|
||||
|
||||
ValidationHelper.VerifyData(streamingValues.ToArray(), batchValues, 0, 100, 1e-9);
|
||||
@@ -55,7 +55,7 @@ public class TwapValidationTests
|
||||
|
||||
// Span
|
||||
var spanOutput = new double[typicalPrices.Length];
|
||||
Twap.Calculate(typicalPrices, spanOutput, period);
|
||||
Twap.Batch(typicalPrices, spanOutput, period);
|
||||
|
||||
ValidationHelper.VerifyData(streamingValues.ToArray(), spanOutput, 0, 100, 1e-9);
|
||||
}
|
||||
@@ -138,12 +138,12 @@ public class TwapValidationTests
|
||||
}
|
||||
|
||||
// Batch
|
||||
var batchResult = Twap.Calculate(_data.Bars, period);
|
||||
var batchResult = Twap.Batch(_data.Bars, period);
|
||||
var batchValues = batchResult.Values.ToArray();
|
||||
|
||||
// Span
|
||||
var spanOutput = new double[typicalPrices.Length];
|
||||
Twap.Calculate(typicalPrices, spanOutput, period);
|
||||
Twap.Batch(typicalPrices, spanOutput, period);
|
||||
|
||||
// Verify all modes match
|
||||
ValidationHelper.VerifyData(streamingValues.ToArray(), batchValues, 0, 100, 1e-9);
|
||||
|
||||
+10
-3
@@ -197,7 +197,7 @@ public sealed class Twap : ITValuePublisher
|
||||
}
|
||||
|
||||
var output = new double[source.Count];
|
||||
Calculate(prices, output, _period);
|
||||
Batch(prices, output, _period);
|
||||
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
@@ -224,7 +224,7 @@ public sealed class Twap : ITValuePublisher
|
||||
/// <param name="source">The bar series.</param>
|
||||
/// <param name="period">The session period in bars (0 = never reset).</param>
|
||||
/// <returns>The result series.</returns>
|
||||
public static TSeries Calculate(TBarSeries source, int period = DefaultPeriod)
|
||||
public static TSeries Batch(TBarSeries source, int period = DefaultPeriod)
|
||||
{
|
||||
var twap = new Twap(period);
|
||||
var result = new TSeries(source.Count);
|
||||
@@ -244,7 +244,7 @@ public sealed class Twap : ITValuePublisher
|
||||
/// <param name="output">The output TWAP span.</param>
|
||||
/// <param name="period">The session period in bars (0 = never reset). Default is 0.</param>
|
||||
/// <exception cref="ArgumentException">Thrown when output length doesn't match price length or period is invalid.</exception>
|
||||
public static void Calculate(ReadOnlySpan<double> price, Span<double> output, int period = DefaultPeriod)
|
||||
public static void Batch(ReadOnlySpan<double> price, Span<double> output, int period = DefaultPeriod)
|
||||
{
|
||||
if (output.Length != price.Length)
|
||||
{
|
||||
@@ -288,4 +288,11 @@ public sealed class Twap : ITValuePublisher
|
||||
output[i] = sumPrices / count;
|
||||
}
|
||||
}
|
||||
|
||||
public static (TSeries Results, Twap Indicator) Calculate(TBarSeries source, int period = DefaultPeriod)
|
||||
{
|
||||
var indicator = new Twap(period);
|
||||
TSeries results = indicator.Update(source);
|
||||
return (results, indicator);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user