I work with Binancer [1] and PMwR [2] packages to write a trading
journal.
Here is a sample of my data frame:
portfolio <- data.frame(
symbol = c("BTCUSDT", "BTCUSDT", "ETHUBTC", "AAVEBTC", "ENAUSDT"),
time = c("2024-12-17 10:01:30", "2024-12-18 21:32:53", "2025-01-02
10:34:49", "2025-01-02 11:14:43", "2025-01-02 11:15:22")
)
I want to fetch the 'BTCUSDT' price for a specific time if the symbol
of same row end with these 3 letters : 'BTC'. In my sample, it will be
rows 3 and 4.
Here is how I find BTCUSDT price for date 2025-01-02 10:34:49:
get_btc_price <- function(time) {
klines <- binance_klines('BTCUSDT', start_time = '2025-01-02
10:34:49', end_time = '2025-01-02 10:34:49')
}
I was thinking of this code to write my new column BTCUSDT_price:
df <- portfolio %>%
mutate(
BTCUSDT_price = ifelse(str_detect(symbol, "BTC$"), map_dbl(time,
get_btc_price), NA_real_)
)}
But the code returns:
Error in `mutate()`:
? In argument: `BTCUSDT_price = ifelse(...)`.
Caused by error in `map_dbl()`:
? In index: 1.
Caused by error in `[.data.table`:
! Item 1 of j is 12 which is outside the column number range [1,ncol=0]
I have tried other workarounds but all give me errors.
Thank you for help
[1]https://cran.r-project.org/web/packages/binancer
[2]https://cran.r-project.org/web/packages/PMwR
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: This is a digitally signed message part
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20250305/b2d7a372/attachment.sig>
apply a function to a data.frame column with condition
10 messages · Enrico Schumann, Brian G. Peterson, Arnaud Gaboury
On Wed, 05 Mar 2025, Arnaud Gaboury writes:
I work with Binancer [1] and PMwR [2] packages to write a trading
journal.
Here is a sample of my data frame:
portfolio <- data.frame(
symbol = c("BTCUSDT", "BTCUSDT", "ETHUBTC", "AAVEBTC", "ENAUSDT"),
time = c("2024-12-17 10:01:30", "2024-12-18 21:32:53", "2025-01-02
10:34:49", "2025-01-02 11:14:43", "2025-01-02 11:15:22")
)
I want to fetch the 'BTCUSDT' price for a specific time if the symbol
of same row end with these 3 letters : 'BTC'. In my sample, it will be
rows 3 and 4.
Here is how I find BTCUSDT price for date 2025-01-02 10:34:49:
get_btc_price <- function(time) {
klines <- binance_klines('BTCUSDT', start_time = '2025-01-02
10:34:49', end_time = '2025-01-02 10:34:49')
}
I was thinking of this code to write my new column BTCUSDT_price:
df <- portfolio %>%
mutate(
BTCUSDT_price = ifelse(str_detect(symbol, "BTC$"), map_dbl(time,
get_btc_price), NA_real_)
)}
But the code returns:
Error in `mutate()`:
? In argument: `BTCUSDT_price = ifelse(...)`.
Caused by error in `map_dbl()`:
? In index: 1.
Caused by error in `[.data.table`:
! Item 1 of j is 12 which is outside the column number range [1,ncol=0]
I have tried other workarounds but all give me errors.
Thank you for help
[1]https://cran.r-project.org/web/packages/binancer
[2]https://cran.r-project.org/web/packages/PMwR
You want to compute a new column for your data.frame as
a function of two existing columns.
In base R, if you don't want to use a loop (and there
would be nothing wrong with a loop), you could use
'mapply'; and then cbind the column to your data.frame:
new.column <- mapply(
function(symbol, time) {
if (grepl("BTC$", symbol)) {
## your get_price computation, using
## 'symbol' and 'time'
paste("do something with", symbol, time)
} else
NA
},
portfolio$symbol,
portfolio$time
)
## new.column
## BTCUSDT
## NA
## BTCUSDT
## NA
## ETHUBTC
## "do something with ETHUBTC 2025-01-02 10:34:49"
## AAVEBTC
## "do something with AAVEBTC 2025-01-02 11:14:43"
## ENAUSDT
## NA
##
Enrico Schumann Lucerne, Switzerland https://enricoschumann.net
On Thu, 2025-03-06 at 07:29 +0100, Enrico Schumann wrote:
On Wed, 05 Mar 2025, Arnaud Gaboury writes:
I work with Binancer [1] and PMwR [2] packages to write a trading
journal.
Here is a sample of my data frame:
portfolio <- data.frame(
? symbol = c("BTCUSDT", "BTCUSDT", "ETHUBTC", "AAVEBTC",
"ENAUSDT"),
? time = c("2024-12-17 10:01:30", "2024-12-18 21:32:53", "2025-01-
02
10:34:49", "2025-01-02 11:14:43", "2025-01-02 11:15:22")
)
I want to fetch the 'BTCUSDT' price for a specific time if the
symbol
of same row end with these 3 letters : 'BTC'. In my sample, it will
be
rows 3 and 4.
Here is how I find BTCUSDT price for date 2025-01-02 10:34:49:
get_btc_price <- function(time) {
? klines <- binance_klines('BTCUSDT', start_time = '2025-01-02
10:34:49', end_time = '2025-01-02 10:34:49')
}
I was thinking of this code to write my new column BTCUSDT_price:
df <- portfolio %>%
? mutate(
??? BTCUSDT_price = ifelse(str_detect(symbol, "BTC$"),
map_dbl(time,
get_btc_price), NA_real_)
? )}
But the code returns:
Error in `mutate()`:
? In argument: `BTCUSDT_price = ifelse(...)`.
Caused by error in `map_dbl()`:
? In index: 1.
Caused by error in `[.data.table`:
! Item 1 of j is 12 which is outside the column number range
[1,ncol=0]
I have tried other workarounds but all give me errors.
Thank you for help
[1]https://cran.r-project.org/web/packages/binancer
[2]https://cran.r-project.org/web/packages/PMwR
You want to compute a new column for your data.frame as
a function of two existing columns.
In base R, if you don't want to use a loop (and there
would be nothing wrong with a loop), you could use
'mapply'; and then cbind the column to your data.frame:
??? new.column <- mapply(
??????? function(symbol, time) {
??????????? if (grepl("BTC$", symbol)) {
??????????????? ## your get_price computation, using
??????????????? ## 'symbol' and 'time'
??????????????? paste("do something with", symbol, time)
???????????????
??????????? } else
??????????????? NA
??????? },
??????? portfolio$symbol,
??????? portfolio$time
??? )
??? ## new.column
??? ##???????????????????????????????????????? BTCUSDT
??? ##????????????????????????????????????????????? NA
??? ##???????????????????????????????????????? BTCUSDT
??? ##????????????????????????????????????????????? NA
??? ##???????????????????????????????????????? ETHUBTC
??? ## "do something with ETHUBTC 2025-01-02 10:34:49"
??? ##???????????????????????????????????????? AAVEBTC
??? ## "do something with AAVEBTC 2025-01-02 11:14:43"
??? ##???????????????????????????????????????? ENAUSDT
??? ##????????????????????????????????????????????? NA
Thank you for your answer. A very warm thanks for your PMwR package. It helps me managing my portfolio in a professional way. Unfortunately there isn't so much material to manage, follow, compute ratio etc. By any chance, do you have any cool build-in functions to cook monthly data (PL, valuation etc) as we compute every month always the same data ?
??? ##
-------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 488 bytes Desc: This is a digitally signed message part URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20250306/b177c458/attachment.sig>
On Thu, 2025-03-06 at 07:29 +0100, Enrico Schumann wrote:
On Wed, 05 Mar 2025, Arnaud Gaboury writes:
I work with Binancer [1] and PMwR [2] packages to write a trading
journal.
Here is a sample of my data frame:
portfolio <- data.frame(
? symbol = c("BTCUSDT", "BTCUSDT", "ETHUBTC", "AAVEBTC",
"ENAUSDT"),
? time = c("2024-12-17 10:01:30", "2024-12-18 21:32:53", "2025-01-
02
10:34:49", "2025-01-02 11:14:43", "2025-01-02 11:15:22")
)
I want to fetch the 'BTCUSDT' price for a specific time if the
symbol
of same row end with these 3 letters : 'BTC'. In my sample, it will
be
rows 3 and 4.
Here is how I find BTCUSDT price for date 2025-01-02 10:34:49:
get_btc_price <- function(time) {
? klines <- binance_klines('BTCUSDT', start_time = '2025-01-02
10:34:49', end_time = '2025-01-02 10:34:49')
}
I was thinking of this code to write my new column BTCUSDT_price:
df <- portfolio %>%
? mutate(
??? BTCUSDT_price = ifelse(str_detect(symbol, "BTC$"),
map_dbl(time,
get_btc_price), NA_real_)
? )}
But the code returns:
Error in `mutate()`:
? In argument: `BTCUSDT_price = ifelse(...)`.
Caused by error in `map_dbl()`:
? In index: 1.
Caused by error in `[.data.table`:
! Item 1 of j is 12 which is outside the column number range
[1,ncol=0]
I have tried other workarounds but all give me errors.
Thank you for help
[1]https://cran.r-project.org/web/packages/binancer
[2]https://cran.r-project.org/web/packages/PMwR
You want to compute a new column for your data.frame as
a function of two existing columns.
In base R, if you don't want to use a loop (and there
would be nothing wrong with a loop), you could use
'mapply'; and then cbind the column to your data.frame:
??? new.column <- mapply(
??????? function(symbol, time) {
??????????? if (grepl("BTC$", symbol)) {
??????????????? ## your get_price computation, using
??????????????? ## 'symbol' and 'time'
??????????????? paste("do something with", symbol, time)
???????????????
??????????? } else
??????????????? NA
??????? },
??????? portfolio$symbol,
??????? portfolio$time
??? )
I am sorry but I can't see where I shall put my df ? And my function to
get BTC price:
get_btc_price <- function(my_time) {
# my_time_ms <- as.integer(as.numeric(my_time) * 1000)
klines <- binance_klines("BTCUSDT", interval = '1h', start_time =
as.Date(my_time), end_time = as.Date(my_time) + 1)
return(as.numeric(klines$close[[18]]))
}
Thank you for your help
??? ## new.column ??? ##???????????????????????????????????????? BTCUSDT ??? ##????????????????????????????????????????????? NA ??? ##???????????????????????????????????????? BTCUSDT ??? ##????????????????????????????????????????????? NA ??? ##???????????????????????????????????????? ETHUBTC ??? ## "do something with ETHUBTC 2025-01-02 10:34:49" ??? ##???????????????????????????????????????? AAVEBTC ??? ## "do something with AAVEBTC 2025-01-02 11:14:43" ??? ##???????????????????????????????????????? ENAUSDT ??? ##????????????????????????????????????????????? NA ??? ##
-------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 488 bytes Desc: This is a digitally signed message part URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20250306/f65e0a36/attachment.sig>
On Thu, 06 Mar 2025, Arnaud Gaboury writes:
On Thu, 2025-03-06 at 07:29 +0100, Enrico Schumann wrote:
On Wed, 05 Mar 2025, Arnaud Gaboury writes:
I work with Binancer [1] and PMwR [2] packages to write a trading
journal.
Here is a sample of my data frame:
portfolio <- data.frame(
? symbol = c("BTCUSDT", "BTCUSDT", "ETHUBTC", "AAVEBTC",
"ENAUSDT"),
? time = c("2024-12-17 10:01:30", "2024-12-18 21:32:53", "2025-01-
02
10:34:49", "2025-01-02 11:14:43", "2025-01-02 11:15:22")
)
I want to fetch the 'BTCUSDT' price for a specific time if the
symbol
of same row end with these 3 letters : 'BTC'. In my sample, it will
be
rows 3 and 4.
Here is how I find BTCUSDT price for date 2025-01-02 10:34:49:
get_btc_price <- function(time) {
? klines <- binance_klines('BTCUSDT', start_time = '2025-01-02
10:34:49', end_time = '2025-01-02 10:34:49')
}
I was thinking of this code to write my new column BTCUSDT_price:
df <- portfolio %>%
? mutate(
??? BTCUSDT_price = ifelse(str_detect(symbol, "BTC$"),
map_dbl(time,
get_btc_price), NA_real_)
? )}
But the code returns:
Error in `mutate()`:
? In argument: `BTCUSDT_price = ifelse(...)`.
Caused by error in `map_dbl()`:
? In index: 1.
Caused by error in `[.data.table`:
! Item 1 of j is 12 which is outside the column number range
[1,ncol=0]
I have tried other workarounds but all give me errors.
Thank you for help
[1]https://cran.r-project.org/web/packages/binancer
[2]https://cran.r-project.org/web/packages/PMwR
You want to compute a new column for your data.frame as
a function of two existing columns.
In base R, if you don't want to use a loop (and there
would be nothing wrong with a loop), you could use
'mapply'; and then cbind the column to your data.frame:
??? new.column <- mapply(
??????? function(symbol, time) {
??????????? if (grepl("BTC$", symbol)) {
??????????????? ## your get_price computation, using
??????????????? ## 'symbol' and 'time'
??????????????? paste("do something with", symbol, time)
???????????????
??????????? } else
??????????????? NA
??????? },
??????? portfolio$symbol,
??????? portfolio$time
??? )
I am sorry but I can't see where I shall put my df ? And my function to
get BTC price:
get_btc_price <- function(my_time) {
# my_time_ms <- as.integer(as.numeric(my_time) * 1000)
klines <- binance_klines("BTCUSDT", interval = '1h', start_time =
as.Date(my_time), end_time = as.Date(my_time) + 1)
return(as.numeric(klines$close[[18]]))
}
Thank you for your help
Is the ticker you want to fetch data for always the
same? If yes, then something like this could be
used. [I don't know/use binance, so with a 'dummy' of
your function.]
get_btc_price <- function(my_time) {
paste(42, my_time)
}
new.column <- mapply(
function(symbol, mytime) {
if (grepl("BTC$", symbol)) {
get_btc_price(mytime)
} else
NA
},
portfolio$symbol,
portfolio$time
)
## BTCUSDT BTCUSDT
## NA NA
## ETHUBTC AAVEBTC
##"42 2025-01-02 10:34:49" "42 2025-01-02 11:14:43"
## ENAUSDT
## NA
Enrico Schumann Lucerne, Switzerland https://enricoschumann.net
On Thu, 06 Mar 2025, Arnaud Gaboury writes:
On Thu, 2025-03-06 at 07:29 +0100, Enrico Schumann wrote:
On Wed, 05 Mar 2025, Arnaud Gaboury writes:
I work with Binancer [1] and PMwR [2] packages to write a trading
journal.
Here is a sample of my data frame:
portfolio <- data.frame(
? symbol = c("BTCUSDT", "BTCUSDT", "ETHUBTC", "AAVEBTC",
"ENAUSDT"),
? time = c("2024-12-17 10:01:30", "2024-12-18 21:32:53", "2025-01-
02
10:34:49", "2025-01-02 11:14:43", "2025-01-02 11:15:22")
)
I want to fetch the 'BTCUSDT' price for a specific time if the
symbol
of same row end with these 3 letters : 'BTC'. In my sample, it will
be
rows 3 and 4.
Here is how I find BTCUSDT price for date 2025-01-02 10:34:49:
get_btc_price <- function(time) {
? klines <- binance_klines('BTCUSDT', start_time = '2025-01-02
10:34:49', end_time = '2025-01-02 10:34:49')
}
I was thinking of this code to write my new column BTCUSDT_price:
df <- portfolio %>%
? mutate(
??? BTCUSDT_price = ifelse(str_detect(symbol, "BTC$"),
map_dbl(time,
get_btc_price), NA_real_)
? )}
But the code returns:
Error in `mutate()`:
? In argument: `BTCUSDT_price = ifelse(...)`.
Caused by error in `map_dbl()`:
? In index: 1.
Caused by error in `[.data.table`:
! Item 1 of j is 12 which is outside the column number range
[1,ncol=0]
I have tried other workarounds but all give me errors.
Thank you for help
[1]https://cran.r-project.org/web/packages/binancer
[2]https://cran.r-project.org/web/packages/PMwR
You want to compute a new column for your data.frame as
a function of two existing columns.
In base R, if you don't want to use a loop (and there
would be nothing wrong with a loop), you could use
'mapply'; and then cbind the column to your data.frame:
??? new.column <- mapply(
??????? function(symbol, time) {
??????????? if (grepl("BTC$", symbol)) {
??????????????? ## your get_price computation, using
??????????????? ## 'symbol' and 'time'
??????????????? paste("do something with", symbol, time)
???????????????
??????????? } else
??????????????? NA
??????? },
??????? portfolio$symbol,
??????? portfolio$time
??? )
??? ## new.column
??? ##???????????????????????????????????????? BTCUSDT
??? ##????????????????????????????????????????????? NA
??? ##???????????????????????????????????????? BTCUSDT
??? ##????????????????????????????????????????????? NA
??? ##???????????????????????????????????????? ETHUBTC
??? ## "do something with ETHUBTC 2025-01-02 10:34:49"
??? ##???????????????????????????????????????? AAVEBTC
??? ## "do something with AAVEBTC 2025-01-02 11:14:43"
??? ##???????????????????????????????????????? ENAUSDT
??? ##????????????????????????????????????????????? NA
Thank you for your answer. A very warm thanks for your PMwR package. It helps me managing my portfolio in a professional way. Unfortunately there isn't so much material to manage, follow, compute ratio etc. By any chance, do you have any cool build-in functions to cook monthly data (PL, valuation etc) as we compute every month always the same data ?
Thank you. I am afraid there are no built-in, ready-to-use reports in PMwR. That was a deliberate choice: PMwR should only provide simple tools for computing P/L, returns, or for backtesting, say. For analyzing the results of such computations, you have all of R for analysis and summarizing :-) For my reporting needs, I typically prepare templates in plain text, HTML or LaTeX and then fill in stats such as returns etc. See e.g. https://cran.r-project.org/web/packages/PMwR/vignettes/FinTeX.pdf For equity series, i.e. total value of a portfolio, using NAVseries and its summary method might be a start, see e.g. https://enricoschumann.net/R/packages/PMwR/manual/PMwR.html#NAVseries-summaries There are handful of functions for analyzing such series, e. g. `drawdowns` or `streaks`. But as I said, most analysis I do with basic R functions.
Enrico Schumann Lucerne, Switzerland https://enricoschumann.net
On Fri, 2025-03-07 at 08:55 +0100, Enrico Schumann wrote:
On Thu, 06 Mar 2025, Arnaud Gaboury writes:
On Thu, 2025-03-06 at 07:29 +0100, Enrico Schumann wrote:
On Wed, 05 Mar 2025, Arnaud Gaboury writes:
I work with Binancer [1] and PMwR [2] packages to write a
trading
journal.
Here is a sample of my data frame:
portfolio <- data.frame(
? symbol = c("BTCUSDT", "BTCUSDT", "ETHUBTC", "AAVEBTC",
"ENAUSDT"),
? time = c("2024-12-17 10:01:30", "2024-12-18 21:32:53", "2025-
01-
02
10:34:49", "2025-01-02 11:14:43", "2025-01-02 11:15:22")
)
I want to fetch the 'BTCUSDT' price for a specific time if the
symbol
of same row end with these 3 letters : 'BTC'. In my sample, it
will
be
rows 3 and 4.
Here is how I find BTCUSDT price for date 2025-01-02 10:34:49:
get_btc_price <- function(time) {
? klines <- binance_klines('BTCUSDT', start_time = '2025-01-02
10:34:49', end_time = '2025-01-02 10:34:49')
}
I was thinking of this code to write my new column
BTCUSDT_price:
df <- portfolio %>%
? mutate(
??? BTCUSDT_price = ifelse(str_detect(symbol, "BTC$"),
map_dbl(time,
get_btc_price), NA_real_)
? )}
But the code returns:
Error in `mutate()`:
? In argument: `BTCUSDT_price = ifelse(...)`.
Caused by error in `map_dbl()`:
? In index: 1.
Caused by error in `[.data.table`:
! Item 1 of j is 12 which is outside the column number range
[1,ncol=0]
I have tried other workarounds but all give me errors.
Thank you for help
[1]https://cran.r-project.org/web/packages/binancer
[2]https://cran.r-project.org/web/packages/PMwR
You want to compute a new column for your data.frame as
a function of two existing columns.
In base R, if you don't want to use a loop (and there
would be nothing wrong with a loop), you could use
'mapply'; and then cbind the column to your data.frame:
??? new.column <- mapply(
??????? function(symbol, time) {
??????????? if (grepl("BTC$", symbol)) {
??????????????? ## your get_price computation, using
??????????????? ## 'symbol' and 'time'
??????????????? paste("do something with", symbol, time)
???????????????
??????????? } else
??????????????? NA
??????? },
??????? portfolio$symbol,
??????? portfolio$time
??? )
I am sorry but I can't see where I shall put my df ? And my
function to
get BTC price:
get_btc_price <- function(my_time) {
??? # my_time_ms <- as.integer(as.numeric(my_time) * 1000)
??? klines <- binance_klines("BTCUSDT", interval = '1h', start_time
=
as.Date(my_time), end_time = as.Date(my_time) + 1)
??? return(as.numeric(klines$close[[18]]))
}
Thank you for your help
Is the ticker you want to fetch data for always the same?? If yes, then something like this could be used. [I don't know/use binance, so with a 'dummy' of your function.]
Yes, in this cas I only fecth BTCUSD price, thus my get_btc_price().
For generic, I created get_price().
Your function gives me exactly what I wanted.
Question: why do you paste 42 here ?
get_btc_price <- function(my_time) {
paste(42, my_time)
}
??? get_btc_price <- function(my_time) {
??????? paste(42, my_time)
??? }
???
??? new.column <- mapply(
??????? function(symbol, mytime) {
??????????? if (grepl("BTC$", symbol)) {
??????????????? get_btc_price(mytime)
??????????? } else
??????????????? NA
??????? },
??????? portfolio$symbol,
??????? portfolio$time
??? )
??? ##???????????????? BTCUSDT????????????????? BTCUSDT
??? ##????????????????????? NA?????????????????????? NA
??? ##???????????????? ETHUBTC????????????????? AAVEBTC
??? ##"42 2025-01-02 10:34:49" "42 2025-01-02 11:14:43"
??? ##???????????????? ENAUSDT
??? ##????????????????????? NA
-------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 488 bytes Desc: This is a digitally signed message part URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20250307/ff8354f9/attachment.sig>
Arnaud, You may want to look at 'PerformanceAnalytics' for a large number of performance and risk functions for financial time series, including a large number of standard tables and charts. I can also recommend Bernhard Pfaff's excellent book 'Financial Risk Modeling and Portfolio Optimization with R, 2nd ed.' Regards, Brian
Brian G. Peterson
ph: +1.773.459.4973
im: bgpbraverock
On Fri, 2025-03-07 at 11:31 +0100, Enrico Schumann wrote:
> On Thu, 06 Mar 2025, Arnaud Gaboury writes:
>
> > On Thu, 2025-03-06 at 07:29 +0100, Enrico Schumann wrote:
> > > On Wed, 05 Mar 2025, Arnaud Gaboury writes:
> > >
> > > > I work with Binancer [1] and PMwR [2] packages to write a
> > > > trading
> > > > journal.
> > > > Here is a sample of my data frame:
> > > > portfolio <- data.frame(
> > > > ? symbol = c("BTCUSDT", "BTCUSDT", "ETHUBTC", "AAVEBTC",
> > > > "ENAUSDT"),
> > > > ? time = c("2024-12-17 10:01:30", "2024-12-18 21:32:53", "2025-
> > > > 01-
> > > > 02
> > > > 10:34:49", "2025-01-02 11:14:43", "2025-01-02 11:15:22")
> > > > )
> > > >
> > > > I want to fetch the 'BTCUSDT' price for a specific time if the
> > > > symbol
> > > > of same row end with these 3 letters : 'BTC'. In my sample, it
> > > > will
> > > > be
> > > > rows 3 and 4.
> > > > Here is how I find BTCUSDT price for date 2025-01-02 10:34:49:
> > > >
> > > > get_btc_price <- function(time) {
> > > > ? klines <- binance_klines('BTCUSDT', start_time = '2025-01-02
> > > > 10:34:49', end_time = '2025-01-02 10:34:49')
> > > > }
> > > >
> > > > I was thinking of this code to write my new column
> > > > BTCUSDT_price:
> > > >
> > > > df <- portfolio %>%
> > > > ? mutate(
> > > > ??? BTCUSDT_price = ifelse(str_detect(symbol, "BTC$"),
> > > > map_dbl(time,
> > > > get_btc_price), NA_real_)
> > > > ? )}
> > > >
> > > > But the code returns:
> > > > Error in `mutate()`:
> > > > ? In argument: `BTCUSDT_price = ifelse(...)`.
> > > > Caused by error in `map_dbl()`:
> > > > ? In index: 1.
> > > > Caused by error in `[.data.table`:
> > > > ! Item 1 of j is 12 which is outside the column number range
> > > > [1,ncol=0]
> > > >
> > > > I have tried other workarounds but all give me errors.
> > > >
> > > > Thank you for help
> > > >
> > > >
> > > > [1]https://cran.r-project.org/web/packages/binancer
> > > > [2]https://cran.r-project.org/web/packages/PMwR
> > > >
> > >
> > > You want to compute a new column for your data.frame as
> > > a function of two existing columns.
> > >
> > > In base R, if you don't want to use a loop (and there
> > > would be nothing wrong with a loop), you could use
> > > 'mapply'; and then cbind the column to your data.frame:
> > >
> > > ??? new.column <- mapply(
> > > ??????? function(symbol, time) {
> > > ??????????? if (grepl("BTC$", symbol)) {
> > > ??????????????? ## your get_price computation, using
> > > ??????????????? ## 'symbol' and 'time'
> > > ??????????????? paste("do something with", symbol, time)
> > > ???????????????
> > > ??????????? } else
> > > ??????????????? NA
> > > ??????? },
> > > ??????? portfolio$symbol,
> > > ??????? portfolio$time
> > > ??? )
> > >
> > > ??? ## new.column
> > > ??? ##???????????????????????????????????????? BTCUSDT
> > > ??? ##????????????????????????????????????????????? NA
> > > ??? ##???????????????????????????????????????? BTCUSDT
> > > ??? ##????????????????????????????????????????????? NA
> > > ??? ##???????????????????????????????????????? ETHUBTC
> > > ??? ## "do something with ETHUBTC 2025-01-02 10:34:49"
> > > ??? ##???????????????????????????????????????? AAVEBTC
> > > ??? ## "do something with AAVEBTC 2025-01-02 11:14:43"
> > > ??? ##???????????????????????????????????????? ENAUSDT
> > > ??? ##????????????????????????????????????????????? NA
> >
> > Thank you for your answer.
> > A very warm thanks for your PMwR package. It helps me managing my
> > portfolio in a professional way. Unfortunately there isn't so much
> > material to manage, follow, compute ratio etc.
> > By any chance, do you have any cool build-in functions to cook
> > monthly
> > data (PL, valuation etc) as we compute every month always the same
> > data
> > ?
>
> Thank you.
>
> I am afraid there are no built-in, ready-to-use reports in
> PMwR.? That was a deliberate choice: PMwR should only
> provide simple tools for computing P/L, returns, or for
> backtesting, say. For analyzing the results of such
> computations, you have all of R for analysis and summarizing
> :-)
>
> For my reporting needs, I typically prepare templates in
> plain text, HTML or LaTeX and then fill in stats such as
> returns etc. See e.g.
> https://cran.r-project.org/web/packages/PMwR/vignettes/FinTeX.pdf
>
> For equity series, i.e. total value of a portfolio, using
> NAVseries and its summary method might be a start, see e.g.
> https://enricoschumann.net/R/packages/PMwR/manual/PMwR.html#NAVseries-summaries
> There are handful of functions for analyzing such series,
> e. g. `drawdowns` or `streaks`.? But as I said, most
> analysis I do with basic R functions.
>
>
[[alternative HTML version deleted]]
On Fri, 07 Mar 2025, Arnaud Gaboury writes: [...]
Question: why do you paste 42 here ?
get_btc_price <- function(my_time) {
paste(42, my_time)
}
Oh, just some random computation with 'my_time' :-)
Enrico Schumann Lucerne, Switzerland https://enricoschumann.net
On Fri, 2025-03-07 at 07:40 -0600, Brian G. Peterson wrote:
Arnaud, You may want to look at 'PerformanceAnalytics' for a large number of performance and risk functions for financial time series, including a large number of standard tables and charts.
Yes, I know a little bit the 'PerformanceAnalytics' package. I will certainly have a close look and do different work with it , but first I wanted to test Enrico package as it is simple and very straightforward. My portfolio is very simple, thus its management too.
I can also recommend Bernhard Pfaff's excellent book 'Financial Risk Modeling and Portfolio Optimization with R, 2nd ed.' Regards, Brian
-------------- next part -------------- An HTML attachment was scrubbed... URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20250307/165970f1/attachment.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 488 bytes Desc: This is a digitally signed message part URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20250307/165970f1/attachment.sig>