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 ...
随机推荐
- new 与override 区别
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace Newover ...
- bzoj1303
首先找到b的位置x,要求满足b为中位数的数列把比b大的设为1,比b小设为-1则数列满足条件则x 左边的的和+x右边的和=0 然后应该就很好搞了 ..] of longint; i,n,m,x,k,an ...
- C#调用C++动态库(dll)
在实际软件开发过程中,由于公司使用了多种语言开发,在C#中可能需要实现某个功能,而该功能可能用其他语言已经实现了,那么我们可以调用其他语言写好的模块吗?还有就是,由于C#开发好的项目,我们可以利用re ...
- DD-WRT相关资源
版本网站下载:Other Downloads,进入betas->2014 FTP下载:如ftp://ftp.dd-wrt.com/betas/2014/06-23-2014-r24461/ Re ...
- 【转】unity3d input输入
Input 输入 按键 Input.GetKey(“up”) = Input.GetKey(KeyCode.UpArrow) 按住键盘上键 Input.GetKeyDown (“up”) 按下键盘上键 ...
- h2 database
java -cp h2-1.4.187.jar org.h2.tools.Shell -url jdbc:h2:file:~/.h2/hzhssh -user sa 如果有个数据库的文件名为:hzhs ...
- Java中的一些常见错误
1.空指针错误 在java数组的使用中,有时候需要对字符串数组中的元素进行对比.那么当元素不为null时,程序会正常运行:然而,一旦对比的元素为null,那么程序就会出现空指针错误. 解决方法:加入保 ...
- Hibernate自定查询返回list<?>
/** * * @param session * :一个会话 * * @param hql * :是需要执行的hql语句, * * @param offset * 设置开始位置 * * @param ...
- java实现链表结构
1. 定义节点node public class Node<T> { private Node<T> pre; private Node<T> next; priv ...
- MySQL基础 (DML)
DML语句 DML操作是指对数据库中表记录的操作,主要包括表记录的插入(insert).更新(update).删除(delete)和查询(select) 1.插入记录 插入一条 ...