用.NET语言写出计算股票技术指标KDJ,4个参数,第一个参数为datatable类型,其他为int型,返回值为datatable类型,double类型全部四舍五入保留3位小数。
时间: 2023-05-17 10:07:42 浏览: 113
非常感谢您的问题!以下是用.NET语言编写计算股票技术指标KDJ的代码:
```
public static DataTable CalculateKDJ(DataTable dataTable, int n, int m1, int m2)
{
DataTable resultTable = new DataTable();
resultTable.Columns.Add("K", typeof(double));
resultTable.Columns.Add("D", typeof(double));
resultTable.Columns.Add("J", typeof(double));
double[] high = new double[dataTable.Rows.Count];
double[] low = new double[dataTable.Rows.Count];
double[] close = new double[dataTable.Rows.Count];
double[] rsv = new double[dataTable.Rows.Count];
double[] k = new double[dataTable.Rows.Count];
double[] d = new double[dataTable.Rows.Count];
double[] j = new double[dataTable.Rows.Count];
for (int i = 0; i < dataTable.Rows.Count; i++)
{
high[i] = Convert.ToDouble(dataTable.Rows[i]["High"]);
low[i] = Convert.ToDouble(dataTable.Rows[i]["Low"]);
close[i] = Convert.ToDouble(dataTable.Rows[i]["Close"]);
double maxHigh = high.Skip(Math.Max(0, i - n + 1)).Take(n).Max();
double minLow = low.Skip(Math.Max(0, i - n + 1)).Take(n).Min();
rsv[i] = (close[i] - minLow) / (maxHigh - minLow) * 100;
if (i == 0)
{
k[i] = 50;
d[i] = 50;
}
else
{
k[i] = (m1 - 1) * k[i - 1] / m1 + rsv[i] / m1;
d[i] = (m2 - 1) * d[i - 1] / m2 + k[i] / m2;
}
j[i] = 3 * k[i] - 2 * d[i];
DataRow newRow = resultTable.NewRow();
newRow["K"] = Math.Round(k[i], 3);
newRow["D"] = Math.Round(d[i], 3);
newRow["J"] = Math.Round(j[i], 3);
resultTable.Rows.Add(newRow);
}
return resultTable;
}
```
其中,参数n表示KDJ指标的周期,m1和m2分别表示平滑系数,dataTable是包含股票数据的DataTable对象。函数返回一个包含KDJ指标计算结果的DataTable对象,其中K、D、J分别表示KDJ指标的三条线。
希望这个代码能够帮到您!
阅读全文