請教for循環的問題 [開拓者 TB]
- 咨詢內容:
公式:
Params
Vars
Numeric i(0);
Numeric Count(0);
Numeric CurrentBars;
Numeric a;
Numeric b;
Begin
CurrentBars=BarsSinceToday;
Count=0;
for i = 1 to CurrentBars
{
a=High[i];
b=OpenD(0);
if(a>b)
{
Count=Count+1;
Commentary(Text(a));
}
}
Commentary("Count="+Text(Count));
Commentary("BarsSinceToday="+Text(BarsSinceToday));
End
問題:
當CurrentBars=BarsSinceToday 時,if(a>b)永遠都是為真的,就算a<b,條件里的語句都會執行
當CurrentBars=5 時,if(a>b)就是正常的
請問這是什么原因呢?
請大家幫忙看看, 謝謝
- TB技術人員:
本帖最后由 ample 于 2013-10-31 10:44 編輯
樓主的程序中,當BarsSinceToday=0時,也就是第一個bar,for循環條件不滿足是無法執行 的,因此這里沒有進行比較。這個問題樓主要分兩種情況來考慮,=0的情況單獨寫代碼 - TB客服:
本帖最后由 ample 于 2013-10-31 10:45 編輯
樓主的程序,到了一個新bar,循環是會把今日所有的bar重新計算一遍的(從今日第一個bar開始),不知道樓主希望達到的目的是什么,如果是想計算當日有多少個bar的high值比當日的開盤價高的話,不需要這么麻煩,設置一個序列變量,每個bar比較一次,保存下來就可以了,也是要區分當日第一個bar或者其它bar的情況的- Vars
- NumericSeries Count;
- Numeric CurrentBars;
- Numeric a;
- Numeric b;
- Begin
- CurrentBars = BarsSinceToday;
- b=OpenD(0);
- a=High;
- if (CurrentBars==0)
- {
- if(a>b)
- Count=1;
- Else
- count=0;
-
- }
- Else
- {
- if(a>b)
- {
- Count=Count[1]+1;
- Commentary("a"+Text(a));
- }
- }
- Commentary("b"+Text(b));
- Commentary("Count="+Text(Count));
- Commentary("BarsSinceToday="+Text(BarsSinceToday));
- PlotNumeric("aa",b);
- End
- Vars
有思路,想編寫各種指標公式,程序化交易模型,選股公式,預警公式的朋友
可聯系技術人員 QQ: 1145508240 進行 有償 編寫!(不貴!點擊查看價格!)
相關文章
-
沒有相關內容