mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-22 12:38:06 +00:00
Refactor code formatting and improve consistency across various test files
- Removed unnecessary blank lines in multiple test files to enhance readability. - Ensured consistent spacing and formatting in the `Trima`, `Usf`, `Vidya`, `Wma`, and `Atr` test classes. - Updated comments for clarity and consistency in the `Atr` and `Adl` classes. - Adjusted project files for better structure and maintainability.
This commit is contained in:
@@ -23,7 +23,7 @@ public class BetaTests
|
||||
{
|
||||
int period = 5;
|
||||
var beta = new Beta(period);
|
||||
|
||||
|
||||
// We need period returns.
|
||||
// 1st update: initializes prev prices. No return.
|
||||
// 2nd update: 1st return.
|
||||
@@ -45,7 +45,7 @@ public class BetaTests
|
||||
{
|
||||
// Scenario: Asset returns are exactly 2x Market returns.
|
||||
// We need variable market returns to have non-zero variance.
|
||||
|
||||
|
||||
int period = 10;
|
||||
var beta = new Beta(period);
|
||||
|
||||
@@ -66,9 +66,9 @@ public class BetaTests
|
||||
|
||||
marketPrice *= (1 + marketReturn);
|
||||
assetPrice *= (1 + assetReturn);
|
||||
|
||||
|
||||
TValue result = beta.Update(assetPrice, marketPrice);
|
||||
|
||||
|
||||
if (beta.IsHot)
|
||||
{
|
||||
Assert.Equal(2.0, result.Value, precision: 6);
|
||||
@@ -88,7 +88,7 @@ public class BetaTests
|
||||
|
||||
beta.Reset();
|
||||
Assert.False(beta.IsHot);
|
||||
|
||||
|
||||
// Re-initialize
|
||||
beta.Update(100, 100);
|
||||
Assert.False(beta.IsHot);
|
||||
|
||||
@@ -25,13 +25,13 @@ public sealed class BetaValidationTests : IDisposable
|
||||
{
|
||||
// Generate Market Data (use existing Data)
|
||||
var marketQuotes = _data.Data;
|
||||
|
||||
|
||||
// Generate Asset Data correlated to Market
|
||||
// Asset Returns = 1.5 * Market Returns + Noise
|
||||
var assetQuotes = new List<TBar>();
|
||||
double assetPrice = 100;
|
||||
double targetBeta = 1.5;
|
||||
|
||||
|
||||
// Use GBM for noise generation (sigma=0.2 gives ~0.0006 per step noise which matches original random noise level)
|
||||
var noiseGbm = new GBM(startPrice: 100, mu: 0, sigma: 0.2, seed: 777);
|
||||
|
||||
@@ -40,13 +40,13 @@ public sealed class BetaValidationTests : IDisposable
|
||||
for (int i = 1; i < marketQuotes.Count; i++)
|
||||
{
|
||||
double marketReturn = (marketQuotes[i].Value - marketQuotes[i-1].Value) / marketQuotes[i-1].Value;
|
||||
|
||||
|
||||
// Get noise from GBM return
|
||||
var noiseBar = noiseGbm.Next();
|
||||
double noise = (noiseBar.Close - noiseBar.Open) / noiseBar.Open;
|
||||
|
||||
|
||||
double assetReturn = targetBeta * marketReturn + noise;
|
||||
|
||||
|
||||
assetPrice *= (1 + assetReturn);
|
||||
assetQuotes.Add(new TBar(marketQuotes[i].Time, assetPrice, assetPrice, assetPrice, assetPrice, 1000));
|
||||
}
|
||||
@@ -73,7 +73,7 @@ public sealed class BetaValidationTests : IDisposable
|
||||
// Skip warmup period. Skender Beta needs period returns, so period+1 prices?
|
||||
// Skender results align with input quotes.
|
||||
// First valid value should be at index 'period'.
|
||||
|
||||
|
||||
// We verify the last 100 values
|
||||
int count = qlBeta.Count;
|
||||
int skip = period + 5; // Safety margin
|
||||
@@ -82,7 +82,7 @@ public sealed class BetaValidationTests : IDisposable
|
||||
{
|
||||
double sk = (skenderBeta[i].Beta ?? 0);
|
||||
double ql = qlBeta[i];
|
||||
|
||||
|
||||
// Skender might return null/0 for warmup.
|
||||
if (Math.Abs(sk) > 1e-10)
|
||||
{
|
||||
|
||||
@@ -9,14 +9,14 @@ namespace QuanTAlib;
|
||||
/// <remarks>
|
||||
/// Beta is calculated as the covariance of the asset's returns and the market's returns,
|
||||
/// divided by the variance of the market's returns.
|
||||
///
|
||||
///
|
||||
/// Formula:
|
||||
/// Beta = Cov(Ra, Rm) / Var(Rm)
|
||||
///
|
||||
///
|
||||
/// Where:
|
||||
/// Ra = Return of Asset
|
||||
/// Rm = Return of Market
|
||||
///
|
||||
///
|
||||
/// This implementation uses the O(1) slope formula for linear regression of Ra vs Rm:
|
||||
/// Beta = (N * Sum(Ra*Rm) - Sum(Ra) * Sum(Rm)) / (N * Sum(Rm^2) - Sum(Rm)^2)
|
||||
/// </remarks>
|
||||
@@ -25,7 +25,7 @@ public sealed class Beta : AbstractBase
|
||||
{
|
||||
private readonly RingBuffer _returnsAsset;
|
||||
private readonly RingBuffer _returnsMarket;
|
||||
|
||||
|
||||
private double _prevAsset;
|
||||
private double _prevMarket;
|
||||
private double _p_prevAsset;
|
||||
|
||||
Reference in New Issue
Block a user