【问题标题】:Calculate EMA for MACD with 2 lines indicator in C#在 C# 中使用 2 线指标计算 MACD 的 EMA
【发布时间】:2021-09-01 13:17:35
【问题描述】:

我正在尝试编写一个指标脚本,该脚本将在练习交易工具中用 2 条线绘制 MACD。

目前,我遵循的公式是using the EMA formula to calculate it.

我可以绘制图表。但不知何故,我的指标结果与元交易者 4 或交易视图中的结果并不完全相同。这些应用上的指标结果完全相同。

当我尝试从公式转换为实际代码时,我想我错过了一些东西。请帮我修复它。谢谢。

这是计算 EMA 的部分。

/// ==================================================================
/// ======================== calculations ============================
/// ==================================================================
public void Calculate()
{
    for (int i = 0; i < Bars.Length; i++){
        if (i >= SlowEMA) {
            MACD[i] = CalculateEMA(FastEMA, i) - CalculateEMA(SlowEMA, i);
            Signal[i] = CalculateEMA_MACD(MACD, SignalEMA, i);
            Histogram[i] = MACD[i] - Signal[i];
        }
    }
}

private double CalculateEMA(int Period, int index)
{
    var currentValue = 0d;
    var currentEMA = 0d;
    var yesterdayEMA = 0d;
    var smooth = 2d;
    var multiplier = smooth / (1 + Period);
    
    for (int i = 0; i < Period; i++){
        currentValue = GetPrice(index + i - Period);
        currentEMA = (currentValue * multiplier) + (yesterdayEMA * (1 - multiplier));
        yesterdayEMA = currentEMA;
    };
    return yesterdayEMA;
}

private double CalculateEMA_MACD(double[] MACD, int Period, int index)
{
    var currentValue = 0d;
    var currentEMA = 0d;
    var yesterdayEMA = 0d;
    var smooth = 2d;
    var multiplier = smooth / (1 + Period);
    for (int i = 0; i < Period; i++){
        currentValue = MACD[index + i - Period];
        currentEMA = (currentValue * multiplier) + (yesterdayEMA * (1 - multiplier));
        yesterdayEMA = currentEMA;
    };
    return yesterdayEMA;
}

private double GetPrice(int index)
{
    Bar bar = Bars[index];
    switch (Source)
    {
        case Sources.Close:
            return bar.Close;
        case Sources.Open:
            return bar.Open;
        case Sources.High:
            return bar.High;
        case Sources.Low:
            return bar.Low;
        case Sources.MedianPrice:
            return (bar.High + bar.Low) / 2;
        case Sources.TypicalPrice:
            return (bar.High + bar.Low + bar.Close) / 3;
        case Sources.WeightedClose:
            return (bar.High + bar.Low + bar.Close + bar.Close) / 4;
    }
    throw new NotSupportedException("Unsupported price source type: " + Source);
}

【问题讨论】:

    标签: c# algorithm charts algorithmic-trading indicator


    【解决方案1】:

    您的 EMA 计算逻辑似乎有误。根据您的代码,yesterdayEMA 始终是 0,因此 EMA 方程的右侧部分也是 0

    private double CalculateEMA(int Period, int index)
    {
        ...
        var yesterdayEMA = 0D;
        ...
        currentEMA = (currentValue * multiplier) + (yesterdayEMA * (1 - multiplier));
        currentEMA = (currentValue * multiplier) + 0
        ...
    }
    

    您需要将yesterdayEMA 存储在CalculateEMA 之外,并将其作为参数传递以进行递归计算。

    【讨论】:

      猜你喜欢
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-11
      • 2021-09-08
      • 1970-01-01
      • 2021-03-01
      • 1970-01-01
      相关资源
      最近更新 更多