請教TimeFrameSet() 的用法是否有誤?!
本帖最後由 skyler 於 14-10-7 14:16 編輯各位先進
我寫了個小範例測試TimeFrameSet()
但發現有些問題
因此想請教一下有使用的先進
自己的寫法是否有錯?!
策略想法:(以澳幣 6A #F為標的)
Buy:
10分K,前根K棒 MA5向上、MA20向上
30分K,前根K棒 MA5向上、MA20向上
Sell:
10分K,前根K棒 MA5向下
Short:
10分K,前根K棒 MA5向下、MA20向下
30分K,前根K棒 MA5向下、MA20向下
Cover:
10分K,前根K棒 MA5向上
AFL如下:
_SECTION_BEGIN( "Product Settings" );
{
SetPositionSize( 1 , spsShares );
SetOption( "MaxOpenPositions" , 1 );
SetOption( "InitialEquity" , 50000 );
SetOption( "FuturesMode" , 1 );
SetOption( "CommissionMode" , 3 );
SetOption( "CommissionAmount" , 2.5 );
RoundLotSize = 1;
newName = strmid( Name (), 0, 2 );
roundNum = 1;//做四捨五入用
//澳幣
if ( newName == "6A")
{
TickSize = 0.0001;
MarginDeposit = 1663.75;
PS = 10000;
PointValue = 10 * PS;
roundNum = 10000;//四捨五入到小數點第四位
}
}
_SECTION_END();
_SECTION_BEGIN( "Logic" );
{
MA5 = int(MA( C, 5 ) * roundNum + 0.5)/roundNum; // MA5 四捨五入至小數點第四位
MA5_P1 = Ref( MA5, -1 ); // 前1根K棒的 MA5
MA5_P2 = Ref( MA5, -2 ); // 前2根K棒的 MA5
Cond_MA5_P1_Buy = MA5_P1 > MA5_P2; // Buy
Cond_MA5_P1_Short = MA5_P1 < MA5_P2; // Short
MA20 = int(MA( C, 20 ) * roundNum + 0.5)/roundNum;// MA20 四捨五入至小數點第四位
MA20_P1 = Ref( MA20, -1 ); // 前1根K棒的 MA20
MA20_P2 = Ref( MA20, -2 ); // 前2根K棒的 MA20
Cond_MA20_P1_Buy = MA20_P1 > MA20_P2; // Buy
Cond_MA20_P1_Short = MA20_P1 < MA20_P2; // Short
TimeFrameSet( 6 * in5Minute ); // 切換到 30分鐘的K線圖
MA5_30min = int(MA( C, 5 ) * roundNum + 0.5)/roundNum; // MA5 四捨五入至小數點第四位
MA5_P1_30min = Ref( MA5_30min, -1 ); // 前1根K棒的 MA5
MA5_P2_30min = Ref( MA5_30min, -2 ); // 前2根K棒的 MA5
Cond_MA5_P1_Buy_30min = MA5_P1_30min > MA5_P2_30min; // Buy
Cond_MA5_P1_Short_30min = MA5_P1_30min < MA5_P2_30min; // Short
MA20_30min = int(MA( C, 20 ) * roundNum + 0.5)/roundNum; // MA20 四捨五入至小數點第四位
MA20_P1_30min = Ref( MA20_30min, -1 ); // 前1根K棒的 MA20
MA20_P2_30min = Ref( MA20_30min, -2 ); // 前2根K棒的 MA20
Cond_MA20_P1_Buy_30min = MA20_P1_30min > MA20_P2_30min; // Buy
Cond_MA20_P1_Short_30min = MA20_P1_30min < MA20_P2_30min; // Short
TimeFrameRestore(); // 回復至10分K線圖
Buy = Cond_MA5_P1_Buy AND Cond_MA20_P1_Buy AND Cond_MA5_P1_Buy_30min AND
Cond_MA20_P1_Buy_30min;
Sell = Cond_MA5_P1_Short;
Short = Cond_MA5_P1_Short AND Cond_MA20_P1_Short AND Cond_MA5_P1_Short_30min AND
Cond_MA20_P1_Short_30min;
Cover = Cond_MA5_P1_Buy;
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell , Buy );
Short = ExRem( Short, Cover );
Cover = ExRem( Cover , Short );
// Explore 用
Filter = Buy OR Sell OR Short OR Cover;
// 買賣時各加二點當滑價
BuyPrice = (O + 2 * TickSize) ;
SellPrice = (O - 2 * TickSize);
ShortPrice = (O - 2 * TickSize);
CoverPrice = (O + 2 * TickSize);
// 以下顯示Explore 用
AddColumn( Buy, "Buy", 1 );
AddColumn( Sell, "Sell", 1 );
AddColumn( Short, "Short", 1 );
AddColumn( Cover, "Cover", 1 );
AddColumn( MA5_P1, "MA5_P1", 1.4 );
AddColumn( MA5_P2, "MA5_P2", 1.4 );
AddColumn( Cond_MA5_P1_Buy, "Cond_MA5_P1_Buy", 1 );
AddColumn( Cond_MA5_P1_Short, "Cond_MA5_P1_Short", 1 );
AddColumn( MA20_P1, "MA20_P1", 1.4 );
AddColumn( MA20_P2, "MA20_P2", 1.4 );
AddColumn( Cond_MA20_P1_Buy, "Cond_MA20_P1_Buy", 1 );
AddColumn( Cond_MA20_P1_Short, "Cond_MA20_P1_Short", 1);
AddColumn( MA5_P1_30min, "MA5_P1_30min", 1.4 );
AddColumn( MA5_P2_30min, "MA5_P2_30min", 1.4 );
AddColumn( Cond_MA5_P1_Buy_30min, "Cond_MA5_P1_Buy_30min", 1 );
AddColumn( Cond_MA5_P1_Short_30min, "Cond_MA5_P1_Short_30min", 1 );
AddColumn( MA20_P1_30min, "MA20_P1_30min", 1.4 );
AddColumn( MA20_P2_30min, "MA20_P2_30min", 1.4 );
AddColumn( Cond_MA20_P1_Buy_30min, "Cond_MA20_P1_Buy_30min", 1 );
AddColumn( Cond_MA20_P1_Short_30min, "Cond_MA20_P1_Short_30min", 1);
}
_SECTION_END();
問題:
1.
以上面的回測跑出結果
取第一筆來實證
2.
3.
4.
紅框符合圖2的情況
5.
紅框符合圖2的情況
但是
黃框中
30分K的 前1根與前2根的MA5
居然是 0.8XXX
而對照圖3
明顯看出 MA5 並沒有在 1以下
6.
黃框中
30分K的 前1根與前2根的MA20
居然是 0.8XXX
而對照圖3
明顯看出 MA20 並沒有在 1以下
由 5 跟 6 的問題
導致回測的結果不正確
以上是我自己的測試結果
還請各位先進指導
感謝~
Hi,
這邊建議不同周期的語法
用 TimeFrameExpand 轉出來
比如說在10k的CHART要叫回30k裡的買進條件
TimeFrameExpand(30K BUY 條件, 6*in5Minute)
去把那個週期裡的 ARRAY 取出來 10k 的圖用
傳回是 1 就表示符合該週期的買進條件,依此類推
---
建議可以用最簡單的 PRINTF 來觀察 (或是用 _TRACE)
看你有用到哪些周期,可以開多個圖表來檢視
這樣比對會比看語法哪裡有誤還快又確定
參考看看了
kilroy 發表於 14-10-7 17:03 static/image/common/back.gif
Hi,
這邊建議不同周期的語法
Hi, 大大
我發覺用Multiple TF很難debug
大大提到開多個圖表是如何做到
請問有否辦法在同一個chart內同時顯示不同週期的圖表?
謝謝
本帖最後由 kilroy 於 14-10-7 19:20 編輯
jacklcl 發表於 14-10-7 18:08 static/image/common/back.gif
Hi, 大大
我發覺用Multiple TF很難debug
大大提到開多個圖表是如何做到
Hi,
mutiple time frame 最容易犯的錯誤
以我粗淺的經驗
就是長周期和短周期的參考
比如說短周期去參考長周期時
會產生 look into the future
因為長周期還沒跑完 XD
---
我就幹過這種事 = ="
回測看沒問題,即時資料時,或 bar replay 一看就發現問題了
---
如果是往左邊參考長周期,那還 ok
而在同一週期圖表裡顯示不同週期的K線
這個範例很多,但我覺得應該都不是能滿足你的需求,或是與你想要的相符合
要用到多週期參考,就是不要去參考還在走的週期
只能 reference 以前,這樣就不會有 bug 了
但不能保證這樣寫的策略,績效或各方面會比較好
參考看看了
jacklcl 發表於 14-10-7 18:08 static/image/common/back.gif
Hi, 大大
我發覺用Multiple TF很難debug
大大提到開多個圖表是如何做到
補充一下
上面提到多圖表
主要是看他在其他週期的買賣條件可行嗎
在用短周期的圖表運用timeframeset() timeframerestore() timeframeexpand()
去把它叫出來,看看是否一致
確定都 ok 後,那用一個圖表跑就可以了
本帖最後由 skyler 於 14-10-7 21:56 編輯
kilroy 發表於 14-10-7 17:03 static/image/common/back.gif
Hi,
這邊建議不同周期的語法
感謝K大的指導依照您的方法
將上面的範例
改成
TimeFrameExpand
跑出來的結果看起來是對的
不過我還是不懂
TimeFrameSet( 6 * in5Minute );
他的問題在那裡?
不知道大大有什麼想法?!
------
原來要用_TRACE() 來印的
但是印出來的資料有點怪
才改用 Explore 將所有符合條件的資料印出來
http://www.wikinet.pro/images/0/0f/Mao_rtfm.png
You have forgottten timeframeexpand! It has three expand option: expandlast,expandfirst,expandpoint.
Code not tested! There could be other bugs in your code.
_SECTION_BEGIN ( "Product Settings" );
SetPositionSize ( 1, spsShares );
SetOption ( "MaxOpenPositions", 1 );
SetOption ( "InitialEquity", 50000 );
SetOption ( "FuturesMode", 1 );
SetOption ( "CommissionMode", 3 );
SetOption ( "CommissionAmount", 2.5 );
RoundLotSize = 1;
newName = strmid (Name (), 0, 2);
roundNum = 1; // done by rounding
// AUD
if (newName == "6A")
{
TickSize = 0.0001;
MarginDeposit = 1663.75;
PS = 10000;
PointValue = 10 * PS;
roundNum = 10000; // rounded to the fourth decimal place
}
_SECTION_END ();
_SECTION_BEGIN ("Logic");
MA5 = int ( MA ( C, 5 ) * roundNum + 0.5 ) / roundNum; // MA5 rounded to the fourth decimal point
MA5_P1 = Ref ( MA5, -1 ); // before a K rod MA5
MA5_P2 = Ref ( MA5, -2 ); // before 2 K rod MA5
Cond_MA5_P1_Buy = MA5_P1 > MA5_P2; // Buy
Cond_MA5_P1_Short = MA5_P1 < MA5_P2; // Short
MA20 = int ( MA ( C, 20 ) * roundNum + 0.5 ) / roundNum; // MA20 rounded to the fourth decimal point
MA20_P1 = Ref ( MA20, -1 ); // rod before a K MA20
MA20_P2 = Ref ( MA20, -2 ); // 2 K bar before MA20
Cond_MA20_P1_Buy = MA20_P1 > MA20_P2; // Buy
Cond_MA20_P1_Short = MA20_P1 < MA20_P2; // Short
htf = 30 * 60;
TimeFrameSet (htf); // switch to the 30 minutes of the K line graph
MA5_30min = int (MA (C, 5) * roundNum + 0.5) / roundNum; // MA5 rounded to the fourth decimal point
MA5_P1_30min = Ref (MA5_30min, -1); // before a K rod MA5
MA5_P2_30min = Ref (MA5_30min, -2); // before 2 K rod MA5
MA20_30min = int (MA (C, 20) * roundNum + 0.5) / roundNum; // MA20 rounded to the fourth decimal point
MA20_P1_30min = Ref (MA20_30min, -1); // rod before a K MA20
MA20_P2_30min = Ref (MA20_30min, -2); // before 2 K rod MA20
TimeFrameRestore (); // reply to 10 K line graph
expandoption = expandpoint;
MA5_P1_30min = TimeFrameExpand( MA5_P1_30min, htf, expandoption );
MA5_P2_30min = TimeFrameExpand( MA5_P2_30min, htf, expandoption );
MA20_P1_30min = TimeFrameExpand( MA20_P1_30min, htf, expandoption );
MA20_P2_30min = TimeFrameExpand( MA20_P2_30min, htf, expandoption );
Cond_MA5_P1_Buy_30min = MA5_P1_30min > MA5_P2_30min; // Buy
Cond_MA5_P1_Short_30min = MA5_P1_30min < MA5_P2_30min; // Short
Cond_MA20_P1_Buy_30min = MA20_P1_30min > MA20_P2_30min; // Buy
Cond_MA20_P1_Short_30min = MA20_P1_30min < MA20_P2_30min; // Short
Buy = Cond_MA5_P1_Buy AND
Cond_MA20_P1_Buy AND
Cond_MA5_P1_Buy_30min AND
Cond_MA20_P1_Buy_30min;
Sell = Cond_MA5_P1_Short;
Short = Cond_MA5_P1_Short AND
Cond_MA20_P1_Short AND
Cond_MA5_P1_Short_30min AND
Cond_MA20_P1_Short_30min;
Cover = Cond_MA5_P1_Buy;
//Each added two points when @ trading price when the slide
BuyPrice = (O + 2 * TickSize);
SellPrice = (O - 2 * TickSize);
ShortPrice = (O - 2 * TickSize);
CoverPrice = (O + 2 * TickSize);
if( Status( "action" ) == actionExplore );
{
Buy = ExRem (Buy, Sell);
Sell = ExRem (Sell, Buy);
Short = ExRem (Short, Cover);
Cover = ExRem (Cover, Short);
// Explore by
Filter = Buy OR Sell OR Short OR Cover;
// Explore with the following display
AddColumn (Buy, "Buy", 1);
AddColumn (Sell, "Sell", 1);
AddColumn (Short, "Short", 1);
AddColumn (Cover, "Cover", 1);
AddColumn (MA5_P1, "MA5_P1", 1.4);
AddColumn (MA5_P2, "MA5_P2", 1.4);
AddColumn (Cond_MA5_P1_Buy, "Cond_MA5_P1_Buy", 1);
AddColumn (Cond_MA5_P1_Short, "Cond_MA5_P1_Short", 1);
AddColumn (MA20_P1, "MA20_P1", 1.4);
AddColumn (MA20_P2, "MA20_P2", 1.4);
AddColumn (Cond_MA20_P1_Buy, "Cond_MA20_P1_Buy", 1);
AddColumn (Cond_MA20_P1_Short, "Cond_MA20_P1_Short", 1);
AddColumn (MA5_P1_30min, "MA5_P1_30min", 1.4);
AddColumn (MA5_P2_30min, "MA5_P2_30min", 1.4);
AddColumn (Cond_MA5_P1_Buy_30min, "Cond_MA5_P1_Buy_30min", 1);
AddColumn (Cond_MA5_P1_Short_30min, "Cond_MA5_P1_Short_30min", 1);
AddColumn (MA20_P1_30min, "MA20_P1_30min", 1.4);
AddColumn (MA20_P2_30min, "MA20_P2_30min", 1.4);
AddColumn (Cond_MA20_P1_Buy_30min, "Cond_MA20_P1_Buy_30min", 1);
AddColumn (Cond_MA20_P1_Short_30min, "Cond_MA20_P1_Short_30min", 1);
}
_SECTION_END ();
Simply use exploration to understand what compress without expand means and what each expand option does mean.Filter = 1;
TFvar = 30;
HTF = TFvar * 60;
AddTextColumn( Interval( 2 ), "LTF", 1.4 , colorDefault, colorDefault, 80 );
AddColumn( Close, "LTF close", 1.4 );
AddTextColumn( StrFormat( "%g-minute", tfvar ), "HTF", 1.4 , colorDefault, colorDefault, 80 );
TimeFrameSet( HTF );
AddColumn( wc = Close, "HTF close compressed", 1.4 );
TimeFrameRestore();
AddColumn( TimeFrameExpand( wc, HTF, expandLast ), "HTF close expandlast", 1.4 );
AddColumn( TimeFrameExpand( wc, HTF, expandFirst ), "HTF close expandfirst", 1.4 );
AddColumn( TimeFrameExpand( wc, HTF, expandPoint ), "HTF close expandpoint", 1.4 ); 本帖最後由 joshsmi 於 14-10-7 23:15 編輯
jacklcl 發表於 14-10-7 18:08 static/image/common/back.gif
Hi, 大大
我發覺用Multiple TF很難debug
大大提到開多個圖表是如何做到
"Is there any way to display charts of different periods within the same chart at the same time?
Thank you "
Yes, there is!
See picture by trash http://www.traderji.com/amibroke ... s-3.html#post887681
There are other methods not using Gfx also.
Here is another pic from traderji.com using GFx or expandpoint
http://img854.imageshack.us/img854/1184/4si.png
etc.
skyler 發表於 14-10-7 21:53 static/image/common/back.gif
感謝K大的指導依照您的方法
將上面的範例
改成
TIMEFRAMESET 是把周期跳到你想要的週期
取好你要的東西之後
回到原本周期,要用 TIMEFRAMEEXPAND 去取該 ARRAY 這樣才抓得到正確的值
所以是少了這個步驟
joshsmi 發表於 14-10-7 23:00 static/image/common/back.gif
Simply use exploration to understand what compress without expand means and what each expand option...
joshsmi 大感謝您的回覆
您第一個改寫我原先的範例後
我跑了一下
但在30min 跑出來的值有疑問
您第二個程式碼
我跑出來
一樣對30min 的值有疑問
就一起跟您請教了
以下就是以您第二個範例來跑的
以
2011/12/13:00:00:00
2011/12/13:00:10:00
在10min 顯示的Close 價格是對的
但在 30min
我不太清楚 Explore 跑出的 30min 數值 與圖示為何不同?
本帖最後由 joshsmi 於 14-10-8 16:03 編輯
skyler 發表於 14-10-8 09:52 static/image/common/back.gif
joshsmi 大感謝您的回覆
I don't know where you went to school but it is actually very simple and very easy to understand.
See responses in picture
本帖最後由 skyler 於 14-10-8 19:03 編輯
joshsmi 發表於 14-10-8 16:01 static/image/common/back.gif
I don't know where you went to school but it is actually very simple and very easy to understand.
...
Dear joshsmi
It's my mistake
I already understand.
Thanks your help.
lol
頁:
[1]