COCO研究院

 找回密碼
 註冊
搜索
查看: 2705|回復: 12

請教TimeFrameSet() 的用法是否有誤?!

[複製鏈接]
發表於 14-10-7 13:43 | 顯示全部樓層 |閱讀模式
本帖最後由 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.
以上面的回測跑出結果
取第一筆來實證
01.png

2.
02.png

3.
03.png

4.
紅框符合圖2的情況
04.png

5.
紅框符合圖2的情況
但是
黃框中
30分K的 前1根與前2根的MA5
居然是 0.8XXX
而對照圖3
明顯看出 MA5 並沒有在 1以下



05.png

6.
黃框中
30分K的 前1根與前2根的MA20
居然是 0.8XXX
而對照圖3
明顯看出 MA20 並沒有在 1以下

06.png


由 5 跟 6 的問題
導致回測的結果不正確


以上是我自己的測試結果
還請各位先進指導

感謝~

發表於 14-10-7 17:03 | 顯示全部樓層
Hi,

這邊建議不同周期的語法

用 TimeFrameExpand 轉出來

比如說在10k的CHART要叫回30k裡的買進條件

TimeFrameExpand(30K BUY 條件, 6*in5Minute)

去把那個週期裡的 ARRAY 取出來 10k 的圖用

傳回是 1 就表示符合該週期的買進條件,依此類推

---
建議可以用最簡單的 PRINTF 來觀察 (或是用 _TRACE)


看你有用到哪些周期,可以開多個圖表來檢視


這樣比對會比看語法哪裡有誤還快又確定




參考看看了










評分

參與人數 1金錢 +2 收起 理由
skyler + 2 感謝分享

查看全部評分

發表於 14-10-7 18:08 | 顯示全部樓層
kilroy 發表於 14-10-7 17:03
Hi,

這邊建議不同周期的語法

Hi, 大大
我發覺用Multiple TF很難debug
大大提到開多個圖表是如何做到
請問有否辦法在同一個chart內同時顯示不同週期的圖表?

謝謝
發表於 14-10-7 18:57 | 顯示全部樓層
本帖最後由 kilroy 於 14-10-7 19:20 編輯
jacklcl 發表於 14-10-7 18:08
Hi, 大大
我發覺用Multiple TF很難debug
大大提到開多個圖表是如何做到

Hi,

mutiple time frame 最容易犯的錯誤

以我粗淺的經驗

就是長周期和短周期的參考

比如說短周期去參考長周期時

會產生 look into the future

因為長周期還沒跑完 XD

---
我就幹過這種事 = ="

回測看沒問題,即時資料時,或 bar replay 一看就發現問題了

---
如果是往左邊參考長周期,那還 ok

而在同一週期圖表裡顯示不同週期的K線

這個範例很多,但我覺得應該都不是能滿足你的需求,或是與你想要的相符合

要用到多週期參考,就是不要去參考還在走的週期

只能 reference 以前,這樣就不會有 bug 了

但不能保證這樣寫的策略,績效或各方面會比較好


參考看看了

評分

參與人數 2金錢 +4 收起 理由
jacklcl + 2 感謝分享
skyler + 2 感謝分享

查看全部評分

發表於 14-10-7 19:29 | 顯示全部樓層
jacklcl 發表於 14-10-7 18:08
Hi, 大大
我發覺用Multiple TF很難debug
大大提到開多個圖表是如何做到

補充一下
上面提到多圖表

主要是看他在其他週期的買賣條件可行嗎

在用短周期的圖表運用  timeframeset() timeframerestore() timeframeexpand()

去把它叫出來,看看是否一致

確定都 ok 後,那用一個圖表跑就可以了
 樓主| 發表於 14-10-7 21:53 | 顯示全部樓層
本帖最後由 skyler 於 14-10-7 21:56 編輯
kilroy 發表於 14-10-7 17:03
Hi,

這邊建議不同周期的語法

感謝K大的指導依照您的方法
將上面的範例
改成
TimeFrameExpand
跑出來的結果看起來是對的
不過我還是不懂
TimeFrameSet( 6 * in5Minute );
他的問題在那裡?
不知道大大有什麼想法?!

------
原來要用  _TRACE() 來印的
但是印出來的資料有點怪
才改用 Explore 將所有符合條件的資料印出來

發表於 14-10-7 22:48 | 顯示全部樓層


You have forgottten timeframeexpand! It has three expand option: expandlast,expandfirst,expandpoint.

Code not tested! There could be other bugs in your code.

  1. _SECTION_BEGIN ( "Product Settings" );
  2. SetPositionSize ( 1, spsShares );
  3. SetOption ( "MaxOpenPositions", 1 );
  4. SetOption ( "InitialEquity", 50000 );
  5. SetOption ( "FuturesMode", 1 );
  6. SetOption ( "CommissionMode", 3 );
  7. SetOption ( "CommissionAmount", 2.5 );

  8. RoundLotSize = 1;

  9. newName = strmid (Name (), 0, 2);

  10. roundNum = 1; // done by rounding

  11. // AUD
  12. if (newName == "6A")
  13. {
  14.     TickSize = 0.0001;
  15.     MarginDeposit = 1663.75;
  16.     PS = 10000;
  17.     PointValue = 10 * PS;

  18.     roundNum = 10000; // rounded to the fourth decimal place
  19. }
  20. _SECTION_END ();

  21. _SECTION_BEGIN ("Logic");

  22. MA5 = int ( MA ( C, 5 ) * roundNum + 0.5 ) / roundNum; // MA5 rounded to the fourth decimal point
  23. MA5_P1 = Ref ( MA5, -1 ); // before a K rod MA5
  24. MA5_P2 = Ref ( MA5, -2 ); // before 2 K rod MA5

  25. Cond_MA5_P1_Buy = MA5_P1 > MA5_P2; // Buy
  26. Cond_MA5_P1_Short = MA5_P1 < MA5_P2; // Short

  27. MA20 = int ( MA ( C, 20 ) * roundNum + 0.5 ) / roundNum; // MA20 rounded to the fourth decimal point
  28. MA20_P1 = Ref ( MA20, -1 ); // rod before a K MA20
  29. MA20_P2 = Ref ( MA20, -2 ); // 2 K bar before MA20

  30. Cond_MA20_P1_Buy = MA20_P1 > MA20_P2; // Buy
  31. Cond_MA20_P1_Short = MA20_P1 < MA20_P2; // Short

  32. htf = 30 * 60;
  33. TimeFrameSet (htf); // switch to the 30 minutes of the K line graph

  34.     MA5_30min = int (MA (C, 5) * roundNum + 0.5) / roundNum; // MA5 rounded to the fourth decimal point
  35.     MA5_P1_30min = Ref (MA5_30min, -1); // before a K rod MA5
  36.     MA5_P2_30min = Ref (MA5_30min, -2); // before 2 K rod MA5

  37.     MA20_30min = int (MA (C, 20) * roundNum + 0.5) / roundNum; // MA20 rounded to the fourth decimal point
  38.     MA20_P1_30min = Ref (MA20_30min, -1); // rod before a K MA20
  39.     MA20_P2_30min = Ref (MA20_30min, -2); // before 2 K rod MA20

  40. TimeFrameRestore (); // reply to 10 K line graph


  41. expandoption = expandpoint;
  42. MA5_P1_30min = TimeFrameExpand( MA5_P1_30min, htf, expandoption );
  43. MA5_P2_30min = TimeFrameExpand( MA5_P2_30min, htf, expandoption );

  44. MA20_P1_30min = TimeFrameExpand( MA20_P1_30min, htf, expandoption );
  45. MA20_P2_30min = TimeFrameExpand( MA20_P2_30min, htf, expandoption );


  46. Cond_MA5_P1_Buy_30min = MA5_P1_30min > MA5_P2_30min; // Buy
  47. Cond_MA5_P1_Short_30min = MA5_P1_30min < MA5_P2_30min; // Short

  48. Cond_MA20_P1_Buy_30min = MA20_P1_30min > MA20_P2_30min; // Buy
  49. Cond_MA20_P1_Short_30min = MA20_P1_30min < MA20_P2_30min; // Short


  50. Buy = Cond_MA5_P1_Buy AND
  51.       Cond_MA20_P1_Buy AND
  52.       Cond_MA5_P1_Buy_30min AND
  53.       Cond_MA20_P1_Buy_30min;

  54. Sell = Cond_MA5_P1_Short;

  55. Short = Cond_MA5_P1_Short AND
  56.         Cond_MA20_P1_Short AND
  57.         Cond_MA5_P1_Short_30min AND
  58.         Cond_MA20_P1_Short_30min;

  59. Cover = Cond_MA5_P1_Buy;

  60. //Each added two points when @ trading price when the slide
  61. BuyPrice = (O + 2 * TickSize);
  62. SellPrice = (O - 2 * TickSize);
  63. ShortPrice = (O - 2 * TickSize);
  64. CoverPrice = (O + 2 * TickSize);

  65. if( Status( "action" ) == actionExplore );
  66. {
  67.     Buy = ExRem (Buy, Sell);
  68.     Sell = ExRem (Sell, Buy);
  69.     Short = ExRem (Short, Cover);
  70.     Cover = ExRem (Cover, Short);

  71.     // Explore by
  72.     Filter = Buy OR Sell OR Short OR Cover;

  73.     // Explore with the following display
  74.     AddColumn (Buy, "Buy", 1);
  75.     AddColumn (Sell, "Sell", 1);
  76.     AddColumn (Short, "Short", 1);
  77.     AddColumn (Cover, "Cover", 1);

  78.     AddColumn (MA5_P1, "MA5_P1", 1.4);
  79.     AddColumn (MA5_P2, "MA5_P2", 1.4);
  80.     AddColumn (Cond_MA5_P1_Buy, "Cond_MA5_P1_Buy", 1);
  81.     AddColumn (Cond_MA5_P1_Short, "Cond_MA5_P1_Short", 1);

  82.     AddColumn (MA20_P1, "MA20_P1", 1.4);
  83.     AddColumn (MA20_P2, "MA20_P2", 1.4);
  84.     AddColumn (Cond_MA20_P1_Buy, "Cond_MA20_P1_Buy", 1);
  85.     AddColumn (Cond_MA20_P1_Short, "Cond_MA20_P1_Short", 1);

  86.     AddColumn (MA5_P1_30min, "MA5_P1_30min", 1.4);
  87.     AddColumn (MA5_P2_30min, "MA5_P2_30min", 1.4);
  88.     AddColumn (Cond_MA5_P1_Buy_30min, "Cond_MA5_P1_Buy_30min", 1);
  89.     AddColumn (Cond_MA5_P1_Short_30min, "Cond_MA5_P1_Short_30min", 1);

  90.     AddColumn (MA20_P1_30min, "MA20_P1_30min", 1.4);
  91.     AddColumn (MA20_P2_30min, "MA20_P2_30min", 1.4);
  92.     AddColumn (Cond_MA20_P1_Buy_30min, "Cond_MA20_P1_Buy_30min", 1);
  93.     AddColumn (Cond_MA20_P1_Short_30min, "Cond_MA20_P1_Short_30min", 1);
  94. }

  95. _SECTION_END ();
複製代碼


發表於 14-10-7 23:00 | 顯示全部樓層
Simply use exploration to understand what compress without expand means and what each expand option does mean.
  1. Filter = 1;

  2. TFvar = 30;
  3. HTF = TFvar * 60;

  4. AddTextColumn( Interval( 2 ), "LTF", 1.4 , colorDefault, colorDefault, 80 );
  5. AddColumn( Close, "LTF close", 1.4 );
  6. AddTextColumn( StrFormat( "%g-minute", tfvar ), "HTF", 1.4 , colorDefault, colorDefault, 80 );

  7. TimeFrameSet( HTF );
  8. AddColumn( wc = Close, "HTF close compressed", 1.4 );
  9. TimeFrameRestore();

  10. AddColumn( TimeFrameExpand( wc, HTF, expandLast ), "HTF close expandlast", 1.4 );
  11. AddColumn( TimeFrameExpand( wc, HTF, expandFirst ), "HTF close expandfirst", 1.4 );
  12. AddColumn( TimeFrameExpand( wc, HTF, expandPoint ), "HTF close expandpoint", 1.4 );
複製代碼
發表於 14-10-7 23:06 | 顯示全部樓層
本帖最後由 joshsmi 於 14-10-7 23:15 編輯
jacklcl 發表於 14-10-7 18:08
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


etc.

評分

參與人數 1金錢 +2 收起 理由
jacklcl + 2 好文章,我推薦

查看全部評分

發表於 14-10-7 23:29 | 顯示全部樓層
skyler 發表於 14-10-7 21:53
感謝K大的指導依照您的方法
將上面的範例
改成

TIMEFRAMESET 是把周期跳到你想要的週期
取好你要的東西之後

回到原本周期,要用 TIMEFRAMEEXPAND 去取該 ARRAY 這樣才抓得到正確的值

所以是少了這個步驟


評分

參與人數 1金錢 +2 收起 理由
skyler + 2 感謝分享

查看全部評分

 樓主| 發表於 14-10-8 09:52 | 顯示全部樓層
joshsmi 發表於 14-10-7 23:00
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 價格是對的

01.png


02.png

03.png

但在 30min
我不太清楚 Explore 跑出的 30min 數值 與圖示為何不同?


04.png




發表於 14-10-8 16:01 | 顯示全部樓層
本帖最後由 joshsmi 於 14-10-8 16:03 編輯
skyler 發表於 14-10-8 09:52
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



easy.png

評分

參與人數 1金錢 +2 收起 理由
skyler + 2 Thanks

查看全部評分

 樓主| 發表於 14-10-8 19:01 | 顯示全部樓層
本帖最後由 skyler 於 14-10-8 19:03 編輯
joshsmi 發表於 14-10-8 16:01
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

2014-10-08_190008.png
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

手機版|Archiver|站長信箱|廣告洽詢|COCO研究院

GMT+8, 24-4-24 05:32

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回復 返回頂部 返回列表
理財討論網站 | AI繪圖AI超擬真美女AI beauty AI Stable DiffusionAI正妹AI Lookbook