[R-SIG-Finance] pl() function from PMwR package split an error: 'x' and 'y' lengths differ
Enrico Schumann
e@ @end|ng |rom enr|co@chum@nn@net
Mon Apr 21 17:09:00 CEST 2025
On Mon, 21 Apr 2025, Arnaud Gaboury writes:
> I am following this page[1] to compute pl of my portoflio.
> Here are the various ingredients to compute correctly my pl function
> following this method: PL <- pl(J, vprice = P, along.timestamp =
> timestamp.P)
>
> Here is J, my journal:
> J <- structure(list(instrument = c("FTMUSDC", "SUIUSDC", "ETHUSDC",
> "ENAUSDC", "BTCUSDC", "AAVEUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC",
> "ETHUSDC", "AAVEUSDC", "BTCUSDC", "ENAUSDC", "ETHUSDC", "FTMUSDC",
> "SUIUSDC", "ETHUSDC", "ENAUSDC", "AAVEUSDC", "FTMUSDC", "SUIUSDC",
> "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC", "AAVEUSDC",
> "BTCUSDC", "BTCUSDC", "BTCUSDC", "AAVEUSDC", "AAVEUSDC", "AAVEUSDC",
> "AAVEUSDC", "AAVEUSDC", "AAVEUSDC", "BTCUSDC", "AAVEUSDC", "AAVEUSDC",
> "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC",
> "SUIUSDC", "BTCUSDC", "AAVEUSDC", "SUIUSDC", "BTCUSDC", "BTCUSDC",
> "BTCUSDC", "ETHUSDC", "ETHUSDC", "ETHUSDC", "BTCUSDC", "SUIUSDC",
> "SUIUSDC", "BTCUSDC", "SUIUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC",
> "SUIUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC", "SUSDC",
> "BTCUSDC", "SUIUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC",
> "BTCUSDC", "SUIUSDC", "SUIUSDC", "SUIUSDC", "SUIUSDC", "SUSDC",
> "BTCUSDC", "SUSDC", "SUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC",
> "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC",
> "SUSDC", "SUSDC", "OMUSDC", "SUIUSDC", "OMUSDC", "SUIUSDC", "BTCUSDC",
> "OMUSDC", "OMUSDC", "OMUSDC", "BTCUSDC", "ETHUSDC", "SUIUSDC",
> "OMUSDC", "OMUSDC", "OMUSDC", "BTCUSDC", "SUIUSDC", "BTCUSDC",
> "SUSDC", "ENAUSDC", "ENAUSDC", "ETHUSDC", "BTCUSDC", "ETHUSDC",
> "BTCUSDC", "SUSDC", "SUIUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC",
> "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC",
> "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC",
> "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC", "SUSDC",
> "BTCUSDC", "SUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC",
> "BTCUSDC", "BTCUSDC", "BTCUSDC", "BTCUSDC"), timestamp =
> structure(c(1734372120,
> 1734372240, 1734372360, 1734372480, 1734372600, 1734372720,
> 1734426090.135,
> 1734553973.032, 1735810398.277, 1735810489.902, 1735812883.798,
> 1735812912.427, 1735812922.868, 1735812934.674, 1735812945.707,
> 1735812955.168, 1736177563.787, 1736177649.381, 1736261991.422,
> 1736262093.964, 1736779190.992, 1736854468.083, 1736865119.74,
> 1736865312.915, 1736865453.951, 1736974766.64, 1736975085.933,
> 1736975211.156, 1736975380.095, 1737041368.809, 1737295523.705,
> 1737295650.336, 1737315384.148, 1737333105.442, 1737335082.949,
> 1737336735.697, 1737625581.878, 1737625599.839, 1737967031.492,
> 1737969957.279, 1737982296.161, 1737983492.087, 1737989818.863,
> 1737990022.153, 1738058503.483, 1738059550.671, 1738076112.736,
> 1738142709.422, 1738157053.345, 1738157206.539, 1738161507.243,
> 1738163335.254, 1738169351.788, 1738169494.283, 1738169531.258,
> 1738171799.704, 1738176968.536, 1738216843.704, 1738769416.056,
> 1738854703.218, 1738856868.753, 1738936692.68, 1738937334.883,
> 1739296718.084, 1739379482.659, 1739446986.289, 1739979238.947,
> 1739996323.332, 1740115921.015, 1740491373.055, 1740596755.646,
> 1740667962.173, 1740768770.074, 1740768953.683, 1740786989.548,
> 1741012632.874, 1741100761.788, 1741109444.315, 1741185527.418,
> 1741208042.454, 1741283447.971, 1741283872.186, 1741295325.927,
> 1741295477.314, 1741295693.088, 1741342541.164, 1741629884.419,
> 1741635304.779, 1741685200.989, 1741686554.221, 1741698998.622,
> 1741707118.96, 1741802675.392, 1742141908.962, 1742257808.523,
> 1742289195.923, 1742297055.385, 1742302133.885, 1742302466.715,
> 1742325681.41, 1742325806.343, 1742325896.024, 1742325942.537,
> 1742378556.512, 1742387323.985, 1742391238.519, 1742398133.361,
> 1742398190.194, 1742398284.155, 1742398685.416, 1742403613.643,
> 1742404126.746, 1742871155.426, 1742891897.907, 1742892224.948,
> 1742998171.435, 1743000224.05, 1743001791.162, 1743011323.324,
> 1743017318.549, 1743406396.586, 1743408729.623, 1743425658.334,
> 1743428419.647, 1743429808.131, 1743430795.489, 1743431687.034,
> 1743439548.713, 1743440894.393, 1743445384.52, 1743495501.973,
> 1743504926.243, 1743519851.83, 1743520420.807, 1743530499.577,
> 1743531476.483, 1743546009.677, 1743584674.851, 1743585101.461,
> 1743601492.33, 1743601850.895, 1743672899.162, 1743679321.994,
> 1743679387.991, 1743780565.767, 1743785970.297, 1743999757.64,
> 1744034059.473, 1744192196.925, 1744216689.942, 1744308914.666,
> 1744353324.949), tzone = "UTC", class = c("POSIXct", "POSIXt"
> )), amount = c(284270, 80024, 91.8981, 207836, 9.3372107, 1152.058,
> 0.04811, 0.12324, -0.06504, -32.0833, 94.408, 0.96557, 17708.79,
> 4.882, 22584, 4641.4, -64.6968, 194251.85, -1246.466, -306854,
> -61748.4, 2.42992, 2.42014, 4.78121, 0.04825, 0.29346, 835.68,
> -0.001, -0.01505, -0.125, -621.323, -214.357, 835, 15.472, 866.666,
> -834.998, 1.20446, 54.943, -936.69, -1.76856, -1.73956, -1.71811,
> -0.72513, -0.98166, 4.72757, 67649.3, 4.73485, -0.393, -67622,
> -1.7, -1.7, 8.98573, 36.1158, -36.1158, -8.8583, -0.70622, -27.3,
> 73781.2, 0.84257, -73781.2, -3.50343, 6.34343, -0.45516, 84682,
> 0.26061, 0.254, -0.36895, 0.75773, 323689.3, -2.69675, 22917.9,
> 0.58439, 2.06429, -2.06429, 2.07134, -6.12345, -107599.9, 102163,
> -102163, 92527.9, -323689.3, -3.09273, 273454, -273454, 3.09273,
> 1.54911, 2.54393, 2, 1.52842, 1.48698, 1.36813, -0.01848, 0.42519,
> 528333.3, -528333.3, 37580, -43102.8, -37580, -49425.1, -3, 354,
> 502, 34532, -0.01435, 124.7401, 106269.9, -2178, -586, -32624,
> 2.82199, 13326.3, 1.40107, 447761.1, 142861.76, 490049.63, -0.3663,
> -0.07286, -140.3605, 0.4878, 63813.8, -119596.2, 5.78271, -3.37969,
> -3.41518, 6.7704, -6.75106, 6.71609, -6.72629, 6.71421, -6.7396,
> 6.6851, -6.74, 6.717, -0.17957, -6.59629, -3.30205, 10.01426,
> -0.03538, -6.5872, 6.56085, -6.56, -359471.3, -6.56, -152437.4,
> 7, 7.40359, -7.11794, 7.11136, -0.02586, 0.5978, -10.47979, 10.24101
> ), price = c(1.382, 4.7256, 4038, 1.1855, 107012, 386.55, 107358.81,
> 101422.93, 96408.23, 3443.981937, 332.340557, 96606.12, 1.043348,
> 3460.4, 0.856681, 4.411298, 3698.158383, 1.231695, 323.031195,
> 0.732393, 4.368867, 96526.3, 96916.53115, 97133.32, 97228.23,
> 99651.73, 0.003195, 99519.61, 99663.37, 98394.97, 0.00302, 0.00301,
> 0.003131, 0.003003, 0.003005, 0.003131, 101941.107914, 333.943712,
> 293.405892, 98950.008481, 100600, 101335.283692, 102050, 101868.208951,
> 102900, 0.000038, 102741.596027, 294.738321, 3.649213, 101846.9,
> 101845.270978, 102368.611214, 0.03027, 0.03026, 0.03027, 102384.062461,
> 3.757, 0.000038, 98487.23, 3.274561, 97076.26334, 98853.757704,
> 99403.962752, 0.000035, 96870.45, 96050, 96382, 96426.612765,
> 0.000009, 88900, 2.791, 85558.02, 84977.109628, 84683.133951,
> 84394.870663, 92227.890836, 2.306263, 2.428989, 2.447165, 2.701886,
> 0.543194, 88418.629504, 0.5525, 0.5515, 89720.689866, 88856.101525,
> 78618.406955, 79413.144936, 81500, 80700, 80000, 81125.99, 81928.04,
> 0.000006, 0.000006, 0.000085, 0.000028, 0.000083, 0.000027,
> 82025.99514,
> 6.946406, 6.945675, 6.949552, 83618, 0.024, 0.000028, 6.732009,
> 6.733288, 6.725032, 84392.65, 2.401251, 84686.879799, 0.000007,
> 0.000005, 0.000005, 2033.5, 86605.99, 0.023223, 86100, 0.595482,
> 2.247826, 82214.102411, 82936.397434, 82074.529897, 82801.529337,
> 83038.750762, 83471.184268, 83344.568664, 83494.401651, 83179.906336,
> 83857.962478, 84146.764724, 84460.185312, 84642.9, 84987.11819,
> 84795.687321, 85376.758785, 84783.59, 85104.418136, 85446.292013,
> 85867.737502, 0.000006, 83126.156483, 0.000005, 83255.75794,
> 83355.928936, 77600, 77700, 77333.1, 77678.233504, 79200.31448,
> 81046.510329)), class = "journal")
>
> vprice, or P, is a matrix of price for each symbol. This matrix has
> been obtained this way:
>
> token_zoo_list <- sapply(token_usdc$V1, get_token_zoo, simplify =
> FALSE)
> tokens <- pull(token_usdc, V1)
> names(token_zoo_list) <- tokens
> list2env(token_zoo_list, envir = .GlobalEnv)
> P <- pricetable(BTCUSDC, SUIUSDC, ENAUSDC, AAVEUSDC, ETHUSDC, LINKUSDC,
> SUSDC, OMUSDC, FTMUSDC,
> instrument = c('BTCUSDC', 'SUIUSDC', 'ENAUSDC',
> 'AAVEUSDC', 'ETHUSDC',
> 'LINKUSDC', 'SUSDC', 'OMUSDC',
> 'FTMUSDC'))
>
> P <- P[my_sequence, c('AAVEUSDC', 'BTCUSDC', 'ENAUSDC', 'ETHUSDC',
> 'FTMUSDC',
> 'LINKUSDC', 'OMUSDC', 'SUIUSDC', 'SUSDC'),
> missing = "previous"]
>
> str(token_zoo_list)
> List of 9
> $ BTCUSDC :‘zoo’ series from 2024-12-16 to 2025-04-10
> Data: num [1:116] 105849 105961 101128 98300 97409 ...
> Index: Date[1:116], format: "2024-12-16" "2024-12-17" "2024-12-18"
> "2024-12-19" ...
> $ SUIUSDC :‘zoo’ series from 2024-12-16 to 2025-04-10
> Data: num [1:116] 4.65 4.61 4.35 4.17 4.56 ...
> Index: Date[1:116], format: "2024-12-16" "2024-12-17" "2024-12-18"
> "2024-12-19" ...
> $ ENAUSDC :‘zoo’ series from 2024-12-16 to 2025-04-10
> Data: num [1:116] 1.183 1.113 1.1 0.978 1.183 ...
> Index: Date[1:116], format: "2024-12-16" "2024-12-17" "2024-12-18"
> "2024-12-19" ...
> $ AAVEUSDC:‘zoo’ series from 2024-12-16 to 2025-04-10
> Data: num [1:116] 383 362 343 319 328 ...
> Index: Date[1:116], format: "2024-12-16" "2024-12-17" "2024-12-18"
> "2024-12-19" ...
> $ ETHUSDC :‘zoo’ series from 2024-12-16 to 2025-04-10
> Data: num [1:116] 4032 3883 3679 3457 3470 ...
> Index: Date[1:116], format: "2024-12-16" "2024-12-17" "2024-12-18"
> "2024-12-19" ...
> $ LINKUSDC:‘zoo’ series from 2024-12-16 to 2025-04-10
> Data: num [1:116] 29.1 27.9 25.4 23.1 23.4 ...
> Index: Date[1:116], format: "2024-12-16" "2024-12-17" "2024-12-18"
> "2024-12-19" ...
> $ SUSDC :‘zoo’ series from 2024-12-16 to 2025-04-10
> Data: num [1:116] NA NA NA NA NA NA NA NA NA NA ...
> Index: Date[1:116], format: "2024-12-16" "2024-12-17" "2024-12-18"
> "2024-12-19" ...
> $ OMUSDC :‘zoo’ series from 2024-12-16 to 2025-04-10
> Data: num [1:116] 4.02 4.04 3.92 4 3.79 ...
> Index: Date[1:116], format: "2024-12-16" "2024-12-17" "2024-12-18"
> "2024-12-19" ...
> $ FTMUSDC :‘zoo’ series from 2024-12-16 to 2025-04-10
> Data: num [1:116] 1.39 1.33 1.15 1.02 0.99 ...
> Index: Date[1:116], format: "2024-12-16" "2024-12-17" "2024-12-18"
> "2024-12-19" ...
>
> BTCUSDC <- structure(c(105848.87, 105961.05, 101128.08, 98300.3,
> 97408.97,
> 97088.03, 95443.06, 95064.95, 98696.01, 98736, 95712.33, 94301.25,
> 95148, 92948.39, 92626.69, 93310.64, 94596.01, 96809.96, 98269.43,
> 98400.01, 98669.28, 102046.23, 97148, 95266.39, 92448.01, 94754,
> 94550, 93904, 94370, 96666.41, 99735.9, 99817.99, 104447.48,
> 104200.85, 103300, 103460.9, 106014.03, 103850.01, 104004.11,
> 104780, 105073.27, 104312, 101603.43, 101198.14, 103484, 105110.59,
> 102234.61, 100645.3, 97986.41, 101807.99, 97735.92, 96468, 96957.69,
> 96075.99, 96503.47, 95850, 97407.37, 95571.39, 97816, 96431.99,
> 97161.47, 97569.99, 96875.98, 95888.45, 95264, 96538.74, 98168.81,
> 95782.78, 96643.33, 95801.79, 92423.89, 88732, 84314.01, 84624.07,
> 84114.42, 86087.98, 94238, 86587.78, 87474.27, 90445.6, 90320.93,
> 86719.99, 86329.81, 80345.99, 79462.01, 83126.01, 83642.53, 81361.22,
> 84450.63, 84386.01, 82098.91, 84069.99, 82376.81, 86169.02, 84111.93,
> 84155.21, 83946.31, 85366, 87474.32, 87488.01, 86846.85, 87328.61,
> 84433.04, 82367.24, 82215.33, 82546.71, 85222.02, 84846.44, 82698.01,
> 84155.29, 82889.55, 78904.69, 79207.82, 77037.71, 83269.99, 79668.21
> ), index = structure(c(20073, 20074, 20075, 20076, 20077, 20078,
> 20079, 20080, 20081, 20082, 20083, 20084, 20085, 20086, 20087,
> 20088, 20089, 20090, 20091, 20092, 20093, 20094, 20095, 20096,
> 20097, 20098, 20099, 20100, 20101, 20102, 20103, 20104, 20105,
> 20106, 20107, 20108, 20109, 20110, 20111, 20112, 20113, 20114,
> 20115, 20116, 20117, 20118, 20119, 20120, 20121, 20122, 20123,
> 20124, 20125, 20126, 20127, 20128, 20129, 20130, 20131, 20132,
> 20133, 20134, 20135, 20136, 20137, 20138, 20139, 20140, 20141,
> 20142, 20143, 20144, 20145, 20146, 20147, 20148, 20149, 20150,
> 20151, 20152, 20153, 20154, 20155, 20156, 20157, 20158, 20159,
> 20160, 20161, 20162, 20163, 20164, 20165, 20166, 20167, 20168,
> 20169, 20170, 20171, 20172, 20173, 20174, 20175, 20176, 20177,
> 20178, 20179, 20180, 20181, 20182, 20183, 20184, 20185, 20186,
> 20187, 20188), class = "Date"), class = "zoo")
>
> and so on for the other symbols.
>
> my_sequence is a list of date at which I will fecth valuation price of
> each symbol.
> my_sequence <- structure(c(20073, 20074, 20075, 20076, 20077, 20078,
> 20079,
> 20080, 20081, 20082, 20083, 20084, 20085, 20086, 20087, 20088,
> 20089, 20090, 20091, 20092, 20093, 20094, 20095, 20096, 20097,
> 20098, 20099, 20100, 20101, 20102, 20103, 20104, 20105, 20106,
> 20107, 20108, 20109, 20110, 20111, 20112, 20113, 20114, 20115,
> 20116, 20117, 20118, 20119, 20120, 20121, 20122, 20123, 20124,
> 20125, 20126, 20127, 20128, 20129, 20130, 20131, 20132, 20133,
> 20134, 20135, 20136, 20137, 20138, 20139, 20140, 20141, 20142,
> 20143, 20144, 20145, 20146, 20147, 20148, 20149, 20150, 20151,
> 20152, 20153, 20154, 20155, 20156, 20157, 20158, 20159, 20160,
> 20161, 20162, 20163, 20164, 20165, 20166, 20167, 20168, 20169,
> 20170, 20171, 20172, 20173, 20174, 20175, 20176, 20177, 20178,
> 20179, 20180, 20181, 20182, 20183, 20184, 20185, 20186, 20187,
> 20188), class = "Date")
>
> length(P[,1])
> [1] 116
> length(my_sequence)
> [1] 116
>
> Now comes the final command:
> PL <- pl(J, along.timestamp = my_sequence, vprice = P) which returns an
> error:
> Error in xy.coords(x, y, setLab = FALSE) : 'x' and 'y' lengths differ
I did not try to run your code, because there is something
strange here: PMwR::pl does not use 'xy.coords'. (No function
in PMwR does.) So where does the error message come from?
kind regards
Enrico
> Why this error as all elements of the pl() command look all right ?
>
> Thank you for help.
>
>
> [1] https://enricoschumann.net/notes/computing-portfolio-pl.html
>
--
Enrico Schumann
Lucerne, Switzerland
https://enricoschumann.net
More information about the R-SIG-Finance
mailing list