Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kernel/engine_prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ func (e *StrategyEngine) formatMarketData(data *market.Data) string {
}

if indicators.EnableATR {
sb.WriteString(fmt.Sprintf("3m ATR (14-period): %.3f\n\n", data.IntradaySeries.ATR14))
sb.WriteString(fmt.Sprintf("%s ATR (14-period): %.3f\n\n", klineConfig.PrimaryTimeframe, data.IntradaySeries.ATR14))
}
}

Expand Down
42 changes: 31 additions & 11 deletions market/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ func GetWithExchange(symbol, exchange string) (*Data, error) {
useHyperliquidAPI := isXyzAsset || strings.ToLower(exchange) == "hyperliquid"

// Get 3-minute K-line data (or 5-minute for xyz assets as 3m may not be available)
intradayInterval := "3m"
if useHyperliquidAPI {
// Use Hyperliquid API for xyz dex assets (use 5m since 3m may not be available)
klines3m, err = getKlinesFromHyperliquid(symbol, "5m", 100)
intradayInterval = "5m"
klines3m, err = getKlinesFromHyperliquid(symbol, intradayInterval, 100)
if err != nil {
return nil, fmt.Errorf("Failed to get 5-minute K-line from Hyperliquid: %v", err)
}
Expand Down Expand Up @@ -93,14 +95,9 @@ func GetWithExchange(symbol, exchange string) (*Data, error) {
currentRSI7 := calculateRSI(klines3m, 7)

// Calculate price change percentage
// 1-hour price change = price from 20 3-minute K-lines ago
priceChange1h := 0.0
if len(klines3m) >= 21 { // Need at least 21 K-lines (current + 20 previous)
price1hAgo := klines3m[len(klines3m)-21].Close
if price1hAgo > 0 {
priceChange1h = ((currentPrice - price1hAgo) / price1hAgo) * 100
}
}
// 1-hour price change = price from one hour's worth of intraday K-lines ago
// (20 bars for 3m, 12 bars for 5m)
priceChange1h := intradayPriceChange1h(klines3m, intradayInterval)

// 4-hour price change = price from 1 4-hour K-line ago
priceChange4h := 0.0
Expand All @@ -123,6 +120,7 @@ func GetWithExchange(symbol, exchange string) (*Data, error) {

// Calculate intraday series data
intradayData := calculateIntradaySeries(klines3m)
intradayData.Interval = intradayInterval

// Calculate longer-term data
longerTermData := calculateLongerTermData(klines4h)
Expand Down Expand Up @@ -377,7 +375,9 @@ func Format(data *Data) string {
sb.WriteString(fmt.Sprintf("Funding Rate: %.2e\n\n", data.FundingRate))

if data.IntradaySeries != nil {
sb.WriteString("Intraday series (3‑minute intervals, oldest → latest):\n\n")
interval := data.IntradaySeries.intervalOrDefault()
sb.WriteString(fmt.Sprintf("Intraday series (%d‑minute intervals, oldest → latest):\n\n",
parseTimeframeToMinutes(interval)))

if len(data.IntradaySeries.MidPrices) > 0 {
sb.WriteString(fmt.Sprintf("Mid prices: %s\n\n", formatFloatSlice(data.IntradaySeries.MidPrices)))
Expand All @@ -403,7 +403,7 @@ func Format(data *Data) string {
sb.WriteString(fmt.Sprintf("Volume: %s\n\n", formatFloatSlice(data.IntradaySeries.Volume)))
}

sb.WriteString(fmt.Sprintf("3m ATR (14‑period): %.3f\n\n", data.IntradaySeries.ATR14))
sb.WriteString(fmt.Sprintf("%s ATR (14‑period): %.3f\n\n", interval, data.IntradaySeries.ATR14))
}

if data.LongerTermContext != nil {
Expand Down Expand Up @@ -640,6 +640,26 @@ func BuildDataFromKlines(symbol string, primary []Kline, longer []Kline) (*Data,
return data, nil
}

// intradayPriceChange1h returns the 1-hour price change of an intraday series
// sampled at interval (e.g. "3m", "5m"). It returns 0 when the series does not
// cover a full hour or the interval is unknown.
func intradayPriceChange1h(klines []Kline, interval string) float64 {
minutes := parseTimeframeToMinutes(interval)
if minutes <= 0 || minutes > 60 {
return 0
}
barsBack := 60 / minutes
if len(klines) < barsBack+1 { // current + barsBack previous
return 0
}
currentPrice := klines[len(klines)-1].Close
price1hAgo := klines[len(klines)-1-barsBack].Close
if price1hAgo <= 0 {
return 0
}
return ((currentPrice - price1hAgo) / price1hAgo) * 100
}

func priceChangeFromSeries(series []Kline, duration time.Duration) float64 {
if len(series) == 0 || duration <= 0 {
return 0
Expand Down
62 changes: 62 additions & 0 deletions market/data_interval_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package market

import (
"strings"
"testing"
)

func seriesWithCloses(n int, close func(i int) float64) []Kline {
klines := make([]Kline, n)
for i := range klines {
klines[i] = Kline{Close: close(i)}
}
return klines
}

func TestIntradayPriceChange1hUsesIntervalBarCount(t *testing.T) {
// Close price equals the bar index, so the lookback is visible in the result.
klines := seriesWithCloses(100, func(i int) float64 { return float64(i + 1) })
last := 100.0

// 3m: one hour is 20 bars back -> close 80.
want3m := (last - 80) / 80 * 100
if got := intradayPriceChange1h(klines, "3m"); got != want3m {
t.Fatalf("3m 1h change = %v, want %v", got, want3m)
}

// 5m: one hour is 12 bars back -> close 88 (not 20 bars / 100 minutes).
want5m := (last - 88) / 88 * 100
if got := intradayPriceChange1h(klines, "5m"); got != want5m {
t.Fatalf("5m 1h change = %v, want %v", got, want5m)
}
}

func TestIntradayPriceChange1hNeedsAFullHour(t *testing.T) {
klines := seriesWithCloses(12, func(i int) float64 { return float64(i + 1) })
if got := intradayPriceChange1h(klines, "5m"); got != 0 {
t.Fatalf("12 bars of 5m cannot cover an hour, got %v", got)
}
if got := intradayPriceChange1h(klines, "bogus"); got != 0 {
t.Fatalf("unknown interval should yield 0, got %v", got)
}
}

func TestFormatLabelsIntradayInterval(t *testing.T) {
series := &IntradayData{MidPrices: []float64{1, 2}, ATR14: 1.5}

series.Interval = "5m"
out := Format(&Data{Symbol: "BTC", IntradaySeries: series})
if !strings.Contains(out, "5‑minute intervals") || !strings.Contains(out, "5m ATR") {
t.Fatalf("5m series must be labelled as 5-minute:\n%s", out)
}
if strings.Contains(out, "3‑minute") || strings.Contains(out, "3m ATR") {
t.Fatalf("5m series must not be labelled as 3-minute:\n%s", out)
}

// Legacy data without an interval keeps the 3m label.
series.Interval = ""
out = Format(&Data{Symbol: "BTC", IntradaySeries: series})
if !strings.Contains(out, "3‑minute intervals") || !strings.Contains(out, "3m ATR") {
t.Fatalf("default series must keep the 3-minute label:\n%s", out)
}
}
23 changes: 17 additions & 6 deletions market/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ type OIData struct {
Average float64
}

// IntradayData intraday data (3-minute interval)
// IntradayData intraday data (3-minute interval by default; 5-minute when
// sourced from Hyperliquid)
type IntradayData struct {
// Interval is the K-line interval of the series (e.g. "3m", "5m").
// Empty means the legacy 3m default.
Interval string
MidPrices []float64
EMA20Values []float64
MACDValues []float64
Expand All @@ -64,6 +68,13 @@ type IntradayData struct {
ATR14 float64
}

func (d *IntradayData) intervalOrDefault() string {
if d == nil || parseTimeframeToMinutes(d.Interval) <= 0 {
return "3m"
}
return d.Interval
}

// LongerTermData longer-term data (4-hour timeframe)
type LongerTermData struct {
EMA20 float64
Expand Down Expand Up @@ -231,11 +242,11 @@ const (
type GridDirection string

const (
GridDirectionNeutral GridDirection = "neutral" // 50% buy + 50% sell
GridDirectionLong GridDirection = "long" // 100% buy
GridDirectionShort GridDirection = "short" // 100% sell
GridDirectionLongBias GridDirection = "long_bias" // 70% buy + 30% sell (default)
GridDirectionShortBias GridDirection = "short_bias" // 30% buy + 70% sell (default)
GridDirectionNeutral GridDirection = "neutral" // 50% buy + 50% sell
GridDirectionLong GridDirection = "long" // 100% buy
GridDirectionShort GridDirection = "short" // 100% sell
GridDirectionLongBias GridDirection = "long_bias" // 70% buy + 30% sell (default)
GridDirectionShortBias GridDirection = "short_bias" // 30% buy + 70% sell (default)
)

// GetBuySellRatio returns the buy and sell ratio for this direction
Expand Down