135 lines
6.5 KiB
Ziran
135 lines
6.5 KiB
Ziran
#import "candle_chart"
|
|
#import "kryon/geometry"
|
|
|
|
#program_export
|
|
main :: () -> s32 {
|
|
bars: [5]PriceBar
|
|
closes: [5]float32 = .{1.0, 2.0, 3.0, 10.0, 5.0}
|
|
index: s32 = 0
|
|
while index < 5 {
|
|
bars[index].timestamp = 1000 + cast(s64)index * 86400
|
|
bars[index].close = closes[index]
|
|
bars[index].high = closes[index]
|
|
bars[index].low = closes[index]
|
|
bars[index].volume = cast(float32)(index + 1)
|
|
index += 1
|
|
}
|
|
values: []PriceBar = bars[:]
|
|
if SMAAt(values, 1, 3) != 0.0 { return 1 }
|
|
if SMAAt(values, 2, 3) != 2.0 { return 2 }
|
|
if SMAAt(values, 4, 3) != 6.0 { return 3 }
|
|
if EMAAt(values, 2, 3) != 2.0 { return 4 }
|
|
if EMAAt(values, 3, 3) != 6.0 { return 5 }
|
|
if EMAAt(values, 4, 3) != 5.5 { return 6 }
|
|
if CandleVisibleStart(5, 3) != 2 { return 7 }
|
|
range: CandleRange = CandleRangeFor(values, 3)
|
|
if range.low != 3.0 || range.high != 10.0 ||
|
|
range.max_volume != 5.0 { return 8 }
|
|
bounds: Rectangle = Rectangle.{0.0, 0.0, 300.0, 100.0}
|
|
visible: float32 = CandleMarkerX(values, 3, bounds,
|
|
bars[3].timestamp)
|
|
missing: float32 = CandleMarkerX(values, 3, bounds,
|
|
bars[0].timestamp)
|
|
if visible != 150.0 { return 9 }
|
|
if missing != -1.0 { return 10 }
|
|
intrabar: float32 = CandleMarkerX(values, 3, bounds,
|
|
bars[3].timestamp + 1234)
|
|
if intrabar != visible { return 11 }
|
|
micro: [2]PriceBar
|
|
micro[0] = .{.timestamp = 1, .low = 0.000010,
|
|
.high = 0.000011, .close = 0.0000105}
|
|
micro[1] = .{.timestamp = 2, .low = 0.0000102,
|
|
.high = 0.0000112, .close = 0.000011}
|
|
micro_range: CandleRange = CandleRangeFor(micro[:], 2)
|
|
if micro_range.max_value - micro_range.min_value >= 0.001 {
|
|
return 12
|
|
}
|
|
single: [1]PriceBar
|
|
single[0].timestamp = 1000
|
|
if CandleMarkerX(single[:], 1, bounds, 1000) != 150.0 { return 13 }
|
|
if CandleMarkerX(single[:], 1, bounds, 999) != -1.0 { return 14 }
|
|
if CandleMarkerX(single[:], 1, bounds, 1001) != -1.0 { return 15 }
|
|
after_last: s64 = bars[4].timestamp + 86400
|
|
if CandleMarkerX(values, 3, bounds, after_last) != -1.0 { return 16 }
|
|
log_range: CandleRange
|
|
log_range.min_value = 10.0; log_range.max_value = 1000.0
|
|
log_range.logarithmic = true
|
|
middle := CandleY(100.0, bounds, log_range)
|
|
if middle < 49.99 || middle > 50.01 { return 17 }
|
|
inverse := CandlePriceAtY(middle, bounds, log_range)
|
|
if inverse < 99.99 || inverse > 100.01 { return 18 }
|
|
if CandleY(0.0, bounds, log_range) != -1.0 { return 19 }
|
|
padded := CandleRangeFor(micro[:], 2, true)
|
|
if padded.min_value <= 0.0 || padded.min_value >= padded.low ||
|
|
padded.max_value <= padded.high { return 20 }
|
|
if CandleWindowEnd(values, bars[2].timestamp) != 3 { return 21 }
|
|
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
|
|
}
|