新修改了EMA的计算方法,合并线性回归率的计算。和通达信的结果一模一样

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

namespace myEMA
{
public class myEMA
{


/// <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; }

}

//-------------------------------------------------------------------------------------------------------------------------------
#region 计算MYEMA
/// <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();

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

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

return result;
}
#endregion
public class mySlope
{
// public List<double> Values { get; set; }

public double SlopeResult { get; set; }

}
//-------------------------------------------------------------------------------------------------------------------------------
#region 计算slope
/// <summary>
/// Calculates slope()
/// </summary>
/// <param name="input">Input y_signal</param>
/// <param name="period">Number of periods</param>
/// <returns>Object containing operation results</returns>
public static mySlope Slope(IEnumerable<double> input_y, int period)
{ // var returnValues = new List<double>();
List<double> input_x = new List<double>();
for (int i = 1; i <= period; i++)
{
input_x.Add(i);

}

var copyInputValues_x = input_x.ToList();
var copyInputValues_y = input_y.ToList();
List<double> arr_xy=new List<double>();
List<double> arr_xx=new List<double>();
List<double> arr_x = new List<double>();
List<double> arr_y = new List<double>();
arr_x = copyInputValues_x;
for(int j=copyInputValues_y.Count-period;j<copyInputValues_y.Count;j++)
{

//arr_x.Add(copyInputValues_x[j]);
arr_y.Add(copyInputValues_y[j]);

}
double x_arr_dataAv = arr_x.Take(period).Average();
double y_arr_dataAv = arr_y.Take(period).Average();
for(int i=0;i<arr_x.Count;i++)
{
arr_x[i] = arr_x[i] - x_arr_dataAv;
arr_y[i] = arr_y[i] - y_arr_dataAv;
arr_xx.Add( arr_x[i] * arr_x[i]);
arr_xy.Add ( arr_y[i] * arr_x[i]);
}
double sumxx = arr_xx.Sum();
double sumxy = arr_xy.Sum();



var result = new mySlope()
{
SlopeResult = sumxy/sumxx,
// Values = returnValues,
};
return result;
}
#endregion

 

}
}

 

你可能感兴趣的:(方法)