CMO - Chande Momentum Oscillator

This commit is contained in:
Miha Kralj
2024-10-22 05:46:48 -07:00
parent e3d7cd9896
commit 7748bdd101
7 changed files with 163 additions and 5 deletions
+1 -1
View File
@@ -345,7 +345,7 @@ public class SkenderTests
var atrValues = quotes.GetAtr(lookbackPeriods: period).Select(i => i.Atr.Null2NaN()!);
const int AdditionalPeriods = 500;
for (int i = QL.Length - 1; i > period + AdditionalPeriods; i--)
for (int i = QL.Length - 1; i > 1000 + AdditionalPeriods; i--)
{
Assert.InRange(atrValues.ElementAt(i) - QL[i].Value, -range, range);
}
+1 -2
View File
@@ -19,7 +19,7 @@
|CURVATURE - Rate of Change in Direction or Slope|`Curvature`||||
|ENTROPY - Measure of Uncertainty or Disorder|`Entropy`||||
|KURTOSIS - Measure of Tails/Peakedness|`Kurtosis`||||
|HUBER - Huber Loss|||||
|HUBER - Huber Loss|`Huberloss`||||
|MAX - Maximum with exponential decay|`Max`||||
|MAE - Mean Absolute Error|`Mae`||||
|MAPD - Mean Absolute Percentage Deviation|`Mapd`||||
@@ -80,7 +80,6 @@
|SMMA - Smoothed Moving Average|`Smma`|`✔️`|||
|SSF - Ehler's Super Smoother Filter|||||
|SUPERTREND - Supertrend||`✔️`|||
|SWMA - Symmetric Weighted Moving Average|||||
|T3 - Tillson T3 Moving Average|`T3`|`✔️`|`✔️`||
|TEMA - Triple EMA Average|`Tema`|`✔️`|`✔️`|`✔️`|
|TRIMA - Triangular Moving Average|`Trima`|`✔️`||`✔️`|
+1 -1
View File
@@ -97,7 +97,7 @@
--table-row-odd-background: var(--mono-shade2);
/* Layout */
--content-max-width: 55em;
--content-max-width: 100em;
/* Cover */
--cover-margin: 0 auto;
+78
View File
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
namespace QuanTAlib;
/// <summary>
/// Represents a Chande Momentum Oscillator (CMO) calculator.
/// </summary>
public class Cmo : AbstractBase
{
private readonly int _period;
private readonly CircularBuffer _diffBuffer;
private readonly CircularBuffer _sumH;
private readonly CircularBuffer _sumL;
private double _prevValue, _p_prevValue;
public Cmo(int period)
{
if (period < 1)
throw new ArgumentOutOfRangeException(nameof(period));
_period = period;
_diffBuffer = new(period);
_sumH = new(period);
_sumL = new(period);
WarmupPeriod = period+1;
Name = $"CMO({period})";
}
protected override void ManageState(bool isNew)
{
if (isNew)
{
_index++;
_p_prevValue = _prevValue;
}
else
{
_prevValue = _p_prevValue;
}
}
protected override double Calculation()
{
ManageState(Input.IsNew);
if (_index == 0)
{
_prevValue = Input.Value;
}
double diff = Input.Value - _prevValue;
_prevValue = Input.Value;
if (diff > 0)
{
_sumH.Add(diff, Input.IsNew);
_sumL.Add(0, Input.IsNew);
}
else
{
_sumH.Add(0, Input.IsNew);
_sumL.Add(-diff, Input.IsNew);
}
// Calculate sums for the specified period only
double sumH = _sumH.Sum();
double sumL = _sumL.Sum();
double divisor = sumH + sumL;
return (Math.Abs(divisor) > double.Epsilon) ?
100.0 * ((sumH - sumL) / divisor) :
0.0;
}
}
+1
View File
@@ -41,6 +41,7 @@ public class AtrIndicator : Indicator, IWatchlistIndicator
AtrSeries!.SetMarker(0, Color.Transparent); //OnPaintChart draws the line, hidden here
}
#pragma warning disable CA1416 // Validate platform compatibility
public override string ShortName => $"ATR ({Periods})";
+72
View File
@@ -0,0 +1,72 @@
using System.Drawing;
using TradingPlatform.BusinessLayer;
namespace QuanTAlib;
public class CmoIndicator : Indicator, IWatchlistIndicator
{
[InputParameter("Periods", sortIndex: 1, 1, 2000, 1, 0)]
public int Periods { get; set; } = 9;
[InputParameter("Data source", sortIndex: 5, variants: [
"Open", SourceType.Open,
"High", SourceType.High,
"Low", SourceType.Low,
"Close", SourceType.Close,
"HL/2 (Median)", SourceType.HL2,
"OC/2 (Midpoint)", SourceType.OC2,
"OHL/3 (Mean)", SourceType.OHL3,
"HLC/3 (Typical)", SourceType.HLC3,
"OHLC/4 (Average)", SourceType.OHLC4,
"HLCC/4 (Weighted)", SourceType.HLCC4
])]
public SourceType Source { get; set; } = SourceType.Close;
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private Cmo? cmo;
protected string? SourceName;
protected LineSeries? CmoSeries;
public int MinHistoryDepths => Periods + 1;
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public CmoIndicator()
{
Name = "CMO - Chande Momentum Oscillator";
Description = "Measures the momentum of price changes using the difference between the sum of recent gains and the sum of recent losses.";
SeparateWindow = true;
SourceName = Source.ToString();
CmoSeries = new($"CMO {Periods}", Color.Blue, 2, LineStyle.Solid);
AddLineSeries(CmoSeries);
}
protected override void OnInit()
{
cmo = new Cmo(Periods);
base.OnInit();
}
protected override void OnUpdate(UpdateArgs args)
{
TValue input = this.GetInputValue(args, Source);
cmo!.Calc(input);
CmoSeries!.SetValue(cmo.Value);
CmoSeries!.SetMarker(0, Color.Transparent); //OnPaintChart draws the line, hidden here
}
public override string ShortName => $"CMO ({Periods}:{SourceName})";
#pragma warning disable CA1416 // Validate platform compatibility
public override void OnPaintChart(PaintChartEventArgs args)
{
base.OnPaintChart(args);
this.PaintHLine(args, 0, new Pen(Color.DarkGray, width: 1));
this.PaintHLine(args, 50, new Pen(Color.DarkRed, width: 1));
this.PaintHLine(args, -50, new Pen(Color.DarkGreen, width: 1));
this.PaintSmoothCurve(args, CmoSeries!, cmo!.WarmupPeriod, showColdValues: ShowColdValues, tension: 0.2);
}
}
+9 -1
View File
@@ -53,7 +53,15 @@ public class FlowIndicator : Indicator, IWatchlistIndicator
int barX2 = barX1 + width;
int barY2 = (int)converter.GetChartY(this.HistoricalData.Close(i));
using (Brush transparentBrush = new SolidBrush(Color.FromArgb(250, 70, 70, 70)))
{
gr.FillRectangle(transparentBrush, barX1, barYHigh - 1, CurrentChart.BarsWidth, Math.Abs(barYLow - barYHigh) + 2);
}
using (Brush circ = new SolidBrush(Color.FromArgb(100, 255, 255, 0)))
{
int size = 3;
gr.FillEllipse(circ, barX1 - size, barY1 - size, 2 * size, 2 * size);
gr.FillEllipse(circ, barX2 - size, barY2 - size, 2 * size, 2 * size);
}
using (Pen defaultPen = new(Color.Yellow, 3))
{
defaultPen.StartCap = LineCap.Round;
@@ -69,9 +77,9 @@ public class FlowIndicator : Indicator, IWatchlistIndicator
dottedPen.DashStyle = DashStyle.Dot;
gr.DrawLine(dottedPen, barX2, barY2, barX0, barY0);
}
}
}
}