开始使用免费开始使用

使用 add.rule() 实现建仓规则

太好了!您已经掌握了在 quantstrat 中构建规则的各个要素。到目前为止,您是按步骤逐一添加规则的。现在请把这些内容整合起来,检验一下自己对本章内容的掌握情况。

与平仓规则相对的是入场规则(enter rule)。在入场规则中,orderqty 不能设置为 "all",因为还没有任何初始持仓可以操作。本练习中,您将实现一个入场规则,它会引用策略中的 longentry 信号,并买入某资产的 1 股。

本练习是课程的一部分

R 中的金融交易

查看课程

练习说明

  • add.rule() 中指定参数以实现新的入场规则。
  • longentry 信号等于 TRUE 时触发规则。
  • 规则应以 "market" 类型下单,买入 1 股资产。
  • 规则方向应为 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 = "___")
编辑并运行代码