Files
QuanTAlib/Source/Statistics/BIAS_Series.cs
T

35 lines
992 B
C#
Raw Normal View History

2022-04-19 22:34:42 -07:00
namespace QuanTAlib;
using System;
2022-11-17 07:53:45 -08:00
using System.Linq;
2022-04-19 14:35:10 -07:00
2022-04-19 22:34:42 -07:00
/* <summary>
BIAS: Rate of change between the source and a moving average.
Bias is a statistical term which means a systematic deviation from the actual value.
2022-04-19 14:35:10 -07:00
2022-04-19 22:34:42 -07:00
BIAS = (close - SMA) / SMA
2022-04-19 14:35:10 -07:00
= (close / SMA) - 1
Sources:
2022-04-19 22:34:42 -07:00
https://en.wikipedia.org/wiki/Bias_of_an_estimator
2022-04-19 14:35:10 -07:00
2022-04-19 22:34:42 -07:00
</summary> */
2022-04-19 14:35:10 -07:00
public class BIAS_Series : Single_TSeries_Indicator
{
public BIAS_Series(TSeries source, int period, bool useNaN = false) : base(source, period, useNaN)
{
if (base._data.Count > 0) { base.Add(base._data); }
}
private readonly System.Collections.Generic.List<double> _buffer = new();
public override void Add((System.DateTime t, double v) TValue, bool update)
{
2022-11-17 07:53:45 -08:00
Add_Replace_Trim(_buffer, TValue.v, _p, update);
2022-04-19 14:35:10 -07:00
2022-11-17 07:53:45 -08:00
double _sma = _buffer.Average();
double _bias = (_buffer[_buffer.Count - 1] / ((_sma != 0) ? _sma : 1)) - 1;
2022-04-19 14:35:10 -07:00
2022-11-17 07:53:45 -08:00
base.Add((TValue.t, _bias), update, _NaN);
2022-04-19 14:35:10 -07:00
}
}