C# C在忍者脚本的数组中查找Min
我对编码还不熟悉,我正在尝试用C语言为NinjaScript/NinjaTrader编写一些代码,希望有人能提供帮助。 我有一个变量“tovBullBar”,它在三分钟内为某种类型的价格条计算一些值。在这段时间内可能会出现一个以上的条形图。这些值都计算正确,我可以在输出窗口中看到它们。我试图使用数组来识别该期间最小计算值的条形图,以便该值可以包含在我的netLvtTOV最终计算中。但是,我的最终计算结果始终是该期间的最后一个“tovBullBar”值,而不是最小值。你能看一下我的代码,看看能不能告诉我哪里出了问题 我已经为数组中最多10个元素编写了代码,但它们几乎肯定会更低,并且在每3分钟内都会有所不同。看过这里的一些帖子后,我想我应该使用一个动态列表,稍后我将不得不考虑它,但是我看不出为什么它不应该与数组一起工作,只要我定义的元素数量超过我需要的数量 谢谢C# C在忍者脚本的数组中查找Min,c#,arrays,min,C#,Arrays,Min,我对编码还不熟悉,我正在尝试用C语言为NinjaScript/NinjaTrader编写一些代码,希望有人能提供帮助。 我有一个变量“tovBullBar”,它在三分钟内为某种类型的价格条计算一些值。在这段时间内可能会出现一个以上的条形图。这些值都计算正确,我可以在输出窗口中看到它们。我试图使用数组来识别该期间最小计算值的条形图,以便该值可以包含在我的netLvtTOV最终计算中。但是,我的最终计算结果始终是该期间的最后一个“tovBullBar”值,而不是最小值。你能看一下我的代码,看看能不能
#region Using declarations
using System;
using System.Linq;
#endregion
#region Variables
//Declare an array that will hold data for tovBullBar
private double[] tovBull;
private int length = 10;
#endregion
protected override void Initialize()
{
//specify the number of elements in the array, which is the
integer called length
tovBull = new double[length];
}
protected override void OnBarUpdate()
{
//the array now contains the number length of object references that need to be set to instances of objects
for (int count = 0; count<length; count++)
tovBull[count]=tovBullBar;
//Do a for loop to find the minimum for the tovBull
double tovBullBarMin = tovBull[0];
for (int count = 0; count < tovBull.Length; count++)
if (tovBullBarMin > tovBull[count])
tovBullBarMin = tovBull[count];
netLvtTOV = Math.Round((tovBearBar + tovBullBarMin + lvtBullBar)
Print (Time.ToString()+" "+"ArrayLength:"+tovBull.Length);
}
请看OnBarUpdate方法开头的这段代码: 最后的比较会给你正确的结果 如果仅在最后n分钟内查看值时遇到问题,则必须多做一些工作 首先,你需要有一个小班来记录时间和价值:
private class BarEvent
{
public readonly DateTime Time;
public readonly double Value;
public BarEvent(double value)
{
Value = value;
Time = DateTime.Now;
}
}
然后,将tovBull更改为:
List<BarEvent> tovBull = new List<BarEvent>();
和更改OnBarUpdate如下所示:
protected override void OnBarUpdate()
{
// first remove all items more than 3 minutes old
DateTime oldest = DateTime.Now - TimeSpan.FromMinutes(3);
tovBull.RemoveAll(be => be.Time < oldest);
// add the latest value
tovBull.Add(new BarEvent(tovBullBar));
//Find the minimum for the tovBull using linq
double tovBullBarMin = tovBull.Min(be => be.Value);
netLvtTOV = Math.Round((tovBearBar + tovBullBarMin + lvtBullBar)
Print (Time.ToString()+" "+"ArrayLength:"+tovBull.Count);
}
我使用了您建议的编码行,它们有助于为我的最终计算分配正确的数组值。但我仍然有一个主要问题。如果TOVBullBar在第一个信号发出后的3分钟内没有出现,则代码不会在最终计算中为tovBullBarMin指定0的值,而是返回3分钟开始前的时段,并拾取要在计算中使用的最后一个TOVBullBar值。我尝试使用数组。在3分钟结束时清除以停止此操作,但不起作用。将尝试在3分钟内的其他信号和netLvtTOV计算后,在firstsig:if check&&Time[0]
tovBull = new double[length];
for (int i = 0; i < tovBull.Length; i++)
tovBull[i] = double.MaxValue;
private class BarEvent
{
public readonly DateTime Time;
public readonly double Value;
public BarEvent(double value)
{
Value = value;
Time = DateTime.Now;
}
}
List<BarEvent> tovBull = new List<BarEvent>();
protected override void OnBarUpdate()
{
// first remove all items more than 3 minutes old
DateTime oldest = DateTime.Now - TimeSpan.FromMinutes(3);
tovBull.RemoveAll(be => be.Time < oldest);
// add the latest value
tovBull.Add(new BarEvent(tovBullBar));
//Find the minimum for the tovBull using linq
double tovBullBarMin = tovBull.Min(be => be.Value);
netLvtTOV = Math.Round((tovBearBar + tovBullBarMin + lvtBullBar)
Print (Time.ToString()+" "+"ArrayLength:"+tovBull.Count);
}