Skip to content

Feature: Chart pattern similarity as observation/state for RL agents #1411

Description

@grahammccain

Idea

FinRL agents currently use price/volume/technical indicators as state observations. Historical chart pattern similarity could add a useful signal — essentially asking "when the chart looked like this before, what happened next?"

Chart Library has 24M+ pre-computed pattern embeddings across 19K US equities (10 years). The API returns the top-K most similar historical patterns and their forward returns, which could serve as additional state features for RL agents.

How it could work in FinRL

import requests

def get_pattern_features(symbol: str, date: str) -> dict:
    """Add pattern similarity features to FinRL state space."""
    resp = requests.get("https://chartlibrary.io/api/v1/search", params={
        "symbol": symbol, "date": date, "timeframe": "RTH"
    }, headers={"X-API-Key": "your-key"})
    
    matches = resp.json()["matches"]
    
    # Extract features for RL state
    avg_1d_return = sum(m["return_1d"] for m in matches) / len(matches)
    avg_5d_return = sum(m["return_5d"] for m in matches) / len(matches)
    avg_10d_return = sum(m["return_10d"] for m in matches) / len(matches)
    win_rate = sum(1 for m in matches if m["return_5d"] > 0) / len(matches)
    avg_distance = sum(m["distance"] for m in matches) / len(matches)
    
    return {
        "pattern_avg_1d": avg_1d_return,
        "pattern_avg_5d": avg_5d_return,
        "pattern_avg_10d": avg_10d_return,
        "pattern_win_rate": win_rate,
        "pattern_confidence": 1.0 / (1.0 + avg_distance),  # closer = more confident
    }

# These features could be added to StockTradingEnv observation space

Why this might help

  • Pattern similarity captures non-linear chart structure that moving averages and RSI miss
  • The forward returns from historical matches act as a "base rate" prior
  • Low-distance matches (high confidence) correlate with more predictable outcomes
  • Could be especially useful for the regime-switching aspects of trading

Practical details

  • Free tier: 200 calls/day, enough for daily rebalancing experiments
  • Response time: ~100ms per search
  • Docs: https://chartlibrary.io/developers
  • Coverage: 19K US equities, 8 timeframes, 10 years

Curious if the team has considered pattern-based features in the observation space. Happy to discuss.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions