+#I just started to code for some cryptocurrencies analysis. But I just encounterd an issue with the code.
_____________________________________________________________________________
library(magrittr)
library(dplyr)
library(tidyverse)
library(crypto)
library(lubridate)
library(xts)
library(quantmod)
library(tidyr)
df <- getCoins(limit = 50)
yourfunction <- function(df, frequency = NULL) {
freq <- frequency
df$date <- lubridate::round_date(df$date, freq)
data <-
df %>% dplyr::group_by(date, slug, symbol, name, ranknow) %>%
dplyr::summarise(
open = dplyr::first(open),
high = max(high),
low = min(low),
close = dplyr::last(close),
volume = sum(volume),
market = dplyr::last(market))
data$volume <- round(data$volume, digits = 0)
data$market <- round(data$market, digits = 0)
data <- as.data.frame(data)
results <- xts::xts(data[, 2:ncol(data)], as.POSIXct(data[, 1], format =
"%d.%m.%Y %H:%M:%S"))
return(results)
}
#### IF NO ERRORS IN ABOVE RUN THESE INDIVIDUALLY------
week_xts <- yourfunction(df, frequency = "week")
str(week_xts)
as.tbl(week_xts) %>%
select(date, slug, open) %>%
spread(slug, open) %>%
tail()
#### IF NO ERRORS IN ABOVE RUN THESE INDIVIDUALLY------
month_xts <- yourfunction(df, frequency = "month")
str(month_xts)
as.tbl(month_xts) %>%
select(date, slug, open) %>%
spread(slug, open) %>%
tail()
########################
#Making daily, weekly and monthly open for ten biggest coins
daily_open <- spread(df[,c(1,4,6)],slug,open)
weekly_open <- spread(week_xts[,c(1, 2, 6)],slug,open)
monthly_open <- spread(month_xts[,c(1, 2, 6)],slug,open)
ten_biggest <- c(list_of_coins[1:10,"slug"])
daily_open <- daily_open[,c("date",ten_biggest)]
weekly_open <- weekly_open[,c("date",ten_biggest)]
monthly_open <- monthly_open[,c("date",ten_biggest)]
#Making daily, weekly and monthly high for ten biggest coins
daily_high <- spread(df[,c(1,4,7)],slug,high)
weekly_high <- spread(week_xts[,c(1,2,7)],slug,high)
monthly_high <- spread(month_xts[,c(1,2,7)],slug,high)
daily_high <- daily_high[,c("date",ten_biggest)]
weekly_high <- weekly_high[,c("date",ten_biggest)]
monthly_high <- monthly_high[,c("date",ten_biggest)]
#Making daily, weekly and monthly low for ten biggest coins
daily_low <- spread(df[,c(1,4,8)],slug,low)
weekly_low <- spread(week_xts[,c(1,2,8)],slug,low)
monthly_low <- spread(month_xts[,c(1,2,8)],slug,low)
daily_low <- daily_low[,c("date",ten_biggest)]
weekly_low <- weekly_low[,c("date",ten_biggest)]
monthly_low <- monthly_low[,c("date",ten_biggest)]
#Making daily, weekly and monthly close for ten biggest coins
daily_close <- spread(df[,c(1,4,9)],slug,close)
weekly_close <- spread(week_xts[,c(1,2,9)],slug,close)
monthly_close <- spread(month_xts[,c(1,2,9)],slug,close)
daily_close <- daily_close[,c("date",ten_biggest)]
weekly_close <- weekly_close[,c("date",ten_biggest)]
monthly_close <- monthly_close[,c("date",ten_biggest)]
#Making daily, weekly and monthly volume for ten biggest coins
daily_volume <- spread(df[,c(1,4,10)],slug,volume)
weekly_volume <- spread(week_xts[,c(1,2,10)],slug,volume)
monthly_volume <- spread(month_xts[,c(1,2,10)],slug,volume)
daily_volume <- daily_volume[,c("date",ten_biggest)]
weekly_volume <- weekly_volume[,c("date",ten_biggest)]
monthly_volume <- monthly_volume[,c("date",ten_biggest)]
#Making daily,weekly and monthly market for ten biggest coins
daily_market <- spread(df[,c(1,4,11)],slug,market)
weekly_market <- spread(week_xts[,c(1,2,11)],slug,market)
monthly_market <- spread(month_xts[,c(1,2,11)],slug,market)
daily_market <- daily_market[,c("date",ten_biggest)]
weekly_market <- weekly_market[,c("date",ten_biggest)]
monthly_market <- monthly_market[,c("date",ten_biggest)]
#Doesn't work
weekly_open_returns <- periodReturn(weekly_open, period="daily", subset=NULL, type="arithmetic",leading="TRUE")
weekly_open_returns <- Return.calculate(weekly_open)
日次、月次、週次の各データタイプ (open、high、close、volume、market、close_ratio、spread) のデータフレームを作成したいと思います。これらのデータ フレームは、各列が 1 つの通貨を表し、行が観察結果を表すように構築する必要があります。ただし、これを行うために週次および月次の周波数にスプレッド関数を使用しようとすると、エラーが発生します。どのように進めればよいですか?
よろしくお願いします。楽しい一日を。
エラーは次のとおりです。
> weekly_open_returns <- periodReturn(weekly_open, period="daily", subset=NULL, type="arithmetic",leading="TRUE")
Error in try.xts(x) :
Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format
> weekly_open_returns <- Return.calculate(weekly_open)
Error in checkData(prices, method = "xts") :
The data cannot be converted into a time series. If you are trying to pass in names from a data object with one column, you should use the form 'data[rows, columns, drop = FALSE]'. Rownames should have standard date formats, such as '1985-03-15'.
(コメントより:)
日次、月次、週次の各データタイプ (open、high、close、volume、market、close_ratio、spread) のデータフレームを作成したいと思います。これらのデータ フレームは、各列が 1 つの通貨を表し、行が観察結果を表すように構築する必要があります。ただし、これを行うために週次および月次の周波数にスプレッド関数を使用しようとすると、エラーが発生します。どのように進めればよいですか?よろしくお願いします。楽しい一日を。– MP PM 28 分前