Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
其中有一个R函数,用于根据值更改图表的背景_R_Finance - Fatal编程技术网

其中有一个R函数,用于根据值更改图表的背景

其中有一个R函数,用于根据值更改图表的背景,r,finance,R,Finance,我尝试在图表底部应用一种颜色(绿色或红色),这取决于我的投资 在这种情况下,我希望在2020年11月1日至2021年2月28日期间购买(背景中这些日期的颜色为绿色),然后在3月1日至今天期间无盖出售,因此使用红色。我在网上搜索了一些答案,但找不到是什么让我这么做的 为了能够在其他情况下使用此功能,我想我应该使用position_Nov20等。。。但我不明白到底是怎么回事。这是我的密码: 有人知道怎么做吗 install.packages("PerformanceAnalytics&qu

我尝试在图表底部应用一种颜色(绿色或红色),这取决于我的投资

在这种情况下,我希望在2020年11月1日至2021年2月28日期间购买(背景中这些日期的颜色为绿色),然后在3月1日至今天期间无盖出售,因此使用红色。我在网上搜索了一些答案,但找不到是什么让我这么做的

为了能够在其他情况下使用此功能,我想我应该使用position_Nov20等。。。但我不明白到底是怎么回事。这是我的密码:

有人知道怎么做吗

install.packages("PerformanceAnalytics")
install.packages("BatchGetSymbols")

library(quantmod)
library(PerformanceAnalytics)
library(BatchGetSymbols)

getSymbols("AMZN", from = "2020-04-30", to = "2021-04-01", src = "yahoo", adjust =  TRUE)
close_price <- (AMZN$AMZN.Close)
April20_close <- (close_price[1])
May20_close<- (close_price[21])
June20_close <- (close_price[43])
July20_close <- (close_price[65])
August20_close <- (close_price[86])
Sept20_close <- (close_price[107])
Oct20_close <- (close_price[129])
Nov20_close <- (close_price[149])
Dec20_close <- (close_price[171])
Jan21_close <- (close_price[190])
Feb21_close <- (close_price[209])
March21_close <- (close_price[232])

position_Nov20 <- coredata(Oct20_close) - April20_close
position_Dec20 <- coredata(Nov20_close) - May20_close
position_Jan21 <- coredata(Dec20_close) - June20_close
position_Feb21 <- coredata(Jan21_close) - July20_close
position_March21 <- coredata(Feb21_close) - August20_close
position_April21 <- coredata(March21_close) - Sept20_close

if (position_April21 > 0){
  print('buy')
} else {
  print('sell')
}

#from here, we try to do it as a loop
#investment decision

overall_positions <- c(position_Nov20, position_Dec20, position_Jan21, 
                       position_Feb21, position_March21, position_April21)
investment_decision <- ifelse(overall_positions>0, "+1", "-1")
print(investment_decision)

daily_return_Asset = CalculateReturns(AMZN$AMZN.Adjusted)
daily_return_Asset <- daily_return_Asset[2:nrow(daily_return_Asset),]

Multipied_vector <- daily_return_Asset*investment_decision

chart.CumReturns(
  daily_return_Asset,
  wealth.index = FALSE,
  geometric = TRUE,
  legend.loc = NULL,
  colorset = (1:12),
  begin = c("first", "axis"),
  plot.engine = "default"
)
install.packages(“性能分析”)
install.packages(“BatchGetSymbols”)
图书馆(quantmod)
库(性能分析)
库(BatchGetSymbols)
getSymbols(“AMZN”,from=“2020-04-30”,to=“2021-04-01”,src=“yahoo”,adjust=TRUE)
收盘价