Almost, you haven't explicitly specified the used libraries (i.e. tidyquant) and you haven't put all elements together in a self-contained reprex. This would be closer to a proper reprex for your problem I think.
---
title: "reprex"
output: html_document
---
```{r setup, include=FALSE}
library(tidyquant)
knitr::opts_chunk$set(echo=FALSE, warning=FALSE, message=FALSE)
```
```{r}
spy_pr <- tq_get("SPY", get = "stock.prices", from = " 2020-01-01")
fit <- StructTS(spy_pr$adjusted, type = "level")
plot(spy_pr$adjusted, type = "l", lwd = 2)
lines(fitted(fit), lty = "dashed", lwd = 2, col = "green")
lines(tsSmooth(fit), lty ="dotted", lwd = 2, col = "blue")
```
The above example runs as expected when you knit it or execute the complete code chunks but indeed fails when you execute individual lines of code by sending them to the console with Ctrl + Enter . I think this has to do with the use of different graphics devices for the inline output and for the "Plots" panel.
As a workaround you could consider using ggplot2 instead of base R plots