EMA计算的C#实现(c# Exponential Moving Average (EMA) indicator )

原来国外有个源码(TechnicalAnalysisEngine src 1.25)内部对EMA的计算是:

var copyInputValues = input.ToList();

for (int i = period; i < copyInputValues.Count; i++)
{
var resultValue = (copyInputValues[i] - returnValues.Last()) * multiplier + returnValues.Last();

returnValues.Add(resultValue);
}

var result = new EMAResult()
{
Values = returnValues,
StartIndexOffset = period - 1
};

可以明显看出,这样的计算方式与我们传统的不一致,甚至可能无法得出结果。经过对国内通达信等主力软件内的EMA算法研究得出用一下方法可以实现对EMA的计算。贴上C#的实现方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myEMA
{
public class myEMA
{

static void Main(string[] args)
{
double[] arr;
arr = new double[5]{2077,2077,2078,2083,2082};
List<double> dd=new List<double>(){2077,2077,2077,2078,2083,2082};

//EMAResult du= ;
var result=EMA(dd,5);
Console.WriteLine("{0}个result的值",result.Values.Count);

for(int i=0;i<result.Values.Count;i++ ){
Console.WriteLine("第{0}的ema={1}",i,result.Values[i]);
}
Console.WriteLine("emaR={0}",result.EmaR );
}
/// <summary>
/// Contains calculation results for EMA indicator
/// </summary>
public class EMAResult
{
public List<double> Values { get; set; }
public int StartIndexOffset { get; set; }
public double EmaR { get; set; }

}

//-------------------------------------------------------------------------------------------------------------------------------

/// <summary>
/// Calculates Exponential Moving Average (EMA) indicator
/// </summary>
/// <param name="input">Input signal</param>
/// <param name="period">Number of periods</param>
/// <returns>Object containing operation results</returns>
public static EMAResult EMA(IEnumerable<double> input, int period)
{
var returnValues = new List<double>();

double multiplier = (2.0 / (period + 1));
//double initialSMA = input.Take(period).Average();

//returnValues.Add(initialSMA);

var copyInputValues = input.ToList();

int j=0;
for (int i = copyInputValues.Count-period; i < copyInputValues.Count; i++)
{
if(j<1)
{
var resultValue =copyInputValues[i];
returnValues.Add(resultValue);
}
else
{
var resultValue = (copyInputValues[i]*multiplier )+(1- multiplier)* returnValues.Last();
returnValues.Add(resultValue);
}
j++;

}

var result = new EMAResult()
{
EmaR=returnValues.Last(),
Values = returnValues,
StartIndexOffset = period - 1
};

return result;
}

}
}

时间: 2024-08-24 04:30:27

EMA计算的C#实现(c# Exponential Moving Average (EMA) indicator )的相关文章

[LeetCode] 346. Moving Average from Data Stream 从数据流中移动平均值

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example,MovingAverage m = new MovingAverage(3);m.next(1) = 1m.next(10) = (1 + 10) / 2m.next(3) = (1 + 10 + 3) / 3m.next(5) = (10 + 3

(转)滑动平均法、滑动平均模型算法(Moving average,MA)

原文链接:https://blog.csdn.net/qq_39521554/article/details/79028012 什么是移动平均法? 移动平均法是用一组最近的实际数据值来预测未来一期或几期内公司产品的需求量.公司产能等的一种常用方法.移动平均法适用于即期预测.当产品需求既不快速增长也不快速下降,且不存在季节性因素时,移动平均法能有效地消除预测中的随机波动,是非常有用的.移动平均法根据预测时使用的各元素的权重不同 移动平均法是一种简单平滑预测技术,它的基本思想是:根据时间序列资料.逐

Leetcode 346. Moving Average from Data Stream

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example, MovingAverage m = new MovingAverage(3); m.next(1) = 1 m.next(10) = (1 + 10) / 2 m.next(3) = (1 + 10 + 3) / 3 m.next(5) = (1

Moving Average from Data Stream

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example,MovingAverage m = new MovingAverage(3);m.next(1) = 1m.next(10) = (1 + 10) / 2m.next(3) = (1 + 10 + 3) / 3m.next(5) = (10 + 3

[LeetCode] Moving Average from Data Stream 从数据流中移动平均值

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example,MovingAverage m = new MovingAverage(3);m.next(1) = 1m.next(10) = (1 + 10) / 2m.next(3) = (1 + 10 + 3) / 3m.next(5) = (10 + 3

LeetCode 346. Moving Average from Data Stream (数据流动中的移动平均值)

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example, MovingAverage m = new MovingAverage(3); m.next(1) = 1 m.next(10) = (1 + 10) / 2 m.next(3) = (1 + 10 + 3) / 3 m.next(5) = (1

Moving Average from Data Stream -- LeetCode

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example, MovingAverage m = new MovingAverage(3); m.next(1) = 1 m.next(10) = (1 + 10) / 2 m.next(3) = (1 + 10 + 3) / 3 m.next(5) = (1

346. Moving Average from Data Stream

/* * 346. Moving Average from Data Stream * 2016-7-11 by Mingyang * 这里注意的就是(double) sum / count * sum需要转换成double才能继续往下除,因为不转的话最后不能成为整数14除以3等于4 */ class MovingAverage { public Queue<Integer> queue; public int sum = 0; public int si; public int count

tensorflow中moving average的正确用法

一般在保存模型参数的时候,都会保存一份moving average,是取了不同迭代次数模型的移动平均,移动平均后的模型往往在性能上会比最后一次迭代保存的模型要好一些. tensorflow-models项目中tutorials下cifar中相关的代码写的有点问题,在这写下我自己的做法: 1.构建训练模型时,添加如下代码 1 variable_averages = tf.train.ExponentialMovingAverage(0.999, global_step) 2 variables_a