搜索

PythonReactSEOPDF
GitHub Skill

indicator-stream

Implement StreamHub real-time indicators with O(1) performance. Use for ChainHub or QuoteProvider implementations. Covers provider selection, RollbackState patterns, performance anti-patterns, and comprehensive testing with StreamHubTestBase.

DaveSkender☆ 1,185 仓库 Stars266 仓库 Forks

安装与使用

请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。

查看来源 ↗使用指南

技能说明

StreamHub indicator development

Provider selection

Provider Base Input Output Use Case
ChainHub<IReusable, TResult> Single value IReusable Chainable indicators
ChainHub<IQuote, TResult> OHLCV IReusable Quote-driven, chainable output
QuoteProvider<IQuote, TResult> OHLCV IQuote Quote-to-quote transformation
StreamHub<TProviderResult, TResult> Any hub result Any result Compound hubs (internal hub dependency)

Performance requirements

Target: StreamHub ≤ 1.5x slower than Series.

Forbid O(n²) recalculation — rebuild entire history on each tick:

// WRONG
for (int k = 0; k <= i; k++) { subset.Add(cache[k]); }
var result = subset.ToIndicator();

O(1) incremental update:

// CORRECT
_avgGain = ((_avgGain * (period - 1)) + gain) / period;

Use RollingWindowMax/Min utilities instead of O(n) linear scans.

RollbackState pattern

Override when maintaining stateful fields. The base class computes restoreIndex via IndexBefore before calling this method. restoreIndex is the last ProviderCache index to preserve, or -1 to reset everything.

protected override void RollbackState(int restoreIndex)
{
    _window.Clear();
    if (restoreIndex < 0) return;
    int startIdx = Math.Max(0, restoreIndex + 1 - LookbackPeriods);
    for (int p = startIdx; p <= restoreIndex; p++)
        _window.Add(ProviderCache[p].Value);
}

Replay up to restoreIndex (inclusive). The item at the rollback timestamp is recalculated via normal processing.

Testing requirements

  • Inherit StreamHubTestBase
  • Abstract method (compile error if missing): ToStringOverride_ReturnsExpectedName()
  • Implement ONE observer interface:
    • ITestChainObserver (most indicators — chain input): inherits ITestQuoteObserver, adds ChainObserver_ChainedProvider_MatchesSeriesExactly()
    • ITestQuoteObserver (direct quote input only): QuoteObserver_WithWarmupLateArrivalAndRemoval_MatchesSeriesExactly(), WithCachePruning_MatchesSeriesExactly()
  • If hub acts as chain provider, also implement ITestChainProvider: ChainProvider_MatchesSeriesExactly()

Required implementation

  • Source code: src/**/{IndicatorName}.StreamHub.cs file exists
    • Uses appropriate provider base (ChainHub or QuoteProvider)
    • Validates parameters in constructor; calls Reinitialize() as needed
    • Implements O(1) state updates; avoids O(n²) recalculation
    • Overrides RollbackState() when maintaining stateful fields
    • Overrides ToString() with concise hub name
  • Unit testing: tests/indicators/**/{IndicatorName}.StreamHub.Tests.cs exists
    • Inherits StreamHubTestBase with correct test interfaces
    • Comprehensive rollback validation present
    • Verifies Series parity
  • Catalog registration: Registered in Catalog.Listings.cs
  • Performance benchmark: Add to tools/performance/Perf.Stream.cs
  • Public documentation: Update docs/indicators/{IndicatorName}.md
  • Regression tests: Add to tests/indicators/**/{IndicatorName}.Regression.Tests.cs
  • Migration guide: Update docs/migration.md for notable and breaking changes from v2

Examples

Constraints

  • O(n²) recalculation is forbidden; all updates must be O(1)
  • RollbackState(int restoreIndex) receives the last index to preserve (-1 = reset all); replay is inclusive of restoreIndex, exclusive of the rollback timestamp
  • Series parity required: results must be numerically identical to StaticSeries

使用示例

查看来源仓库中的使用示例与配置说明。

查看来源 ↗

来源历史

收录时间
2026-03-11
索引更新时间
2026-03-11
Branch
main

收录与同步日期属于本站记录。源码修改历史请查看来源仓库。

GitHub ↗

chat Comments (0)

chat_bubble_outline

No comments yet. Be the first to share your thoughts!