shape-runtime 0.3.2

Bytecode compiler, builtins, and runtime infrastructure for Shape
Documentation
/// @module std::finance::types::ohlcv
/// OHLCV Data Type Definition
/// Standard Open-High-Low-Close-Volume market data structure
///
/// OHLCV data is now represented as generic Objects with specific fields.
/// These helper functions provide convenience methods for working with OHLCV data.

/// Return whether `row` exposes the canonical OHLCV fields.
pub fn is_ohlcv(row) {
    row.open != None
        and row.high != None
        and row.low != None
        and row.close != None
        and row.volume != None
}

// Computed properties

/// Return the absolute candle body size.
///
/// @see std::finance::types::ohlcv::upper_wick
/// @see std::finance::types::ohlcv::lower_wick
pub fn body(row) {
    abs(row.close - row.open)
}

/// Return the length of the upper wick.
pub fn upper_wick(row) {
    row.high - max(row.open, row.close)
}

/// Return the length of the lower wick.
pub fn lower_wick(row) {
    min(row.open, row.close) - row.low
}

/// Return the full high-low range of the candle.
pub fn candle_range(row) {
    row.high - row.low
}

/// Return whether the candle closes at or above the open.
pub fn is_green(row) {
    row.close >= row.open
}

/// Return whether the candle closes below the open.
pub fn is_red(row) {
    row.close < row.open
}

/// Return whether the candle body is small relative to its range.
///
/// @param row Candidate OHLCV row.
/// @param threshold Optional maximum body-to-range ratio.
/// @returns True when the candle is classified as a doji.
pub fn is_doji(row, threshold) {
    let thresh = if threshold == None then 0.001 else threshold;
    let body_size = body(row);
    let range_size = range(row);
    range_size > 0 and (body_size / range_size) < thresh
}