style: format code with dotnet-format

This commit fixes the style issues introduced in 1e77eb8 according to the output
from dotnet-format.

Details: None
This commit is contained in:
deepsource-autofix[bot]
2024-10-06 06:59:26 +00:00
committed by GitHub
parent 1e77eb80a4
commit 5fe968754f
116 changed files with 1773 additions and 2748 deletions
+14 -7
View File
@@ -8,7 +8,8 @@ namespace QuanTAlib;
/// and methods used by inheriting indicator types. It handles the basic flow of
/// receiving bar data, performing calculations, and publishing results.
/// </remarks>
public abstract class AbstractBarBase : iTValue {
public abstract class AbstractBarBase : iTValue
{
public DateTime Time { get; set; }
public double Value { get; set; }
public bool IsNew { get; set; }
@@ -20,7 +21,8 @@ public abstract class AbstractBarBase : iTValue {
public event ValueSignal Pub = delegate { };
protected int _index;
protected double _lastValidValue;
protected AbstractBarBase() {
protected AbstractBarBase()
{
// Add parameters into constructor if needed
}
@@ -34,7 +36,8 @@ public abstract class AbstractBarBase : iTValue {
/// <summary>
/// Initializes the indicator's state.
/// </summary>
public virtual void Init() {
public virtual void Init()
{
_index = 0;
_lastValidValue = 0;
}
@@ -44,9 +47,11 @@ public abstract class AbstractBarBase : iTValue {
/// </summary>
/// <param name="input">The input bar data.</param>
/// <returns>A TValue containing the calculated result.</returns>
public virtual TValue Calc(TBar input) {
public virtual TValue Calc(TBar input)
{
Input = input;
if (double.IsNaN(input.Close) || double.IsInfinity(input.Close)) {
if (double.IsNaN(input.Close) || double.IsInfinity(input.Close))
{
return Process(new TValue(Time: input.Time, Value: GetLastValid(), IsNew: input.IsNew, IsHot: true));
}
this.Value = Calculation();
@@ -57,7 +62,8 @@ public abstract class AbstractBarBase : iTValue {
/// Retrieves the last valid calculated value.
/// </summary>
/// <returns>The last valid value of the indicator.</returns>
protected virtual double GetLastValid() {
protected virtual double GetLastValid()
{
return this.Value;
}
@@ -79,7 +85,8 @@ public abstract class AbstractBarBase : iTValue {
/// </summary>
/// <param name="value">The calculated TValue to process.</param>
/// <returns>The processed TValue.</returns>
protected virtual TValue Process(TValue value) {
protected virtual TValue Process(TValue value)
{
this.Time = value.Time;
this.Value = value.Value;
this.IsNew = value.IsNew;
+96 -46
View File
@@ -12,7 +12,8 @@ namespace QuanTAlib;
/// a fixed-size buffer of double values. It uses SIMD operations for improved performance
/// on supported hardware.
/// </remarks>
public class CircularBuffer : IEnumerable<double> {
public class CircularBuffer : IEnumerable<double>
{
private readonly double[] _buffer;
private int _start = 0;
private int _size = 0;
@@ -31,7 +32,8 @@ public class CircularBuffer : IEnumerable<double> {
/// Initializes a new instance of the CircularBuffer class with the specified capacity.
/// </summary>
/// <param name="capacity">The maximum number of elements the buffer can hold.</param>
public CircularBuffer(int capacity) {
public CircularBuffer(int capacity)
{
Capacity = capacity;
_buffer = GC.AllocateArray<double>(capacity, pinned: true);
}
@@ -42,16 +44,23 @@ public class CircularBuffer : IEnumerable<double> {
/// <param name="item">The item to add to the buffer.</param>
/// <param name="isNew">Indicates whether the item is a new value or an update to the last added value.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(double item, bool isNew = true) {
if (_size == 0 || isNew) {
if (_size < Capacity) {
public void Add(double item, bool isNew = true)
{
if (_size == 0 || isNew)
{
if (_size < Capacity)
{
_buffer[(_start + _size) % Capacity] = item;
_size++;
} else {
}
else
{
_buffer[_start] = item;
_start = (_start + 1) % Capacity;
}
} else {
}
else
{
_buffer[(_start + _size - 1) % Capacity] = item;
}
}
@@ -61,15 +70,18 @@ public class CircularBuffer : IEnumerable<double> {
/// </summary>
/// <param name="index">The zero-based index of the element to get or set.</param>
/// <returns>The element at the specified index.</returns>
public double this[Index index] {
public double this[Index index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get {
get
{
int actualIndex = index.IsFromEnd ? _size - index.Value : index.Value;
actualIndex = Math.Clamp(actualIndex, 0, _size - 1);
return _buffer[(_start + actualIndex) % Capacity];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set {
set
{
int actualIndex = index.IsFromEnd ? _size - index.Value : index.Value;
actualIndex = Math.Clamp(actualIndex, 0, _size - 1);
_buffer[(_start + actualIndex) % Capacity] = value;
@@ -77,7 +89,8 @@ public class CircularBuffer : IEnumerable<double> {
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowArgumentOutOfRangeException() {
private static void ThrowArgumentOutOfRangeException()
{
throw new ArgumentOutOfRangeException("index", "Index is out of range.");
}
@@ -86,7 +99,8 @@ public class CircularBuffer : IEnumerable<double> {
/// </summary>
/// <returns>The newest element in the buffer.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double Newest() {
public double Newest()
{
if (_size == 0)
return 0;
return _buffer[(_start + _size - 1) % Capacity];
@@ -97,14 +111,16 @@ public class CircularBuffer : IEnumerable<double> {
/// </summary>
/// <returns>The oldest element in the buffer.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double Oldest() {
public double Oldest()
{
if (_size == 0)
ThrowInvalidOperationException();
return _buffer[_start];
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowInvalidOperationException() {
private static void ThrowInvalidOperationException()
{
throw new InvalidOperationException("Buffer is empty.");
}
@@ -119,13 +135,15 @@ public class CircularBuffer : IEnumerable<double> {
/// <summary>
/// Represents an enumerator for the CircularBuffer.
/// </summary>
public struct Enumerator : IEnumerator<double> {
public struct Enumerator : IEnumerator<double>
{
private readonly CircularBuffer _buffer;
private int _index;
private double _current;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal Enumerator(CircularBuffer buffer) {
internal Enumerator(CircularBuffer buffer)
{
_buffer = buffer;
_index = -1;
_current = default;
@@ -136,7 +154,8 @@ public class CircularBuffer : IEnumerable<double> {
/// </summary>
/// <returns>true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext() {
public bool MoveNext()
{
if (_index + 1 >= _buffer._size)
return false;
@@ -154,7 +173,8 @@ public class CircularBuffer : IEnumerable<double> {
/// <summary>
/// Sets the enumerator to its initial position, which is before the first element in the buffer.
/// </summary>
public void Reset() {
public void Reset()
{
_index = -1;
_current = default;
}
@@ -171,13 +191,17 @@ public class CircularBuffer : IEnumerable<double> {
/// <param name="destination">The one-dimensional array that is the destination of the elements copied from the buffer.</param>
/// <param name="destinationIndex">The zero-based index in array at which copying begins.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyTo(double[] destination, int destinationIndex) {
public void CopyTo(double[] destination, int destinationIndex)
{
if (_size == 0)
return;
if (_start + _size <= Capacity) {
if (_start + _size <= Capacity)
{
Array.Copy(_buffer, _start, destination, destinationIndex, _size);
} else {
}
else
{
int firstPartLength = Capacity - _start;
Array.Copy(_buffer, _start, destination, destinationIndex, firstPartLength);
Array.Copy(_buffer, 0, destination, destinationIndex + firstPartLength, _size - firstPartLength);
@@ -189,13 +213,17 @@ public class CircularBuffer : IEnumerable<double> {
/// </summary>
/// <returns>A read-only span over the buffer contents.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan<double> GetSpan() {
public ReadOnlySpan<double> GetSpan()
{
if (_size == 0)
return ReadOnlySpan<double>.Empty;
if (_start + _size <= Capacity) {
if (_start + _size <= Capacity)
{
return new ReadOnlySpan<double>(_buffer, _start, _size);
} else {
}
else
{
return new ReadOnlySpan<double>(ToArray());
}
}
@@ -216,7 +244,8 @@ public class CircularBuffer : IEnumerable<double> {
/// Removes all elements from the buffer.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear() {
public void Clear()
{
Array.Clear(_buffer, 0, _buffer.Length);
_start = 0;
_size = 0;
@@ -227,7 +256,8 @@ public class CircularBuffer : IEnumerable<double> {
/// </summary>
/// <returns>The maximum value in the buffer.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double Max() {
public double Max()
{
if (_size == 0)
ThrowInvalidOperationException();
@@ -239,7 +269,8 @@ public class CircularBuffer : IEnumerable<double> {
/// </summary>
/// <returns>The minimum value in the buffer.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double Min() {
public double Min()
{
if (_size == 0)
ThrowInvalidOperationException();
@@ -251,7 +282,8 @@ public class CircularBuffer : IEnumerable<double> {
/// </summary>
/// <returns>The sum of all values in the buffer.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double Sum() {
public double Sum()
{
return SumSimd();
}
@@ -260,7 +292,8 @@ public class CircularBuffer : IEnumerable<double> {
/// </summary>
/// <returns>The average of all values in the buffer.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double Average() {
public double Average()
{
if (_size == 0)
ThrowInvalidOperationException();
@@ -268,22 +301,26 @@ public class CircularBuffer : IEnumerable<double> {
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double MaxSimd() {
private double MaxSimd()
{
var span = GetSpan();
var vectorSize = Vector<double>.Count;
var maxVector = new Vector<double>(double.MinValue);
int i = 0;
for (; i <= span.Length - vectorSize; i += vectorSize) {
for (; i <= span.Length - vectorSize; i += vectorSize)
{
maxVector = Vector.Max(maxVector, new Vector<double>(span.Slice(i, vectorSize)));
}
double max = double.MinValue;
for (int j = 0; j < vectorSize; j++) {
for (int j = 0; j < vectorSize; j++)
{
max = Math.Max(max, maxVector[j]);
}
for (; i < span.Length; i++) {
for (; i < span.Length; i++)
{
max = Math.Max(max, span[i]);
}
@@ -291,22 +328,26 @@ public class CircularBuffer : IEnumerable<double> {
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double MinSimd() {
private double MinSimd()
{
var span = GetSpan();
var vectorSize = Vector<double>.Count;
var minVector = new Vector<double>(double.MaxValue);
int i = 0;
for (; i <= span.Length - vectorSize; i += vectorSize) {
for (; i <= span.Length - vectorSize; i += vectorSize)
{
minVector = Vector.Min(minVector, new Vector<double>(span.Slice(i, vectorSize)));
}
double min = double.MaxValue;
for (int j = 0; j < vectorSize; j++) {
for (int j = 0; j < vectorSize; j++)
{
min = Math.Min(min, minVector[j]);
}
for (; i < span.Length; i++) {
for (; i < span.Length; i++)
{
min = Math.Min(min, span[i]);
}
@@ -314,22 +355,26 @@ public class CircularBuffer : IEnumerable<double> {
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double SumSimd() {
private double SumSimd()
{
var span = GetSpan();
var vectorSize = Vector<double>.Count;
var sumVector = Vector<double>.Zero;
int i = 0;
for (; i <= span.Length - vectorSize; i += vectorSize) {
for (; i <= span.Length - vectorSize; i += vectorSize)
{
sumVector += new Vector<double>(span.Slice(i, vectorSize));
}
double sum = 0;
for (int j = 0; j < vectorSize; j++) {
for (int j = 0; j < vectorSize; j++)
{
sum += sumVector[j];
}
for (; i < span.Length; i++) {
for (; i < span.Length; i++)
{
sum += span[i];
}
@@ -340,7 +385,8 @@ public class CircularBuffer : IEnumerable<double> {
/// Copies the buffer elements to a new array.
/// </summary>
/// <returns>An array containing copies of the buffer elements.</returns>
public double[] ToArray() {
public double[] ToArray()
{
double[] array = new double[_size];
CopyTo(array, 0);
return array;
@@ -350,10 +396,12 @@ public class CircularBuffer : IEnumerable<double> {
/// Performs a parallel operation on the buffer elements.
/// </summary>
/// <param name="operation">The operation to perform on each partition of the buffer.</param>
public void ParallelOperation(Func<double[], int, int, double> operation) {
public void ParallelOperation(Func<double[], int, int, double> operation)
{
const int MinimumPartitionSize = 1024;
if (_size < MinimumPartitionSize) {
if (_size < MinimumPartitionSize)
{
var span = GetSpan();
var array = span.ToArray();
operation(array, 0, array.Length);
@@ -363,7 +411,8 @@ public class CircularBuffer : IEnumerable<double> {
int partitionCount = Environment.ProcessorCount;
int partitionSize = _size / partitionCount;
if (partitionSize < MinimumPartitionSize) {
if (partitionSize < MinimumPartitionSize)
{
partitionCount = Math.Max(1, _size / MinimumPartitionSize);
partitionSize = _size / partitionCount;
}
@@ -371,7 +420,8 @@ public class CircularBuffer : IEnumerable<double> {
var buffer = ToArray();
var results = new double[partitionCount];
Parallel.For(0, partitionCount, i => {
Parallel.For(0, partitionCount, i =>
{
int start = i * partitionSize;
int length = (i == partitionCount - 1) ? _size - start : partitionSize;
results[i] = operation(buffer, start, length);
+19 -19
View File
@@ -14,28 +14,28 @@ public interface iTBar
public readonly record struct TBar(DateTime Time, double Open, double High, double Low, double Close, double Volume, bool IsNew = true) : iTBar
{
public DateTime Time { get; init; } = Time;
public double Open { get; init; } = Open;
public double High { get; init; } = High;
public double Low { get; init; } = Low;
public double Close { get; init; } = Close;
public double Volume { get; init; } = Volume;
public bool IsNew { get; init; } = IsNew;
public double Open { get; init; } = Open;
public double High { get; init; } = High;
public double Low { get; init; } = Low;
public double Close { get; init; } = Close;
public double Volume { get; init; } = Volume;
public bool IsNew { get; init; } = IsNew;
public double HL2 => (High + Low) * 0.5;
public double OC2 => (Open + Close) * 0.5;
public double OHL3 => (Open + High + Low) / 3;
public double HLC3 => (High + Low + Close) / 3;
public double OHLC4 => (Open + High + Low + Close) * 0.25;
public double HLCC4 => (High + Low + Close + Close) * 0.25;
public double HL2 => (High + Low) * 0.5;
public double OC2 => (Open + Close) * 0.5;
public double OHL3 => (Open + High + Low) / 3;
public double HLC3 => (High + Low + Close) / 3;
public double OHLC4 => (Open + High + Low + Close) * 0.25;
public double HLCC4 => (High + Low + Close + Close) * 0.25;
public TBar() : this(DateTime.UtcNow, 0, 0, 0, 0, 0) { }
public TBar(double Open, double High, double Low, double Close, double Volume, bool IsNew = true) : this(DateTime.UtcNow, Open, High, Low, Close, Volume, IsNew) { }
public TBar(double value) : this(Time: DateTime.UtcNow, Open: value, High: value, Low: value, Close: value, Volume: value, IsNew: true) { }
public TBar(TValue value) : this(Time: value.Time, Open: value.Value, High: value.Value, Low: value.Value, Close: value.Value, Volume: value.Value, IsNew: value.IsNew) { }
public TBar() : this(DateTime.UtcNow, 0, 0, 0, 0, 0) { }
public TBar(double Open, double High, double Low, double Close, double Volume, bool IsNew = true) : this(DateTime.UtcNow, Open, High, Low, Close, Volume, IsNew) { }
public TBar(double value) : this(Time: DateTime.UtcNow, Open: value, High: value, Low: value, Close: value, Volume: value, IsNew: true) { }
public TBar(TValue value) : this(Time: value.Time, Open: value.Value, High: value.Value, Low: value.Value, Close: value.Value, Volume: value.Value, IsNew: value.IsNew) { }
public static implicit operator double(TBar bar) => bar.Close;
public static implicit operator DateTime(TBar tv) => tv.Time;
public override string ToString() => $"[{Time:yyyy-MM-dd HH:mm:ss}: O={Open:F2}, H={High:F2}, L={Low:F2}, C={Close:F2}, V={Volume:F2}]";
public static implicit operator double(TBar bar) => bar.Close;
public static implicit operator DateTime(TBar tv) => tv.Time;
public override string ToString() => $"[{Time:yyyy-MM-dd HH:mm:ss}: O={Open:F2}, H={High:F2}, L={Low:F2}, C={Close:F2}, V={Volume:F2}]";
}
public delegate void BarSignal(object source, in TBarEventArgs args);
+12 -12
View File
@@ -11,19 +11,19 @@ public interface iTValue
public readonly record struct TValue(DateTime Time, double Value, bool IsNew = true, bool IsHot = true) : iTValue
{
public DateTime Time { get; init; } = Time;
public double Value { get; init; } = Value;
public bool IsNew { get; init; } = IsNew;
public bool IsHot { get; init; } = IsHot;
public DateTime t => Time;
public double v => Value;
public double Value { get; init; } = Value;
public bool IsNew { get; init; } = IsNew;
public bool IsHot { get; init; } = IsHot;
public DateTime t => Time;
public double v => Value;
public TValue() : this(DateTime.UtcNow, 0) { }
public TValue(double value, bool isNew = true, bool isHot = true) : this(DateTime.UtcNow, value, IsNew: isNew, IsHot: isHot) { }
public static implicit operator double(TValue tv) => tv.Value;
public static implicit operator DateTime(TValue tv) => tv.Time;
public static implicit operator TValue(double value) => new TValue(DateTime.UtcNow, value);
public TValue() : this(DateTime.UtcNow, 0) { }
public TValue(double value, bool isNew = true, bool isHot = true) : this(DateTime.UtcNow, value, IsNew: isNew, IsHot: isHot) { }
public static implicit operator double(TValue tv) => tv.Value;
public static implicit operator DateTime(TValue tv) => tv.Time;
public static implicit operator TValue(double value) => new TValue(DateTime.UtcNow, value);
public override string ToString() => $"[{Time:yyyy-MM-dd HH:mm:ss}, {Value:F2}, IsNew: {IsNew}, IsHot: {IsHot}]";
public override string ToString() => $"[{Time:yyyy-MM-dd HH:mm:ss}, {Value:F2}, IsNew: {IsNew}, IsHot: {IsHot}]";
}
public delegate void ValueSignal(object source, in ValueEventArgs args);
@@ -66,7 +66,7 @@ public class TSeries : List<TValue>
public new virtual void Add(TValue tick)
{
if (tick.IsNew || base.Count==0) { base.Add(tick); }
if (tick.IsNew || base.Count == 0) { base.Add(tick); }
else { this[^1] = tick; }
Pub?.Invoke(this, new ValueEventArgs(tick));
}