|
| 1 | +namespace Skender.Stock.Indicators; |
| 2 | + |
| 3 | +// TUPLE OBSERVER and TUPLE PROVIDER (CHAIN STREAM) |
| 4 | + |
| 5 | +public abstract class ChainProvider |
| 6 | + : TupleObserver, IObservable<(DateTime Date, double Value)> |
| 7 | +{ |
| 8 | + // fields |
| 9 | + private readonly List<IObserver<(DateTime Date, double Value)>> observers; |
| 10 | + |
| 11 | + // initialize |
| 12 | + protected ChainProvider() |
| 13 | + { |
| 14 | + observers = new(); |
| 15 | + ProtectedChain = new(); |
| 16 | + Warmup = true; |
| 17 | + } |
| 18 | + |
| 19 | + // properties |
| 20 | + internal IEnumerable<(DateTime Date, double Value)> Output => ProtectedChain; |
| 21 | + |
| 22 | + internal List<(DateTime Date, double Value)> ProtectedChain { get; set; } |
| 23 | + |
| 24 | + private int OverflowCount { get; set; } |
| 25 | + |
| 26 | + private bool Warmup { get; set; } |
| 27 | + |
| 28 | + // METHODS |
| 29 | + |
| 30 | + // subscribe observer |
| 31 | + public IDisposable Subscribe(IObserver<(DateTime Date, double Value)> observer) |
| 32 | + { |
| 33 | + if (!observers.Contains(observer)) |
| 34 | + { |
| 35 | + observers.Add(observer); |
| 36 | + } |
| 37 | + |
| 38 | + return new Unsubscriber(observers, observer); |
| 39 | + } |
| 40 | + |
| 41 | + // close all observations |
| 42 | + public void EndTransmission() |
| 43 | + { |
| 44 | + foreach (IObserver<(DateTime Date, double Value)> observer in observers.ToArray()) |
| 45 | + { |
| 46 | + if (observers.Contains(observer)) |
| 47 | + { |
| 48 | + observer.OnCompleted(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + observers.Clear(); |
| 53 | + } |
| 54 | + |
| 55 | + // add one |
| 56 | + internal void SendToChain<TResult>(TResult result) |
| 57 | + where TResult : IReusableResult |
| 58 | + { |
| 59 | + // candidate result |
| 60 | + (DateTime Date, double Value) r = new(result.Date, result.Value.Null2NaN()); |
| 61 | + |
| 62 | + int length = ProtectedChain.Count; |
| 63 | + |
| 64 | + // initialize |
| 65 | + if (length == 0 && result.Value != null) |
| 66 | + { |
| 67 | + // add new tuple |
| 68 | + ProtectedChain.Add(r); |
| 69 | + Warmup = false; |
| 70 | + |
| 71 | + // notify observers |
| 72 | + NotifyObservers(r); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + // do not proceed until first non-null Value recieved |
| 77 | + if (Warmup && result.Value == null) |
| 78 | + { |
| 79 | + return; |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + Warmup = false; |
| 84 | + } |
| 85 | + |
| 86 | + (DateTime lastDate, _) = ProtectedChain[length - 1]; |
| 87 | + |
| 88 | + // add tuple |
| 89 | + if (r.Date > lastDate) |
| 90 | + { |
| 91 | + // add new tuple |
| 92 | + ProtectedChain.Add(r); |
| 93 | + |
| 94 | + // notify observers |
| 95 | + NotifyObservers(r); |
| 96 | + } |
| 97 | + |
| 98 | + // same date or tuple recieved |
| 99 | + else if (r.Date <= lastDate) |
| 100 | + { |
| 101 | + // check for overflow condition |
| 102 | + // where same tuple continues (possible circular condition) |
| 103 | + if (r.Date == lastDate) |
| 104 | + { |
| 105 | + OverflowCount++; |
| 106 | + |
| 107 | + if (OverflowCount > 100) |
| 108 | + { |
| 109 | + string msg = "A repeated Chain update exceeded the 100 attempt threshold. " |
| 110 | + + "Check and remove circular chains or check your Chain provider."; |
| 111 | + |
| 112 | + EndTransmission(); |
| 113 | + |
| 114 | + throw new OverflowException(msg); |
| 115 | + } |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + OverflowCount = 0; |
| 120 | + } |
| 121 | + |
| 122 | + // seek old tuple |
| 123 | + int foundIndex = ProtectedChain |
| 124 | + .FindIndex(x => x.Date == r.Date); |
| 125 | + |
| 126 | + // found |
| 127 | + if (foundIndex >= 0) |
| 128 | + { |
| 129 | + ProtectedChain[foundIndex] = r; |
| 130 | + } |
| 131 | + |
| 132 | + // add missing tuple |
| 133 | + else |
| 134 | + { |
| 135 | + ProtectedChain.Add(r); |
| 136 | + |
| 137 | + // re-sort cache |
| 138 | + ProtectedChain = ProtectedChain |
| 139 | + .ToSortedList(); |
| 140 | + } |
| 141 | + |
| 142 | + // let observer handle old + duplicates |
| 143 | + NotifyObservers(r); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // add many |
| 148 | + internal void SendToChain<TResult>(IEnumerable<TResult> results) |
| 149 | + where TResult : IReusableResult |
| 150 | + { |
| 151 | + List<TResult> added = results |
| 152 | + .ToSortedList(); |
| 153 | + |
| 154 | + for (int i = 0; i < added.Count; i++) |
| 155 | + { |
| 156 | + SendToChain(added[i]); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + // notify observers |
| 161 | + private void NotifyObservers((DateTime Date, double Value) tuple) |
| 162 | + { |
| 163 | + List<IObserver<(DateTime Date, double Value)>> obsList = observers.ToList(); |
| 164 | + |
| 165 | + for (int i = 0; i < obsList.Count; i++) |
| 166 | + { |
| 167 | + IObserver<(DateTime Date, double Value)> obs = obsList[i]; |
| 168 | + obs.OnNext(tuple); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + // unsubscriber |
| 173 | + private class Unsubscriber : IDisposable |
| 174 | + { |
| 175 | + private readonly List<IObserver<(DateTime Date, double Value)>> observers; |
| 176 | + private readonly IObserver<(DateTime Date, double Value)> observer; |
| 177 | + |
| 178 | + // identify and save observer |
| 179 | + public Unsubscriber(List<IObserver<(DateTime Date, double Value)>> observers, IObserver<(DateTime Date, double Value)> observer) |
| 180 | + { |
| 181 | + this.observers = observers; |
| 182 | + this.observer = observer; |
| 183 | + } |
| 184 | + |
| 185 | + // remove single observer |
| 186 | + public void Dispose() |
| 187 | + { |
| 188 | + if (observer != null && observers.Contains(observer)) |
| 189 | + { |
| 190 | + observers.Remove(observer); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments