mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-19 19:18:05 +00:00
Adr, Ap, Atrp, Atrs, Vp, Vwap, Vwma
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// VF: Volume Force
|
||||
/// A volume-based indicator that measures the strength of volume relative to price
|
||||
/// movement. It helps identify whether volume is supporting or contradicting the
|
||||
/// current price trend.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The VF calculation process:
|
||||
/// 1. Calculate price change
|
||||
/// 2. Calculate volume force as volume * price change
|
||||
/// 3. Optionally smooth the result with EMA
|
||||
///
|
||||
/// Key characteristics:
|
||||
/// - Volume-weighted measure
|
||||
/// - Trend strength indicator
|
||||
/// - No upper/lower bounds
|
||||
/// - Raw and smoothed versions
|
||||
/// - Divergence indicator
|
||||
///
|
||||
/// Formula:
|
||||
/// VF = Volume * (Close - Close[1])
|
||||
/// Smoothed VF = EMA(VF, period)
|
||||
///
|
||||
/// Market Applications:
|
||||
/// - Volume analysis
|
||||
/// - Trend confirmation
|
||||
/// - Price/volume divergence
|
||||
/// - Market participation
|
||||
/// - Momentum confirmation
|
||||
///
|
||||
/// Note: Higher values indicate stronger volume force
|
||||
/// </remarks>
|
||||
|
||||
[SkipLocalsInit]
|
||||
public sealed class Vf : AbstractBase
|
||||
{
|
||||
private readonly Ema _ema;
|
||||
private double _prevClose;
|
||||
private double _p_prevClose;
|
||||
private const int DefaultPeriod = 13;
|
||||
|
||||
/// <param name="period">The smoothing period for EMA calculation (default 13).</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 1.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Vf(int period = DefaultPeriod)
|
||||
{
|
||||
if (period < 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(period));
|
||||
|
||||
_ema = new(period);
|
||||
WarmupPeriod = period + 1;
|
||||
Name = $"VF({period})";
|
||||
}
|
||||
|
||||
/// <param name="source">The data source object that publishes updates.</param>
|
||||
/// <param name="period">The smoothing period for EMA calculation.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Vf(object source, int period = DefaultPeriod) : this(period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new BarSignal(Sub));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_ema.Init();
|
||||
_prevClose = double.NaN;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_index++;
|
||||
_p_prevClose = _prevClose;
|
||||
}
|
||||
else
|
||||
{
|
||||
_prevClose = _p_prevClose;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(BarInput.IsNew);
|
||||
|
||||
if (_index == 1)
|
||||
{
|
||||
_prevClose = BarInput.Close;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Calculate raw volume force
|
||||
double priceChange = BarInput.Close - _prevClose;
|
||||
double volumeForce = BarInput.Volume * priceChange;
|
||||
|
||||
// Update previous close
|
||||
_prevClose = BarInput.Close;
|
||||
|
||||
// Apply EMA smoothing
|
||||
return _ema.Calc(volumeForce, BarInput.IsNew);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// VP: Volume Profile
|
||||
/// A volume-based indicator that analyzes volume distribution across price levels.
|
||||
/// It helps identify significant price levels where most trading activity occurs.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The VP calculation process:
|
||||
/// 1. Track volume at each price level within a period
|
||||
/// 2. Calculate Point of Control (POC) - price with highest volume
|
||||
/// 3. Calculate Value Area (70% of total volume)
|
||||
///
|
||||
/// Key characteristics:
|
||||
/// - Price level analysis
|
||||
/// - Volume distribution
|
||||
/// - Support/resistance identification
|
||||
/// - Trading activity concentration
|
||||
/// - Market structure analysis
|
||||
///
|
||||
/// Formula:
|
||||
/// VP = Σ Volume at each price level
|
||||
/// POC = Price level with max volume
|
||||
/// Value Area = Price range containing 70% of volume
|
||||
///
|
||||
/// Market Applications:
|
||||
/// - Support/resistance levels
|
||||
/// - Market structure analysis
|
||||
/// - Trading activity patterns
|
||||
/// - Price level significance
|
||||
/// - Volume concentration
|
||||
///
|
||||
/// Note: Returns Point of Control (price level with highest volume)
|
||||
/// </remarks>
|
||||
|
||||
[SkipLocalsInit]
|
||||
public sealed class Vp : AbstractBase
|
||||
{
|
||||
private readonly CircularBuffer _volumes;
|
||||
private readonly CircularBuffer _prices;
|
||||
private const int DefaultPeriod = 14;
|
||||
|
||||
/// <param name="period">The number of periods to analyze volume distribution (default 14).</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 1.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Vp(int period = DefaultPeriod)
|
||||
{
|
||||
if (period < 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(period));
|
||||
|
||||
_volumes = new(period);
|
||||
_prices = new(period);
|
||||
WarmupPeriod = period;
|
||||
Name = $"VP({period})";
|
||||
}
|
||||
|
||||
/// <param name="source">The data source object that publishes updates.</param>
|
||||
/// <param name="period">The number of periods to analyze volume distribution.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Vp(object source, int period = DefaultPeriod) : this(period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new BarSignal(Sub));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
_index++;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
private static int FindMaxVolumeIndex(CircularBuffer volumes)
|
||||
{
|
||||
int maxIndex = 0;
|
||||
double maxVolume = volumes[0];
|
||||
|
||||
for (int i = 1; i < volumes.Count; i++)
|
||||
{
|
||||
if (volumes[i] > maxVolume)
|
||||
{
|
||||
maxVolume = volumes[i];
|
||||
maxIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
return maxIndex;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(BarInput.IsNew);
|
||||
|
||||
// Store volume and price
|
||||
_volumes.Add(BarInput.Volume, BarInput.IsNew);
|
||||
_prices.Add(BarInput.Close, BarInput.IsNew);
|
||||
|
||||
// Find price level with highest volume (Point of Control)
|
||||
int pocIndex = FindMaxVolumeIndex(_volumes);
|
||||
return _prices[pocIndex];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// VWAP: Volume Weighted Average Price
|
||||
/// A trading benchmark that shows the ratio of the value traded to total volume
|
||||
/// traded over a specific period. VWAP equals the dollar value of all trading
|
||||
/// periods divided by the total trading volume for the current day.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The VWAP calculation process:
|
||||
/// 1. Calculate typical price for each period
|
||||
/// 2. Multiply typical price by volume
|
||||
/// 3. Calculate cumulative values
|
||||
/// 4. Divide cumulative (price * volume) by cumulative volume
|
||||
///
|
||||
/// Key characteristics:
|
||||
/// - Intraday trading benchmark
|
||||
/// - Volume-weighted measure
|
||||
/// - Institutional trading reference
|
||||
/// - Price momentum indicator
|
||||
/// - Trading efficiency measure
|
||||
///
|
||||
/// Formula:
|
||||
/// VWAP = Σ(Price * Volume) / ΣVolume
|
||||
/// where Price = (High + Low + Close)/3
|
||||
///
|
||||
/// Market Applications:
|
||||
/// - Best execution analysis
|
||||
/// - Trading algorithms
|
||||
/// - Price momentum
|
||||
/// - Market impact analysis
|
||||
/// - Order timing
|
||||
///
|
||||
/// Sources:
|
||||
/// https://www.investopedia.com/terms/v/vwap.asp
|
||||
///
|
||||
/// Note: Commonly used by institutional traders
|
||||
/// </remarks>
|
||||
|
||||
[SkipLocalsInit]
|
||||
public sealed class Vwap : AbstractBase
|
||||
{
|
||||
private double _cumulativeTPV; // Cumulative (Typical Price * Volume)
|
||||
private double _cumulativeVolume;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Vwap()
|
||||
{
|
||||
WarmupPeriod = 1;
|
||||
Name = "VWAP";
|
||||
Init();
|
||||
}
|
||||
|
||||
/// <param name="source">The data source object that publishes updates.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Vwap(object source) : this()
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new BarSignal(Sub));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_cumulativeTPV = 0;
|
||||
_cumulativeVolume = 0;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
_index++;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(BarInput.IsNew);
|
||||
|
||||
// Update cumulative values only for new bars
|
||||
if (BarInput.IsNew)
|
||||
{
|
||||
_cumulativeTPV += BarInput.HLC3 * BarInput.Volume;
|
||||
_cumulativeVolume += BarInput.Volume;
|
||||
}
|
||||
|
||||
// Calculate VWAP
|
||||
return _cumulativeVolume > 0 ? _cumulativeTPV / _cumulativeVolume : BarInput.HLC3;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// VWMA: Volume Weighted Moving Average
|
||||
/// A technical indicator that combines price and volume to show the average price
|
||||
/// weighted by volume over a period. It gives more weight to prices with higher
|
||||
/// volume, making it more responsive to high-volume price movements.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The VWMA calculation process:
|
||||
/// 1. Multiply price by volume for each period
|
||||
/// 2. Sum (price * volume) over the period
|
||||
/// 3. Sum volume over the period
|
||||
/// 4. Divide sums to get weighted average
|
||||
///
|
||||
/// Key characteristics:
|
||||
/// - Volume-sensitive average
|
||||
/// - Trend indicator
|
||||
/// - Support/resistance levels
|
||||
/// - Price momentum
|
||||
/// - Volume emphasis
|
||||
///
|
||||
/// Formula:
|
||||
/// VWMA = Σ(Price * Volume) / ΣVolume
|
||||
/// where sums are taken over the specified period
|
||||
///
|
||||
/// Market Applications:
|
||||
/// - Trend identification
|
||||
/// - Support/resistance levels
|
||||
/// - Volume analysis
|
||||
/// - Price momentum
|
||||
/// - Trading signals
|
||||
///
|
||||
/// Note: More responsive to high-volume price movements
|
||||
/// </remarks>
|
||||
|
||||
[SkipLocalsInit]
|
||||
public sealed class Vwma : AbstractBase
|
||||
{
|
||||
private readonly CircularBuffer _priceVolume;
|
||||
private readonly CircularBuffer _volume;
|
||||
private const int DefaultPeriod = 20;
|
||||
|
||||
/// <param name="period">The number of periods for VWMA calculation (default 20).</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 1.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Vwma(int period = DefaultPeriod)
|
||||
{
|
||||
if (period < 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(period));
|
||||
|
||||
_priceVolume = new(period);
|
||||
_volume = new(period);
|
||||
WarmupPeriod = period;
|
||||
Name = $"VWMA({period})";
|
||||
}
|
||||
|
||||
/// <param name="source">The data source object that publishes updates.</param>
|
||||
/// <param name="period">The number of periods for VWMA calculation.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Vwma(object source, int period = DefaultPeriod) : this(period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new BarSignal(Sub));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
_index++;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(BarInput.IsNew);
|
||||
|
||||
// Calculate and store price * volume
|
||||
double priceVolume = BarInput.Close * BarInput.Volume;
|
||||
_priceVolume.Add(priceVolume, BarInput.IsNew);
|
||||
_volume.Add(BarInput.Volume, BarInput.IsNew);
|
||||
|
||||
// Calculate sums
|
||||
double sumPriceVolume = _priceVolume.Sum();
|
||||
double sumVolume = _volume.Sum();
|
||||
|
||||
// Calculate VWMA
|
||||
return sumVolume > 0 ? sumPriceVolume / sumVolume : BarInput.Close;
|
||||
}
|
||||
}
|
||||
+5
-5
@@ -1,5 +1,5 @@
|
||||
# Volume indicators
|
||||
Done: 15, Todo: 3
|
||||
Done: 19, Todo: 0
|
||||
|
||||
✔️ ADL - Chaikin Accumulation Distribution Line
|
||||
✔️ ADOSC - Chaikin Accumulation Distribution Oscillator
|
||||
@@ -16,7 +16,7 @@ Done: 15, Todo: 3
|
||||
✔️ PVR - Price Volume Rank
|
||||
✔️ PVT - Price Volume Trend
|
||||
✔️ TVI - Trade Volume Index
|
||||
VF - Volume Force
|
||||
VP - Volume Profile
|
||||
VWAP - Volume Weighted Average Price
|
||||
VWMA - Volume Weighted Moving Average
|
||||
✔️ VF - Volume Force
|
||||
✔️ VP - Volume Profile
|
||||
✔️ VWAP - Volume Weighted Average Price
|
||||
✔️ VWMA - Volume Weighted Moving Average
|
||||
|
||||
Reference in New Issue
Block a user