Documentation
¶
Overview ¶
Package sr provides deterministic, closed-candle support/resistance detection for Go trading systems, backtests, and live strategies.
It is designed for reproducible S/R signals without lookahead: zone-mode pivots are confirmation-based, and a given closed-candle prefix plus options produces deterministic output. Results include qualified levels, raw zones, nearest support/resistance prices, distances, strengths, scores, and proximity flags suitable for strategy logic.
Two compute modes are supported:
- ModeLegacy: line-based pivots with fixed-tolerance proximity
- ModeZones: zone-based detection with composite scoring and raw/qualified output
The package also provides S/R-specific multi-timeframe helpers for aggregating candles and calculating warmup/fetch requirements. Exchange parsing, app-specific timeframe policy, strategy scoring, and order execution remain outside the package.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RequiredKlineLimit ¶
RequiredKlineLimit returns the raw kline fetch size needed to build an S/R bundle for targetInterval from a baseInterval stream. The returned size includes alignment slack for UTC target buckets and one extra live candle because exchange REST responses usually include the currently forming bar.
func WarmupCandles ¶
WarmupCandles returns the minimum closed-candle history needed before a support/resistance calculation can be considered fully warmed up.
Types ¶
type Candle ¶
type Candle struct {
OpenTime time.Time
CloseTime time.Time
Open float64
High float64
Low float64
Close float64
Volume float64
}
Candle represents a single OHLCV closed candlestick.
func AggregateCandlesToTimeframe ¶
AggregateCandlesToTimeframe rolls a closed-candle slice into a higher timeframe using UTC-aligned buckets. Any leading or trailing partial bucket is dropped.
Example ¶
start := time.Date(2024, 4, 1, 0, 0, 0, 0, time.UTC)
candles := make5mCandles(start, []struct {
open float64
high float64
low float64
close float64
volume float64
}{
{100, 101, 99, 100.5, 10},
{100.5, 102, 100, 101.5, 20},
{101.5, 103, 101, 102.5, 30},
})
agg := AggregateCandlesToTimeframe(candles, "5m", "15m")
fmt.Println(len(agg), agg[0].Open, agg[0].Close, agg[0].Volume)
Output: 1 100 102.5 60
type Level ¶
type Level struct {
Price float64
Top float64
Bottom float64
Strength int
Score float64
IsHigh bool
Timeframe string
LastTouchIndex int
SourcePivotIndexes []int
Pivots []PivotInfo
}
Level represents a support or resistance zone built from clustered swing pivots.
type Levels ¶
type Levels struct {
Timeframe string
Levels []Level
RawZones []Level
NearSupport bool
NearResistance bool
NearestSupport float64
NearestResistance float64
NearestSupportDistance float64
NearestResistanceDistance float64
NearestSupportStrength int
NearestResistanceStrength int
NearestSupportScore float64
NearestResistanceScore float64
}
Levels holds computed support/resistance data for the current candle series.
func Compute ¶
Compute detects support/resistance levels for the given candle prefix.
Example (Legacy) ¶
levels, err := Compute(buildSRCategoryCandles(), Options{
Timeframe: "5m",
Lookback: 120,
Mode: ModeLegacy,
Tolerance: 0.002,
})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(levels.Timeframe, len(levels.Levels) >= 2, levels.NearestSupport > 0, levels.NearestResistance > 0)
Output: 5m true true true
Example (Zone) ¶
levels, err := Compute(buildSRCategoryCandles(), Options{
Timeframe: "5m",
Lookback: 120,
Mode: ModeZones,
})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(levels.Timeframe, len(levels.Levels) >= 2, levels.NearestSupport > 0, levels.NearestResistance > 0)
Output: 5m true true true
func EmptyLevels ¶
EmptyLevels returns the zero-value bundle for a timeframe label.
type Options ¶
type Options struct {
Timeframe string
Lookback int
Mode Mode
// Tolerance applies only to ModeLegacy. When Tolerance <= 0, legacy mode
// uses the default fallback of 0.002.
Tolerance float64
// MinStrength filters zone-mode raw zones by Strength.
// 0 or negative -> use the default of 2 (back-compat).
// 1+ -> require Strength >= MinStrength.
// No effect on Mode=Legacy.
MinStrength int
}
Options configures a support/resistance computation.
type PivotInfo ¶
type PivotInfo struct {
Index int `json:"index"`
ConfirmedAtIndex int `json:"confirmed_at_index"`
Time time.Time `json:"time"`
Price float64 `json:"price"`
IsHigh bool `json:"is_high"`
Timeframe string `json:"timeframe"`
ATRSnapshot float64 `json:"atr_snapshot"`
AvgVolumeSnapshot float64 `json:"avg_volume_snapshot"`
Volume float64 `json:"volume"`
VolumeRatio float64 `json:"volume_ratio"`
MergeWidth float64 `json:"merge_width"`
BounceATR float64 `json:"bounce_atr"`
}
PivotInfo captures the local snapshot data used to build a support or resistance zone.
