Simple Moving Average (SMA)
SMA = (P₁ + P₂ + ... + Pₙ) / nPeriod
Closing Price
SMA₅ = (20 + 22 + 21 + 23 + 24) / 5 = 110 / 5 = 22.0Reading Simple Moving Average (SMA) on Supra L1
Last updated
SMA = (P₁ + P₂ + ... + Pₙ) / nSMA₅ = (20 + 22 + 21 + 23 + 24) / 5 = 110 / 5 = 22.0Last updated
SMA₅ = (22 + 21 + 23 + 24 + 26) / 5 = 116 / 5 = 23.2#[view]
public fun compute_sma(
pair_id: u32, // Unique identifier of the trading pair
period: u64, // Number of candles to average [9, 20, 50, 200]
candle_duration: u64, // Duration in milliseconds
missing_candles_tolerance_percentage: u64 // Max missing candles (two-decimal fixed-point)
): Option<u128>use supra_oracle::supra_oracle_ti;
use std::option;
// Get 20-period SMA for BTC on 1-hour timeframe with 10% tolerance
let sma = supra_oracle_ti::compute_sma(
1, // pair_id (BTC)
20, // period
3_600_000, // candle_duration (1 hour)
1000 // 10.00% missing candles tolerance
);
if (option::is_some(&sma)) {
let sma_value = option::extract(&mut sma);
// Use sma_value in your logic