Skip to content

calculate_macd return NaN signals always #3

Description

@mongris

in calcuate_macd,

let signal = calculate_ema(&temp_df, column, signal_period)?;

but some values are NaN, then in calculate_ema:

let mut sma_sum = 0.0;
for i in 0..window {
        let val = series_ca.get(i).unwrap_or(0.0);
        sma_sum += val;

       ...
}

cause sam_sum always be NaN.
series_ca.get(i).unwrap_or(0.0) return NaN if value is NaN.

and,

for i in window..series.len() {
        let price = series_ca.get(i).unwrap_or(0.0);
        ...
        let ema = alpha * price + (1.0 - alpha) * prev_ema;

ema will be NaN too.

Can be fixed as:

for i in 0..window {
        let val = series_ca.get(i).unwrap_or(0.0);
        if !val.is_nan() {
            sma_sum += val;
        }

        // Fill with nulls until we have enough data
        if i < window - 1 {
            ema_values.push(f64::NAN);
        }
    }
for i in window..series.len() {
        let price = series_ca.get(i).unwrap_or(0.0);
        if price.is_nan() {
            ema_values.push(f64::NAN);
            continue;
        }
        let ema = alpha * price + (1.0 - alpha) * prev_ema;
        log::info!("alpha: {}, price: {}, prev_ema: {}, ema: {}", alpha, price, prev_ema, ema);
        ema_values.push(ema);
        prev_ema = ema;
    }

but maybe shouldn't apply to the original calculate_ema fn.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions