Files
plot/examples/dashboard_charts.zi
2026-11-21 17:12:47 -03:00

104 lines
4.7 KiB
Ziran

using Plot :: #import "Plot";
#import "kryon/geometry"
#import "kryon/session"
// One card uses the shared scale. The second call uses the original strategy
// values and unit with its own observed range.
Equity :: (session: Session, bounds: Rectangle) {
selected: [4]Plot.XYPointValue = .{
.{1640995200.0, 100.0, false}, .{1648771200.0, 102.9, false},
.{1664582400.0, 0.0, true}, .{1672444800.0, 110.37, false}}
benchmark: [3]Plot.XYPointValue = .{
.{1640995200.0, 100.0, false}, .{1648771200.0, 163.7, false},
.{1672444800.0, 293.58, false}}
series: [2]Plot.XYSeries = .{
.{.name = "Selected", .points = selected[:]},
.{.name = "Buy and hold", .points = benchmark[:]}}
props := Plot.ChartProps.{.key = 11, .bounds = bounds,
.label = "Equity comparison", .date_x = true, .y_unit = "index",
.x_min = 1640995200.0, .x_max = 1672444800.0}
unused Plot.XYChart(session, props, series[:])
props.key = 12
props.label = "Selected under stress"
props.bounds.x += bounds.width + 12.0
unused Plot.XYChart(session, props, series[0:1])
}
Returns :: (session: Session, bounds: Rectangle) {
years: [3]string = .{"2020", "2022", "2025"}
strategy: [3]Plot.ChartValue = .{.{4.0, false}, .{10.37, false}, .{-6.43, false}}
hold: [3]Plot.ChartValue = .{.{-14.8, false}, .{-64.2, false}, .{-18.5, false}}
returns: [2]Plot.BarSeries = .{
.{.name = "Selected", .values = strategy[:]}, .{.name = "Buy and hold", .values = hold[:]}}
unused Plot.BarChart(session, .{.key = 13, .bounds = bounds,
.label = "Returns in down years", .y_unit = "%"}, years[:], returns[:])
differences: [3]Plot.ChartValue
for i: 0..2 { differences[i].value = strategy[i].value - hold[i].value }
delta: [1]Plot.BarSeries = .{.{.name = "Return difference", .values = differences[:]}}
beside := bounds
beside.x += bounds.width + 12.0
unused Plot.BarChart(session, .{.key = 14, .bounds = beside,
.label = "Outperformance", .y_unit = "pp", .horizontal = true,
.hide_legend = true}, years[:], delta[:])
}
Contributions :: (session: Session, bounds: Rectangle) {
cols: [3]string = .{"2020", "2022", "2025"}
rows: [2]string = .{"Momentum", "Carry"}
cells: [6]Plot.ChartValue = .{.{6.0, false}, .{-12.0, false}, .{0.0, true},
.{0.0, false}, .{5.0, false}, .{-4.2, false}}
unused Plot.HeatmapChart(session, .{.key = 15, .bounds = bounds,
.label = "Trait returns", .y_unit = "%"}, cols[:], rows[:], cells[:])
steps: [4]Plot.WaterfallStep = .{.{"Start", 18.5, true, false},
.{"Signals", 7.25, false, false}, .{"Costs", -4.38, false, false},
.{"End", 21.37, true, false}}
beside := bounds
beside.x += bounds.width + 12.0
unused Plot.WaterfallChart(session, .{.key = 16, .bounds = beside,
.label = "Return contributions", .y_unit = "pp"}, steps[:])
}
Diagnostics :: (session: Session, bounds: Rectangle) {
bars: [3]Plot.PriceBar64 = .{
.{.timestamp = 1640995200, .open = 100, .high = 118, .low = 98, .close = 111},
.{.timestamp = 1641081600, .open = 110, .high = 115, .low = 104, .close = 106},
.{.timestamp = 1641340800, .open = 106, .high = 125, .low = 105, .close = 121}}
allocation: [4]Plot.XYPointValue = .{.{1640995200.0, 25.0, false},
.{1641081600.0, 60.0, false}, .{1641254400.0, 15.0, false},
.{1641340800.0, 15.0, false}}
trades: [2]Plot.XYPointValue = .{.{1641081600.0, 60.0, false}, .{1641254400.0, 15.0, false}}
marks: [2]Plot.XYSeries = .{
.{.name = "Allocation", .mark = .XYStep, .points = allocation[:]},
.{.name = "Trade", .mark = .XYPoint, .points = trades[:]}}
price := Plot.ChartProps.{.key = 17, .label = "Price", .bounds = bounds,
.x_min = 1640995200.0, .x_max = 1641340800.0, .y_unit = "USD"}
allocations := price
allocations.key = 18
allocations.label = "Allocation and trades"
allocations.y_unit = "%"
allocations.date_x = true
allocations.bounds.x += bounds.width + 12.0
// Resolve the local hit first, then use it for the second pane. A linked
// crosshair can also be fed to every pane from an app-owned selected x.
hit := Plot.TimeCandleChart64(session, price, bars[:])
allocations.link_hover = hit.active
allocations.hover_x = hit.x
unused Plot.XYChart(session, allocations, marks[:])
}
#program_export
Draw :: (session: Session, viewport: Rectangle) {
width := (viewport.width - 36.0) / 2.0
height := (viewport.height - 54.0) / 4.0
bounds := Rectangle.{12.0, 9.0, width, height}
Equity(session, bounds)
bounds.y += height + 12.0
Returns(session, bounds)
bounds.y += height + 12.0
Contributions(session, bounds)
bounds.y += height + 12.0
Diagnostics(session, bounds)
}