Thông tin dữ liệu: S&P500 stock data
BỘ dữ liệu về chứng khoán của tất cả công ty có trong S&P500 thời gian từ ngày 08/02/2013 đến ngày 10/09/2014
Mô tả dữ liệu:
Thông tin dữ liệu được mô tả ở các cột sau :
tính số lần tăng,giảm, giữ nguyên của cổ phiếu đó, trong khoảng thời gian đó, tỉ lệ % tăng giảm
Link download : https://docs.google.com/spreadsheets/d/14GTxp_yktiXY_s7B6zZULs3XlUiLq0aJ/edit?usp=sharing&ouid=115122603206656803042&rtpof=true&sd=true
library(readxl)
library(DT)
# Đọc thông tin file
stock <- read_excel("d:/stock.xlsx")
# Hiển thị số dòng mỗi trang
line <- 10
# Xem kết quả
datatable(stock, options = list(pageLength = line))
## Warning in instance$preRenderHook(instance): It seems your data is too big for
## client-side DataTables. You may consider server-side processing:
## https://rstudio.github.io/DT/server.html
# Tải các gói cần thiết
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
# Vẽ biểu đồ giá đóng cửa
ggplot(stock, aes(x = date, y = close)) +
geom_bar(stat = "identity", fill = "pink") +
labs(title = "Biểu đồ giá cổ phiếu phiên đóng cửa",
x = "Ngay",
y = "Gia dong cua") +
theme_minimal()
library(readxl)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(dplyr)
# Đọc dữ liệu từ file excel
data <- read_excel("D:/stock.xlsx")
# Chuyển cột ngày sang định dạng POSIXct
data$date <- as.POSIXct(data$date)
# Lọc dữ liệu cho cổ phiếu ARE
stock_ARE <- data[data$Name == "ARE", ]
# Tạo biểu đồ nến Nhật Bản
plot_ly(stock_ARE, type = "candlestick", x = ~date, open = ~open, high = ~high, low = ~low, close = ~close) %>%
layout(title = "Biểu đồ nến cho cổ phiếu ARE",
xaxis = list(title = "Ngày"),
yaxis = list(title = "Giá đóng cửa"))
# Tạo bảng tần suất
freq_table <- table(stock$Name)
# In ra số lượng từng mã cổ phiếu
print(freq_table)
##
## A AAL AAP AAPL ABBV ABC ABT ACN ADBE ADI ADM ADP ADS
## 1259 1259 1259 1259 1259 1259 1259 1259 1259 1259 1259 1259 1259
## ADSK AEE AEP AES AET AFL AGN AIG AIV AIZ AJG AKAM ALB
## 1259 1259 1259 1259 1259 1259 1259 1259 1259 1259 1259 1259 1259
## ALGN ALK ALL ALLE ALXN AMAT AMD AME AMG AMGN AMP AMT AMZN
## 1259 1259 1259 1063 1259 1259 1259 1259 1259 1259 1259 1259 1259
## ANDV ANSS ANTM AON AOS APA APC APD APH APTV ARE ARNC ATVI
## 1259 1259 1259 1259 1259 1259 1259 1259 1259 44 1259 1259 1259
## AVB AVGO AVY AWK AXP AYI AZO BA BAC BAX BBT BBY BDX
## 1259 1259 1259 1259 1259 1259 1259 1259 1259 1259 1259 1259 1259
## BEN BF.B BHF BHGE BIIB BK BLK BLL BMY BRK.B BSX BWA BXP
## 1259 1259 143 152 1259 1259 1259 1259 1257 1259 1259 1259 1259
## CA CAG CAH CAT CBG
## 1259 1259 1259 1259 400
# Lọc dữ liệu cho cổ phiếu AAL
stock_AAL <- stock[stock$Name == "AAL", ]
# Tính toán thay đổi giá trong phiên giao dịch đóng cửa
price_changes <- diff(stock_AAL$close)
# Đếm số lần tăng, giảm và giữ nguyên giá
num_increases <- sum(diff(stock_AAL$close) > 0)
num_decreases <- sum(diff(stock_AAL$close) < 0)
num_unchanged <- sum(diff(stock_AAL$close) == 0)
# xem kết quả
ketqua <- sprintf("Số lần tăng: %d\nSố lần giảm: %d\nSố lần giữ nguyên: %d", num_increases, num_decreases, num_unchanged)
cat(ketqua)
## Số lần tăng: 664
## Số lần giảm: 588
## Số lần giữ nguyên: 6
# Tính toán số lần tăng và giảm trong phiên giao dịch đóng cửa
num_increases <- sum(diff(stock_AAL$close) > 0)
num_decreases <- sum(diff(stock_AAL$close) < 0)
# Tính toán tỷ lệ phần trăm tăng và giảm
total_trades <- num_increases + num_decreases
percent_increase <- (num_increases / total_trades) * 100
percent_decrease <- (num_decreases / total_trades) * 100
# xem kết quả
tylephantram <- sprintf("Tỷ lệ phần trăm tăng: %.2f %%\nTỷ lệ phần trăm giảm: %.2f %%", percent_increase, percent_decrease)
cat(tylephantram)
## Tỷ lệ phần trăm tăng: 53.04 %
## Tỷ lệ phần trăm giảm: 46.96 %
library(readxl)
library(dplyr)
library(ggplot2)
# Chuyển cột ngày sang định dạng POSIXct
data$date <- as.POSIXct(data$date)
# Tính toán số lần tăng, giảm và giữ nguyên giá hàng ngày
daily_price_changes <- data %>%
group_by(date, Name) %>%
summarize(num_increases = sum(diff(close) > 0),
num_decreases = sum(diff(close) < 0),
num_unchanged = sum(diff(close) == 0))
## `summarise()` has grouped output by 'date'. You can override using the
## `.groups` argument.
# Tính toán tổng khối lượng giao dịch hàng ngày
daily_volume <- data %>%
group_by(date, Name) %>%
summarize(total_volume = sum(volume))
## `summarise()` has grouped output by 'date'. You can override using the
## `.groups` argument.
# Tạo biểu đồ so sánh số lần tăng, giảm và giữ nguyên giá của cổ phiếu AER và các cổ phiếu khác
ggplot(daily_price_changes, aes(x = date, y = num_increases - num_decreases, color = Name)) +
geom_line() +
labs(title = "So sánh mức độ tăng giảm của cổ phiếu AER với các cổ phiếu khác",
x = "Ngày",
y = "Số lần tăng - Số lần giảm") +
theme_minimal()
# Tạo biểu đồ so sánh tổng khối lượng giao dịch hàng ngày của cổ phiếu AER và các cổ phiếu khác
ggplot(daily_volume, aes(x = date, y = total_volume, color = Name)) +
geom_line() +
labs(title = "So sánh tổng khối lượng giao dịch hàng ngày của cổ phiếu AER với các cổ phiếu khác",
x = "Ngày",
y = "Tổng khối lượng giao dịch") +
theme_minimal()