dengyishuo / fPortfolio

Portfolio Selection and Optimization
GNU General Public License v2.0
2 stars 1 forks source link

portfolioFrontier() Throws an error. #1

Open rccline opened 2 years ago

rccline commented 2 years ago

https://stackoverflow.com/questions/58155569/portfolioanalytics-error-while-creating-efficient-frontier

library(ROI.plugin.glpk) library(ROI) library(fPortfolio) library(timeSeries) library(quantmod) library(caTools) library(dplyr) library(PerformanceAnalytics)

effFrontier <- portfolioFrontier(portfolioReturns, constraints = "LongOnly")

Error in is.unsorted(x@positions) : 3 arguments passed to .Internal(is.unsorted) which requires 2

I have no idea how to resolve this issue.

Reprex

quantmod pt 8: Efficient Frontier Portfolio Method using fPortfolio -----------

#

Quantmod Part 8 Portfolio Optimization - CODEBLISS

# ###########################################################################

library(fPortfolio)

library(timeSeries)

> Warning: package 'timeSeries' was built under R version 4.1.2

> Loading required package: timeDate

library(quantmod)

> Warning: package 'quantmod' was built under R version 4.1.2

> Loading required package: xts

> Loading required package: zoo

>

> Attaching package: 'zoo'

> The following object is masked from 'package:timeSeries':

>

> time<-

> The following objects are masked from 'package:base':

>

> as.Date, as.Date.numeric

> Loading required package: TTR

> Registered S3 method overwritten by 'quantmod':

> method from

> as.zoo.data.frame zoo

library(caTools)

> Warning: package 'caTools' was built under R version 4.1.2

library(dplyr)

>

> Attaching package: 'dplyr'

> The following objects are masked from 'package:xts':

>

> first, last

> The following objects are masked from 'package:timeSeries':

>

> filter, lag

> The following objects are masked from 'package:stats':

>

> filter, lag

> The following objects are masked from 'package:base':

>

> intersect, setdiff, setequal, union

library(PerformanceAnalytics)

>

> Attaching package: 'PerformanceAnalytics'

> The following objects are masked from 'package:timeDate':

>

> kurtosis, skewness

> The following object is masked from 'package:graphics':

>

> legend

library(ggplot2) library(ROI.plugin.glpk)

> Warning: package 'ROI.plugin.glpk' was built under R version 4.1.2

library(ROI)

> Warning: package 'ROI' was built under R version 4.1.2

> ROI: R Optimization Infrastructure

> Registered solver plugins: nlminb, glpk.

> Default solver: auto.

library(fPortfolio)

> Warning: package 'fPortfolio' was built under R version 4.1.2

> Loading required package: fBasics

> Warning: package 'fBasics' was built under R version 4.1.2

>

> Attaching package: 'fBasics'

> The following object is masked from 'package:ROI':

>

> vech

> The following object is masked from 'package:TTR':

>

> volatility

> Loading required package: fAssets

> Warning: package 'fAssets' was built under R version 4.1.2

Codebliss: https://www.youtube.com/watch?v=5gmhZEl0kI8

Codebliss Website: http://ww1.programmingforfinance.com/

tickers <- c("AXP", "C", "WFC", "AMZN", "JNJ", "HD")

portfolioprices <- NULL for (ticker in tickers) portfolioprices <- cbind(portfolioprices, getSymbols(ticker, from = "2016-01-01", auto.assign = FALSE)[,4])

> 'getSymbols' currently uses auto.assign=TRUE by default, but will

> use auto.assign=FALSE in 0.5-0. You will still be able to use

> 'loadSymbols' to automatically load data. getOption("getSymbols.env")

> and getOption("getSymbols.auto.assign") will still be checked for

> alternate defaults.

>

> This message is shown once per session and may be disabled by setting

> options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.

Delete all dates with no prices

portfolioprices <- portfolioprices[apply(portfolioprices, 1, function(x) all(!is.na(x))),]

Rename columns

colnames(portfolioprices) <- tickers

Calculate Returns: Daily ROC

portfolioReturns <- na.omit(ROC(portfolioprices, type = "discrete")) portfolioReturns <- as.timeSeries(portfolioReturns)

Calculate monthly or weekly Returns

Stock_Data <- tickers %>%

lapply(function(x) getSymbols.google(x, from = "2016-01-01", auto.assign = FALSE[1,4]) %>%

lapply(function(x) monthlyReturn(x)))

STEP TWO: Calculate and Plot Frontier and Efficient Portfolios ---------

Calculate the efficient frontier

https://stackoverflow.com/questions/58155569/portfolioanalytics-error-while-creating-efficient-frontier

effFrontier <- portfolioFrontier(portfolioReturns, constraints = "LongOnly")

> Error in is.unsorted(x@positions): 3 arguments passed to .Internal(is.unsorted) which requires 2

Created on 2021-12-04 by the reprex package (v2.0.1)

zope410 commented 2 years ago

Hello Sir:

I am Carlos from the udemy course. I finally got the solution. There was a problem with is.unsorted in R versions between April and September 2021. If you update (for example 4.2) you do not get the error message anymore:

https://github.com/Bioconductor/S4Vectors/issues/95