開始使用免費開始

在 chart.Posn() 圖上加入指標

chart.Posn() 的一個有趣用法,是把指標疊加在圖上。這能幫助你看出策略實際在做什麼,以及原因。不過,要這麼做,你需要在策略範圍之外重新計算這些指標。完成後,只要把它們加到 chart.Posn 圖上即可。

在這個練習中,你會把策略中的三個指標加到剛建立的 chart.Posn 圖上。兩條移動平均(SMA50SMA200)會疊加在價格序列上,而 DVO_2_126 會有自己的視窗。

本練習屬於課程

R 的金融交易

檢視課程

練習說明

  • 先在策略之外,為 SPY 重新計算 SMA50SMA200DVO_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(___)
編輯並執行程式碼