Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Stock.Indicators.sln
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests (other)", "Tests (oth
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{F4DC4349-B08A-4557-BAD9-EB3A8111C98A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.SseServer", "tools\server\Test.SseServer.csproj", "{01CC3E53-1AC1-71D3-1076-C3420B6A2DF2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -137,6 +139,18 @@ Global
{F449BEB0-5A04-6992-4A1F-DE3FFE72A1C6}.Release|x64.Build.0 = Release|Any CPU
{F449BEB0-5A04-6992-4A1F-DE3FFE72A1C6}.Release|x86.ActiveCfg = Release|Any CPU
{F449BEB0-5A04-6992-4A1F-DE3FFE72A1C6}.Release|x86.Build.0 = Release|Any CPU
{01CC3E53-1AC1-71D3-1076-C3420B6A2DF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{01CC3E53-1AC1-71D3-1076-C3420B6A2DF2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{01CC3E53-1AC1-71D3-1076-C3420B6A2DF2}.Debug|x64.ActiveCfg = Debug|Any CPU
{01CC3E53-1AC1-71D3-1076-C3420B6A2DF2}.Debug|x64.Build.0 = Debug|Any CPU
{01CC3E53-1AC1-71D3-1076-C3420B6A2DF2}.Debug|x86.ActiveCfg = Debug|Any CPU
{01CC3E53-1AC1-71D3-1076-C3420B6A2DF2}.Debug|x86.Build.0 = Debug|Any CPU
{01CC3E53-1AC1-71D3-1076-C3420B6A2DF2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{01CC3E53-1AC1-71D3-1076-C3420B6A2DF2}.Release|Any CPU.Build.0 = Release|Any CPU
{01CC3E53-1AC1-71D3-1076-C3420B6A2DF2}.Release|x64.ActiveCfg = Release|Any CPU
{01CC3E53-1AC1-71D3-1076-C3420B6A2DF2}.Release|x64.Build.0 = Release|Any CPU
{01CC3E53-1AC1-71D3-1076-C3420B6A2DF2}.Release|x86.ActiveCfg = Release|Any CPU
{01CC3E53-1AC1-71D3-1076-C3420B6A2DF2}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -149,6 +163,7 @@ Global
{D6CF664F-8232-4EE9-B044-CA34A0BA522E} = {2137B81B-5620-4225-8CF4-7FE72E81DF23}
{F449BEB0-5A04-6992-4A1F-DE3FFE72A1C6} = {990C3D8F-0F57-4FD3-AF4B-57D6AE2EDBB1}
{F4DC4349-B08A-4557-BAD9-EB3A8111C98A} = {89F97043-8E6C-4953-95AA-ACAC19C3B164}
{01CC3E53-1AC1-71D3-1076-C3420B6A2DF2} = {990C3D8F-0F57-4FD3-AF4B-57D6AE2EDBB1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8BDD5593-A28F-4DC3-859F-1E267864898D}
Expand Down
117 changes: 117 additions & 0 deletions src/_common/StrategyGroup/StrategyGroup.Utilities.cs
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;
}

136 changes: 136 additions & 0 deletions src/_common/StrategyGroup/StrategyGroup.cs
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)
Copy link
Owner

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.

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.
Loading
Loading