【问题标题】:C# find Min in an array for NinjaScriptC# 在 NinjaScript 的数组中查找 Min
【发布时间】:2016-06-02 10:04:34
【问题描述】:

我是编码新手,我正在尝试用 C# 为 NinjaScript / NinjaTrader 编写一些代码,希望有人能提供帮助。 我有一个变量“tovBullBar”,它在三分钟内计算某种类型的价格柱的一些值。在此期间可能会出现不止一个这些柱形图。这些值都计算正确,我可以在输出窗口中看到它们。我正在尝试使用一个数组来识别该时期内具有 Min 计算值的条形图,以便该值可以包含在我对 netLvtTOV 的最终计算中。但是,我的最终计算始终以该期间的最后一个“tovBullBar”值结束,而不是具有最小值的那个。你能看看我的代码,看看你能不能告诉我哪里出错了?

我已经对数组中的最多 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);
}

【问题讨论】:

    标签: c# arrays min


    【解决方案1】:

    查看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];  
    

    所以当然它们都会以相同的值结束...

    我认为您在方法开始时想要做的是将最新值“推送”到数组中,然后通过简单地将它们在数组中一一移动然后将最新值添加到数组的前面:

    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);
    }           
    

    【讨论】:

    • 我使用了您建议的编码行,它们有助于为我的最终计算分配正确的数组值。但我仍然有一个主要问题。如果 TOVBullBar 在第一个信号之后的 3 分钟内没有出现,而不是在最终计算中为 tovBullBarMin 分配 0 值,代码会回顾 3 分钟开始之前的时间段并获取最后一个 tovBullBar 值在计算中使用。我曾尝试在 3 分钟结束时使用 Array.Clear 来阻止此操作,但无法正常工作。将尝试发布代码。
    • 第一个信号后的行:if (check &amp;&amp; Time[0] &lt; firstSig.AddMinutes(3)) 3分钟内其他信号后的行和netLvtTOV计算:if (check &amp;&amp; Time[0] &gt;= firstSig.AddMinutes(3)) { Array.Clear(tovBull,0,tovBull.Length); Print(Time[0] + " Final Signal."); check = false;
    • 如何定义开始 3 分钟时钟的 BarEvent,因为这仅在 lvtBullBar 之后开始?目前它似乎正在计算所有柱,因此 ArrayLength 为 800+,对于每个 3 分钟的块,如果仅计算 tovBullBars,它应该最多不超过 7 个。谢谢!
    • @ Übercoder,我现在可以通过在清除数组后将 tovBullBar 归零来使用数组,如下所示:Array.Clear(tovBull,0,10);tovBullBar = 0; 感谢您的帮助!
    猜你喜欢
    • 2017-06-30
    • 1970-01-01
    • 2015-02-18
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 2018-12-02
    • 2017-02-27
    相关资源
    最近更新 更多