Coder Social home page Coder Social logo

enricoschumann / pmwr Goto Github PK

View Code? Open in Web Editor NEW
60.0 60.0 11.0 1.65 MB

Portfolio Management with R: Backtesting investment and trading strategies, computing profit-and-loss and returns, reporting, and more.

Home Page: http://enricoschumann.net/PMwR

License: Other

R 100.00%
accounting backtesting backtesting-trading-strategies equity-trading financial-data financial-portfolio-management r

pmwr's People

Contributors

enricoschumann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

pmwr's Issues

Cont last issue

Sorry, but I have similar issue again. I have ran the same codeas in previous issue on the larger dataset :

# create signal function
mom = function(P, k) {
  # P = coredata(prices["2004-02-26 20:00:00 UTC/2004-03-26 20:00:00 UTC"])
  o <- order(P[nrow(P), ]/ P[1, ], decreasing = TRUE)
  w <- numeric(ncol(P))
  w[o[1:k]] <- 1/k
  w
}
mom.latest <- mom(head(coredata(prices), 250), 10)
signal <- function(fun, ...) {
  print("Debug 1")
  print(Timestamp())
  # if (as.character(Timestamp()) == '"1998-05-28 19:00:00 UTC"') print(Close(n = 250))
  P <- Close(n = 250)
  print("Debug 2")
  w = fun(P, ...)
  print(all(is.finite(w)))
  print(w)
  # if (any(!is.finite(w))) {
  #   print("problem!")
  # }
}
bt.mom <- btest(prices = list(coredata(P)),
                signal = signal,
                do.signal = "lastofmonth",
                convert.weights = TRUE,
                initial.cash = 1000,
                k = 3,
                fun = mom,
                b = 250,
                timestamp = timestamp,
                instrument = instrument)

and I get error :

Error in if (max(abs(dXs)) < tol) rebalance <- FALSE else if (!is.na(tol.p) &&  : 
  missing value where TRUE/FALSE neede

I tested if all values are finite in w as you see above and they are.
If I uncomment this part:

  # if (any(!is.finite(w))) {
  #   print("problem!")
  # }

it doesn't return an error. Weird behavior.

I have added the dataset on the cloud. You can download it but it is larger file this time:
https://snpmarketdata.blob.core.windows.net/test/prices_large.csv

Error in if (max(abs(dXs)) < tol) rebalance <- FALSE else if (!is.na(tol.p) &&

I contuined with my momentum backtest and get some error again which I can' debug.

Here is exampled code:

library(data.table)
library(xts)
library(PMwR)
prices = fread("https://snpmarketdata.blob.core.windows.net/test/prices.csv")
prices = as.xts.data.table(prices)
timestamp <- index(prices)
instrument <- colnames(prices)
mom = function(P, k) {
  print("TEST")
  o <- order(P[nrow(P), ]/P[1, ], decreasing = TRUE)
  w <- numeric(ncol(P))
  w[o[1:k]] <- 1/k
  w
}
mom.latest <- mom(head(coredata(prices), 250), 10)
signal <- function(fun, ...) {
  print("HERE")
  P <- Close(n = 250)
  fun(P, ...)
}
bt.mom <- btest(prices = list(coredata(prices)),
                signal = signal,
                do.signal = "lastofmonth",
                convert.weights = TRUE,
                initial.cash = 1000,
                k = 2,
                fun = mom,
                b = 250,
                timestamp = timestamp,
                instrument = instrument)

Here is the output

[1] "2000-02-28 20:00:00 UTC"
[1] "2000-03-30 20:00:00 UTC"
[1] "2000-04-27 19:00:00 UTC"
[1] "2000-05-30 19:00:00 UTC"
[1] "2000-06-29 19:00:00 UTC"
[1] "2000-07-28 19:00:00 UTC"
[1] "2000-08-30 19:00:00 UTC"
[1] "2000-09-28 19:00:00 UTC"
[1] "2000-10-30 20:00:00 UTC"
[1] "2000-11-29 20:00:00 UTC"
[1] "2000-12-28 20:00:00 UTC"
[1] "2001-01-30 20:00:00 UTC"
Error in if (max(abs(dXs)) < tol) rebalance <- FALSE else if (!is.na(tol.p) &&  : 
  missing value where TRUE/FALSE needed

Wrong result with unit_prices function

I have following NAv and cf object:

library(data.table)
NAV = structure(list(timestamp = structure(c(19312, 19313, 19314, 19317, 
                                             19318, 19319, 19320, 19321, 19324, 19325, 19326, 19327, 19328, 
                                             19331, 19332, 19333, 19334, 19335, 19338, 19339, 19340, 19341, 
                                             19342, 19345, 19346, 19347, 19348, 19349, 19352, 19353, 19354, 
                                             19355, 19356, 19359, 19360, 19361, 19362, 19363, 19366, 19367, 
                                             19368, 19369, 19370, 19373, 19374, 19375, 19376, 19377, 19380, 
                                             19381, 19382, 19383, 19384, 19387, 19388, 19389, 19390, 19391, 
                                             19394, 19395, 19396, 19397, 19398, 19401, 19402, 19403, 19404, 
                                             19405, 19408, 19409, 19410, 19411, 19412, 19415, 19416, 19417, 
                                             19418, 19419, 19422, 19423, 19424, 19425, 19426, 19429, 19430, 
                                             19431, 19432, 19433, 19436, 19437, 19438, 19439, 19440, 19443, 
                                             19444, 19445, 19446, 19447, 19450, 19451, 19452, 19453, 19454, 
                                             19457, 19458, 19459, 19460, 19461, 19464, 19465, 19466, 19467, 
                                             19468, 19471, 19472, 19473, 19474, 19475, 19478, 19479), class = "Date"), 
                     NAV = c(970, 2500, 2500, 2494.56, 2526.42, 2541.54, 2541.54, 
                             2540.28, 2500.68, 2496.25, 2570.6, 2568.44, 2565.26, 2520.26, 
                             2485.35, 2480.98, 2499.11, 2481, 2513.97, 2531.74, 2515.96, 
                             2456.99, 2429.14, 2408.53, 2411.28, 2426.09, 2421.59, 2434.36, 
                             2434.36, 2425.3, 2396.49, 2436.8, 2430.37, 2430.37, 2419.23, 
                             2436.5, 2407.55, 2459.38, 2470.14, 2469.76, 2469.38, 2478.34, 
                             2487.58, 2487.58, 2483.2, 2445.13, 2427.65, 2470.71, 2498.07, 
                             2495.1, 2495.61, 2521.62, 1090107.32, 1086971.73, 1086971.34, 
                             1098903.22, 1111018.21, 1102344.67, 1096241.55, 1096241.55, 
                             1096069.72, 1096069.72, 1101335.625, 1107990.41, 1107990.41, 
                             1116275.52, 1123598.4, 1123598.4, 1123598.4, 1123598.4, 1123598.4, 
                             1124678.98, 1105307.14, 1109141.14, 1104793.89, 1100303.32, 
                             1108848.65, 1130268.42, 1130532.8, 1112747.75, 1114419.18, 
                             1093341.95, 1077124.06, 1074777.33, 1074607.62, 1074435.35, 
                             1074435.35, 1074435.35, 1074435.35, 1074435.35, 1056056.925, 
                             1059896.37, 1059896.37, 1060085.77, 1057218.13, 1060825.9678, 
                             1067748.42, 1067748.42, 1068830.855, 1062896.015, 1063951.985, 
                             1067918.395, 1067918.395, 1068319.775, 1068432.975, 1063912.785, 
                             1077848.515, 1075044.865, 1078374.615, 1078902.625, 1078545.505, 
                             1072487.845, 1073146.795, 1073743.695, 1056519.745, 1051870.815, 
                             1072200.585, 1073637.47, 1073110.76, 1072934.49)), row.names = c(NA, 
                                                                                              -120L), class = c("data.table", "data.frame"))
cf <- structure(list(timestamp = structure(c(19312L, 19313L, 19384L), class = c("IDate", "Date")), 
                     `cashflow ` = c(970, 1530, 1089500)), row.names = c(NA, -3L), class = c("data.table", "data.frame"))

I tried to use unit_prices function on this 2 objects (following example):

  nav_units <- unit_prices(as.data.frame(NAV ),
                         cashflow = as.data.frame(cf),
                         initial.price = 100)
plot(nav_units$price)

buy as you can see on the plot, results are worng, there is a big break that shouldn't exists.

Trying to do momentum strtategy using btest but got error replacement has length zero

Hi,

I am trying to use code from your book Numerical methods from chapter 15 when you apply btest function on momentum strategy.
I have prepare the data to be the same as in the exmaple, but I get an error:

Here is reprex:

library(data.table)
library(xts)
library(PMwR)
prices = fread("https://snpmarketdata.blob.core.windows.net/test/prices.csv")
prices = as.xts.data.table(prices)
timestamp <- index(prices)
instrument <- colnames(prices)
mom = function(P, k) {
  print("TEST")
  o <- order(P[nrow(P), ]/P[1, ], decreasing = TRUE)
  w <- numeric(ncol(P))
  w[o[1:k]] <- 1/k
  w
}
mom.latest <- mom(head(coredata(prices), 250), 10)
signal <- function(fun, ...) {
  print("HERE")
  P <- Close(n = 250)
  fun(P, ...)
}
bt.mom <- btest(prices = list(prices),
                signal = signal,
                do.signal = "lastofmonth",
                convert.weights = TRUE,
                initial.cash = 1000,
                k = 2,
                fun = mom,
                b = 250,
                timestamp = timestamp,
                instrument = instrument)

I get an error

Error in v[t] <- X[t, nzero] %*% mC[t, nzero] + cash[t] : 

replacement has length zero

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.