C# 循环遍历数组并将x个观测值保存在新数组中
我正在尝试通过数组和forC# 循环遍历数组并将x个观测值保存在新数组中,c#,.net,for-loop,C#,.net,For Loop,我正在尝试通过数组和forI循环。我需要设置前10个值的最大值 所以如果i=20,我需要math.max i-10,i-9,i-8…等等。但我正在努力让它发挥作用 static public void TA(DateTime[] datePrice, double[] openPrice, double[] highPrice, double[] lowPrice, double[] closePrice) { #region declare variables int outB
I
循环。我需要设置前10个值的最大值
所以如果i=20
,我需要math.max i-10,i-9,i-8…
等等。但我正在努力让它发挥作用
static public void TA(DateTime[] datePrice, double[] openPrice, double[] highPrice, double[] lowPrice, double[] closePrice)
{
#region declare variables
int outBegIdx;
int outNbElement;
int SmaPeriod = 20;
int TOTAL_PERIODS = closePrice.Length;
double[] outputSma = new double[closePrice.Length];
double[] outputStdDev = new double[closePrice.Length];
int[] outputShootingStar = new int[closePrice.Length];
int[] outputHangingMan = new int[closePrice.Length];
int[] outputEngulf = new int[closePrice.Length];
int[] outputMaxIndex = new int[closePrice.Length];
double[] outputTrueRange = new double[closePrice.Length];
double accProfit = 0;
int position = 0;
double openPosPrice = 0;
double profit = 0;
#endregion
#region not sure what this code is for
//for (int i = 0; i < closePrice.Length-TOTAL_PERIODS; i++) //had to change from -1 to -TOTAL_PERIODS
//{
// openPrice[i] = (double)i;
// highPrice[i] = (double)i;
// lowPrice[i] = (double)i;
// closePrice[i] = (double)i;
//}
#endregion
#region Technical Libary
TicTacTec.TA.Library.Core.RetCode Sma = Core.Sma(0, closePrice.Length - 1, closePrice, SmaPeriod, out outBegIdx, out outNbElement, outputSma);
TicTacTec.TA.Library.Core.RetCode StdDev = Core.StdDev(0, closePrice.Length - 1, closePrice, closePrice.Length, 1, out outBegIdx, out outNbElement, outputStdDev);
TicTacTec.TA.Library.Core.RetCode ShootingStar = Core.CdlShootingStar(0, closePrice.Length - 1, openPrice, highPrice, lowPrice, closePrice, out outBegIdx, out outNbElement, outputShootingStar);
TicTacTec.TA.Library.Core.RetCode HangingMan = Core.CdlHangingMan(0, closePrice.Length - 1, openPrice, highPrice, lowPrice, closePrice, out outBegIdx, out outNbElement, outputHangingMan);
TicTacTec.TA.Library.Core.RetCode BullIngulf = Core.CdlEngulfing(0, closePrice.Length - 1, openPrice, highPrice, lowPrice, closePrice, out outBegIdx, out outNbElement, outputEngulf);
TicTacTec.TA.Library.Core.RetCode TrueRange = Core.TrueRange(0, closePrice.Length - 1, highPrice, lowPrice, closePrice, out outBegIdx, out outNbElement, outputTrueRange);
//TicTacTec.TA.Library.Core.RetCode xx = Core.bu
//TicTacTec.TA.Library.Core.RetCode MaxIndex = Core.MaxIndex(0, closePrice.Length - 1, highPrice, 20, out outBegIdx, out outNbElement, outputMaxIndex);
#endregion
for (int i = 20; i < closePrice.Length - 1; i++)
{
for (int j = 0; j < 10; j--)
{
}
}
}
static public void TA(DateTime[]datePrice,double[]openPrice,double[]highPrice,double[]lowPrice,double[]closePrice)
{
#区域声明变量
int outBegIdx;
int-outNbElement;
int SMA周期=20;
int TOTAL_PERIODS=closePrice.Length;
double[]outputSma=新的double[closePrice.Length];
double[]outputstdev=新的double[closePrice.Length];
int[]outputShootingStar=新int[closePrice.Length];
int[]outputhangman=new int[closePrice.Length];
int[]outpuneGulf=新int[closePrice.Length];
int[]outputMaxIndex=newint[closePrice.Length];
double[]OutputRueRange=新的double[closePrice.Length];
双倍利润=0;
int位置=0;
双openPosPrice=0;
双倍利润=0;
#端区
#区域不确定此代码的用途
//对于(int i=0;i
您可以使用以下代码:
int[] arr = new int[200]; // Original list of lines
List<int> maxList = new List<int>(); // Result list to hold max values
int[] range = new int[10]; // Sub-list of lines to check max value
for (int i = 0; i < arr.Length - 9; i++)
{
Array.Copy(arr, i, range, 0, 10); // Get sub-set of lines
maxList.Add(range.Max()); // Find max, add it to the result list
}
int[]arr=newint[200];//原始行列表
List maxList=新列表();//保存最大值的结果列表
int[]范围=新的int[10];//要检查最大值的行的子列表
对于(int i=0;i
您可以使用以下代码:
int[] arr = new int[200]; // Original list of lines
List<int> maxList = new List<int>(); // Result list to hold max values
int[] range = new int[10]; // Sub-list of lines to check max value
for (int i = 0; i < arr.Length - 9; i++)
{
Array.Copy(arr, i, range, 0, 10); // Get sub-set of lines
maxList.Add(range.Max()); // Find max, add it to the result list
}
int[]arr=newint[200];//原始行列表
List maxList=新列表();//保存最大值的结果列表
int[]范围=新的int[10];//要检查最大值的行的子列表
对于(int i=0;i
您可以使用以下代码:
int[] arr = new int[200]; // Original list of lines
List<int> maxList = new List<int>(); // Result list to hold max values
int[] range = new int[10]; // Sub-list of lines to check max value
for (int i = 0; i < arr.Length - 9; i++)
{
Array.Copy(arr, i, range, 0, 10); // Get sub-set of lines
maxList.Add(range.Max()); // Find max, add it to the result list
}
int[]arr=newint[200];//原始行列表
List maxList=新列表();//保存最大值的结果列表
int[]范围=新的int[10];//要检查最大值的行的子列表
对于(int i=0;i
您可以使用以下代码:
int[] arr = new int[200]; // Original list of lines
List<int> maxList = new List<int>(); // Result list to hold max values
int[] range = new int[10]; // Sub-list of lines to check max value
for (int i = 0; i < arr.Length - 9; i++)
{
Array.Copy(arr, i, range, 0, 10); // Get sub-set of lines
maxList.Add(range.Max()); // Find max, add it to the result list
}
int[]arr=newint[200];//原始行列表
List maxList=新列表();//保存最大值的结果列表
int[]范围=新的int[10];//要检查最大值的行的子列表
对于(int i=0;i
这里是LINQ中的另一个版本
var list = new List<int>();
var max = list.Select((x, i) => //get items (x as value, i as index
list.Skip(Math.Max(i - 10, 0)) //forget about previous items
.Take(Math.Min(10, i + 1)) // get 10(or less) elements before this element
.Max()) // get maximum of them
.ToList();//convert result to list
var list=newlist();
var max=list.Select((x,i)=>//获取项(x作为值,i作为索引
list.Skip(Math.Max(i-10,0))//忘记前面的项目
.Take(Math.Min(10,i+1))//在此元素之前获取10个(或更少)元素
.Max()//获取它们的最大值
.ToList();//将结果转换为列表
测试是这样的
var l = new List<int>
{
1, 4, 3, 8, 2, 11, 4, 3, 5, 19, 2, 8,
11, 13, 14, 12, 12, 12, 12, 12
};
var max = l.Select((x, i) => l.Skip(Math.Max(i - 10, 0)).Take(Math.Min(10, i + 1)).Max()).ToList();
Console.WriteLine(string.Join(",", max));
//1,4,4,8,8,11,11,11,11,19,19,19,19,19,19,19,19,19,19,19,14,14,14
var l=新列表
{
1, 4, 3, 8, 2, 11, 4, 3, 5, 19, 2, 8,
11, 13, 14, 12, 12, 12, 12, 12
};
var max=l.Select((x,i)=>l.Skip(Math.max(i-10,0)).Take(Math.Min(10,i+1)).max()).ToList();
Console.WriteLine(string.Join(“,”,max));
//1,4,4,8,8,11,11,11,11,19,19,19,19,19,19,19,19,19,19,19,14,14,14
这里是LINQ中的另一个版本
var list = new List<int>();
var max = list.Select((x, i) => //get items (x as value, i as index
list.Skip(Math.Max(i - 10, 0)) //forget about previous items
.Take(Math.Min(10, i + 1)) // get 10(or less) elements before this element
.Max()) // get maximum of them
.ToList();//convert result to list
var list=newlist();
var max=list.Select((x,i)=>//获取项(x作为值,i作为索引
list.Skip(Math.Max(i-10,0))//忘记前面的项目
.Take(Math.Min(10,i+1))//在此元素之前获取10个(或更少)元素
.Max()//获取它们的最大值
.ToList();//将结果转换为列表
测试是这样的
var l = new List<int>
{
1, 4, 3, 8, 2, 11, 4, 3, 5, 19, 2, 8,
11, 13, 14, 12, 12, 12, 12, 12
};
var max = l.Select((x, i) => l.Skip(Math.Max(i - 10, 0)).Take(Math.Min(10, i + 1)).Max()).ToList();
Console.WriteLine(string.Join(",", max));
//1,4,4,8,8,11,11,11,11,19,19,19,19,19,19,19,19,19,19,19,14,14,14
var l=新列表
{
1, 4, 3, 8, 2, 11, 4, 3, 5, 19, 2, 8,
11,