Files
RustAst/examples/HMA.myc
T
Brummel 16af3ae9fc Refactor Examples to use assert_eq
This commit updates all example files to use `assert_eq!` for verifying
output, replacing the previous `Output:` comments. This change makes the
examples more robust and self-testing.

The benchmark results in some examples have also been slightly adjusted,
reflecting minor performance variations after the refactoring.

Additionally, a runtime panic handling mechanism has been introduced in
the VM for function calls, which improves error reporting for unexpected
panics.
2026-03-26 15:07:48 +01:00

71 lines
2.1 KiB
Plaintext

;; Benchmark: 98.9us
;; Benchmark-Repeat: 22
(do
;; make-sma Factory (O(1))
(def make-sma
(fn [lookback]
(do
(def s (series lookback :float))
(def running-sum 0.0)
(def count 0)
(fn [x]
(do
(if (< count lookback)
(do (assign running-sum (+ running-sum x)) (assign count (+ count 1)))
(do (assign running-sum (+ (- running-sum (s (- lookback 1))) x))))
(push s x)
(/ running-sum count))))))
;; make-wma Factory (O(1))
(def make-wma
(fn [lookback]
(do
;; Sicherstellen, dass lookback mindestens 1 ist
(def n (if (< lookback 1) 1 lookback))
(def s (series n :float))
(def price-sum 0.0)
(def weighted-sum 0.0)
(def count 0)
(def weight-total (/ (* n (+ n 1)) 2))
(fn [x]
(do
(if (< count n)
(do
(assign count (+ count 1))
(assign price-sum (+ price-sum x))
(assign weighted-sum (+ weighted-sum (* x count)))
(push s x)
(/ weighted-sum (/ (* count (+ count 1)) 2)))
(do
(def oldest (s (- n 1)))
(assign weighted-sum (+ (- weighted-sum price-sum) (* x n)))
(assign price-sum (+ (- price-sum oldest) x))
(push s x)
(/ weighted-sum weight-total))))))))
;; make-hma Factory (O(1))
;; Hull Moving Average: WMA(sqrt(n), 2*WMA(n/2) - WMA(n))
(def make-hma
(fn [lookback]
(do
(def n lookback)
(def wma1 (make-wma (trunc (/ n 2))))
(def wma2 (make-wma n))
(def wma-final (make-wma (trunc (sqrt n))))
(fn [x]
(do
(def v1 (wma1 x))
(def v2 (wma2 x))
(wma-final (- (* 2.0 v1) v2)))))))
;; Beispiele & Test
(def hma16 (make-hma 16))
(print "HMA (16) Initialisierung gestartet...")
(def i 1.0)
(while (<= i 20.0)
(do
(print "HMA Step" i ":" (hma16 (* i 10.0)))
(assign i (+ i 1))))
)