/// <summary> /// Crunches when any of <see cref="IUpdatable"/> are updated for every n <paramref name="interval"/>. /// </summary> /// <param name="name">Name of the cruncher for debugging purposes.</param> /// <param name="updatables">The updatables to observe and crunch.</param> /// <param name="interval">The interval for how many fires must any of <paramref name="updatables"/> trigger <see cref="IUpdatable.Updated"/> in order to trigger Concat's update event.</param> /// <param name="properties"> /// How many properties all of the <paramref name="updatables"/> emit. /// this can be less than their minimal properties. /// e.g. if <paramref name="updatables"/> emit <see cref="BarValue"/> (4 properties), selecting 1 will take only <see cref="BarValue.Close"/>. /// </param> /// <returns>A new cruncher configured.</returns> public static Concat OnEveryUpdate(IEnumerable <IUpdatable> updatables, int interval = 1, int properties = 1, string name = null) { if (interval <= 0) { throw new ArgumentOutOfRangeException(nameof(interval)); } // ReSharper disable once UseObjectOrCollectionInitializer var c = new Concat(); c.Name = name ?? "Concat"; c.Options = ConcatOptions.OnEveryUpdate; var obsing = c.observing = updatables.ToArray(); c.concatenating = c.observing.ToArray(); var len = c.length = c.concatenating.Length; var props = c.Properties = properties; var outputCount = c.outputCount = c.concatenating.Sum(upd => upd.OutputCount); c.signalCounter = null; var workingTarget = new DoubleArrayPinned2DManaged(outputCount, props); c.workingTarget = workingTarget; c.counter = interval; c.BindValues(); for (var i = 0; i < obsing.Length; i++) { IUpdatable srcUpdatable = obsing[i]; if (properties == 1) { if (interval == 1) { srcUpdatable.Updated += (time, updated) => { c.OnUpdated(time); }; } else { srcUpdatable.Updated += (time, updated) => { if (--c.counter <= 0) { c.OnUpdated(time); c.counter = interval; } }; srcUpdatable.Resetted += sender => { c.counter = interval; }; } } else { //case when values collected are multi-valued output (tradebar value) if (interval == 1) { srcUpdatable.Updated += (time, updated) => { c.OnUpdated(time); }; } else { srcUpdatable.Updated += (time, updated) => { if (--c.counter <= 0) { c.OnUpdated(time); c.counter = interval; } }; srcUpdatable.Resetted += sender => { c.counter = interval; }; } } } return(c); }
/// <summary> /// Crunches <paramref name="updatables"/> whenever <paramref name="crunchTriggers"/> is updated for n <paramref name="interval"/> times. /// </summary> /// <param name="name">Name of the cruncher for debugging purposes.</param> /// <param name="updatables">The updatables to observe and crunch.</param> /// <param name="crunchTriggers">The <see cref="IUpdatable"/>s to observe for fires of <see cref="IUpdatable.Updated"/>.</param> /// <param name="interval">The interval for how many fires must <paramref name="crunchTriggers"/> trigger <see cref="IUpdatable.Updated"/> in order to trigger Concat's update event.</param> /// <param name="properties"> /// How many properties all of the <paramref name="updatables"/> emit. /// this can be less than their minimal properties. /// e.g. if <paramref name="updatables"/> emit <see cref="BarValue"/> (4 properties), selecting 1 will take only <see cref="BarValue.Close"/>. /// </param> /// <param name="triggerMustBeReady">Does <paramref name="crunchTriggers"/> must be ready to trigger Concat's update event? By default </param> /// <returns>A new cruncher configured.</returns> public static Concat OnSpecificUpdate(IEnumerable <IUpdatable> updatables, IUpdatable[] crunchTriggers, int interval = 1, int properties = 1, string name = null, bool[] triggerMustBeReady = null) { if (interval <= 0) { throw new ArgumentOutOfRangeException(nameof(interval)); } // ReSharper disable once UseObjectOrCollectionInitializer var c = new Concat { Name = name ?? "Concat" }; c.Options = ConcatOptions.OnSpecificUpdated; c.observing = crunchTriggers; c.concatenating = updatables.ToArray(); var len = c.length = c.concatenating.Length; var props = c.Properties = properties; var outputCount = c.outputCount = c.concatenating.Sum(upd => upd.OutputCount); c.signalCounter = null; var workingTarget = new DoubleArrayPinned2DManaged(outputCount, props); c.workingTarget = workingTarget; c.counter = interval; c.BindValues(); for (var i = 0; i < crunchTriggers.Length; i++) { var onlyWhenReady = triggerMustBeReady[i]; var crunchTrigger = crunchTriggers[i]; if (interval == 1) { if (onlyWhenReady) { crunchTrigger.Updated += (time, updated) => { if (crunchTrigger.IsReady) { c.OnUpdated(time); } }; } else { crunchTrigger.Updated += (time, updated) => c.OnUpdated(time); } } else { if (onlyWhenReady) { crunchTrigger.Updated += (time, updated) => { if (!crunchTrigger.IsReady) { return; } if (--c.counter <= 0) { c.OnUpdated(time); c.counter = interval; } }; } else { crunchTrigger.Updated += (time, updated) => { if (--c.counter <= 0) { c.OnUpdated(time); c.counter = interval; } }; } } crunchTrigger.Resetted += _ => c.counter = interval; } return(c); }
/// <summary> /// Crunches when all <see cref="IUpdatable"/> were updated atleast once. /// </summary> /// <param name="name">Name of the cruncher for debugging purposes.</param> /// <param name="updatables">The updatables to observe and crunch.</param> /// <param name="properties"> /// How many properties all of the <paramref name="updatables"/> emit. /// this can be less than their minimal properties. /// e.g. if <paramref name="updatables"/> emit <see cref="BarValue"/> (4 properties), selecting 1 will take only <see cref="BarValue.Close"/>. /// </param> /// <returns>A new cruncher configured.</returns> public static Concat OnAllUpdatedOnce(IEnumerable <IUpdatable> updatables, int properties = 1, string name = null) { // ReSharper disable once UseObjectOrCollectionInitializer var c = new Concat(); c.Name = name ?? "Concat"; c.Options = ConcatOptions.OnAllUpdatedOnce; var obsing = c.observing = updatables.ToArray(); c.concatenating = c.observing.ToArray(); var len = c.length = c.concatenating.Length; var outputCount = c.outputCount = c.concatenating.Sum(upd => upd.OutputCount); var props = c.Properties = properties; var cntr = c.signalCounter = new bool[len]; var workingTarget = new DoubleArrayPinned2DManaged(outputCount, props); c.workingTarget = workingTarget; c.counter = len; //debug handle //bind the values to their repsective memory address. c.BindValues(); for (var i = 0; i < obsing.Length; i++) { IUpdatable srcUpdatable = obsing[i]; int updId = i; //case when values collected are indicator output (single value) if (properties == 1) { srcUpdatable.Updated += (time, updated) => { if (!cntr[updId]) { cntr[updId] = true; if (--c.counter <= 0) { c.OnUpdated(time); Array.Clear(cntr, 0, len); c.counter = len; } } }; srcUpdatable.Resetted += sender => { if (cntr[updId]) { c.counter++; cntr[updId] = false; } }; } else { //case when values collected are multi-valued output (tradebar value) srcUpdatable.Updated += (time, updated) => { if (!cntr[updId]) { cntr[updId] = true; if (--c.counter <= 0) { c.OnUpdated(time); Array.Clear(cntr, 0, len); c.counter = len; } } }; srcUpdatable.Resetted += sender => { if (cntr[updId]) { c.counter++; cntr[updId] = false; } }; } } return(c); }