-
Notifications
You must be signed in to change notification settings - Fork 265
feat: Add StrategyGroup with back-pair alignment
#1899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
12
commits into
v3
Choose a base branch
from
copilot/implement-strategyhub-container
base: v3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ca3d955
Initial plan
Copilot 8bddc2c
feat: add strategy hub helper for streaming indicators
Copilot 5ab67c1
Merge branch 'v3' into copilot/implement-strategyhub-container
DaveSkender 2004d95
manual calcs for golden cross
DaveSkender d50f6cd
Merge branch 'v3' into copilot/implement-strategyhub-container
DaveSkender a95fbf7
feat: Add SSE server and Golden Cross strategy simulation (#1901)
Copilot 04aa08e
refactor: replace strategy hub with aligned strategy group
Copilot fb79520
test: clarify price source in strategy group example
Copilot c9a53f7
docs: add xml docs for strategy group helpers
Copilot d8a5e95
fix: align back pairs across hubs and update simulation
Copilot 57a9049
Merge branch 'v3' into copilot/implement-strategyhub-container
DaveSkender 989cc4f
Refactor StrategyGroup and utilities into separate files
DaveSkender File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| namespace Skender.Stock.Indicators; | ||
|
|
||
| /// <summary> | ||
| /// Represents the previous and current results for a hub. | ||
| /// </summary> | ||
| public readonly record struct BackPair<T>(T Previous, T Current) | ||
| where T : ISeries; | ||
|
|
||
| internal static class StrategyGroupUtilities | ||
| { | ||
| /// <summary> | ||
| /// Attempts to retrieve the last two results from the specified stream observable as a back pair. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// If the stream observable contains fewer than two results, the method returns | ||
| /// <see langword="false"/> and <paramref name="pair"/> is set to its default value. | ||
| /// </remarks> | ||
| /// <typeparam name="TResult"> | ||
| /// The type of series elements contained in the stream observable. Must implement <see cref="ISeries"/>. | ||
| /// </typeparam> | ||
| /// <param name="hub"> | ||
| /// The stream observable from which to retrieve the last two results. | ||
| /// </param> | ||
| /// <param name="pair"> | ||
| /// When this method returns, contains a <see cref="BackPair{TResult}"/> representing | ||
| /// the last two results if available; otherwise, the default value. | ||
| /// </param> | ||
| /// <param name="offset">Offset periods for <see cref="BackPair{T}.Previous"/>. Default is 1.</param> | ||
| /// <returns> | ||
| /// <see langword="true"/> if the stream observable contains at least two results and | ||
| /// the back pair was retrieved; otherwise, <see langword="false"/>. | ||
| /// </returns> | ||
| internal static bool TryGetBackPair<TResult>( | ||
| this IStreamObservable<TResult> hub, | ||
| out BackPair<TResult> pair, | ||
| int offset = 1) | ||
| where TResult : ISeries | ||
| { | ||
| IReadOnlyList<TResult> cache = hub.Results; | ||
| int n = 1 - offset; | ||
|
|
||
| if (cache.Count < n) | ||
| { | ||
| pair = default; | ||
| return false; | ||
| } | ||
|
|
||
| pair = new(cache[^n], cache[^1]); | ||
| return true; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Determines whether a set of <see cref="BackPair{T}"/> instances are temporally aligned | ||
| /// by comparing their current and previous | ||
| /// timestamps. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Use this method to verify that two series pairs are synchronized at both their current and | ||
| /// previous positions. This is useful when processing time-aligned data from multiple sources. | ||
| /// </remarks> | ||
| /// <typeparam name="T1">The type of the 1st series, which must implement the <see cref="ISeries"/> interface.</typeparam> | ||
| /// <typeparam name="T2">The type of the 2nd series, which must implement the <see cref="ISeries"/> interface.</typeparam> | ||
| /// <param name="pair1">The 1st <see cref="BackPair{T}"/> instance to compare.</param> | ||
| /// <param name="pair2">The 2nd <see cref="BackPair{T}"/> instance to compare.</param> | ||
| /// <returns> | ||
| /// <see langword="true"/> if all pairs current and previous timestamps are equal, otherwise, <see langword="false"/>. | ||
| /// </returns> | ||
| internal static bool AreAligned<T1, T2>( | ||
| BackPair<T1> pair1, | ||
| BackPair<T2> pair2) | ||
| where T1 : ISeries | ||
| where T2 : ISeries | ||
| => pair1.Current.Timestamp == pair2.Current.Timestamp | ||
| && pair1.Previous.Timestamp == pair2.Previous.Timestamp; | ||
|
|
||
| /// <inheritdoc cref="AreAligned{T1, T2}(BackPair{T1}, BackPair{T2})" /> | ||
| /// <typeparam name="T1">The type of the 1st series, which must implement the <see cref="ISeries"/> interface.</typeparam> | ||
| /// <typeparam name="T2">The type of the 2nd series, which must implement the <see cref="ISeries"/> interface.</typeparam> | ||
| /// <typeparam name="T3">The type of the 3rd series, which must implement the <see cref="ISeries"/> interface.</typeparam> | ||
| /// <param name="pair1">The 1st <see cref="BackPair{T}"/> instance to compare.</param> | ||
| /// <param name="pair2">The 2nd <see cref="BackPair{T}"/> instance to compare.</param> | ||
| /// <param name="pair3">The 3rd <see cref="BackPair{T}"/> instance to compare.</param> | ||
|
|
||
| internal static bool AreAligned<T1, T2, T3>( | ||
| BackPair<T1> pair1, | ||
| BackPair<T2> pair2, | ||
| BackPair<T3> pair3) | ||
| where T1 : ISeries | ||
| where T2 : ISeries | ||
| where T3 : ISeries | ||
| => AreAligned(pair1, pair2) | ||
| && pair1.Current.Timestamp == pair3.Current.Timestamp | ||
| && pair1.Previous.Timestamp == pair3.Previous.Timestamp; | ||
|
|
||
| /// <inheritdoc cref="AreAligned{T1, T2}(BackPair{T1}, BackPair{T2})" /> | ||
| /// <typeparam name="T1">The type of the 1st series, which must implement the <see cref="ISeries"/> interface.</typeparam> | ||
| /// <typeparam name="T2">The type of the 2nd series, which must implement the <see cref="ISeries"/> interface.</typeparam> | ||
| /// <typeparam name="T3">The type of the 3rd series, which must implement the <see cref="ISeries"/> interface.</typeparam> | ||
| /// <typeparam name="T4">The type of the 4th series, which must implement the <see cref="ISeries"/> interface.</typeparam> | ||
| /// <param name="pair1">The 1st <see cref="BackPair{T}"/> instance to compare.</param> | ||
| /// <param name="pair2">The 2nd <see cref="BackPair{T}"/> instance to compare.</param> | ||
| /// <param name="pair3">The 3rd <see cref="BackPair{T}"/> instance to compare.</param> | ||
| /// <param name="pair4">The 4th <see cref="BackPair{T}"/> instance to compare.</param> | ||
| internal static bool AreAligned<T1, T2, T3, T4>( | ||
| BackPair<T1> pair1, | ||
| BackPair<T2> pair2, | ||
| BackPair<T3> pair3, | ||
| BackPair<T4> pair4) | ||
| where T1 : ISeries | ||
| where T2 : ISeries | ||
| where T3 : ISeries | ||
| where T4 : ISeries | ||
| => AreAligned(pair1, pair2, pair3) | ||
| && pair1.Current.Timestamp == pair4.Current.Timestamp | ||
| && pair1.Previous.Timestamp == pair4.Previous.Timestamp; | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
|
|
||
| namespace Skender.Stock.Indicators; | ||
|
|
||
| /// <summary> | ||
| /// Groups two stream observable hubs for coordinated access and alignment. | ||
| /// </summary> | ||
| public sealed class StrategyGroup<T1, T2>(IStreamObservable<T1> hub1, IStreamObservable<T2> hub2) | ||
| where T1 : ISeries | ||
| where T2 : ISeries | ||
| { | ||
| /// <summary> | ||
| /// Gets the 1st stream observable hub the <see cref="StrategyGroup{T1, T2, T3, T4}"/>. | ||
| /// </summary> | ||
| public IStreamObservable<T1> Hub1 { get; } = hub1 ?? throw new ArgumentNullException(nameof(hub1)); | ||
| /// <summary> | ||
| /// Gets the 2nd stream observable hub the <see cref="StrategyGroup{T1, T2, T3, T4}"/>. | ||
| /// </summary> | ||
| public IStreamObservable<T2> Hub2 { get; } = hub2 ?? throw new ArgumentNullException(nameof(hub2)); | ||
|
|
||
| /// <summary> | ||
| /// Attempts to retrieve and align the most recent pairs from both hubs. | ||
| /// </summary> | ||
| public bool TryGetBackPair(out BackPair<T1> pair1, out BackPair<T2> pair2) | ||
| { | ||
| if (!Hub1.TryGetBackPair(out pair1) | ||
| || !Hub2.TryGetBackPair(out pair2)) | ||
| { | ||
| pair1 = default; | ||
| pair2 = default; | ||
| return false; | ||
| } | ||
|
|
||
| return StrategyGroupUtilities.AreAligned(pair1, pair2); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Groups three stream observable hubs for coordinated access and alignment. | ||
| /// </summary> | ||
| public sealed class StrategyGroup<T1, T2, T3>( | ||
| IStreamObservable<T1> hub1, | ||
| IStreamObservable<T2> hub2, | ||
| IStreamObservable<T3> hub3) | ||
| where T1 : ISeries | ||
| where T2 : ISeries | ||
| where T3 : ISeries | ||
| { | ||
| /// <summary> | ||
| /// Gets the 1st stream observable hub the <see cref="StrategyGroup{T1, T2, T3, T4}"/>. | ||
| /// </summary> | ||
| public IStreamObservable<T1> Hub1 { get; } = hub1 ?? throw new ArgumentNullException(nameof(hub1)); | ||
| /// <summary> | ||
| /// Gets the 2nd stream observable hub the <see cref="StrategyGroup{T1, T2, T3, T4}"/>. | ||
| /// </summary> | ||
| public IStreamObservable<T2> Hub2 { get; } = hub2 ?? throw new ArgumentNullException(nameof(hub2)); | ||
| /// <summary> | ||
| /// Gets the 3rd stream observable hub the <see cref="StrategyGroup{T1, T2, T3, T4}"/>. | ||
| /// </summary> | ||
| public IStreamObservable<T3> Hub3 { get; } = hub3 ?? throw new ArgumentNullException(nameof(hub3)); | ||
|
|
||
| /// <summary> | ||
| /// Attempts to retrieve and align the most recent pairs from all hubs. | ||
| /// </summary> | ||
| public bool TryGetBackPair( | ||
| out BackPair<T1> pair1, | ||
| out BackPair<T2> pair2, | ||
| out BackPair<T3> pair3) | ||
| { | ||
| if (!Hub1.TryGetBackPair(out pair1) | ||
| || !Hub2.TryGetBackPair(out pair2) | ||
| || !Hub3.TryGetBackPair(out pair3)) | ||
| { | ||
| pair1 = default; | ||
| pair2 = default; | ||
| pair3 = default; | ||
| return false; | ||
| } | ||
|
|
||
| return StrategyGroupUtilities.AreAligned(pair1, pair2, pair3); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Groups four stream observable hubs for coordinated access and alignment. | ||
| /// </summary> | ||
| public sealed class StrategyGroup<T1, T2, T3, T4>( | ||
| IStreamObservable<T1> hub1, | ||
| IStreamObservable<T2> hub2, | ||
| IStreamObservable<T3> hub3, | ||
| IStreamObservable<T4> hub4) | ||
| where T1 : ISeries | ||
| where T2 : ISeries | ||
| where T3 : ISeries | ||
| where T4 : ISeries | ||
| { | ||
| /// <summary> | ||
| /// Gets the 1st stream observable hub the <see cref="StrategyGroup{T1, T2, T3, T4}"/>. | ||
| /// </summary> | ||
| public IStreamObservable<T1> Hub1 { get; } = hub1 ?? throw new ArgumentNullException(nameof(hub1)); | ||
| /// <summary> | ||
| /// Gets the 2nd stream observable hub the <see cref="StrategyGroup{T1, T2, T3, T4}"/>. | ||
| /// </summary> | ||
| public IStreamObservable<T2> Hub2 { get; } = hub2 ?? throw new ArgumentNullException(nameof(hub2)); | ||
| /// <summary> | ||
| /// Gets the 3rd stream observable hub the <see cref="StrategyGroup{T1, T2, T3, T4}"/>. | ||
| /// </summary> | ||
| public IStreamObservable<T3> Hub3 { get; } = hub3 ?? throw new ArgumentNullException(nameof(hub3)); | ||
| /// <summary> | ||
| /// Gets the 4th stream observable hub the <see cref="StrategyGroup{T1, T2, T3, T4}"/>. | ||
| /// </summary> | ||
| public IStreamObservable<T4> Hub4 { get; } = hub4 ?? throw new ArgumentNullException(nameof(hub4)); | ||
|
|
||
| /// <summary> | ||
| /// Attempts to retrieve and align the most recent pairs from all hubs. | ||
| /// </summary> | ||
| public bool TryGetBackPair( | ||
| out BackPair<T1> pair1, | ||
| out BackPair<T2> pair2, | ||
| out BackPair<T3> pair3, | ||
| out BackPair<T4> pair4) | ||
| { | ||
| if (!Hub1.TryGetBackPair(out pair1) | ||
| || !Hub2.TryGetBackPair(out pair2) | ||
| || !Hub3.TryGetBackPair(out pair3) | ||
| || !Hub4.TryGetBackPair(out pair4)) | ||
| { | ||
| pair1 = default; | ||
| pair2 = default; | ||
| pair3 = default; | ||
| pair4 = default; | ||
| return false; | ||
| } | ||
|
|
||
| return StrategyGroupUtilities.AreAligned(pair1, pair2, pair3, pair4); | ||
| } | ||
| } | ||
Binary file not shown.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m not settled on naming here, “strategy” is not the right terminology.