mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-19 11:08:05 +00:00
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:
+22
-11
@@ -8,7 +8,8 @@ namespace QuanTAlib;
|
||||
/// of the true range. The true range is the greatest of: current high - current low,
|
||||
/// absolute value of current high - previous close, or absolute value of current low - previous close.
|
||||
/// </remarks>
|
||||
public class Atr : AbstractBarBase {
|
||||
public class Atr : AbstractBarBase
|
||||
{
|
||||
private readonly Ema _ma;
|
||||
private double _prevClose, _p_prevClose;
|
||||
|
||||
@@ -19,11 +20,13 @@ public class Atr : AbstractBarBase {
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when period is less than 1.
|
||||
/// </exception>
|
||||
public Atr(int period) {
|
||||
if (period < 1) {
|
||||
public Atr(int period)
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
|
||||
}
|
||||
_ma = new(1.0/period);
|
||||
_ma = new(1.0 / period);
|
||||
WarmupPeriod = _ma.WarmupPeriod;
|
||||
Name = $"ATR({period})";
|
||||
}
|
||||
@@ -33,7 +36,8 @@ public class Atr : AbstractBarBase {
|
||||
/// </summary>
|
||||
/// <param name="source">The source object to subscribe to for bar updates.</param>
|
||||
/// <param name="period">The period over which to calculate the ATR.</param>
|
||||
public Atr(object source, int period) : this(period) {
|
||||
public Atr(object source, int period) : this(period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new BarSignal(Sub));
|
||||
}
|
||||
@@ -41,7 +45,8 @@ public class Atr : AbstractBarBase {
|
||||
/// <summary>
|
||||
/// Initializes the Atr instance by setting up the initial state.
|
||||
/// </summary>
|
||||
public override void Init() {
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_ma.Init();
|
||||
_prevClose = double.NaN;
|
||||
@@ -51,11 +56,15 @@ public class Atr : AbstractBarBase {
|
||||
/// Manages the state of the Atr instance based on whether a new bar is being processed.
|
||||
/// </summary>
|
||||
/// <param name="isNew">Indicates whether the current input is a new bar.</param>
|
||||
protected override void ManageState(bool isNew) {
|
||||
if (isNew) {
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_index++;
|
||||
_p_prevClose = _prevClose;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
_prevClose = _p_prevClose;
|
||||
}
|
||||
}
|
||||
@@ -71,7 +80,8 @@ public class Atr : AbstractBarBase {
|
||||
/// to smooth the true range values. For the first bar, it uses the high-low range
|
||||
/// as the true range.
|
||||
/// </remarks>
|
||||
protected override double Calculation() {
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
double trueRange = Math.Max(
|
||||
@@ -81,7 +91,8 @@ public class Atr : AbstractBarBase {
|
||||
),
|
||||
Math.Abs(Input.Low - _prevClose)
|
||||
);
|
||||
if (_index < 2) {
|
||||
if (_index < 2)
|
||||
{
|
||||
trueRange = Input.High - Input.Low;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,8 @@ namespace QuanTAlib;
|
||||
/// both annualized and non-annualized volatility measures. The calculation uses a sample
|
||||
/// standard deviation formula and assumes 252 trading days in a year for annualization.
|
||||
/// </remarks>
|
||||
public class Historical : AbstractBase {
|
||||
public class Historical : AbstractBase
|
||||
{
|
||||
private readonly int Period;
|
||||
private readonly bool IsAnnualized;
|
||||
private readonly CircularBuffer _buffer;
|
||||
@@ -24,8 +25,10 @@ public class Historical : AbstractBase {
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when period is less than 2.
|
||||
/// </exception>
|
||||
public Historical(int period, bool isAnnualized = true) : base() {
|
||||
if (period < 2) {
|
||||
public Historical(int period, bool isAnnualized = true) : base()
|
||||
{
|
||||
if (period < 2)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 2.");
|
||||
}
|
||||
Period = period;
|
||||
@@ -43,7 +46,8 @@ public class Historical : AbstractBase {
|
||||
/// <param name="source">The source object to subscribe to for value updates.</param>
|
||||
/// <param name="period">The period over which to calculate historical volatility.</param>
|
||||
/// <param name="isAnnualized">Whether to annualize the volatility (default is true).</param>
|
||||
public Historical(object source, int period, bool isAnnualized = true) : this(period, isAnnualized) {
|
||||
public Historical(object source, int period, bool isAnnualized = true) : this(period, isAnnualized)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
@@ -51,7 +55,8 @@ public class Historical : AbstractBase {
|
||||
/// <summary>
|
||||
/// Initializes the Historical instance by clearing buffers and resetting the previous close value.
|
||||
/// </summary>
|
||||
public override void Init() {
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_buffer.Clear();
|
||||
_logReturns.Clear();
|
||||
@@ -62,8 +67,10 @@ public class Historical : AbstractBase {
|
||||
/// Manages the state of the Historical instance based on whether a new value is being processed.
|
||||
/// </summary>
|
||||
/// <param name="isNew">Indicates whether the current input is a new value.</param>
|
||||
protected override void ManageState(bool isNew) {
|
||||
if (isNew) {
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Input.Value;
|
||||
_index++;
|
||||
}
|
||||
@@ -82,19 +89,23 @@ public class Historical : AbstractBase {
|
||||
/// 3. If annualized, multiply by the square root of 252 (assumed trading days in a year).
|
||||
/// The method returns 0 until enough data points are available for the calculation.
|
||||
/// </remarks>
|
||||
protected override double Calculation() {
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
|
||||
double volatility = 0;
|
||||
if (_buffer.Count > 1) {
|
||||
if (_previousClose != 0) {
|
||||
if (_buffer.Count > 1)
|
||||
{
|
||||
if (_previousClose != 0)
|
||||
{
|
||||
double logReturn = Math.Log(Input.Value / _previousClose);
|
||||
_logReturns.Add(logReturn, Input.IsNew);
|
||||
}
|
||||
|
||||
if (_logReturns.Count == Period) {
|
||||
if (_logReturns.Count == Period)
|
||||
{
|
||||
var returns = _logReturns.GetSpan().ToArray();
|
||||
double mean = returns.Average();
|
||||
double sumOfSquaredDifferences = returns.Sum(x => Math.Pow(x - mean, 2));
|
||||
@@ -102,7 +113,8 @@ public class Historical : AbstractBase {
|
||||
double variance = sumOfSquaredDifferences / (Period - 1); // Using sample standard deviation
|
||||
volatility = Math.Sqrt(variance);
|
||||
|
||||
if (IsAnnualized) {
|
||||
if (IsAnnualized)
|
||||
{
|
||||
// Assuming 252 trading days in a year. Adjust as needed.
|
||||
volatility *= Math.Sqrt(252);
|
||||
}
|
||||
|
||||
+22
-11
@@ -9,7 +9,8 @@ namespace QuanTAlib;
|
||||
/// both annualized and non-annualized volatility measures. The calculation uses a rolling
|
||||
/// sum of squared returns for efficiency and assumes 252 trading days in a year for annualization.
|
||||
/// </remarks>
|
||||
public class Realized : AbstractBase {
|
||||
public class Realized : AbstractBase
|
||||
{
|
||||
private readonly int Period;
|
||||
private readonly bool IsAnnualized;
|
||||
private readonly CircularBuffer _returns;
|
||||
@@ -24,8 +25,10 @@ public class Realized : AbstractBase {
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when period is less than 2.
|
||||
/// </exception>
|
||||
public Realized(int period, bool isAnnualized = true) : base() {
|
||||
if (period < 2) {
|
||||
public Realized(int period, bool isAnnualized = true) : base()
|
||||
{
|
||||
if (period < 2)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 2.");
|
||||
}
|
||||
Period = period;
|
||||
@@ -39,7 +42,8 @@ public class Realized : AbstractBase {
|
||||
/// <summary>
|
||||
/// Initializes the Realized instance by clearing buffers and resetting calculation variables.
|
||||
/// </summary>
|
||||
public override void Init() {
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_returns.Clear();
|
||||
_previousClose = 0;
|
||||
@@ -50,8 +54,10 @@ public class Realized : AbstractBase {
|
||||
/// Manages the state of the Realized instance based on whether a new value is being processed.
|
||||
/// </summary>
|
||||
/// <param name="isNew">Indicates whether the current input is a new value.</param>
|
||||
protected override void ManageState(bool isNew) {
|
||||
if (isNew) {
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Input.Value;
|
||||
_index++;
|
||||
}
|
||||
@@ -72,14 +78,17 @@ public class Realized : AbstractBase {
|
||||
/// 5. If annualized, multiply by the square root of 252 (assumed trading days in a year).
|
||||
/// The method returns 0 until enough data points are available for the calculation.
|
||||
/// </remarks>
|
||||
protected override double Calculation() {
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
double volatility = 0;
|
||||
if (_previousClose != 0) {
|
||||
if (_previousClose != 0)
|
||||
{
|
||||
double logReturn = Math.Log(Input.Value / _previousClose);
|
||||
|
||||
if (_returns.Count == Period) {
|
||||
if (_returns.Count == Period)
|
||||
{
|
||||
// Remove the oldest squared return from the sum
|
||||
_sumSquaredReturns -= Math.Pow(_returns[0], 2);
|
||||
}
|
||||
@@ -87,11 +96,13 @@ public class Realized : AbstractBase {
|
||||
_returns.Add(logReturn, Input.IsNew);
|
||||
_sumSquaredReturns += Math.Pow(logReturn, 2);
|
||||
|
||||
if (_returns.Count == Period) {
|
||||
if (_returns.Count == Period)
|
||||
{
|
||||
double variance = _sumSquaredReturns / Period;
|
||||
volatility = Math.Sqrt(variance);
|
||||
|
||||
if (IsAnnualized) {
|
||||
if (IsAnnualized)
|
||||
{
|
||||
// Assuming 252 trading days in a year. Adjust as needed.
|
||||
volatility *= Math.Sqrt(252);
|
||||
}
|
||||
|
||||
+21
-10
@@ -13,7 +13,8 @@ namespace QuanTAlib;
|
||||
/// This implementation uses a combination of Standard Deviation and Simple Moving Average
|
||||
/// calculations to compute the RVI.
|
||||
/// </remarks>
|
||||
public class Rvi : AbstractBase {
|
||||
public class Rvi : AbstractBase
|
||||
{
|
||||
private readonly int Period;
|
||||
private Stddev _upStdDev, _downStdDev;
|
||||
private Sma _upSma, _downSma;
|
||||
@@ -26,8 +27,10 @@ public class Rvi : AbstractBase {
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when period is less than 2.
|
||||
/// </exception>
|
||||
public Rvi(int period) : base() {
|
||||
if (period < 2) {
|
||||
public Rvi(int period) : base()
|
||||
{
|
||||
if (period < 2)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 2.");
|
||||
}
|
||||
Period = period;
|
||||
@@ -45,7 +48,8 @@ public class Rvi : AbstractBase {
|
||||
/// </summary>
|
||||
/// <param name="source">The source object to subscribe to for value updates.</param>
|
||||
/// <param name="period">The period over which to calculate the RVI.</param>
|
||||
public Rvi(object source, int period) : this(period) {
|
||||
public Rvi(object source, int period) : this(period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
@@ -53,7 +57,8 @@ public class Rvi : AbstractBase {
|
||||
/// <summary>
|
||||
/// Initializes the Rvi instance by setting up the initial state.
|
||||
/// </summary>
|
||||
public override void Init() {
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_previousClose = 0;
|
||||
}
|
||||
@@ -62,8 +67,10 @@ public class Rvi : AbstractBase {
|
||||
/// Manages the state of the Rvi instance based on whether a new value is being processed.
|
||||
/// </summary>
|
||||
/// <param name="isNew">Indicates whether the current input is a new value.</param>
|
||||
protected override void ManageState(bool isNew) {
|
||||
if (isNew) {
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Value;
|
||||
_index++;
|
||||
}
|
||||
@@ -84,7 +91,8 @@ public class Rvi : AbstractBase {
|
||||
/// 5. Compute the RVI as a percentage of up volatility to total volatility.
|
||||
/// The method returns 0 if the sum of up and down volatility is zero.
|
||||
/// </remarks>
|
||||
protected override double Calculation() {
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
double close = Input.Value;
|
||||
@@ -97,9 +105,12 @@ public class Rvi : AbstractBase {
|
||||
_downSma.Calc(_downStdDev.Calc(new TValue(Input.Time, downMove, Input.IsNew)));
|
||||
|
||||
double rvi;
|
||||
if (_upSma.Value + _downSma.Value != 0) {
|
||||
if (_upSma.Value + _downSma.Value != 0)
|
||||
{
|
||||
rvi = 100 * _upSma.Value / (_upSma.Value + _downSma.Value);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
rvi = 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user