我是编码的新手,我正在尝试用C#为NinjaScript / NinjaTrader编写一些代码,我希望有人可以提供帮助。 我有一个变量“tovBullBar”,可以在三分钟内计算某种类型价格条的某些值。在此期间可能会出现多个这样的条形。这些值都是正确计算的,我可以在输出窗口中看到它们。我正在尝试使用数组来识别该时段内具有最小计算值的条形,以便该值可以包含在我的netLvtTOV的最终计算中。但是,我的最终计算始终以句点中的最后一个“tovBullBar”值结束,而不是具有Min值的值。你能不能看看我的代码,看看你能不能告诉我哪里出错了?
我已在阵列中编码了多达10个元素,但它们几乎肯定会更低,并且每3分钟会有所不同。看过这里的一些帖子后我想我应该使用一个动态列表(我稍后会讨论),但是看不出为什么它不应该与数组一起使用只要元素的数量我定义超出了我的需要。
谢谢!
#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);
}
答案 0 :(得分:0)
在OnBarUpdate
方法的开头看一下这段代码:
for (int count = 0; count<length; count++)
tovBull[count]=tovBullBar;
这会将数组的所有成员设置为相同的值。
然后迭代同一个数组,找到值最小的数组。
for (int count = 0; count < tovBull.Length; count++)
if (tovBullBarMin > tovBull[count])
tovBullBarMin = tovBull[count];
所以当然他们都会以同样的价值结束......
我认为你在方法开始时想要做的就是推送&#39;在找到最新值之前,将数组中的最新值放入数组中,然后将最新值添加到数组的前面:
for (int count = 1; count<length; count++)
tovBull[count]= tovBull[count - 1];
tovBull[0] = tovBullBar;
请注意,由于您没有初始化tovBull
数组元素,因此它们都将为零。所以你可能需要这样做:
tovBull = new double[length];
for (int i = 0; i < tovBull.Length; i++)
tovBull[i] = double.MaxValue;
然后最后的比较会给你正确的结果。
如果您仅在最近n分钟内查看值时遇到问题,则必须多做一些工作。
首先,您需要有一个跟踪时间和价值的小班:
private class BarEvent
{
public readonly DateTime Time;
public readonly double Value;
public BarEvent(double value)
{
Value = value;
Time = DateTime.Now;
}
}
然后,不要将tovBull作为double数组,而是将其更改为:
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);
}