2016-02-20 100 views
0

我是編程新手,我試圖在C#中爲NinjaScript/NinjaTrader編寫一些代碼,我希望有人可以提供幫助。 我有一個變量「tovBullBar」,它可以在三分鐘內爲某種類型的價格欄計算一些值。這段期間可能會發生多於一個這樣的障礙。這些值都是正確計算的,我可以在輸出窗口中看到它們。我試圖用一個數組來標識那個時期Min計算值的條,這樣這個值可以包含在我最後的netLvtTOV計算中。但是,我的最終計算結果始終是最後一個「tovBullBar」值,而不是最小值。你能看看我的代碼,看看你能告訴我我哪裏出錯了嗎?C#在NonjaScript中找到Min的最小值

我已經編碼了多達10個元素的數組,但他們幾乎肯定會更低,並會在每3分鐘的時間內變化。看過這裏的一些帖子後,我想我應該使用一個動態列表(我將不得不在後面討論),但是看到沒有理由爲什麼它不應該與數組一起工作,只要元素的數量爲I定義比我需要的更多。

謝謝!

#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

看看你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分鐘查看值時遇到問題,則必須進行更多的工作。

首先你需要有一點點類,保持的時間和價值的軌道:而不必tovBull爲雙將其更改爲數組

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

並更改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); 
}   
+0

我已經使用了您建議的代碼行,並且它們有助於分配正確的數組值到我的最終計算。但我仍然有一個主要問題。在第一個信號之後的3分鐘內TOVBullBar沒有出現,而不是在最終計算中爲tovBullBarMin指定值0,代碼將回顧3分鐘開始前的時間段並獲取最後一個tovBullBar值用於計算。我試圖在3分鐘結束時使用Array.Clear來停止這一操作,但不工作。將嘗試發佈代碼。 – GeorgeW

+0

第一行之後的行:'if(check && Time [0] = firstSig.AddMinutes(3 )) \t \t \t { \t \t \t Array.Clear(tovBull,0,tovBull.Length); \t \t \t打印(時間[0] +「最終信號」); \t \t \t check = false;' – GeorgeW

+0

如何定義啓動3分鐘時鐘的BarEvent,因爲這隻能在lvtBullBar之後啓動?目前它似乎正在計算所有條形圖,所以ArrayLength是800+,對於每個3分鐘的塊,如果只計算tovBullBars,它應該不超過7個最大值。謝謝! – GeorgeW

相關問題