中国海油600938日、周K线图2005-1 ~2005-9

python库baostock获取股票数据 – 网事-树莓派

---
title: "R Notebook"
output:
  html_document:
    df_print: paged
---

```{r}
library(readr)
library(quantmod)

# 读取数据
stock <- read_csv("stock_data_600938.csv")

# 创建xts对象时只保留数值列和日期列
# 排除code等非数值列
stock_xts <- xts(stock[,-c(1,2)],                               # 只保留数值型列
                 order.by = as.Date(stock$date))      # 日期作为索引
tail(stock_xts)

# 绘制股票价格图
chartSeries(stock_xts, # 明确指定使用收盘价绘图
            theme = "white", 
            name = "中国海油股价K线图及布林线",
            up.col = "red",dn.col = "green",
            TA = c(addVo(),      # 添加成交量图
                   addBBands())) # 添加SMA20均线

```
# 绘制周股票价格图
```{r}
stock_xts_w <- to.weekly(stock_xts)
tail(stock_xts_w)
chartSeries(stock_xts_w, # 明确指定使用收盘价绘图
            theme = "white", 
            name = "中国海油股价K线图及布林线",
            up.col = "red",dn.col = "green",
            TA = c(addVo(),      # 添加成交量图
                   addBBands())) # 添加SMA20均线
```
            open  high   low close  preclose   volume    amount adjustflag   turn tradestatus  pctChg isST
2025-09-23 26.12 26.37 26.04 26.11 26.16 33420344 874462608 3 1.1177 1 -0.1911 0
2025-09-24 26.20 26.66 26.20 26.33 26.11 35694697 941589316 3 1.1938 1 0.8426 0
2025-09-25 26.34 26.71 26.32 26.60 26.33 44561236 1183035539 3 1.4903 1 1.0254 0
2025-09-26 26.55 26.69 26.47 26.50 26.60 22192550 589291301 3 0.7422 1 -0.3759 0
2025-09-29 26.55 26.58 26.21 26.40 26.50 31542708 831091786 3 1.0549 1 -0.3774 0
2025-09-30 26.16 26.19 25.88 26.13 26.40 51791066 1348014475 3 1.7321 1 -1.0227 0

stock_xts.Open stock_xts.High stock_xts.Low stock_xts.Close stock_xts.Volume
2025-08-29 25.75 26.31 25.47 25.68 281303365
2025-09-05 25.69 26.57 25.41 25.74 309074615
2025-09-12 25.58 26.36 25.55 26.30 193722674
2025-09-19 26.30 26.90 26.00 26.40 232149346
2025-09-26 26.41 26.71 26.04 26.50 163340642
2025-09-30 26.55 26.58 25.88 26.13 83333774