Files
cryptoHermes/internal/entity/market_context.go
dela 21b3078094 feat(indicator): per-interval S/R + Range + LSLine + Donchian 箱体(ADR-0003)
按 ADR-0003 把 Support / Resistance / Range / LSLine 从顶层下沉进
IntervalTechnicals,每个周期独立计算;顶层 5 字段保留为 Intervals[primary]
镜像(单一来源,不双写)以维持向后兼容的 JSON shape。

新增 BoxBreak(Donchian 风格,lookback=20 根不含当前根,K 线 < 21 → nil),
落到 IntervalTechnicals.Box。

IndicatorComputer.Compute 签名升级到 longShortByInterval map:
market_context 改为并发拉取 15m/1h/4h/1d 的全局 LSR,1w 不在 Binance LSR
支持列表 → 加 warning lsr_unsupported_interval:1w。

测试:boxBreak 6 个 case(inside/break_up/break_down/insufficient/2 个 parse
失败)+ per-interval Support/Resistance/Range 跨周期断言 + 镜像不变量 +
缺 LSR key → LongShortLine nil。indicator.go 平均覆盖 97.26%(boxBreak 100%)。
2026-05-25 10:44:33 +08:00

103 lines
3.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package entity
type MarketContext struct {
Symbol string `json:"symbol"`
GeneratedAt int64 `json:"generatedAt"`
Snapshot *Ticker24h `json:"snapshot"`
Klines map[string][]Kline `json:"klines"`
Derivatives DerivativesBundle `json:"derivatives"`
Technical TechnicalStructure `json:"technical"`
DataQuality DataQuality `json:"dataQuality"`
}
type DerivativesBundle struct {
Funding FundingBundle `json:"funding"`
OpenInterest OpenInterestBundle `json:"openInterest"`
LongShortRatio LongShortBundle `json:"longShortRatio"`
TakerBuySellVolume []TakerBuySellVolume `json:"takerBuySellVolume"`
Signal *DerivativesSignal `json:"signal,omitempty"`
}
// DerivativesSignal 把 funding / OI / LSR 的原始数字翻译成可读语义,
// 供上游策略引擎直接消费。任一子字段为空字符串表示数据不足以判定。
type DerivativesSignal struct {
FundingBias string `json:"fundingBias,omitempty"` // crowded_long / crowded_short / neutral
OISignal string `json:"oiSignal,omitempty"` // long_building / short_building / deleveraging / stable
LSRRegime string `json:"lsrRegime,omitempty"` // retail_long_heavy / retail_short_heavy / balanced
}
type FundingBundle struct {
Current *FundingRate `json:"current"`
History []FundingRate `json:"history"`
}
type OpenInterestBundle struct {
Current *OpenInterest `json:"current"`
History []OpenInterest `json:"history"`
}
type LongShortBundle struct {
Global []LongShortRatio `json:"global"`
TopTraderPosition []LongShortRatio `json:"topTraderPosition"`
}
type TechnicalStructure struct {
Support []TechnicalLevel `json:"support"`
Resistance []TechnicalLevel `json:"resistance"`
RangeHigh *string `json:"rangeHigh"`
RangeLow *string `json:"rangeLow"`
LongShortLine *string `json:"longShortLine"`
Intervals map[string]IntervalTechnicals `json:"intervals"`
}
type IntervalTechnicals struct {
Bollinger *Bollinger `json:"bollinger,omitempty"`
Vegas *Vegas `json:"vegas,omitempty"`
Support []TechnicalLevel `json:"support,omitempty"`
Resistance []TechnicalLevel `json:"resistance,omitempty"`
RangeHigh *string `json:"rangeHigh,omitempty"`
RangeLow *string `json:"rangeLow,omitempty"`
LongShortLine *string `json:"longShortLine,omitempty"`
Box *BoxBreak `json:"box,omitempty"`
}
// Bollinger 是 20 周期、2 倍标准差的布林带。
// BandwidthPct = (upper-lower)/mid * 100Squeeze 取 tight/normal/expanded。
type Bollinger struct {
Mid string `json:"mid"`
Upper string `json:"upper"`
Lower string `json:"lower"`
BandwidthPct string `json:"bandwidthPct"`
Squeeze string `json:"squeeze"`
}
// Vegas 通道EMA12/144/169 三条线 + 趋势判定。
// Trend: bull / bear / range按三条线的堆叠顺序判定。
type Vegas struct {
EMA12 string `json:"ema12"`
EMA144 string `json:"ema144"`
EMA169 string `json:"ema169"`
Trend string `json:"trend"`
}
// BoxBreak 是 Donchian 风格的 N 根 lookback 箱体突破信号。
// BoxHigh / BoxLow 取最近 LookbackBars 根(不含当前根)的最高高 / 最低低;
// Status: inside / break_up / break_down由最后一根 K 线收盘相对箱体边判定。
type BoxBreak struct {
BoxHigh string `json:"boxHigh"`
BoxLow string `json:"boxLow"`
Status string `json:"status"`
LookbackBars string `json:"lookbackBars"`
}
type TechnicalLevel struct {
Price string `json:"price"`
Strength string `json:"strength,omitempty"`
Source string `json:"source,omitempty"`
}
type DataQuality struct {
Source string `json:"source"`
Warnings []string `json:"warnings"`
}