開始使用免費開始

使用 add.rule() 實作進場規則

太好了!你已經掌握在 quantstrat 中建立規則的所有要素。到目前為止,你是一步步新增規則,現在把它們整合起來,檢視自己在本章吸收得如何。

「出場規則」的相反就是「進場規則」。在進場規則中,orderqty 不能設為 "all",因為一開始並沒有既有部位可以操作。在這個練習中,你會實作一個進場規則,參照策略中的 longentry 訊號,並買進該資產的 1 股。

本練習屬於課程

R 的金融交易

檢視課程

練習說明

  • add.rule() 中設定參數,實作新的進場規則。
  • longentry 訊號等於 TRUE 時觸發規則。
  • 規則應以下 "market" 委託買進資產 1 股。
  • 規則的 side 應為 long,且 replace 應為 FALSE
  • 規則應在觀察到訊號後的下一天 Open 以市價買進。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Create an entry rule of 1 share when all conditions line up to enter into a position
add.rule(strategy.st, name = "ruleSignal", 
         
         # Use the longentry column as the sigcol
         arguments=list(sigcol = "___", 
                        
                        # Set sigval to TRUE
                        sigval = ___, 
                        
                        # Set orderqty to 1
                        orderqty = ___,
                        
                        # Use a market type of order
                        ordertype = "___",
                        
                        # Take the long orderside
                        orderside = "___",
                        
                        # Do not replace other signals
                        replace = ___, 
                        
                        # Buy at the next day's opening price
                        prefer = "___"),
         
         # This is an enter type rule, not an exit
         type = "___")
編輯並執行程式碼