110,533
社区成员
发帖
与我相关
我的任务
分享
public IList MaNumPlus(IList list, DateTime startdate)
{
IList ma5 = new ArrayList();
IList ma10 = new ArrayList();
IList ma20 = new ArrayList();
IList ma30 = new ArrayList();
DateTime ma5end = startdate.AddDays(-10);
DateTime ma10end = startdate.AddDays(-20);
DateTime ma20end = startdate.AddDays(-40);
DateTime ma30end = startdate.AddDays(-60);
for (int i = 1; i <= list.Count; i++)
{
double tempma30 = 0;
double tempma20 = 0;
double tempma10 = 0;
double tempma5 = 0;
int num30 = 0;
int num20 = 0;
int num10 = 0;
int num5 = 0;
if (list.Count > 30)
{
for (int j = i - 1; j < 29 + i; j++)
{
if (j < list.Count)
{
tempma30 += ((StockInfo)list[j]).NowIndex;//NowIndex=收盘价
num30++;
if (j < 19+i)
{
tempma20 += ((StockInfo)list[j]).NowIndex;
num20++;
}
if (j < 9+i)
{
tempma10 += ((StockInfo)list[j]).NowIndex;
num10++;
}
if (j < 4+i)
{
tempma5 += ((StockInfo)list[j]).NowIndex;
num5++;
}
}
else
{
break;
}
}
if (num30 != 0)
{
((StockInfo)list[i - 1]).Ma30 = Math.Round(tempma30 / num30, 2);
}
if (((StockInfo)list[i - 1]).DayDate >= ma20end && num20 != 0)
{
((StockInfo)list[i - 1]).Ma20 = Math.Round(tempma20 / num20, 2);
}
if (((StockInfo)list[i - 1]).DayDate >= ma10end && num10 != 0)
{
((StockInfo)list[i - 1]).Ma10 = Math.Round(tempma10 / num10, 2);
}
if (((StockInfo)list[i - 1]).DayDate >= ma5end && num5 !=0)
{
((StockInfo)list[i - 1]).Ma5 = Math.Round(tempma5 / num5, 2);
}
}
else if (list.Count > 20)
{
for (int j = list.Count - 1; j >= i - 1; j--)
{
tempma30 += ((StockInfo)list[j]).NowIndex;
num30++;
}
if (num30 != 0)
{
((StockInfo)list[i - 1]).Ma30 = Math.Round(tempma30 / num30, 2);
}
for (int j = i - 1; j < 19 + i; j++)
{
if (j < list.Count)
{
tempma20 += ((StockInfo)list[j]).NowIndex;
num20++;
if (j < 9+i)
{
tempma10 += ((StockInfo)list[j]).NowIndex;
num10++;
}
if (j < 4+i)
{
tempma5 += ((StockInfo)list[j]).NowIndex;
num5++;
}
}
else
{
break;
}
}
if (num20 != 0)
{
((StockInfo)list[i - 1]).Ma20 = Math.Round(tempma20 / num20, 2);
}
if (((StockInfo)list[i - 1]).DayDate >= ma10end && num10 != 0)
{
((StockInfo)list[i - 1]).Ma10 = Math.Round(tempma10 / num10, 2);
}
if (((StockInfo)list[i - 1]).DayDate >= ma5end && num5 != 0)
{
((StockInfo)list[i - 1]).Ma5 = Math.Round(tempma5 / num5, 2);
}
}
else if (list.Count > 10)
{
for (int j = list.Count - 1; j >= i - 1; j--)
{
tempma30 += ((StockInfo)list[j]).NowIndex;
num30++;
}
if (num30 != 0)
{
((StockInfo)list[i - 1]).Ma30 = Math.Round(tempma30 / num30, 2);
((StockInfo)list[i - 1]).Ma20 = Math.Round(tempma30 / num30, 2);
}
for (int j = i - 1; j < 9 + i; j++)
{
if (j < list.Count)
{
tempma10 += ((StockInfo)list[j]).NowIndex;
num10++;
if (j < 4+i)
{
tempma5 += ((StockInfo)list[j]).NowIndex;
num5++;
}
}
else
{
break;
}
}
if (num10 != 0)
{
((StockInfo)list[i - 1]).Ma10 = Math.Round(tempma10 / num10, 2);
}
if (((StockInfo)list[i - 1]).DayDate >= ma5end && num5 != 0)
{
((StockInfo)list[i - 1]).Ma5 = Math.Round(tempma5 / num5, 2);
}
}
else if (list.Count > 5)
{
for (int j = list.Count - 1; j >= i - 1; j--)
{
tempma30 += ((StockInfo)list[j]).NowIndex;
num30++;
}
if (num30 != 0)
{
((StockInfo)list[i - 1]).Ma30 = Math.Round(tempma30 / num30, 2);
((StockInfo)list[i - 1]).Ma20 = Math.Round(tempma30 / num30, 2);
((StockInfo)list[i - 1]).Ma10 = Math.Round(tempma30 / num30, 2);
}
for (int j = i - 1; j < 4 + i; j++)
{
if (j < list.Count)
{
tempma5 += ((StockInfo)list[j]).NowIndex;
num5++;
}
else
{
break;
}
}
if (num5 != 0)
{
((StockInfo)list[i - 1]).Ma5 = Math.Round(tempma5 / num5, 2);
}
}
else
{
for (int j = list.Count - 1; j >= i - 1; j--)
{
tempma30 += ((StockInfo)list[j]).NowIndex;
num30++;
}
if (num30 != 0)
{
((StockInfo)list[i - 1]).Ma30 = Math.Round(tempma30 / num30, 2);
((StockInfo)list[i - 1]).Ma20 = Math.Round(tempma30 / num30, 2);
((StockInfo)list[i - 1]).Ma10 = Math.Round(tempma30 / num30, 2);
((StockInfo)list[i - 1]).Ma5 = Math.Round(tempma30 / num30, 2);
}
}
}
return list;
}
var input = new[] { 10, 21, 32, 43, 54, 65, 76, 87, 98 };
for (int i = 0; i < input.Length; i++) {
input[i] = (int)MABy(input, i, 3);
}
return;
private static double MABy(int[] data, int start,int duration) {
//前后分别补充两个-1,
var para = Enumerable.Repeat(-1, duration - 1)
.Concat(data)
.Concat(Enumerable.Repeat(-1, duration - 1))
.ToArray();
//跳过前start个,取duration个,排除补的-1,得出平均值
/*
para = para.Skip(start).ToArray();
para = para.Take(duration).ToArray();
para = para.Where(x => x > 0).ToArray();
var re = para.Average();
return re;
*/
return para
.Skip(start)
.Take(duration)
.Where(x => x > 0)
.Average();
}
public IList MaNumPlus(IList list, DateTime startdate)
{
IList ma5 = new ArrayList();
IList ma10 = new ArrayList();
IList ma20 = new ArrayList();
IList ma30 = new ArrayList();
DateTime ma5end = startdate.AddDays(-10);
DateTime ma10end = startdate.AddDays(-20);
DateTime ma20end = startdate.AddDays(-40);
DateTime ma30end = startdate.AddDays(-60);
for (int i = 1; i <= list.Count; i++)
{
double tempma30 = 0;
double tempma20 = 0;
double tempma10 = 0;
double tempma5 = 0;
int num30 = 0;
int num20 = 0;
int num10 = 0;
int num5 = 0;
if (list.Count > 30)
{
for (int j = i - 1; j < 29 + i; j++)
{
if (j < list.Count)
{
tempma30 += ((StockInfo)list[j]).NowIndex;
num30++;
if (j < 19+i)
{
tempma20 += ((StockInfo)list[j]).NowIndex;
num20++;
}
if (j < 9+i)
{
tempma10 += ((StockInfo)list[j]).NowIndex;
num10++;
}
if (j < 4+i)
{
tempma5 += ((StockInfo)list[j]).NowIndex;
num5++;
}
}
else
{
break;
}
}
if (num30 != 0)
{
((StockInfo)list[i - 1]).Ma30 = Math.Round(tempma30 / num30, 2);
}
if (((StockInfo)list[i - 1]).DayDate >= ma20end && num20 != 0)
{
((StockInfo)list[i - 1]).Ma20 = Math.Round(tempma20 / num20, 2);
}
if (((StockInfo)list[i - 1]).DayDate >= ma10end && num10 != 0)
{
((StockInfo)list[i - 1]).Ma10 = Math.Round(tempma10 / num10, 2);
}
if (((StockInfo)list[i - 1]).DayDate >= ma5end && num5 !=0)
{
((StockInfo)list[i - 1]).Ma5 = Math.Round(tempma5 / num5, 2);
}
}
else if (list.Count > 20)
{
for (int j = list.Count - 1; j >= i - 1; j--)
{
tempma30 += ((StockInfo)list[j]).NowIndex;
num30++;
}
if (num30 != 0)
{
((StockInfo)list[i - 1]).Ma30 = Math.Round(tempma30 / num30, 2);
}
for (int j = i - 1; j < 19 + i; j++)
{
if (j < list.Count)
{
tempma20 += ((StockInfo)list[j]).NowIndex;
num20++;
if (j < 9+i)
{
tempma10 += ((StockInfo)list[j]).NowIndex;
num10++;
}
if (j < 4+i)
{
tempma5 += ((StockInfo)list[j]).NowIndex;
num5++;
}
}
else
{
break;
}
}
if (num20 != 0)
{
((StockInfo)list[i - 1]).Ma20 = Math.Round(tempma20 / num20, 2);
}
if (((StockInfo)list[i - 1]).DayDate >= ma10end && num10 != 0)
{
((StockInfo)list[i - 1]).Ma10 = Math.Round(tempma10 / num10, 2);
}
if (((StockInfo)list[i - 1]).DayDate >= ma5end && num5 != 0)
{
((StockInfo)list[i - 1]).Ma5 = Math.Round(tempma5 / num5, 2);
}
}
else if (list.Count > 10)
{
for (int j = list.Count - 1; j >= i - 1; j--)
{
tempma30 += ((StockInfo)list[j]).NowIndex;
num30++;
}
if (num30 != 0)
{
((StockInfo)list[i - 1]).Ma30 = Math.Round(tempma30 / num30, 2);
((StockInfo)list[i - 1]).Ma20 = Math.Round(tempma30 / num30, 2);
}
for (int j = i - 1; j < 9 + i; j++)
{
if (j < list.Count)
{
tempma10 += ((StockInfo)list[j]).NowIndex;
num10++;
if (j < 4+i)
{
tempma5 += ((StockInfo)list[j]).NowIndex;
num5++;
}
}
else
{
break;
}
}
if (num10 != 0)
{
((StockInfo)list[i - 1]).Ma10 = Math.Round(tempma10 / num10, 2);
}
if (((StockInfo)list[i - 1]).DayDate >= ma5end && num5 != 0)
{
((StockInfo)list[i - 1]).Ma5 = Math.Round(tempma5 / num5, 2);
}
}
else if (list.Count > 5)
{
for (int j = list.Count - 1; j >= i - 1; j--)
{
tempma30 += ((StockInfo)list[j]).NowIndex;
num30++;
}
if (num30 != 0)
{
((StockInfo)list[i - 1]).Ma30 = Math.Round(tempma30 / num30, 2);
((StockInfo)list[i - 1]).Ma20 = Math.Round(tempma30 / num30, 2);
((StockInfo)list[i - 1]).Ma10 = Math.Round(tempma30 / num30, 2);
}
for (int j = i - 1; j < 4 + i; j++)
{
if (j < list.Count)
{
tempma5 += ((StockInfo)list[j]).NowIndex;
num5++;
}
else
{
break;
}
}
if (num5 != 0)
{
((StockInfo)list[i - 1]).Ma5 = Math.Round(tempma5 / num5, 2);
}
}
else
{
for (int j = list.Count - 1; j >= i - 1; j--)
{
tempma30 += ((StockInfo)list[j]).NowIndex;
num30++;
}
if (num30 != 0)
{
((StockInfo)list[i - 1]).Ma30 = Math.Round(tempma30 / num30, 2);
((StockInfo)list[i - 1]).Ma20 = Math.Round(tempma30 / num30, 2);
((StockInfo)list[i - 1]).Ma10 = Math.Round(tempma30 / num30, 2);
((StockInfo)list[i - 1]).Ma5 = Math.Round(tempma30 / num30, 2);
}
}
}
return list;
}