用.NET语言写出计算股票技术指标KDJ,4个参数,返回值为datatable类型,double类型全部四舍五入保留3位小数。
时间: 2023-05-17 16:07:42 浏览: 94
KDJ股票技术参数讲解
3星 · 编辑精心推荐
以下是用.NET语言写出计算股票技术指标KDJ的代码:
```csharp
using System;
using System.Data;
public class KDJ
{
public static DataTable Calculate(double[] high, double[] low, double[] close, int n, int m1, int m2)
{
int count = close.Length;
double[] rsv = new double[count];
double[] k = new double[count];
double[] d = new double[count];
double[] j = new double[count];
for (int i = 0; i < count; i++)
{
double maxHigh = double.MinValue;
double minLow = double.MaxValue;
for (int j = i; j >= Math.Max(0, i - n + 1); j--)
{
maxHigh = Math.Max(maxHigh, high[j]);
minLow = Math.Min(minLow, low[j]);
}
rsv[i] = (close[i] - minLow) / (maxHigh - minLow) * 100;
}
k[0] = 50;
d[0] = 50;
for (int i = 1; i < count; i++)
{
k[i] = k[i - 1] * (m1 - 1) / m1 + rsv[i] / m1;
d[i] = d[i - 1] * (m2 - 1) / m2 + k[i] / m2;
j[i] = 3 * k[i] - 2 * d[i];
}
DataTable result = new DataTable();
result.Columns.Add("K", typeof(double));
result.Columns.Add("D", typeof(double));
result.Columns.Add("J", typeof(double));
for (int i = 0; i < count; i++)
{
DataRow row = result.NewRow();
row["K"] = Math.Round(k[i], 3);
row["D"] = Math.Round(d[i], 3);
row["J"] = Math.Round(j[i], 3);
result.Rows.Add(row);
}
return result;
}
}
```
其中,high、low、close分别为最高价、最低价、收盘价的数组,n为天数,m1、m2为平滑系数。函数返回一个DataTable类型的结果,包含K、D、J三个指标,每个指标都是double类型,保留3位小数。
阅读全文