在 chart.Posn() 图表中添加指标
chart.Posn() 的一个有趣用法是把指标叠加到图表上。这有助于展示策略在做什么,以及背后的原因。不过,要实现这一点,您需要在策略之外重新计算这些指标。完成后,只需把它们添加到 chart.Posn 图上即可。
在本练习中,您将把策略中的 3 个指标添加到刚刚创建的 chart.Posn 图中。两个移动平均(SMA50 和 SMA200)将叠加在价格序列上,而 DVO_2_126 将显示在单独的窗口中。
本练习是课程的一部分
R 中的金融交易
练习说明
- 先在策略之外为
SPY重新计算SMA50、SMA200和DVO_2_126指标。 - 使用
chart.Posn()重建上一练习中的图表。 - 使用
add_TA()将SMA50以蓝色线条叠加在价格图上。 - 使用
add_TA()将SMA200以红色线条叠加在价格图上。 - 使用
add_TA()将DVO_2_126作为新窗口添加到图表中。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Compute the SMA50
sma50 <- SMA(x = Cl(__), n = ___)
# Compute the SMA200
sma200 <- SMA(x = Cl(___), n = ___)
# Compute the DVO_2_126 with an navg of 2 and a percentlookback of 126
DVO_2_126 <- DVO(HLC = HLC(___), navg = ___, percentlookback = ___)
# Recreate the chart.Posn of the strategy from the previous exercise
chart.Posn(Portfolio = portfolio.st, Symbol = "___")
# Overlay the SMA50 on your plot as a blue line
add_TA(sma50, on = ___, col = "___")
# Overlay the SMA200 on your plot as a red line
add_TA(sma200, on = ___, col = "___")
# Add the DVO_2_126 to the plot in a new window
add_TA(___)