COCO研究院

 找回密碼
 註冊
搜索
12
返回列表 發新帖
樓主: alexliou

如何在日線層級 implement "Buy this bar at Close" + 今日慘痛經驗

[複製鏈接]
 樓主| 發表於 16-5-4 10:36 | 顯示全部樓層
本帖最後由 alexliou 於 16-5-4 10:57 編輯

看到有人在問可能是類似的問題,
決定把如何在日線層級implement "XXX this bar at Close"寫得更具體清楚一點.

舉例而言, 有個日線層級的均線策略想要在突破均線後的次日開盤時買進,
但平倉時想在當天收盤前判斷是否跌破均線,如果跌破就當天收盤賣出.
程式碼如下:
[IntrabarOrderGeneration = False];
inputs: Price( Close ), Length( 9 ), ConfirmBars( 1 ) ;
variables: var0( 0 ) , var1(0);

condition1 = Price > AverageFC( Price, Length ) ;
if condition1 then
        var0 = var0 + 1
else
        var0 = 0 ;

condition2 = CurrentBar > ConfirmBars and var0 = ConfirmBars ;
if condition2 then Buy ( "B0" ) next bar at market ;

var1 = AverageFC( Price, Length );

condition3 = CurrentBar > 1 and Price crosses under var1 ;
if condition3 then  Sell ( "S0" ) this bar at close ;

這種作法只能在回測時行得通,
但在實際自動交易時, Sell("S0")的單子在收盤後根本送不出去,
就算在condition3 前加了 current_time_s>134400 的條件,
如果不打開IOG模式, 因下單永遠在closing tick of the bar, 一樣不能work.

但如果打開IOG模式, 買進condition的成立時點在回測時與實戰時都和想要的不一樣.

想要達到Sell this bar at close,
需要一個開啟IOG模式的輔助策略和上述策略一起執行, 程式碼如下:
[IntrabarOrderGeneration = True];
inputs: Price( Close ), Length( 9 );
variables: var1( 0 );

If GetAPPInfo(aiStrategyAuto)=1 then begin
      var1 = AverageFC( Price, Length );
      condition1 = Price crosses under var1 ;
      if current_time_s>134400  then  begin

            once (condition1) Sell ( "S1" ) next bar at market ;
      end;
end;

這樣的作法, 在回測時, 開啟IOG的S1 Entry信號不會執行, 回測結果等於只有第一個訊號的結果.
自動交易模式時, 第一個訊號的S0 Entry 無法在盤中觸發.
第二個訊號的S1 Entry 會在13:44分後視條件是否達成而觸發,
一般情形下, 與 sell this bar at close 的結果應該很接近.

如果想要在第二天收盤前才判斷價格是否正式跌破均線才決定是否賣出也可透過變更條件而做到.
(甚至某一特定時點如10:30也可, 但這樣就很有可能不該是日線層級的策略,且無對應的績效報告可做參考).
有興趣的人可以用凱為模擬交易所試試看

需要注意的是:  current_time_s>134400 如果移到和 GetAPPInfo(aiStrategyAuto)=1 一起 and 做判斷,
結果會很不同喔

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

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

GMT+8, 24-5-14 09:17

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

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