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;
}
}
}
EMA计算的C#实现(c# Exponential Moving Average (EMA) indicator )的更多相关文章
- 理解滑动平均(exponential moving average)
1. 用滑动平均估计局部均值 滑动平均(exponential moving average),或者叫做指数加权平均(exponentially weighted moving average),可以 ...
- (转)理解滑动平均(exponential moving average)
转自:理解滑动平均(exponential moving average) 1. 用滑动平均估计局部均值 滑动平均(exponential moving average),或者叫做指数加权平均(exp ...
- [Swift]LeetCode346. 从数据流中移动平均值 $ Moving Average from Data Stream
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- 346. Moving Average from Data Stream数据窗口流中位数的数据结构设计
[抄题]: Given a stream of integers and a window size, calculate the moving average of all integers in ...
- (转)滑动平均法、滑动平均模型算法(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 sl ...
- 一文详解滑动平均法、滑动平均模型法(Moving average,MA)
任何关于算法.编程.AI行业知识或博客内容的问题,可以随时扫码关注公众号「图灵的猫」,加入”学习小组“,沙雕博主在线答疑~此外,公众号内还有更多AI.算法.编程和大数据知识分享,以及免费的SSR节点和 ...
- 【LeetCode】346. Moving Average from Data Stream 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcode ...
- [LeetCode] Moving Average from Data Stream 从数据流中移动平均值
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
随机推荐
- 余弦距离、欧氏距离和杰卡德相似性度量的对比分析 by ChaoSimple
1.余弦距离 余弦距离,也称为余弦相似度,是用向量空间中两个向量夹角的余弦值作为衡量两个个体间差异的大小的度量. 向量,是多维空间中有方向的线段,如果两个向量的方向一致,即夹角接近零,那么这两个向 ...
- 去掉eclipse js 错误提示
1.去掉项目目录底下的.project文件中的以下部分:<buildCommand> <name>org.eclipse.wst.jsdt.core.javascri ...
- 【转】Java中如何遍历Map对象的4种方法
原文网址:http://blog.csdn.net/tjcyjd/article/details/11111401 在Java中如何遍历Map对象 How to Iterate Over a Map ...
- 2015第44周五Java集群技术(转)
从http://blog.csdn.net/cdh1213/article/details/21443239上看到这篇文章,感觉很不错,找好久没找到中文出处,最早看是从http://www.these ...
- 在ASP.NET MVC中对手机号码的验证
在ASP.NET MVC中,可以使用RegularExpression特性来验证手机号码. public class Customer { [Required(ErrorMessage = " ...
- Delphi 6 Web Services初步评估
Delphi 6 Web Services初步评估这是我刚到现在公司的时候(2001年8月份)所作的一份测试报告,现公布出来,希望能对大家有所帮助.因为当时d6刚刚发行,Web Service方面还存 ...
- U盘安装 OSX
首先,刚从app store下载完的OS X Lion会放在屏幕下方的Dock中. 用鼠标将Mac OS X Lion 10.7文件从Dock中拖放到桌面. 右键单击Mac OS X Lion 10. ...
- Unity3d 真实的植物渲染
好久没写shader了,有些生疏,刚弄了个植物shader,分享一下. 先上图片: 重点需要注意的是fragment shader的透明部分 需要如此声明 Tags{ "LightMode& ...
- PHP中Content-type的MIME类型大全说明
<?php $mimetypes = array( 'ez' => 'application/andrew-inset', 'hqx' => 'application ...
- Google的一些功能和软件
本博文的主要内容有 .Google的一些功能和软件 Google的一些功能和软件 1. iGoogle 2. Google Earth 3. Google Talk http://www.goo ...