用.NET语言写出计算股票技术指标BRAR,总共两个参数,第一个参数为datatable类型,第二个参数为int型,返回值为datatable类型,double类型全部四舍五入保留3位小数,请写好函数和参数的详细注释。
时间: 2023-05-20 08:01:23 浏览: 130
以下是用.NET语言编写计算股票技术指标BRAR的函数:
```
/// <summary>
/// 计算股票技术指标BRAR
/// </summary>
/// <param name="data">数据表</param>
/// <param name="n">周期</param>
/// <returns>计算结果表</returns>
public static DataTable CalculateBRAR(DataTable data, int n)
{
DataTable result = new DataTable();
result.Columns.Add("BR", typeof(double));
result.Columns.Add("AR", typeof(double));
if (data.Rows.Count < n)
{
return result;
}
double[] br = new double[data.Rows.Count];
double[] ar = new double[data.Rows.Count];
double[] high = new double[n];
double[] low = new double[n];
double sum_br = 0;
double sum_ar = 0;
for (int i = 0; i < data.Rows.Count; i++)
{
double close = Convert.ToDouble(data.Rows[i]["close"]);
double open = Convert.ToDouble(data.Rows[i]["open"]);
double high_price = Convert.ToDouble(data.Rows[i]["high"]);
double low_price = Convert.ToDouble(data.Rows[i]["low"]);
if (i < n)
{
high[i] = high_price;
low[i] = low_price;
}
else
{
double sum_high = 0;
double sum_low = 0;
for (int j = i - n; j < i; j++)
{
sum_high += Convert.ToDouble(data.Rows[j]["high"]);
sum_low += Convert.ToDouble(data.Rows[j]["low"]);
}
high[i % n] = high_price;
low[i % n] = low_price;
double br_value = (sum_high - sum_low) / (n * sum_high + n * sum_low) * 100;
double ar_value = (high_price - open) / (open - low_price) * 100;
sum_br += br_value;
sum_ar += ar_value;
br[i] = sum_br / n;
ar[i] = sum_ar / n;
}
DataRow row = result.NewRow();
row["BR"] = Math.Round(br[i], 3);
row["AR"] = Math.Round(ar[i], 3);
result.Rows.Add(row);
}
return result;
}
```
其中,参数`data`为数据表,包含以下列:`close`为收盘价,`open`为开盘价,`high`为最高价,`low`为最低价。参数`n`为周期,表示计算BRAR指标的天数。返回值为计算结果表,包含两列:`BR`为BR指标值,`AR`为AR指标值。
阅读全文