namespace QuanTAlib; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Data; using System.Linq; /* TSeries is the cornerstone of all QuanTAlib classes. TSeries is a single List of tuples (time, value) and contains several operators, casts, overloads and other helpers that simplify usage of library. Think of TSeries as an equivalent of Numpy array. - includes Length property (to mimic array's method) - includes publishing and subscribing methods that attach to events */ public class TSeriesEventArgs : EventArgs { public bool update { get; set; } } public class TSeries : List<(DateTime t, double v)> { public List t => this.Select(item => item.t).ToList(); public List v => this.Select(item => item.v).ToList(); public (DateTime t, double v) Last => this[^1]; public int Length => Count; public string Name { get; set; } public TSeries() { this.Name = "data"; } public TSeries(string Name) { this.Name = Name; } public virtual (DateTime t, double v) Add(double v, bool update = false) { var Value = (t: Count == 0 ? DateTime.Today : this[^1].t.AddDays(1), v); return Add(Value, update); } public virtual (DateTime t, double v) Add((DateTime t, double v) TValue, bool update = false) { if (update) { this[^1] = TValue; } else { base.Add(TValue); } OnEvent(update); return TValue; } public virtual (DateTime t, double v) Add(TSeries data) { foreach (var item in data) { Add(item, false); } return data.Last; } public void Sub(object source, TSeriesEventArgs e) { var data = (TSeries) source; if (data == null) { return; } foreach (var item in data) { Add(item, update: false); } } public delegate void NewEventHandler(object source, TSeriesEventArgs args); public event NewEventHandler Pub; protected virtual void OnEvent(bool update = false) { Pub?.Invoke(this, new TSeriesEventArgs {update = update}); } /// common helpers public static void BufferTrim(List buffer, double value, int period, bool update) { if (!update) { buffer.Add(value); if (buffer.Count > period && period > 0) { buffer.RemoveAt(0); } return; } buffer[^1] = value; } public virtual void Reset() { } }