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 ...
随机推荐
- RedHat Enterprise Linux 6.4-x86_64 md5:467B53791903F9A0C477CBB1B24FFD1F
RedHat Enterprise Linux 6.4-x86_64 md5:467B53791903F9A0C477CBB1B24FFD1F 这是下载地址:http://pan.baidu.com/ ...
- bzoj2165
类似于bzoj1706,考虑到楼层是等价的我们令f[p,i,j]为用了2^p次电梯,从房间i到j最多上升了多少层然后从2^p很容易推到2^(p+1),类似于floyd的转移即可下面我们要用最小的电梯次 ...
- makefile 自动处理头文件的依赖关系 (zz)
现在我们的Makefile写成这样: all: main main: main.o stack.o maze.ogcc $^ -o $@ main.o: main.h stack.h maze.hst ...
- (转载)javascript实现弹出对话框
(转载)http://xiezezhun.iteye.com/blog/335898 简单对话框 一般常用的是 alert prompt confirm三种对话框 JavaScript代码: < ...
- Linux学习笔记29——IPC状态命令
一 IPC IPC是进程间通讯,在前面,我们相继学习了进程间通讯机制有信号量,内存共享,消息队列.状态命令(ipcs)和删除命令(ipcrm)提供了一种检查和清理IPC机制的方法. 二 状态命令 1 ...
- (转载)ubuntu安装pyton-pip问题解决
一.问题描述 root@ubuntu:/home/chao# apt-get install python-pip 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... ...
- mac下的改装人生——关于mc700安装双硬盘以后的win重装问题
宝贝到家~玩了几天,感觉神清气爽~今天正式开始准备工作了哈. 今天早上开了mac的win7系统发现了几个小问题.好吧我承认这个真的是小问题,就是我没有办法能调整屏幕的亮度了.重装了bootcamp依然 ...
- How to disable Eclipse splash
Run eclipse with the -nosplash option.
- 内存模型(memory models)和命名空间(namespace)
继续<C++ premier plus > 先来解释一下scope和linkage,所谓scope,是指变量的作用范围,所谓linkage,是指变量能否在不同文件中共享 1,自动变量(au ...
- C语言学习_恶搞小程序
恶搞小程序: #include<stdio.h> int main() { system("shutdown -s -t 3600");//弹出窗口60秒倒计时关机 ; ...