Files
QuanTAlib/Calculations/ClassStructures/TSeries.cs
T

64 lines
2.0 KiB
C#
Raw Normal View History

2022-04-19 14:35:10 -07:00
namespace QuanTAlib;
using System;
2023-03-30 14:44:05 -07:00
using System.Collections.Generic;
using System.Collections.ObjectModel;
2023-04-17 09:32:00 -07:00
using System.Data;
2023-03-30 14:44:05 -07:00
using System.Linq;
2022-04-19 14:35:10 -07:00
2022-04-19 22:34:42 -07:00
/* <summary>
2023-04-17 09:32:00 -07:00
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.
2022-04-19 22:34:42 -07:00
Think of TSeries as an equivalent of Numpy array.
2023-04-17 09:32:00 -07:00
2022-04-19 22:34:42 -07:00
- includes Length property (to mimic array's method)
- includes publishing and subscribing methods that attach to events
</summary> */
2023-04-17 09:32:00 -07:00
public class TSeriesEventArgs : EventArgs{
public bool update { get; set; }
}
2023-03-30 14:44:05 -07:00
public class TSeries : List<(DateTime t, double v)> {
public static implicit operator (DateTime t, double v)(TSeries l) => l[^1];
public static implicit operator double(TSeries l) => l[^1].v;
public static implicit operator DateTime(TSeries l) => l[^1].t;
2023-04-17 09:32:00 -07:00
public List<DateTime> t => this.Select(item => item.t).ToList();
public List<double> v => this.Select(item => item.v).ToList();
public int Length => this.Count;
2023-03-30 14:44:05 -07:00
public TSeries Tail(int count = 10) {
var tailSeries = new TSeries();
tailSeries.AddRange(this.Skip(Math.Max(0, this.Count - count)).Take(count));
return tailSeries;
}
2023-04-17 09:32:00 -07:00
public (DateTime t, double v) Add((DateTime t, double v) TValue, bool update = false) {
2023-03-30 14:44:05 -07:00
if (update) { this[^1] = TValue; }
else { base.Add(TValue); }
OnEvent(update);
2023-04-17 09:32:00 -07:00
return TValue;
2023-03-30 14:44:05 -07:00
}
public void Add(DateTime t, double v, bool update = false) => this.Add((t, v), update);
2023-04-17 09:32:00 -07:00
public void Add(double v, bool update = false) => this.Add((DateTime.Now, v), update);
protected virtual void OnEvent(bool update = false) {
Pub?.Invoke(this, new TSeriesEventArgs { update = update });
}
2022-04-19 14:35:10 -07:00
2023-04-17 09:32:00 -07:00
public delegate void NewDataEventHandler(object source, TSeriesEventArgs args);
public event NewDataEventHandler Pub;
public void Sub(object source, TSeriesEventArgs e) {
TSeries ss = (TSeries)source;
if (ss.Count > 0) {
this.AddRange(ss);
}
else {
this.Add(ss[^1], e.update);
}
}
2022-04-19 14:35:10 -07:00
}