diff --git a/R/aa.bl.r b/R/aa.bl.r index e39e8da..1ac7f16 100644 --- a/R/aa.bl.r +++ b/R/aa.bl.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Black-Litterman model Functions -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/aa.gmpl.r b/R/aa.gmpl.r index 8bb8c84..2e869cc 100644 --- a/R/aa.gmpl.r +++ b/R/aa.gmpl.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Read GNU MathProg model -# Copyright (C) 2012 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/aa.omega.r b/R/aa.omega.r index 2a4d9fc..71bdf50 100644 --- a/R/aa.omega.r +++ b/R/aa.omega.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Optimizing Omega Ration Functions -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/aa.r b/R/aa.r index 46d2a86..85af2e5 100644 --- a/R/aa.r +++ b/R/aa.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Asset Allocation Functions -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/aa.test.r b/R/aa.test.r index e8e6e6b..4154741 100644 --- a/R/aa.test.r +++ b/R/aa.test.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Test cases for Asset Allocation Functions -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### @@ -2158,7 +2159,7 @@ aa.test.create.ia <- function() #-------------------------------------------------------------------------- # Create historical input assumptions #-------------------------------------------------------------------------- - ia = create.historical.ia(hist.returns, 12, symbols, symbol.names) + ia = create.historical.ia(hist.returns, 12) return(ia) } diff --git a/R/branchbound.r b/R/branchbound.r index 5809e9d..36a39ff 100644 --- a/R/branchbound.r +++ b/R/branchbound.r @@ -1,23 +1,24 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Binary Branch and Bound and it's adaption for QP problem # Copyright (C) 1998-2000 Alberto Bemporad, Domenico Mignone - author's of the original Matlab version -# Copyright (C) 2011 Michael Kapler - adapted code to R # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/bt.r b/R/bt.r index 2c28f9a..64a991d 100644 --- a/R/bt.r +++ b/R/bt.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Backtest Functions -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### @@ -737,7 +738,7 @@ bt.trim <- function bt$equity = bt$equity[index] bt$equity = bt$equity / as.double(bt$equity[1]) bt$ret = bt$ret[index] - bt$weight = bt$weight[index,,drop=F] + if (!is.null(bt$weight)) bt$weight = bt$weight[index,,drop=F] if (!is.null(bt$share)) bt$share = bt$share[index,,drop=F] bt$best = max(bt$ret) @@ -2038,7 +2039,7 @@ bt.simple.test <- function() #' weight = matrix(weight, nrow=2, byrow=TRUE) #' print(bt.apply.min.weight(weight, 0.1)) #' } -#' @author Ivan Popivanov and Michael Kapler +#' @author Ivan Popivanov #' @export ############################################################################### # Possible use diff --git a/R/bt.share.r b/R/bt.share.r index 7e291e4..b375b63 100644 --- a/R/bt.share.r +++ b/R/bt.share.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### -# Backtest Functions -# Copyright (C) 2015 Systematic Investor +# Backtest Functions based on shares invested # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### @@ -220,6 +221,7 @@ bt.run.share.ex <- function prices = coredata(prices) n = ncol(prices) + nperiods = nrow(prices) # execution.price logic trade = !is.na(weight) @@ -266,7 +268,7 @@ bt.run.share.ex <- function bankrupt = which(bt$ret <= -1) - if(len(bankrupt) > 0) bt$ret[bankrupt[1]:n] = -1 + if(len(bankrupt) > 0) bt$ret[bankrupt[1]:nperiods] = -1 bt$equity = cumprod(1 + bt$ret) diff --git a/R/bt.share.test.r b/R/bt.share.test.r index 2ab247d..f5d75c1 100644 --- a/R/bt.share.test.r +++ b/R/bt.share.test.r @@ -1,3 +1,24 @@ +############################################################################### +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. +############################################################################### +# Evaluating Sample Trading Strategies using Backtesting library +# +# For more information please email at TheSystematicInvestor at gmail +############################################################################### bt.run.share.ex.example.match.adjusted.unadjusted = function() { diff --git a/R/bt.stop.r b/R/bt.stop.r index fef8c77..65d5e63 100644 --- a/R/bt.stop.r +++ b/R/bt.stop.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### -# Stop functionality for Backtests -# Copyright (C) 2013 Michael Kapler +# Stop functionality for Backtest library # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/bt.stop.test.r b/R/bt.stop.test.r index 614b525..450becb 100644 --- a/R/bt.stop.test.r +++ b/R/bt.stop.test.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### -# Test for Stop functionality for Backtests -# Copyright (C) 2013 Michael Kapler +# Test for Stop functionality in the Backtest library # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/bt.summary.r b/R/bt.summary.r index 7e0736c..5449a19 100644 --- a/R/bt.summary.r +++ b/R/bt.summary.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Backtest Summary Report Functions -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/bt.test.r b/R/bt.test.r index 6a65696..c255d1f 100644 --- a/R/bt.test.r +++ b/R/bt.test.r @@ -1,23 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -############################################################################### -# Evaluting Sample Trading Strategies using Backtesting library in -# the Systematic Investor Toolbox -# Copyright (C) 2011 Michael Kapler +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. +############################################################################### +# Evaluating Sample Trading Strategies using Backtesting library # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### bt.empty.test <- function() @@ -9212,7 +9212,7 @@ dev.off() ############################################################################### # Strategy Testing Intraday data from http://thebonnotgang.com/tbg/historical-data/ ############################################################################### -bt.strategy.intraday.thebonnotgang.test <- function() +bt.strategy.intraday.thebonnotgang.test <- function(spath = 'c:/Desktop/') { #***************************************************************** # Load historical data @@ -9220,10 +9220,7 @@ bt.strategy.intraday.thebonnotgang.test <- function() load.packages('quantmod') # data from http://thebonnotgang.com/tbg/historical-data/ - # please save SPY and GLD 1 min data at the given path - spath = 'c:/Desktop/' -spath = 'c:/Documents and Settings/mkapler/Desktop/' -spath = 'c:/Desktop/1car/1shaun/' + # please save SPY and GLD 1 min data at the given path data = bt.load.thebonnotgang.data('SPY,GLD', spath) data1 <- new.env() @@ -9310,7 +9307,7 @@ dev.off() } -bt.pair.strategy.intraday.thebonnotgang.test <- function() +bt.pair.strategy.intraday.thebonnotgang.test <- function(spath = 'c:/Desktop/') { #***************************************************************** # Load historical data @@ -9319,8 +9316,6 @@ bt.pair.strategy.intraday.thebonnotgang.test <- function() # data from http://thebonnotgang.com/tbg/historical-data/ # please save SPY and GLD 1 min data at the given path - spath = 'c:/Desktop/' -spath = 'c:/Documents and Settings/mkapler/Desktop/' data = bt.load.thebonnotgang.data('USO,GLD', spath) bt.prep(data, align='keep.all', fill.gaps = T) diff --git a/R/cluster.r b/R/cluster.r index 2c0eff8..736d8f4 100644 --- a/R/cluster.r +++ b/R/cluster.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### -# Repository of Benchmark Strategies -# Copyright (C) 2014 Michael Kapler +# Collection of utilities to setup calculations on a cluster. # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/data.proxy.r b/R/data.proxy.r index b206b5e..3de7426 100644 --- a/R/data.proxy.r +++ b/R/data.proxy.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### -# Collection of routines to examine and compare proxies and data -# Copyright (C) 2013 Michael Kapler +# Collection of routines to examine and compare data proxies # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/data.r b/R/data.r index ed71075..e482ad4 100644 --- a/R/data.r +++ b/R/data.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Collection of routines to work with data -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### #' @export @@ -260,6 +261,13 @@ PricingZeroCouponBond <- function # Convert Historical TBills rates to Total Returns # http://timelyportfolio.blogspot.com/2011/04/historical-sources-of-bond-returns_17.html # http://timelyportfolio.blogspot.ca/2012/11/cashopportunity-lost-or-opportunity.html +# +# [S&P500 and 10yr TReasurey returns since 1928](http://people.stern.nyu.edu/adamodar/pc/datasets/histret.xls) +# http://pages.stern.nyu.edu/~adamodar/New_Home_Page/datafile/histretSP.html +# Another example to compute Return on bond = +# * Yield at start of the year plus +# * 10YR bond price change given the change in Yield +# http://people.stern.nyu.edu/adamodar/pc/datasets/ #' @export ############################################################################### processTBill <- function @@ -751,6 +759,10 @@ getQuote.google.xml <- function(tickers) { ############################################################################### # Download historical intraday prices from Google Finance +# +# [Best Example](http://stackoverflow.com/questions/15609334/how-to-download-intraday-stock-market-data-with-r) +# as.POSIXct(1357828200-300*60, origin = '1970-01-01') +# # http://www.mathworks.com/matlabcentral/fileexchange/32745-get-intraday-stock-price # http://www.mathworks.com/matlabcentral/fileexchange/36115-volume-weighted-average-price-from-intra-daily-data # http://www.codeproject.com/KB/IP/google_finance_downloader.aspx @@ -774,15 +786,36 @@ getSymbol.intraday.google <- function '&p=', period, '&f=', 'd,o,h,l,c,v', sep='') - load.packages('data.table') - out = fread(url, stringsAsFactors=F) + txt = get.url(url) + #write(txt, file='1.txt') + #txt = read.file('google-spy-getprices.txt') - if(ncol(out) < 5) { + lines = spl(txt,'\n') + if(len(lines) < 7) { cat('Error getting data from', url, '\n') return(NULL) } - setnames(out, spl('Date,Open,High,Low,Close,Volume')) + marker = 'COLUMNS=' + map = c(DATE='Date', CLOSE='Close', HIGH='High', LOW='Low', OPEN='Open', VOLUME='Volume') + cols = map[ spl(gsub(marker, '', lines[ grep(marker,lines)[1] ])) ] + + marker = 'TIMEZONE_OFFSET=' + index = grep(marker,lines) + + load.packages('data.table') + out = c() + for(i in 1:len(index)) { + end = iif( i == len(index), -1, index[(i+1)] - index[i] - 1) + timezone.offset = as.double(gsub( marker, '', lines[index[i]] )) + out = rbind(out, getSymbol.intraday.google.parse(txt, interval, index[i], end, timezone.offset, cols) ) + } + out +} + +getSymbol.intraday.google.parse = function(txt, interval, start, end, timezone.offset, cols) { + out = fread(txt, stringsAsFactors = F, skip = start, nrows = end) + setnames(out, cols) # date logic date = out$Date @@ -793,14 +826,47 @@ getSymbol.intraday.google <- function temp = ifna.prev(temp) date = temp + date * interval date[date.index] = temp[date.index] - class(date) = c("POSIXt", "POSIXct") - - date = date - (as.double(format(date[1],'%H')) - 9)*60*60 + #class(date) = c("POSIXt", "POSIXct") + #date = date - (as.double(format(date[1],'%H')) - 9)*60*60 + date = as.POSIXct(date + timezone.offset*interval, origin = '1970-01-01') make.xts(out[,-1,with=F], date) } +getSymbol.intraday.google.test = function() { + # plot intraday spy over last 20 days + data=getSymbol.intraday.google('SPY','NYSEARCA',period = '20d') + col.names = spl('black,blue') + cols = c(col.add.alpha(col.names[1],100), col.add.alpha(col.names[2],250)) + + ret = Cl(data) / mlag(Cl(data)) - 1 + group = as.numeric(format(index(data),'%H%M')) + stat = tapply(ret,group,mean, na.rm=T) + stat[1] = 0 # omit overnight return + + label = names(stat) + label = iif(nchar(label)<4, paste0('0',label), label) + dates = strptime(paste0('20170101 ', label, '01'), format='%Y%m%d %H%M%S') + equity = 100*(cumprod(1+stat)-1) + equity = xts(equity, dates) + plota(equity, type='l', col = cols[1], lwd=5, LeftMargin=3) + + # 2nd axis + date = as.Date(index(ret)) + stat = ret[date == last(date)] + stat[1] = 0 + dates = strptime(paste0('20170101 ', format(index(stat),'%H%M'), '01'), format='%Y%m%d %H%M%S') + equity = 100*(cumprod(1+stat)-1) + equity = xts(coredata(equity), dates) + + plota2Y(equity, las=1, col=cols[2], col.axis = cols[2]) + plota.lines(equity, type='l', col = cols[2]) + plota.legend('20D Avg(rhs),Today(lhs)', col.names) + + +#!!! add chart for trutrn intrday vs overnight!!! +} ############################################################################### # getSymbols interface to Yahoo today's delayed qoutes @@ -2449,17 +2515,35 @@ edgar.info <- function(ticker) zacks.info <- function(ticker = 'IBM') { url = paste0('http://www.zacks.com/stock/research/', ticker, '/earnings-announcements') - txt = join(readLines(url)) - - out = list() - require(jsonlite) - - for(i in spl('earnings,webcasts,revisions,splits,dividends,guidance')) { - data = extract.token(txt,paste0('') - data = fromJSON(paste('{"data"', data)) - out[[i]] = data$data - } - out + txt = get.url(url) + #write(txt, file='1.txt') + + require(jsonlite) + + # extract data + temp = extract.token(txt,'document.obj_data = ','};') + temp = fromJSON(paste(temp, '}')) + names(temp) = gsub('_table','',gsub('earnings_announcements_','',names(temp))) + data = temp + + # extract headers + url = 'http://staticx.zacks.com/js/zacks/inline/company_event_detail.js' + info = get.url(url) + temp = extract.token(info,'var aryColTableChecked = ','};') + temp = gsub('title','"title"',temp) + temp = gsub('class','"class"',temp) + temp = fromJSON(paste(temp, '}')) + names(temp) = gsub('_table','',gsub('earnings_announcements_','',names(temp))) + header = temp + + # set headers + for(i in names(data)) + if( len(data[[ i ]]) > 0) { + temp = rep('', ncol(data[[ i ]])) + temp[1:nrow(header[[ i ]])] = header[[ i ]][,1] + colnames(data[[ i ]]) = temp + } + data } diff --git a/R/factor.model.r b/R/factor.model.r index c8db351..fc94634 100644 --- a/R/factor.model.r +++ b/R/factor.model.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Collection of Utilities to Biuld and Analyze Multiple Factor Model -# Copyright (C) 2012 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/factor.model.test.r b/R/factor.model.test.r index 68037f6..545ab69 100644 --- a/R/factor.model.test.r +++ b/R/factor.model.test.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Test cases and examples for Multiple Factor Model functions -# Copyright (C) 2012 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/fundamental.data.r b/R/fundamental.data.r index 89ded33..7f1b218 100644 --- a/R/fundamental.data.r +++ b/R/fundamental.data.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### -# Collection of Utilities to work with Fundamental Data from advfn -# Copyright (C) 2012 Michael Kapler +# Collection of Utilities to work with Fundamental Data from advfn website # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/fundamental.test.r b/R/fundamental.test.r index 816b952..79d8157 100644 --- a/R/fundamental.test.r +++ b/R/fundamental.test.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Fundamental Analysis Tests -# Copyright (C) 2012 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/interactive.r b/R/interactive.r index 879367f..c65d595 100644 --- a/R/interactive.r +++ b/R/interactive.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Collection of routines for interactive plots -# Copyright (C) 2015 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/min.corr.paper.r b/R/min.corr.paper.r index ff0f1ef..e376be7 100644 --- a/R/min.corr.paper.r +++ b/R/min.corr.paper.r @@ -1,25 +1,25 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Examples for the Minimum Correlation Algorithm paper -# Copyright (C) 2012 Michael Kapler -# # Forecast-Free Algorithms: A New Benchmark For Tactical Strategies # http://cssanalytics.wordpress.com/2011/08/09/forecast-free-algorithms-a-new-benchmark-for-tactical-strategies/ # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/optimization.r b/R/optimization.r index 0221558..75a31ca 100644 --- a/R/optimization.r +++ b/R/optimization.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Optimization Functions -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### @@ -694,10 +695,14 @@ lm.constraint <- function fit = lm.fit(x, y) return( ols.summary(x, y, fit$coefficients) ) } else { - temp = cov(cbind(y, x)) - Dmat = temp[-1,-1] - dvec = temp[-1,1] - + #[Are “constrained linear least squares” and “quadratic programming” the same thing?](https://math.stackexchange.com/questions/869204/are-constrained-linear-least-squares-and-quadratic-programming-the-same-thin) + #[constrained OLS in R](https://stats.stackexchange.com/questions/57172/prediction-constrained-ols-in-r) + #temp = cov(cbind(y, x)) + #Dmat = temp[-1,-1] + #dvec = temp[-1,1] + Dmat = t(x) %*% x + dvec = t(y) %*% x + sol = solve.QP.bounds(Dmat = Dmat, dvec = dvec , Amat=constraints$A, bvec=constraints$b, constraints$meq, lb = constraints$lb, ub = constraints$ub) @@ -706,6 +711,27 @@ lm.constraint <- function } +############################################################################### +# Run (Total least squares regression)[https://en.wikipedia.org/wiki/Total_least_squares] +# (How to perform orthogonal regression ,total least squares, via PCA?)[http://stats.stackexchange.com/questions/13152/how-to-perform-orthogonal-regression-total-least-squares-via-pca] +#' @export +############################################################################### +tls <- function +( + x, + y +) +{ + n = ncol(x) + C = cbind(x, y) + + V = svd(C)$v + VXY = V[1:n, (n+1):ncol(V)] + VYY = V[(n+1):nrow(V), (n+1):ncol(V)] + -VXY/VYY +} + + ############################################################################### # Run linear least squares regression #' @export diff --git a/R/plot.table.r b/R/plot.table.r index 32af89d..7f486fe 100644 --- a/R/plot.table.r +++ b/R/plot.table.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Plot table ( vector or matrix ) -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/plota.r b/R/plota.r index 7fa00f8..e1e9ef2 100644 --- a/R/plota.r +++ b/R/plota.r @@ -1,23 +1,24 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Create Technical Analysis Plots # plota = plot + ta -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### @@ -1116,7 +1117,6 @@ plota.matplot <- function } - ############################################################################### #' Add Copyright message to the plot #' @@ -1164,3 +1164,58 @@ plota.recession <- function plota.x.highlight(highlight, highlight != 0, col) } + +############################################################################### +#' Create Ascii(txt) plot +#' +#' Based on the folloiwng idea: [ASCII Plotting Functions for R](http://stackoverflow.com/questions/14736556/ascii-plotting-functions-for-r) +#' +#' Plot multiple series by stacking data and using corresponding lables (pch) +#' also works with NA's so we can potentially plot many partial series +#' +#' @param y matrix or vector of y values +#' @param x optional vector of x values, \strong{defaults to 1:nr, where nr is the number of rows in y} +#' @param pch optional vector of markers for time series, \strong{defaults to 1:nc, where nc is the number of columns in y} +#' @param width optional location of the plot legend, \strong{defaults to 80% of the screen width} +#' +#' @return nothing +#' +#' @examples +#' \dontrun{ +#' txtplot(1:10, c(1,2,3,4,5,NA,7,8,NA,10)) +#' +#' x = 1:10;y = 1:10;y1 = 30:21 +#' txtplot(rbind(x,x), rbind(y,y1), pch=c('+','*')) +#' +#' plot.txt(cbind(y,y1),x) +#' plot.txt(cbind(y,y1),x,pch=c('+','*')) +#' } +#' @export +############################################################################### +plot.txt = function +( + y, + x=NULL, + pch=1:ncol(y), + width = round(options()$width * 0.8) +) +{ + # load.packages('txtplot') + require(txtplot) + if(is.null(x)) x = 1:nrow(y) + #txtplot(rep.row(x, ncol(y)), t(y), pch=letters[1:ncol(y)]) + txtplot(rep.row(x, ncol(y)), t(y), pch=pch) + txtplot:::drawLegend(colnames(y), width) +} + +plot.txt.test = function() { + require(txtplot) + + data = cbind(q1=models$high52w.1$equity['2009::2010'],q10=models$high52w.10$equity['2009::2010']) + plot.txt(data) + + data = matrix(rnorm(50), nc=2) + plot.txt(data, pch=c('.','*')) + + txtplot(models$high52w.10$equity['2009::2010']) +} diff --git a/R/random.r b/R/random.r index ac99efe..a34e34e 100644 --- a/R/random.r +++ b/R/random.r @@ -1,23 +1,24 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Collection of Random Generators # Copyright (C) 2005-2006 Roger Stafford - the author of the original Matlab version -# Copyright (C) 2012 Michael Kapler - ported Roger Stafford's code to R # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/rfinance2012.r b/R/rfinance2012.r index 4c09bc8..7dfe7fd 100644 --- a/R/rfinance2012.r +++ b/R/rfinance2012.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### -# Examples for the R/Finance Presentation -# Copyright (C) 2012 Michael Kapler +# Examples for the 2012 R/Finance Presentation # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/rfinance2013.r b/R/rfinance2013.r index 53c0929..fa4bdab 100644 --- a/R/rfinance2013.r +++ b/R/rfinance2013.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### -# Examples for the R/Finance Presentation -# Copyright (C) 2013 Michael Kapler +# Examples for the 2013 R/Finance Presentation # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/rfinance2014.r b/R/rfinance2014.r index f512c7a..3d2b0b0 100644 --- a/R/rfinance2014.r +++ b/R/rfinance2014.r @@ -1,25 +1,24 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Examples for the Adaptive Shrinkage paper -# Copyright (C) 2013 Michael Kapler -# -# Adaptive Shrinkage # http://cssanalytics.wordpress.com/2013/10/24/adaptive-shrinkage/ # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/shiny.R b/R/shiny.R index 3aba8dd..f6e12f2 100644 --- a/R/shiny.R +++ b/R/shiny.R @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Repository of Helper Functions for Shiny Framework -# Copyright (C) 2013 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/strategy.r b/R/strategy.r index 01d9dd8..a22d034 100644 --- a/R/strategy.r +++ b/R/strategy.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Repository of Benchmark Strategies -# Copyright (C) 2012 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### @@ -787,7 +788,23 @@ risk.parity.portfolio <- function( set.risky.asset(sol$solution, risk.index) } - + # ============================================================ + # + # [Risk Parity Strategies For Equity Portfolio Management Can an asset-class strategy translate to equities? By Frank Siu](http://www.axioma.com/media/uploads/research-papers/risk_parity_strategies_for_equity_portfolio_managenemt_frank_siu1.pdf) + # page 8 + # + # Maximum diversification and risk parity are both driven by + # the concept of diversification. Risk parity spreads out risk + # across its various sources; maximum diversification aims to + # reduce the share of portfolio risk coming from correlations. + # + # Mathematically, Equation 9, maximum diversification, is also analogous + # to maximizing the Sharpe ratio, + # where each stocks expected return is equal to its volatility. + # + # Please see Max Sharpe ratio for implementation including linear constraints + # + # ============================================================ # Toward Maximum Diversification by Y. Choueifaty, Y. Coignard # The Journal of Portfolio Management, Fall 2008, Vol. 35, No. 1: pp. 40-51 #' @export @@ -1095,6 +1112,19 @@ random.hist.weight = function( # # Interesting info # http://r.789695.n4.nabble.com/The-best-solver-for-non-smooth-functions-td4636934.html +# +# +# [Maximize the Sharpe Ratio in Axioma Portfolio](http://www.updatefrom.com/axioma/0703/focus.html) +# +# min w*E*w +# s.t. +# mu * w = mean(mu) +# e * w - 1000000 * t = 0 +# s * w - up * t < 0 +# s * w - dn * t > 0 +# w, t >= 0 +# +# final solution is w / t ############################################################################### # only works for constraints that are homogeneous of degree 0 # i.e. if we multiply solution weight by a number, the constraint is unchanged @@ -1144,6 +1174,55 @@ random.hist.weight = function( } +############################################################################### +# [Maximize the Sharpe Ratio in Axioma Portfolio](http://www.updatefrom.com/axioma/0703/focus.html) +# +# min w*E*w +# s.t. +# mu * w = mean(mu) +# e * w - 1000000 * t = 0 +# s * w - up * t < 0 +# s * w - dn * t > 0 +# w, t >= 0 +# +# final solution is w / t +############################################################################### + #' @export + max.sharpe.portfolio.axioma <- function + ( + ia, # input assumptions + constraints, + rf = 0, + excess.return = ia$expected.return - rf + ) + { + # * add non-negative variable t + n = len(constraints$lb) + n1 = n+1 + constraints = add.variables(1, constraints, 0) + constraints = add.constraints(c(rep(0,n),1), type='>=', b=0, constraints) + + # * adjust input assumptions + ia1 = ia + ia1$n = n1 + ia1$cov = matrix(0, n1, n1) + ia1$cov[1:n, 1:n] = ia$cov + ia1$risk = c(ia$risk,1) + + # * reformulate all constraints: multiply right side by t and moving it to the left + constraints$A[n1,] = -constraints$b + constraints$b[] = 0 + + # * add return target constraint: mu * w = mean(mu) + constraints = add.constraints(c(excess.return,0), mean(excess.return) , type = '=', constraints) + + # * optimal solution is w/t + weight = min.var.portfolio(ia1,constraints) + + weight[1:n]/weight[n1] + } + + max.sharpe.portfolio.test <- function() { #***************************************************************** @@ -1181,10 +1260,32 @@ png(filename = 'plot1.png', width = 500, height = 500, units = 'px', pointsize = points(100 * portfolio.risk(weight,ia), 100 * portfolio.return(weight,ia), pch=15, col='orange') portfolio.return(weight,ia) / portfolio.risk(weight,ia) + w1 = max.sharpe.portfolio.axioma(ia,constraints) + round(weight - w1,2) + weight = w1 + points(100 * portfolio.risk(weight,ia), 100 * portfolio.return(weight,ia), pch=15, col='cyan') + portfolio.return(weight,ia) / portfolio.risk(weight,ia) + + # Maximum diversification portfolio is a max Sharpe portfolio, + # but we substitute *Expected Return* with *Risk* in the objective function + weight = max.div.portfolio(ia, constraints) + w1 = max.sharpe.portfolio.axioma(ia,constraints, excess.return=ia$risk) + round(weight - w1,2) + weight = w1 + points(100 * portfolio.risk(weight,ia), 100 * portfolio.return(weight,ia), pch=15, col='blue') + portfolio.return(weight,ia) / portfolio.risk(weight,ia) + + + + + plota.legend('Minimum Variance,Maximum Sharpe','red,orange', x='topright') dev.off() + + + #***************************************************************** # Examples of Maximum Sharpe or Tangency portfolios construction #****************************************************************** @@ -1932,7 +2033,6 @@ min.var2 <- function(power.function = 1) # # Shrinks towards constant correlation matrix, Ledoit and Wolf (2004) # - # x = as.matrix(read.csv('c:/Michael_Kapler/Soft/R/ira/test.csv', header =F)) # x=matrix(rnorm(3*200),200,3) # covCor(x) # @@ -2172,7 +2272,6 @@ static.group <- function(group) # Idea by David Varadi # http://cssanalytics.wordpress.com/2013/11/26/fast-threshold-clustering-algorithm-ftca/ # Original code by Pierre Chretien -# Small updates by Michael Kapler #' @export cluster.group.FTCA <- function ( diff --git a/R/ta.r b/R/ta.r index cc978ca..13665b6 100644 --- a/R/ta.r +++ b/R/ta.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Technical Analysis Functions -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/R/utils.r b/R/utils.r index 828792d..e8c9c20 100644 --- a/R/utils.r +++ b/R/utils.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Collection of General Utilities -# Copyright (C) 2012 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/SIT.tar.gz b/SIT.tar.gz index b8a4203..2826de3 100644 Binary files a/SIT.tar.gz and b/SIT.tar.gz differ diff --git a/make.sit.pkg.r b/make.sit.pkg.r index 10bd05e..0830986 100644 --- a/make.sit.pkg.r +++ b/make.sit.pkg.r @@ -40,11 +40,11 @@ write.dcf(list( Description = 'Systematic Investor Toolbox is a collection of tools that\n I use in my investment research.', Version = format(Sys.Date(),'%Y.%m.%d'), Date = Sys.Date(), - License = 'GPL-3', + License = 'Zlib', LazyLoad = 'yes', Author = 'Michael Kapler ', Maintainer = 'Michael Kapler ', - Depends = 'SIT.date' + Imports = 'SIT.date' ), file = file.path('pkg', "DESCRIPTION") ) @@ -73,7 +73,7 @@ cat(" #' load.packages('quantmod') #' tickers = spl('SPY,TLT,GLD,SHY') #' -#' data <- new.env() +#' data = new.env() #' getSymbols(tickers, src = 'yahoo', from = '1980-01-01', env = data, auto.assign = T) #' # adjust for dividends #' for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T) diff --git a/pkg/DESCRIPTION b/pkg/DESCRIPTION index d0d1e8b..16ba1ab 100644 --- a/pkg/DESCRIPTION +++ b/pkg/DESCRIPTION @@ -3,13 +3,13 @@ Type: Package Title: Systematic Investor Toolbox Description: Systematic Investor Toolbox is a collection of tools that I use in my investment research. -Version: 2016.06.12 -Date: 2016-06-12 -License: GPL-3 +Version: 2017.08.23 +Date: 2017-08-23 +License: Zlib LazyLoad: yes Author: Michael Kapler Maintainer: Michael Kapler -Depends: +Imports: SIT.date Collate: 'aa.bl.r' diff --git a/pkg/NAMESPACE b/pkg/NAMESPACE index 4bca23a..7e12b27 100644 --- a/pkg/NAMESPACE +++ b/pkg/NAMESPACE @@ -294,6 +294,7 @@ export(max.omega.portfolio) export(max.return.portfolio) export(max.sharpe.nlp.portfolio) export(max.sharpe.portfolio) +export(max.sharpe.portfolio.axioma) export(max.sharpe.portfolio.helper) export(max.shrinkage) export(meom.strategy) @@ -363,6 +364,7 @@ export(plot.quantiles) export(plot.table) export(plot.transition.map) export(plot.transitopn.map) +export(plot.txt) export(plot12month.rolling.spread) export(plota) export(plota.add.copyright) @@ -496,6 +498,7 @@ export(test.equality) export(tic) export(time.seasonality) export(timing.strategy) +export(tls) export(to.cash) export(to.nice) export(to.percent) diff --git a/pkg/R/SIT-package.R b/pkg/R/SIT-package.R index 0bf0847..e97732f 100644 --- a/pkg/R/SIT-package.R +++ b/pkg/R/SIT-package.R @@ -22,7 +22,7 @@ #' load.packages('quantmod') #' tickers = spl('SPY,TLT,GLD,SHY') #' -#' data <- new.env() +#' data = new.env() #' getSymbols(tickers, src = 'yahoo', from = '1980-01-01', env = data, auto.assign = T) #' # adjust for dividends #' for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T) diff --git a/pkg/R/aa.bl.r b/pkg/R/aa.bl.r index e39e8da..1ac7f16 100644 --- a/pkg/R/aa.bl.r +++ b/pkg/R/aa.bl.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Black-Litterman model Functions -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/R/aa.gmpl.r b/pkg/R/aa.gmpl.r index 8bb8c84..2e869cc 100644 --- a/pkg/R/aa.gmpl.r +++ b/pkg/R/aa.gmpl.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Read GNU MathProg model -# Copyright (C) 2012 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/R/aa.omega.r b/pkg/R/aa.omega.r index 2a4d9fc..71bdf50 100644 --- a/pkg/R/aa.omega.r +++ b/pkg/R/aa.omega.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Optimizing Omega Ration Functions -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/R/aa.r b/pkg/R/aa.r index 46d2a86..85af2e5 100644 --- a/pkg/R/aa.r +++ b/pkg/R/aa.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Asset Allocation Functions -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/R/aa.test.r b/pkg/R/aa.test.r index e8e6e6b..4154741 100644 --- a/pkg/R/aa.test.r +++ b/pkg/R/aa.test.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Test cases for Asset Allocation Functions -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### @@ -2158,7 +2159,7 @@ aa.test.create.ia <- function() #-------------------------------------------------------------------------- # Create historical input assumptions #-------------------------------------------------------------------------- - ia = create.historical.ia(hist.returns, 12, symbols, symbol.names) + ia = create.historical.ia(hist.returns, 12) return(ia) } diff --git a/pkg/R/branchbound.r b/pkg/R/branchbound.r index 5809e9d..36a39ff 100644 --- a/pkg/R/branchbound.r +++ b/pkg/R/branchbound.r @@ -1,23 +1,24 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Binary Branch and Bound and it's adaption for QP problem # Copyright (C) 1998-2000 Alberto Bemporad, Domenico Mignone - author's of the original Matlab version -# Copyright (C) 2011 Michael Kapler - adapted code to R # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/R/bt.r b/pkg/R/bt.r index 2c28f9a..64a991d 100644 --- a/pkg/R/bt.r +++ b/pkg/R/bt.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Backtest Functions -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### @@ -737,7 +738,7 @@ bt.trim <- function bt$equity = bt$equity[index] bt$equity = bt$equity / as.double(bt$equity[1]) bt$ret = bt$ret[index] - bt$weight = bt$weight[index,,drop=F] + if (!is.null(bt$weight)) bt$weight = bt$weight[index,,drop=F] if (!is.null(bt$share)) bt$share = bt$share[index,,drop=F] bt$best = max(bt$ret) @@ -2038,7 +2039,7 @@ bt.simple.test <- function() #' weight = matrix(weight, nrow=2, byrow=TRUE) #' print(bt.apply.min.weight(weight, 0.1)) #' } -#' @author Ivan Popivanov and Michael Kapler +#' @author Ivan Popivanov #' @export ############################################################################### # Possible use diff --git a/pkg/R/bt.stop.r b/pkg/R/bt.stop.r index fef8c77..65d5e63 100644 --- a/pkg/R/bt.stop.r +++ b/pkg/R/bt.stop.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### -# Stop functionality for Backtests -# Copyright (C) 2013 Michael Kapler +# Stop functionality for Backtest library # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/R/bt.stop.test.r b/pkg/R/bt.stop.test.r index 614b525..450becb 100644 --- a/pkg/R/bt.stop.test.r +++ b/pkg/R/bt.stop.test.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### -# Test for Stop functionality for Backtests -# Copyright (C) 2013 Michael Kapler +# Test for Stop functionality in the Backtest library # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/R/bt.summary.r b/pkg/R/bt.summary.r index 7e0736c..5449a19 100644 --- a/pkg/R/bt.summary.r +++ b/pkg/R/bt.summary.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Backtest Summary Report Functions -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/R/bt.test.r b/pkg/R/bt.test.r index 6a65696..c255d1f 100644 --- a/pkg/R/bt.test.r +++ b/pkg/R/bt.test.r @@ -1,23 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -############################################################################### -# Evaluting Sample Trading Strategies using Backtesting library in -# the Systematic Investor Toolbox -# Copyright (C) 2011 Michael Kapler +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. +############################################################################### +# Evaluating Sample Trading Strategies using Backtesting library # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### bt.empty.test <- function() @@ -9212,7 +9212,7 @@ dev.off() ############################################################################### # Strategy Testing Intraday data from http://thebonnotgang.com/tbg/historical-data/ ############################################################################### -bt.strategy.intraday.thebonnotgang.test <- function() +bt.strategy.intraday.thebonnotgang.test <- function(spath = 'c:/Desktop/') { #***************************************************************** # Load historical data @@ -9220,10 +9220,7 @@ bt.strategy.intraday.thebonnotgang.test <- function() load.packages('quantmod') # data from http://thebonnotgang.com/tbg/historical-data/ - # please save SPY and GLD 1 min data at the given path - spath = 'c:/Desktop/' -spath = 'c:/Documents and Settings/mkapler/Desktop/' -spath = 'c:/Desktop/1car/1shaun/' + # please save SPY and GLD 1 min data at the given path data = bt.load.thebonnotgang.data('SPY,GLD', spath) data1 <- new.env() @@ -9310,7 +9307,7 @@ dev.off() } -bt.pair.strategy.intraday.thebonnotgang.test <- function() +bt.pair.strategy.intraday.thebonnotgang.test <- function(spath = 'c:/Desktop/') { #***************************************************************** # Load historical data @@ -9319,8 +9316,6 @@ bt.pair.strategy.intraday.thebonnotgang.test <- function() # data from http://thebonnotgang.com/tbg/historical-data/ # please save SPY and GLD 1 min data at the given path - spath = 'c:/Desktop/' -spath = 'c:/Documents and Settings/mkapler/Desktop/' data = bt.load.thebonnotgang.data('USO,GLD', spath) bt.prep(data, align='keep.all', fill.gaps = T) diff --git a/pkg/R/data.proxy.r b/pkg/R/data.proxy.r index b206b5e..3de7426 100644 --- a/pkg/R/data.proxy.r +++ b/pkg/R/data.proxy.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### -# Collection of routines to examine and compare proxies and data -# Copyright (C) 2013 Michael Kapler +# Collection of routines to examine and compare data proxies # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/R/data.r b/pkg/R/data.r index ed71075..e482ad4 100644 --- a/pkg/R/data.r +++ b/pkg/R/data.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Collection of routines to work with data -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### #' @export @@ -260,6 +261,13 @@ PricingZeroCouponBond <- function # Convert Historical TBills rates to Total Returns # http://timelyportfolio.blogspot.com/2011/04/historical-sources-of-bond-returns_17.html # http://timelyportfolio.blogspot.ca/2012/11/cashopportunity-lost-or-opportunity.html +# +# [S&P500 and 10yr TReasurey returns since 1928](http://people.stern.nyu.edu/adamodar/pc/datasets/histret.xls) +# http://pages.stern.nyu.edu/~adamodar/New_Home_Page/datafile/histretSP.html +# Another example to compute Return on bond = +# * Yield at start of the year plus +# * 10YR bond price change given the change in Yield +# http://people.stern.nyu.edu/adamodar/pc/datasets/ #' @export ############################################################################### processTBill <- function @@ -751,6 +759,10 @@ getQuote.google.xml <- function(tickers) { ############################################################################### # Download historical intraday prices from Google Finance +# +# [Best Example](http://stackoverflow.com/questions/15609334/how-to-download-intraday-stock-market-data-with-r) +# as.POSIXct(1357828200-300*60, origin = '1970-01-01') +# # http://www.mathworks.com/matlabcentral/fileexchange/32745-get-intraday-stock-price # http://www.mathworks.com/matlabcentral/fileexchange/36115-volume-weighted-average-price-from-intra-daily-data # http://www.codeproject.com/KB/IP/google_finance_downloader.aspx @@ -774,15 +786,36 @@ getSymbol.intraday.google <- function '&p=', period, '&f=', 'd,o,h,l,c,v', sep='') - load.packages('data.table') - out = fread(url, stringsAsFactors=F) + txt = get.url(url) + #write(txt, file='1.txt') + #txt = read.file('google-spy-getprices.txt') - if(ncol(out) < 5) { + lines = spl(txt,'\n') + if(len(lines) < 7) { cat('Error getting data from', url, '\n') return(NULL) } - setnames(out, spl('Date,Open,High,Low,Close,Volume')) + marker = 'COLUMNS=' + map = c(DATE='Date', CLOSE='Close', HIGH='High', LOW='Low', OPEN='Open', VOLUME='Volume') + cols = map[ spl(gsub(marker, '', lines[ grep(marker,lines)[1] ])) ] + + marker = 'TIMEZONE_OFFSET=' + index = grep(marker,lines) + + load.packages('data.table') + out = c() + for(i in 1:len(index)) { + end = iif( i == len(index), -1, index[(i+1)] - index[i] - 1) + timezone.offset = as.double(gsub( marker, '', lines[index[i]] )) + out = rbind(out, getSymbol.intraday.google.parse(txt, interval, index[i], end, timezone.offset, cols) ) + } + out +} + +getSymbol.intraday.google.parse = function(txt, interval, start, end, timezone.offset, cols) { + out = fread(txt, stringsAsFactors = F, skip = start, nrows = end) + setnames(out, cols) # date logic date = out$Date @@ -793,14 +826,47 @@ getSymbol.intraday.google <- function temp = ifna.prev(temp) date = temp + date * interval date[date.index] = temp[date.index] - class(date) = c("POSIXt", "POSIXct") - - date = date - (as.double(format(date[1],'%H')) - 9)*60*60 + #class(date) = c("POSIXt", "POSIXct") + #date = date - (as.double(format(date[1],'%H')) - 9)*60*60 + date = as.POSIXct(date + timezone.offset*interval, origin = '1970-01-01') make.xts(out[,-1,with=F], date) } +getSymbol.intraday.google.test = function() { + # plot intraday spy over last 20 days + data=getSymbol.intraday.google('SPY','NYSEARCA',period = '20d') + col.names = spl('black,blue') + cols = c(col.add.alpha(col.names[1],100), col.add.alpha(col.names[2],250)) + + ret = Cl(data) / mlag(Cl(data)) - 1 + group = as.numeric(format(index(data),'%H%M')) + stat = tapply(ret,group,mean, na.rm=T) + stat[1] = 0 # omit overnight return + + label = names(stat) + label = iif(nchar(label)<4, paste0('0',label), label) + dates = strptime(paste0('20170101 ', label, '01'), format='%Y%m%d %H%M%S') + equity = 100*(cumprod(1+stat)-1) + equity = xts(equity, dates) + plota(equity, type='l', col = cols[1], lwd=5, LeftMargin=3) + + # 2nd axis + date = as.Date(index(ret)) + stat = ret[date == last(date)] + stat[1] = 0 + dates = strptime(paste0('20170101 ', format(index(stat),'%H%M'), '01'), format='%Y%m%d %H%M%S') + equity = 100*(cumprod(1+stat)-1) + equity = xts(coredata(equity), dates) + + plota2Y(equity, las=1, col=cols[2], col.axis = cols[2]) + plota.lines(equity, type='l', col = cols[2]) + plota.legend('20D Avg(rhs),Today(lhs)', col.names) + + +#!!! add chart for trutrn intrday vs overnight!!! +} ############################################################################### # getSymbols interface to Yahoo today's delayed qoutes @@ -2449,17 +2515,35 @@ edgar.info <- function(ticker) zacks.info <- function(ticker = 'IBM') { url = paste0('http://www.zacks.com/stock/research/', ticker, '/earnings-announcements') - txt = join(readLines(url)) - - out = list() - require(jsonlite) - - for(i in spl('earnings,webcasts,revisions,splits,dividends,guidance')) { - data = extract.token(txt,paste0('') - data = fromJSON(paste('{"data"', data)) - out[[i]] = data$data - } - out + txt = get.url(url) + #write(txt, file='1.txt') + + require(jsonlite) + + # extract data + temp = extract.token(txt,'document.obj_data = ','};') + temp = fromJSON(paste(temp, '}')) + names(temp) = gsub('_table','',gsub('earnings_announcements_','',names(temp))) + data = temp + + # extract headers + url = 'http://staticx.zacks.com/js/zacks/inline/company_event_detail.js' + info = get.url(url) + temp = extract.token(info,'var aryColTableChecked = ','};') + temp = gsub('title','"title"',temp) + temp = gsub('class','"class"',temp) + temp = fromJSON(paste(temp, '}')) + names(temp) = gsub('_table','',gsub('earnings_announcements_','',names(temp))) + header = temp + + # set headers + for(i in names(data)) + if( len(data[[ i ]]) > 0) { + temp = rep('', ncol(data[[ i ]])) + temp[1:nrow(header[[ i ]])] = header[[ i ]][,1] + colnames(data[[ i ]]) = temp + } + data } diff --git a/pkg/R/factor.model.r b/pkg/R/factor.model.r index c8db351..fc94634 100644 --- a/pkg/R/factor.model.r +++ b/pkg/R/factor.model.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Collection of Utilities to Biuld and Analyze Multiple Factor Model -# Copyright (C) 2012 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/R/factor.model.test.r b/pkg/R/factor.model.test.r index 68037f6..545ab69 100644 --- a/pkg/R/factor.model.test.r +++ b/pkg/R/factor.model.test.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Test cases and examples for Multiple Factor Model functions -# Copyright (C) 2012 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/R/fundamental.data.r b/pkg/R/fundamental.data.r index 89ded33..7f1b218 100644 --- a/pkg/R/fundamental.data.r +++ b/pkg/R/fundamental.data.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### -# Collection of Utilities to work with Fundamental Data from advfn -# Copyright (C) 2012 Michael Kapler +# Collection of Utilities to work with Fundamental Data from advfn website # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/R/fundamental.test.r b/pkg/R/fundamental.test.r index 816b952..79d8157 100644 --- a/pkg/R/fundamental.test.r +++ b/pkg/R/fundamental.test.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Fundamental Analysis Tests -# Copyright (C) 2012 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/R/min.corr.paper.r b/pkg/R/min.corr.paper.r index ff0f1ef..e376be7 100644 --- a/pkg/R/min.corr.paper.r +++ b/pkg/R/min.corr.paper.r @@ -1,25 +1,25 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Examples for the Minimum Correlation Algorithm paper -# Copyright (C) 2012 Michael Kapler -# # Forecast-Free Algorithms: A New Benchmark For Tactical Strategies # http://cssanalytics.wordpress.com/2011/08/09/forecast-free-algorithms-a-new-benchmark-for-tactical-strategies/ # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/R/optimization.r b/pkg/R/optimization.r index 0221558..75a31ca 100644 --- a/pkg/R/optimization.r +++ b/pkg/R/optimization.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Optimization Functions -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### @@ -694,10 +695,14 @@ lm.constraint <- function fit = lm.fit(x, y) return( ols.summary(x, y, fit$coefficients) ) } else { - temp = cov(cbind(y, x)) - Dmat = temp[-1,-1] - dvec = temp[-1,1] - + #[Are “constrained linear least squares” and “quadratic programming” the same thing?](https://math.stackexchange.com/questions/869204/are-constrained-linear-least-squares-and-quadratic-programming-the-same-thin) + #[constrained OLS in R](https://stats.stackexchange.com/questions/57172/prediction-constrained-ols-in-r) + #temp = cov(cbind(y, x)) + #Dmat = temp[-1,-1] + #dvec = temp[-1,1] + Dmat = t(x) %*% x + dvec = t(y) %*% x + sol = solve.QP.bounds(Dmat = Dmat, dvec = dvec , Amat=constraints$A, bvec=constraints$b, constraints$meq, lb = constraints$lb, ub = constraints$ub) @@ -706,6 +711,27 @@ lm.constraint <- function } +############################################################################### +# Run (Total least squares regression)[https://en.wikipedia.org/wiki/Total_least_squares] +# (How to perform orthogonal regression ,total least squares, via PCA?)[http://stats.stackexchange.com/questions/13152/how-to-perform-orthogonal-regression-total-least-squares-via-pca] +#' @export +############################################################################### +tls <- function +( + x, + y +) +{ + n = ncol(x) + C = cbind(x, y) + + V = svd(C)$v + VXY = V[1:n, (n+1):ncol(V)] + VYY = V[(n+1):nrow(V), (n+1):ncol(V)] + -VXY/VYY +} + + ############################################################################### # Run linear least squares regression #' @export diff --git a/pkg/R/plot.table.r b/pkg/R/plot.table.r index 32af89d..7f486fe 100644 --- a/pkg/R/plot.table.r +++ b/pkg/R/plot.table.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Plot table ( vector or matrix ) -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/R/plota.r b/pkg/R/plota.r index 7fa00f8..e1e9ef2 100644 --- a/pkg/R/plota.r +++ b/pkg/R/plota.r @@ -1,23 +1,24 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Create Technical Analysis Plots # plota = plot + ta -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### @@ -1116,7 +1117,6 @@ plota.matplot <- function } - ############################################################################### #' Add Copyright message to the plot #' @@ -1164,3 +1164,58 @@ plota.recession <- function plota.x.highlight(highlight, highlight != 0, col) } + +############################################################################### +#' Create Ascii(txt) plot +#' +#' Based on the folloiwng idea: [ASCII Plotting Functions for R](http://stackoverflow.com/questions/14736556/ascii-plotting-functions-for-r) +#' +#' Plot multiple series by stacking data and using corresponding lables (pch) +#' also works with NA's so we can potentially plot many partial series +#' +#' @param y matrix or vector of y values +#' @param x optional vector of x values, \strong{defaults to 1:nr, where nr is the number of rows in y} +#' @param pch optional vector of markers for time series, \strong{defaults to 1:nc, where nc is the number of columns in y} +#' @param width optional location of the plot legend, \strong{defaults to 80% of the screen width} +#' +#' @return nothing +#' +#' @examples +#' \dontrun{ +#' txtplot(1:10, c(1,2,3,4,5,NA,7,8,NA,10)) +#' +#' x = 1:10;y = 1:10;y1 = 30:21 +#' txtplot(rbind(x,x), rbind(y,y1), pch=c('+','*')) +#' +#' plot.txt(cbind(y,y1),x) +#' plot.txt(cbind(y,y1),x,pch=c('+','*')) +#' } +#' @export +############################################################################### +plot.txt = function +( + y, + x=NULL, + pch=1:ncol(y), + width = round(options()$width * 0.8) +) +{ + # load.packages('txtplot') + require(txtplot) + if(is.null(x)) x = 1:nrow(y) + #txtplot(rep.row(x, ncol(y)), t(y), pch=letters[1:ncol(y)]) + txtplot(rep.row(x, ncol(y)), t(y), pch=pch) + txtplot:::drawLegend(colnames(y), width) +} + +plot.txt.test = function() { + require(txtplot) + + data = cbind(q1=models$high52w.1$equity['2009::2010'],q10=models$high52w.10$equity['2009::2010']) + plot.txt(data) + + data = matrix(rnorm(50), nc=2) + plot.txt(data, pch=c('.','*')) + + txtplot(models$high52w.10$equity['2009::2010']) +} diff --git a/pkg/R/random.r b/pkg/R/random.r index ac99efe..a34e34e 100644 --- a/pkg/R/random.r +++ b/pkg/R/random.r @@ -1,23 +1,24 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Collection of Random Generators # Copyright (C) 2005-2006 Roger Stafford - the author of the original Matlab version -# Copyright (C) 2012 Michael Kapler - ported Roger Stafford's code to R # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/R/rfinance2012.r b/pkg/R/rfinance2012.r index 4c09bc8..7dfe7fd 100644 --- a/pkg/R/rfinance2012.r +++ b/pkg/R/rfinance2012.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### -# Examples for the R/Finance Presentation -# Copyright (C) 2012 Michael Kapler +# Examples for the 2012 R/Finance Presentation # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/R/shiny.r b/pkg/R/shiny.r index 3aba8dd..f6e12f2 100644 --- a/pkg/R/shiny.r +++ b/pkg/R/shiny.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Repository of Helper Functions for Shiny Framework -# Copyright (C) 2013 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/R/strategy.r b/pkg/R/strategy.r index 01d9dd8..a22d034 100644 --- a/pkg/R/strategy.r +++ b/pkg/R/strategy.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Repository of Benchmark Strategies -# Copyright (C) 2012 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### @@ -787,7 +788,23 @@ risk.parity.portfolio <- function( set.risky.asset(sol$solution, risk.index) } - + # ============================================================ + # + # [Risk Parity Strategies For Equity Portfolio Management Can an asset-class strategy translate to equities? By Frank Siu](http://www.axioma.com/media/uploads/research-papers/risk_parity_strategies_for_equity_portfolio_managenemt_frank_siu1.pdf) + # page 8 + # + # Maximum diversification and risk parity are both driven by + # the concept of diversification. Risk parity spreads out risk + # across its various sources; maximum diversification aims to + # reduce the share of portfolio risk coming from correlations. + # + # Mathematically, Equation 9, maximum diversification, is also analogous + # to maximizing the Sharpe ratio, + # where each stocks expected return is equal to its volatility. + # + # Please see Max Sharpe ratio for implementation including linear constraints + # + # ============================================================ # Toward Maximum Diversification by Y. Choueifaty, Y. Coignard # The Journal of Portfolio Management, Fall 2008, Vol. 35, No. 1: pp. 40-51 #' @export @@ -1095,6 +1112,19 @@ random.hist.weight = function( # # Interesting info # http://r.789695.n4.nabble.com/The-best-solver-for-non-smooth-functions-td4636934.html +# +# +# [Maximize the Sharpe Ratio in Axioma Portfolio](http://www.updatefrom.com/axioma/0703/focus.html) +# +# min w*E*w +# s.t. +# mu * w = mean(mu) +# e * w - 1000000 * t = 0 +# s * w - up * t < 0 +# s * w - dn * t > 0 +# w, t >= 0 +# +# final solution is w / t ############################################################################### # only works for constraints that are homogeneous of degree 0 # i.e. if we multiply solution weight by a number, the constraint is unchanged @@ -1144,6 +1174,55 @@ random.hist.weight = function( } +############################################################################### +# [Maximize the Sharpe Ratio in Axioma Portfolio](http://www.updatefrom.com/axioma/0703/focus.html) +# +# min w*E*w +# s.t. +# mu * w = mean(mu) +# e * w - 1000000 * t = 0 +# s * w - up * t < 0 +# s * w - dn * t > 0 +# w, t >= 0 +# +# final solution is w / t +############################################################################### + #' @export + max.sharpe.portfolio.axioma <- function + ( + ia, # input assumptions + constraints, + rf = 0, + excess.return = ia$expected.return - rf + ) + { + # * add non-negative variable t + n = len(constraints$lb) + n1 = n+1 + constraints = add.variables(1, constraints, 0) + constraints = add.constraints(c(rep(0,n),1), type='>=', b=0, constraints) + + # * adjust input assumptions + ia1 = ia + ia1$n = n1 + ia1$cov = matrix(0, n1, n1) + ia1$cov[1:n, 1:n] = ia$cov + ia1$risk = c(ia$risk,1) + + # * reformulate all constraints: multiply right side by t and moving it to the left + constraints$A[n1,] = -constraints$b + constraints$b[] = 0 + + # * add return target constraint: mu * w = mean(mu) + constraints = add.constraints(c(excess.return,0), mean(excess.return) , type = '=', constraints) + + # * optimal solution is w/t + weight = min.var.portfolio(ia1,constraints) + + weight[1:n]/weight[n1] + } + + max.sharpe.portfolio.test <- function() { #***************************************************************** @@ -1181,10 +1260,32 @@ png(filename = 'plot1.png', width = 500, height = 500, units = 'px', pointsize = points(100 * portfolio.risk(weight,ia), 100 * portfolio.return(weight,ia), pch=15, col='orange') portfolio.return(weight,ia) / portfolio.risk(weight,ia) + w1 = max.sharpe.portfolio.axioma(ia,constraints) + round(weight - w1,2) + weight = w1 + points(100 * portfolio.risk(weight,ia), 100 * portfolio.return(weight,ia), pch=15, col='cyan') + portfolio.return(weight,ia) / portfolio.risk(weight,ia) + + # Maximum diversification portfolio is a max Sharpe portfolio, + # but we substitute *Expected Return* with *Risk* in the objective function + weight = max.div.portfolio(ia, constraints) + w1 = max.sharpe.portfolio.axioma(ia,constraints, excess.return=ia$risk) + round(weight - w1,2) + weight = w1 + points(100 * portfolio.risk(weight,ia), 100 * portfolio.return(weight,ia), pch=15, col='blue') + portfolio.return(weight,ia) / portfolio.risk(weight,ia) + + + + + plota.legend('Minimum Variance,Maximum Sharpe','red,orange', x='topright') dev.off() + + + #***************************************************************** # Examples of Maximum Sharpe or Tangency portfolios construction #****************************************************************** @@ -1932,7 +2033,6 @@ min.var2 <- function(power.function = 1) # # Shrinks towards constant correlation matrix, Ledoit and Wolf (2004) # - # x = as.matrix(read.csv('c:/Michael_Kapler/Soft/R/ira/test.csv', header =F)) # x=matrix(rnorm(3*200),200,3) # covCor(x) # @@ -2172,7 +2272,6 @@ static.group <- function(group) # Idea by David Varadi # http://cssanalytics.wordpress.com/2013/11/26/fast-threshold-clustering-algorithm-ftca/ # Original code by Pierre Chretien -# Small updates by Michael Kapler #' @export cluster.group.FTCA <- function ( diff --git a/pkg/R/ta.r b/pkg/R/ta.r index cc978ca..13665b6 100644 --- a/pkg/R/ta.r +++ b/pkg/R/ta.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Technical Analysis Functions -# Copyright (C) 2011 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/R/utils.r b/pkg/R/utils.r index 828792d..e8c9c20 100644 --- a/pkg/R/utils.r +++ b/pkg/R/utils.r @@ -1,22 +1,23 @@ ############################################################################### -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# This software is provided 'as-is', without any express or implied +# warranty. In no event will the authors be held liable for any damages +# arising from the use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not +# claim that you wrote the original software. If you use this software +# in a product, an acknowledgment in the product documentation would be +# appreciated but is not required. +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# 3. This notice may not be removed or altered from any source distribution. ############################################################################### # Collection of General Utilities -# Copyright (C) 2012 Michael Kapler # -# For more information please visit my blog at www.SystematicInvestor.wordpress.com -# or drop me a line at TheSystematicInvestor at gmail +# For more information please email at TheSystematicInvestor at gmail ############################################################################### diff --git a/pkg/man/SIT.Rd b/pkg/man/SIT.Rd index 3df223d..5ad5d71 100644 --- a/pkg/man/SIT.Rd +++ b/pkg/man/SIT.Rd @@ -20,7 +20,7 @@ load.packages('quantmod') tickers = spl('SPY,TLT,GLD,SHY') -data <- new.env() +data = new.env() getSymbols(tickers, src = 'yahoo', from = '1980-01-01', env = data, auto.assign = T) # adjust for dividends for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T) diff --git a/pkg/man/bt.apply.min.weight.Rd b/pkg/man/bt.apply.min.weight.Rd index 745280e..86c4e43 100644 --- a/pkg/man/bt.apply.min.weight.Rd +++ b/pkg/man/bt.apply.min.weight.Rd @@ -29,6 +29,6 @@ print(bt.apply.min.weight(weight, 0.1)) } } \author{ - Ivan Popivanov and Michael Kapler + Ivan Popivanov } diff --git a/sit.gz b/sit.gz index 08d1d47..06701f7 100644 Binary files a/sit.gz and b/sit.gz differ diff --git a/sit.lite.gz b/sit.lite.gz index 657063a..ca2448f 100644 Binary files a/sit.lite.gz and b/sit.lite.gz differ