Files
QuanTAlib/archive/Calculations/_Updated/TRIMA_Series.cs
T

84 lines
2.9 KiB
C#
Raw Normal View History

2024-09-24 16:41:26 -07:00
namespace QuanTAlib;
using System;
using System.Collections.Generic;
/* <summary>
TRIMA: Triangular Moving Average
A weighted moving average where the shape of the weights are triangular and the greatest
weight is in the middle of the period,
Sources:
https://www.tradingtechnologies.com/help/x-study/technical-indicator-definitions/triangular-moving-average-trima/
Remark:
trima = sma(sma(signal, n/2), n/2)
</summary> */
2024-10-06 07:24:57 +00:00
public class TRIMA_Series : TSeries {
2024-09-24 16:41:26 -07:00
private readonly int _p1a, _p1b;
private readonly SMA_Series sma, trima;
protected readonly int _period;
protected readonly bool _NaN;
protected readonly TSeries _data;
//core constructors
2024-10-06 07:24:57 +00:00
public TRIMA_Series(int period, bool useNaN) {
2024-09-24 16:41:26 -07:00
_period = period;
_NaN = useNaN;
Name = $"xMA({period})";
_p1a = (int)Math.Floor((period * 0.5) + 1);
_p1b = (int)Math.Ceiling(0.5 * period);
sma = new(_p1a);
trima = new(_p1b);
}
2024-10-06 07:24:57 +00:00
public TRIMA_Series(TSeries source, int period, bool useNaN) : this(period, useNaN) {
2024-09-24 16:41:26 -07:00
_data = source;
Name = Name.Substring(0, Name.IndexOf(")")) + $", {(string.IsNullOrEmpty(_data.Name) ? "data" : _data.Name)})";
_data.Pub += Sub;
Add(_data);
}
public TRIMA_Series() : this(period: 0, useNaN: false) { }
public TRIMA_Series(int period) : this(period: period, useNaN: false) { }
public TRIMA_Series(TBars source) : this(source.Close, 0, false) { }
public TRIMA_Series(TBars source, int period) : this(source.Close, period, false) { }
public TRIMA_Series(TBars source, int period, bool useNaN) : this(source.Close, period, useNaN) { }
public TRIMA_Series(TSeries source) : this(source, 0, false) { }
public TRIMA_Series(TSeries source, int period) : this(source: source, period: period, useNaN: false) { }
//////////////////
// core Add() algo
2024-10-06 07:24:57 +00:00
public override (DateTime t, double v) Add((DateTime t, double v) TValue, bool update = false) {
if (double.IsNaN(TValue.v)) {
2024-09-24 16:41:26 -07:00
return base.Add((TValue.t, Double.NaN), update);
}
var _sma = sma.Add(TValue, update);
var _trima = trima.Add(_sma, update);
var res = (_trima.t, Count < _period - 1 && _NaN ? double.NaN : _trima.v);
return base.Add(res, update);
}
2024-10-06 07:24:57 +00:00
public override (DateTime t, double v) Add(TSeries data) {
2024-09-24 16:41:26 -07:00
if (data == null) { return (DateTime.Today, Double.NaN); }
foreach (var item in data) { Add(item, false); }
return _data.Last;
}
2024-10-06 07:24:57 +00:00
public (DateTime t, double v) Add(bool update) {
2024-09-24 16:41:26 -07:00
return this.Add(TValue: _data.Last, update: update);
}
2024-10-06 07:24:57 +00:00
public (DateTime t, double v) Add() {
2024-09-24 16:41:26 -07:00
return Add(TValue: _data.Last, update: false);
}
2024-10-06 07:24:57 +00:00
private new void Sub(object source, TSeriesEventArgs e) {
2024-09-24 16:41:26 -07:00
Add(TValue: _data.Last, update: e.update);
}
//reset calculation
2024-10-06 07:24:57 +00:00
public override void Reset() {
2024-09-24 16:41:26 -07:00
sma.Reset();
trima.Reset();
}
}