You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Backtesting.py makes it easy to test indicator-based strategies. Here's a strategy concept that uses historical pattern similarity as the signal: find the most similar past chart patterns and trade based on their aggregate forward returns.
Chart Library has 24M+ pre-computed embeddings across 19K US stocks (10 years). Its API returns the top matches with actual forward returns for any (symbol, date).
Example strategy
frombacktestingimportBacktest, StrategyimportrequestsclassPatternSimilarity(Strategy):
""" Buy when historically similar patterns had positive forward returns. Sell when they had negative forward returns. """lookback_threshold=0.6# min win rate to enter longdefinit(self):
self.pattern_signals= {}
defnext(self):
date_str=str(self.data.index[-1].date())
ifdate_strnotinself.pattern_signals:
try:
resp=requests.get("https://chartlibrary.io/api/v1/search",
params={"symbol": "AAPL", "date": date_str, "timeframe": "RTH"},
headers={"X-API-Key": "your-key"}, timeout=5)
matches=resp.json()["matches"]
win_rate=sum(1forminmatchesifm["return_5d"] >0) /len(matches)
avg_return=sum(m["return_5d"] forminmatches) /len(matches)
self.pattern_signals[date_str] = {
"win_rate": win_rate, "avg_return": avg_return
}
exceptException:
returnsig=self.pattern_signals.get(date_str)
ifnotsig:
returnifnotself.positionandsig["win_rate"] >=self.lookback_threshold:
self.buy()
elifself.positionandsig["win_rate"] <0.4:
self.position.close()
For production, you'd pre-fetch all signals into a DataFrame column rather than calling the API in the loop.
Idea
Backtesting.py makes it easy to test indicator-based strategies. Here's a strategy concept that uses historical pattern similarity as the signal: find the most similar past chart patterns and trade based on their aggregate forward returns.
Chart Library has 24M+ pre-computed embeddings across 19K US stocks (10 years). Its API returns the top matches with actual forward returns for any (symbol, date).
Example strategy
For production, you'd pre-fetch all signals into a DataFrame column rather than calling the API in the loop.
What the API provides per match
Details
This is essentially a "nearest neighbor" trading strategy — find the most similar historical charts and bet on the average outcome.