Share high-precision candle projection and viewport utilities
Resolve the frozen Ziran and Kryon revisions from their Gitea package sources.
This commit is contained in:
1 parent
49ac5ca6ed
commit
b685655bb9
5 files changed
+216
-27
No files matched your search
@@ -27,7 +27,7 @@ charts and Kryon's public UI surface; app code can use
|
||||
To add Plot to another Ziran project, run:
|
||||
|
||||
```sh
|
||||
ziran add https://github.com/kryonlabs/plot.git
|
||||
ziran add https://git.waozi.xyz/kryonlabs/plot.git
|
||||
```
|
||||
|
||||
Then import its public module, or only the chart surface:
|
||||
@@ -51,6 +51,30 @@ Public module names use `UpperCamelCase` (`Kryon`, `PlotWidget`, `Plot`).
|
||||
Lowercase names such as `plot_widget` are implementation modules; the
|
||||
lowercase `plot` export remains available for existing consumers.
|
||||
|
||||
## High-precision candlesticks
|
||||
|
||||
For OHLCV data stored as `float64`, pass the visible `[]PriceBar64` and a
|
||||
caller-owned `[]PriceBar` of the same length or longer to `CandleProject`.
|
||||
The function offsets and scales prices in `float64` before storing them in
|
||||
the `[1, 2]` domain used by the renderer. It also normalizes volume. Pass
|
||||
the projected bars and `projection.range` to `CandleChart`. Input must have
|
||||
finite, ordered OHLC values and finite, nonnegative volumes. A zero `span`
|
||||
indicates empty input, insufficient output space, or an unrepresentable
|
||||
domain. Flat input receives a small centered display span.
|
||||
|
||||
Use `CandleProjectedPrice(projection, tick)` to recover a `float64` price
|
||||
for an axis label. For example, `CandleProjectedPrice(projection, 1.5)` gives
|
||||
the midpoint price and `CandleY(1.5, price_bounds, projection.range)` gives
|
||||
its position. Use the original bars for detailed captions.
|
||||
|
||||
`CandleViewportFor(bars, right_edge, selected, visible)` resolves ascending
|
||||
timestamps into the half-open window `[first, end)`. A zero `right_edge`
|
||||
follows the latest bar. `selected` is a timestamp on input and an absolute
|
||||
bar index on output. The window retains the selected bar when visible and
|
||||
otherwise selects the rightmost visible bar. An empty window has selection
|
||||
`-1`. Use `CandlePanEnd` to move the window and `CandleCenterX` and
|
||||
`CandleIndexAtX` to map between visible slots and plot coordinates.
|
||||
|
||||
## Band charts in portable bundles
|
||||
|
||||
`BandChart(session, props, points, bands)` draws values over a row of slots
|
||||
|
||||
+114
-13
@@ -15,6 +15,15 @@ PriceBar :: struct {
|
||||
volume: float32
|
||||
}
|
||||
|
||||
PriceBar64 :: struct {
|
||||
timestamp: s64
|
||||
open: float64
|
||||
high: float64
|
||||
low: float64
|
||||
close: float64
|
||||
volume: float64
|
||||
}
|
||||
|
||||
CandleRange :: struct {
|
||||
low: float32
|
||||
high: float32
|
||||
@@ -24,6 +33,21 @@ CandleRange :: struct {
|
||||
logarithmic: bool
|
||||
}
|
||||
|
||||
CandleProjection :: struct {
|
||||
low: float64
|
||||
high: float64
|
||||
span: float64
|
||||
max_volume: float64
|
||||
range: CandleRange
|
||||
}
|
||||
|
||||
CandleViewport :: struct {
|
||||
first: s32
|
||||
end: s32
|
||||
selected: s32
|
||||
right_edge: s64
|
||||
}
|
||||
|
||||
CandleChartProps :: struct {
|
||||
key: u64
|
||||
bounds: Rectangle
|
||||
@@ -80,6 +104,62 @@ CandleIndexAtX :: (x: float32, bounds: Rectangle,
|
||||
cast(float32)visible_count / bounds.width)
|
||||
}
|
||||
|
||||
// The center of a zero-based visible slot, or -1 for an invalid slot or width.
|
||||
#program_export
|
||||
CandleCenterX :: (index: s32, bounds: Rectangle,
|
||||
visible_count: s32) -> float32 {
|
||||
if index < 0 || index >= visible_count || bounds.width <= 0.0 { return -1.0 }
|
||||
step: float32 = bounds.width / cast(float32)visible_count
|
||||
return bounds.x + (cast(float32)index + 0.5) * step
|
||||
}
|
||||
|
||||
// Project the caller's visible bars into the linear [1, 2] price domain.
|
||||
// Input prices and volumes are finite and each bar has ordered OHLC values.
|
||||
// An empty input, short output or unusable domain returns span zero.
|
||||
#program_export
|
||||
CandleProject :: (bars: []PriceBar64, output: []PriceBar) -> CandleProjection {
|
||||
if bars.count == 0 || output.count < bars.count { return .{} }
|
||||
low: float64 = bars[0].low
|
||||
high: float64 = bars[0].high
|
||||
max_volume: float64 = 0.0
|
||||
for bars {
|
||||
if it.low < low { low = it.low }
|
||||
if it.high > high { high = it.high }
|
||||
if it.volume > max_volume { max_volume = it.volume }
|
||||
}
|
||||
span: float64 = high - low
|
||||
if span == 0.0 {
|
||||
// Match the flat price buffer on either side of the original value.
|
||||
scale: float64 = low
|
||||
if scale < 0.0 { scale = -scale }
|
||||
span = scale * 0.002
|
||||
if span / 2.0 == 0.0 { span = 0.000000002 }
|
||||
low -= span / 2.0
|
||||
high = low + span
|
||||
}
|
||||
if !(span > 0.0) || !ChartMath.Finite64(low) ||
|
||||
!ChartMath.Finite64(high) || !ChartMath.Finite64(span) { return .{} }
|
||||
|
||||
for index: 0..bars.count - 1 {
|
||||
bar := bars[index]
|
||||
output[index] = .{.timestamp = bar.timestamp,
|
||||
.open = cast(float32)(1.0 + (bar.open - low) / span),
|
||||
.high = cast(float32)(1.0 + (bar.high - low) / span),
|
||||
.low = cast(float32)(1.0 + (bar.low - low) / span),
|
||||
.close = cast(float32)(1.0 + (bar.close - low) / span),
|
||||
.volume = ifx max_volume > 0.0 then cast(float32)(bar.volume / max_volume) else 0.0}
|
||||
}
|
||||
return .{.low = low, .high = high, .span = span, .max_volume = max_volume,
|
||||
.range = .{.low = 1.0, .high = 2.0, .min_value = 0.975,
|
||||
.max_value = 2.025, .max_volume = ifx max_volume > 0.0 then 1.0 else 0.0}}
|
||||
}
|
||||
|
||||
// Recover a price from a normalized value, including the chart's padded ticks.
|
||||
#program_export
|
||||
CandleProjectedPrice :: (projection: CandleProjection, value: float64) -> float64 {
|
||||
return projection.low + (value - 1.0) * projection.span
|
||||
}
|
||||
|
||||
#program_export
|
||||
CandleRangeFor :: (bars: []PriceBar, visible_count: s32,
|
||||
logarithmic: bool = false) -> CandleRange {
|
||||
@@ -200,12 +280,38 @@ CandleWindowEnd :: (bars: []PriceBar, timestamp: s64) -> s32 {
|
||||
|
||||
#program_export
|
||||
CandlePanEnd :: (end: s32, steps: s32, count: s32, visible: s32) -> s32 {
|
||||
result := end + steps
|
||||
if count <= 0 { return 0 }
|
||||
result: s64 = cast(s64)end + cast(s64)steps
|
||||
minimum := visible
|
||||
if minimum < 0 { minimum = 0 }
|
||||
if minimum > count { minimum = count }
|
||||
if result < minimum { result = minimum }
|
||||
if result > count { result = count }
|
||||
return result
|
||||
if result < minimum { return minimum }
|
||||
if result > count { return count }
|
||||
return cast(s32)result
|
||||
}
|
||||
|
||||
// Resolve ascending timestamps to a bounded window and an absolute selection.
|
||||
// A zero right edge follows the latest data. Missing selection uses the last
|
||||
// visible candle; an empty input has no selection and returns -1.
|
||||
#program_export
|
||||
CandleViewportFor :: (bars: []PriceBar64, right_edge: s64,
|
||||
selected: s64, visible: s32) -> CandleViewport {
|
||||
count: s32 = cast(s32)bars.count
|
||||
if count == 0 { return .{.selected = -1} }
|
||||
width := visible
|
||||
if width <= 0 { width = 1 }
|
||||
if width > count { width = count }
|
||||
end := count
|
||||
if right_edge > 0 {
|
||||
while end > width && bars[end - 1].timestamp > right_edge { end -= 1 }
|
||||
}
|
||||
first := end - width
|
||||
selection := end - 1
|
||||
for index: first..end - 1 {
|
||||
if bars[index].timestamp == selected { selection = index; break }
|
||||
}
|
||||
return .{.first = first, .end = end, .selected = selection,
|
||||
.right_edge = ifx end == count then 0 else bars[end - 1].timestamp}
|
||||
}
|
||||
|
||||
#program_export
|
||||
@@ -245,7 +351,6 @@ DrawAverage :: (session: Session, props: CandleChartProps,
|
||||
props.visible_count)
|
||||
count: s32 = cast(s32)bars.count - start
|
||||
if count <= 0 { return }
|
||||
step: float32 = price.width / cast(float32)count
|
||||
previous: bool = false
|
||||
previous_x: float32 = 0.0
|
||||
previous_y: float32 = 0.0
|
||||
@@ -254,8 +359,7 @@ DrawAverage :: (session: Session, props: CandleChartProps,
|
||||
value: float32 = SMAAt(bars, index, period)
|
||||
if exponential { value = EMAAt(bars, index, period) }
|
||||
if CandleValidPrice(value) {
|
||||
x: float32 = price.x +
|
||||
(cast(float32)(index - start) + 0.5) * step
|
||||
x: float32 = CandleCenterX(index - start, price, count)
|
||||
y: float32 = CandleY(value, price, range)
|
||||
if previous {
|
||||
PaintLine(session, -1,
|
||||
@@ -311,8 +415,7 @@ CandleChart :: (session: Session, props: CandleChartProps,
|
||||
index += 1
|
||||
continue
|
||||
}
|
||||
x: float32 = price.x +
|
||||
(cast(float32)(index - start) + 0.5) * step
|
||||
x: float32 = CandleCenterX(index - start, price, count)
|
||||
color: Color = props.up_color
|
||||
if bar.close < bar.open { color = props.down_color }
|
||||
wick_high: float32 = CandleY(bar.high, price, range)
|
||||
@@ -367,7 +470,7 @@ CandleMarkerX :: (bars: []PriceBar, visible_count: s32,
|
||||
count: s32 = cast(s32)bars.count - start
|
||||
if bars.count == 1 {
|
||||
if timestamp != bars[0].timestamp { return -1.0 }
|
||||
return bounds.x + bounds.width / 2.0
|
||||
return CandleCenterX(0, bounds, 1)
|
||||
}
|
||||
index: s32 = start
|
||||
while index < cast(s32)bars.count {
|
||||
@@ -380,9 +483,7 @@ CandleMarkerX :: (bars: []PriceBar, visible_count: s32,
|
||||
}
|
||||
if timestamp >= bars[index].timestamp &&
|
||||
(next <= bars[index].timestamp || timestamp < next) {
|
||||
return bounds.x +
|
||||
(cast(float32)(index - start) + 0.5) *
|
||||
bounds.width / cast(float32)count
|
||||
return CandleCenterX(index - start, bounds, count)
|
||||
}
|
||||
index += 1
|
||||
}
|
||||
|
||||
@@ -66,5 +66,69 @@ main :: () -> s32 {
|
||||
if CandlePanEnd(5, -20, 5, 3) != 3 { return 22 }
|
||||
if CandlePanEnd(3, 20, 5, 3) != 5 { return 23 }
|
||||
if CandlePanStepsFor(100.0, 200.0, bounds, 3) != -1 { return 24 }
|
||||
|
||||
precise: [3]PriceBar64 = .{
|
||||
.{.timestamp = 10, .open = 100000000.125, .high = 100000000.375,
|
||||
.low = 100000000.125, .close = 100000000.25, .volume = 2.0},
|
||||
.{.timestamp = 20, .open = 100000000.375, .high = 100000000.375,
|
||||
.low = 100000000.125, .close = 100000000.125, .volume = 8.0},
|
||||
.{.timestamp = 30, .open = 100000000.25, .high = 100000000.25,
|
||||
.low = 100000000.25, .close = 100000000.25}}
|
||||
projected: [3]PriceBar
|
||||
projection := CandleProject(precise[:], projected[:])
|
||||
if cast(float32)precise[0].low != cast(float32)precise[0].high ||
|
||||
projection.low != 100000000.125 || projection.high != 100000000.375 ||
|
||||
projection.span != 0.25 || projected[0].low != 1.0 ||
|
||||
projected[0].high != 2.0 || projected[0].close != 1.5 ||
|
||||
projected[1].open <= projected[1].close { return 25 }
|
||||
if projection.max_volume != 8.0 || projection.range.max_volume != 1.0 ||
|
||||
projected[0].volume != 0.25 || projected[1].volume != 1.0 ||
|
||||
projected[1].timestamp != 20 { return 26 }
|
||||
if CandleProjectedPrice(projection, 1.0) != precise[0].low ||
|
||||
CandleProjectedPrice(projection, 1.5) != precise[0].close ||
|
||||
CandleProjectedPrice(projection, 2.0) != precise[0].high ||
|
||||
CandleY(projected[0].high, bounds, projection.range) >=
|
||||
CandleY(projected[0].low, bounds, projection.range) { return 27 }
|
||||
|
||||
flat := CandleProject(precise[2:3], projected[0:1])
|
||||
if flat.span <= 0.0 || flat.low >= precise[2].low || flat.high <= precise[2].high ||
|
||||
flat.range.max_volume != 0.0 || projected[0].volume != 0.0 ||
|
||||
projected[0].open != 1.5 || projected[0].close != 1.5 ||
|
||||
CandleProjectedPrice(flat, 1.5) != precise[2].close { return 28 }
|
||||
zero: [1]PriceBar64
|
||||
flat_zero := CandleProject(zero[:], projected[0:1])
|
||||
if flat_zero.span <= 0.0 || projected[0].close != 1.5 ||
|
||||
CandleProjectedPrice(flat_zero, 1.5) != 0.0 { return 29 }
|
||||
if CandleProject(precise[0:0], projected[:]).span != 0.0 ||
|
||||
CandleProject(precise[:], projected[0:1]).span != 0.0 ||
|
||||
projected[0].close != 1.5 { return 30 }
|
||||
|
||||
latest := CandleViewportFor(precise[:], 0, 20, 2)
|
||||
if latest.first != 1 || latest.end != 3 || latest.selected != 1 ||
|
||||
latest.right_edge != 0 { return 31 }
|
||||
clamped := CandleViewportFor(precise[:], 5, 30, 2)
|
||||
if clamped.first != 0 || clamped.end != 2 || clamped.selected != 1 ||
|
||||
clamped.right_edge != 20 { return 32 }
|
||||
anchored := CandleViewportFor(precise[:], 25, 10, 2)
|
||||
if anchored.first != 0 || anchored.end != 2 || anchored.selected != 0 ||
|
||||
anchored.right_edge != 20 { return 33 }
|
||||
expanded := CandleViewportFor(precise[:], 5, 10, 10)
|
||||
if expanded.first != 0 || expanded.end != 3 || expanded.selected != 0 ||
|
||||
expanded.right_edge != 0 { return 34 }
|
||||
empty := CandleViewportFor(precise[0:0], 0, 0, 2)
|
||||
if empty.first != 0 || empty.end != 0 || empty.selected != -1 ||
|
||||
empty.right_edge != 0 { return 35 }
|
||||
one := CandleViewportFor(precise[:], 0, 0, 0)
|
||||
if one.first != 2 || one.end != 3 || one.selected != 2 { return 36 }
|
||||
if CandlePanEnd(2, 2147483647, 3, 2) != 3 ||
|
||||
CandlePanEnd(2, -2147483647, 3, 2) != 2 { return 37 }
|
||||
|
||||
slots: Rectangle = .{13.0, 2.0, 301.0, 99.0}
|
||||
for slot: 0..16 {
|
||||
index := cast(s32)slot
|
||||
if CandleIndexAtX(CandleCenterX(index, slots, 17), slots, 17) != index { return 38 }
|
||||
}
|
||||
if CandleCenterX(-1, slots, 17) != -1.0 || CandleCenterX(17, slots, 17) != -1.0 ||
|
||||
CandleCenterX(0, slots, 0) != -1.0 || CandleIndexAtX(314.0, slots, 17) != -1 { return 39 }
|
||||
return 0
|
||||
}
|
||||
+11
-11
@@ -90,6 +90,15 @@
|
||||
"source": true,
|
||||
"url": "https://aomedia.googlesource.com/aom"
|
||||
},
|
||||
{
|
||||
"commit": "452813114e40e2385485bf5f6bc555cb90371d16",
|
||||
"dependencies": {"audio": "p59deb49c7f103127", "avif": "p3ed49c5d0912e411", "image_codec": "p69c0e968ae5735d9", "libaom": "p74ab3cb6eb3b86fa", "libavif": "p57c0272208aae8db", "libwebp": "p3b3c785a0e17e605", "sdl3": "p16cd0434af3dff7b", "webp": "p2af20e0c835b1c63"},
|
||||
"dependency_refs": {"audio": "1b2f35b7b39df6fb9af7a52324b4b7cf18e98df2", "avif": "f5f186bb13569ef13338f81366ff63fb9b9282f6", "image_codec": "360553150917f352e49e1beb78917370792ccaef", "libaom": "b141d9c45db236335351c8a4b2a14b3ade27c937", "libavif": "1aadfad932c98c069a1204261b1856f81f3bc199", "libwebp": "1844042d1ca751e62e61df7617dba19c98588339", "sdl3": "829a65d769d935c4852f8159e964312c0957260a", "webp": "ec6ed304767e10c125ab2820f3b01d6fd773e147"},
|
||||
"id": "pb207d1188b60282a",
|
||||
"name": "kryon",
|
||||
"ref": "452813114e40e2385485bf5f6bc555cb90371d16",
|
||||
"url": "https://git.waozi.xyz/kryonlabs/kryon.git"
|
||||
},
|
||||
{
|
||||
"commit": "54706fc6bc0cdecab7e9593974a4039cc038fca7",
|
||||
"dependencies": {},
|
||||
@@ -99,15 +108,6 @@
|
||||
"source": true,
|
||||
"url": "https://github.com/videolan/dav1d.git"
|
||||
},
|
||||
{
|
||||
"commit": "452813114e40e2385485bf5f6bc555cb90371d16",
|
||||
"dependencies": {"audio": "p59deb49c7f103127", "avif": "p3ed49c5d0912e411", "image_codec": "p69c0e968ae5735d9", "libaom": "p74ab3cb6eb3b86fa", "libavif": "p57c0272208aae8db", "libwebp": "p3b3c785a0e17e605", "sdl3": "p16cd0434af3dff7b", "webp": "p2af20e0c835b1c63"},
|
||||
"dependency_refs": {"audio": "1b2f35b7b39df6fb9af7a52324b4b7cf18e98df2", "avif": "f5f186bb13569ef13338f81366ff63fb9b9282f6", "image_codec": "360553150917f352e49e1beb78917370792ccaef", "libaom": "b141d9c45db236335351c8a4b2a14b3ade27c937", "libavif": "1aadfad932c98c069a1204261b1856f81f3bc199", "libwebp": "1844042d1ca751e62e61df7617dba19c98588339", "sdl3": "829a65d769d935c4852f8159e964312c0957260a", "webp": "ec6ed304767e10c125ab2820f3b01d6fd773e147"},
|
||||
"id": "pe02c713a1b0b858a",
|
||||
"name": "kryon",
|
||||
"ref": "452813114e40e2385485bf5f6bc555cb90371d16",
|
||||
"url": "https://github.com/kryonlabs/kryon.git"
|
||||
},
|
||||
{
|
||||
"commit": "ddbe48383984d56acd9e1ab6a090c54ca6b735a6",
|
||||
"dependencies": {},
|
||||
@@ -119,13 +119,13 @@
|
||||
}
|
||||
],
|
||||
"root": {
|
||||
"dependencies": {"kryon": "pe02c713a1b0b858a"},
|
||||
"dependencies": {"kryon": "pb207d1188b60282a"},
|
||||
"dependency_refs": {"kryon": "452813114e40e2385485bf5f6bc555cb90371d16"},
|
||||
"name": "plot"
|
||||
},
|
||||
"toolchain": {
|
||||
"commit": "9432a110709c4a4f72c4f732fcfc368e15213a54",
|
||||
"ref": "9432a110709c4a4f72c4f732fcfc368e15213a54",
|
||||
"url": "https://github.com/ziranlang/ziran.git"
|
||||
"url": "https://git.waozi.xyz/ziranlang/ziran.git"
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -5,11 +5,11 @@ module_roots = ["src", "tests"]
|
||||
bridge_modules = ["app"]
|
||||
|
||||
[toolchain]
|
||||
git = "https://github.com/ziranlang/ziran.git"
|
||||
git = "https://git.waozi.xyz/ziranlang/ziran.git"
|
||||
ref = "9432a110709c4a4f72c4f732fcfc368e15213a54"
|
||||
|
||||
[dependencies.kryon]
|
||||
git = "https://github.com/kryonlabs/kryon.git"
|
||||
git = "https://git.waozi.xyz/kryonlabs/kryon.git"
|
||||
ref = "452813114e40e2385485bf5f6bc555cb90371d16"
|
||||
|
||||
[exports]
|
||||
|
||||
Reference in new issue
Block a user