使用R中的历史股票价格的文本/ .txt文件正确显示烛台图

i86rm4rw  于 2023-02-01  发布在  其他
关注(0)|答案(1)|浏览(145)

见鬼,我买了标普500指数历史性的盘中价格(1分钟到1小时)追溯到2005年,因为大多数股票图表包在2016年或2011年左右停止报告日内价格。我已经成功地导入了价格,并让R只读取市场时间,不包括盘前和盘后。存在两个问题。第一,我需要让图表不显示周六和周日。更大的问题是,情节不是显示烛台,而是酒吧,他们是非常难以阅读。我已经尝试通过增加大小(size = 4),但是条形重叠,仍然不是烛台。我怎样才能使这些显示为适当的烛台?谢谢

library(quantmod)
library(tidyquant)
library(tidyverse)
library(ggplot2)
library(readr)
library(ggforce)
library(dplyr)

dir <-  "E:/Stock Trading/Historical Data/SPY_qjrt28"
setwd(dir)

data <- read_csv("SPY_30min.txt", 
        col_names = FALSE)
               
names(data) <- tolower(c("DateTime", "Open", "High", "Low", "Close", "Volume"))
data

#clean the data
write_rds(data, "cleaned.rds")
read_rds("cleaned.rds")

spy30m <- read_rds("cleaned.rds")
firstwave <- filter(spy30m, datetime >= as.Date('2009-03-02'), datetime <= as.Date('2009-03-19'))

# adding more time objects to the dataset
data <- data %>%
  mutate(hour = hour(datetime),
         minute = minute(datetime),
         hms = as_hms(datetime))

# is the hour function working as expected? Yes!
data %>%
  select(datetime, hour) %>%
  sample_n(10)

# look at bins of observations at 30 minute intervals. Looks good!
data %>%
  group_by(hms) %>%
  summarise(count = n()) %>%
  arrange(hms) %>% 
  print(n=100)

# filter the dataset to only include the times during regular market hours
data_regularmkt <- data %>%
  # `filter` is the dplyr function that limits the number of observations in a data frame
  # `between` function takes 3 arguments: an object/variable, a lower bound value, and upper bound value
  filter(between(hms, as_hms("09:30:00"), as_hms("16:00:00")))

# look at it again
data_regularmkt %>%
  group_by(hms) %>%
  summarise(count = n()) %>%
  arrange(hms) %>% 
  print(n=100)

###########

firstwave <- filter(spy30m, datetime >= as.Date('2009-03-06'), datetime <= as.Date('2009-03-19'))

ggplot(firstwave, aes(x = datetime, y = close)) +
  geom_candlestick(aes(open = open, high = high, low = low, close = close, size = 3))
ee7vknir

ee7vknir1#

假设我们有一个 Dataframe df,其中包含日期(dttm格式)、开盘、高、低、收盘列。
为了克服显示非交易时间的问题,我的第一个想法是使用另一个x轴刻度。

library(tidyverse)
library(lubridate)
library(tidyquant)

df <- df %>% 
  arrange(date) %>% 
  mutate(i = row_number())

# this is for the x-axis labels
df_x <- df %>% 
  group_by(d = floor_date(date, "day")) %>% 
  filter(date %in% c(min(date)))

df %>% 
  ggplot(aes(x = i)) +
  geom_candlestick(aes(open = open, low = low, high = high, close = close)) + 
  scale_x_continuous(breaks = df_x$i,
                     labels = df_x$date)

问题是,如果合约在交易时间暂停,就像夜间或周末一样,也不会有数据,但是,这些时间你可能还是想显示。
我们可以先使用dplyr函数的completeexpand来修复数据,然后仍然使用我的解决方案,即在指数x尺度上绘图。
使用plotly库可能更容易。

plt <- plot_ly(data = df, x = ~date, 
               open = ~open, close = ~close,
               high = ~high, low = ~low,
               type="candlestick") 
plt

这是为了隐藏非交易时间:

plt %>% layout(showlegend = F, xaxis = list(rangebreaks=
                                        list(
                                          list(bounds=list(17, 9),
                                               pattern="hour")),#hide hours outside of 9am-5pm
                                      dtick=86400000.0/2,
                                      tickformat="%H:%M\n%b\n%Y"))

更多信息请访问:www.example.com和https://plotly.com/r/candlestick-charts/https://plotly.com/r/time-series/#hiding-nonbusiness-hours and https://plotly.com/r/candlestick-charts/
至于你不喜欢tidyquant的geom_candlestick的外观,我也建议你试试Plotly。

相关问题