c# - 将反向移动平均公式转换为 C#

标签 c# moving-average trading algorithmic-trading quantitative-finance

我一直在绞尽脑汁试图将这个公式转换为 C#,但没有成功。

REMA Formula . enter image description here
我在做 proggies 方面很不错,但在数学方面不行。

Where 0 < λ ≤ 1 is a decay factor.
When λ < 1, the exponentially weighted moving average assigns greater weights to the prices.

Contrary to the regular exponential moving average that gives greater weights to the most recent prices, the reverse exponential moving average assigns greater weights to the oldest prices and decreases the importance of the most recent prices.

最佳答案

高效的 REMA() 具有错误检查和特定于上下文的返回值

高效?是的,避免重复重复的昂贵操作(或依赖编译器优化技巧)

有错误检查?是的,几乎是 Q/A 程序的必需品。

特定于上下文? 是,返回 { -1。 | REMA( k, lambda ) } 允许调用者处理输入错误的极端情况。

double[] fTimeSeriesPRICE;     // a forward-stepping storage ( vs. a MT4 reversed-stepping model )

public double REMA( int k, double lambda )
{   
    if (  lambda <= 0.0        // lambda shall not fall on/under  0.0
       || lambda >  1.0        //        shall not grow beyond    1.0
       || k      <= 0          // depth  shall not be negative or 0
       )
       return -1.0;            // context-protecting RET value

    int aCurrentPricePTR  = fTimeSeriesPRICE.Length - 1;
    int aMaxIterableDEPTH = Math.Min( aCurrentPricePTR, k );

    double numerator   = 0.0;
    double denominator = 0.0;  // REMA formula-expansion contains +1 at the end, never less, never negative
    double lambdator   = 1.0;  // lambda ^ ( ( k - j ) == 0 ) == 1.0

    for ( int aReverseSteppingPTR  = 0;
              aReverseSteppingPTR <= aMaxIterableDEPTH;
              aReverseSteppingPTR++
              )
    {   numerator   += lambdator * fTimeSeriesPRICE[aCurrentPricePTR - aReverseSteppingPTR];
        denominator += lambdator;
        lambdator   *= lambda;
    }
    return numerator / denominator; // numerically fair, denominator never < +1.0
}

关于c# - 将反向移动平均公式转换为 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34936981/

相关文章:

python - 如何使用基于numpy中日期的窗口获取时间序列的回顾移动平均值?

javascript - 如果 OHLC 中的开盘价和收盘价相同,我可以在烛台上使用不同的颜色吗?

c# - 扩展控件时调整窗口大小

c# - 为什么我的 INSERT 语句会产生重复?

moving-average - Clickhouse移动平均线

MySQL 5 分钟移动平均线(按日期范围对行进行分组)

python - TradingView STC 与任何 python STC

arrays - 如何从 MQL4 中的函数返回数组?

c# - 乐观并发

c# - HttpWebrequests facebook 登录并请求应用 token