# Load data from star schema
etf_daily <- open_dataset(DAILY_PARQUET_DIR)

# Filter for stock_code 2800
etf_2800_all <- etf_daily %>%
  filter(stock_code == STOCK_CODE) %>%
  collect()

if (nrow(etf_2800_all) == 0) {
  stop("No data found for stock_code ", STOCK_CODE)
}

# Ensure trade_date is Date type
etf_2800_all$trade_date <- as.Date(etf_2800_all$trade_date)

# Sort by date (most recent first)
etf_2800_all <- etf_2800_all %>%
  arrange(desc(trade_date))

# Get last 5 days and last 30 days
etf_2800_recent <- etf_2800_all %>% slice_head(n = DAYS_TO_SHOW)
etf_2800_last30 <- etf_2800_all %>% slice_head(n = 30)

# Key variables
key_vars <- c("trade_date", "volume_cleaned", "turnover_cleaned", "aum_cleaned", 
              "closing_price", "nav", "day_high", "day_low", 
              "outstanding_units_cleaned", "premium_discount_percent")

# Calculate historical statistics (needed early for comparisons)
stats <- etf_2800_all %>%
  summarise(
    volume_median = median(volume_cleaned, na.rm = TRUE),
    volume_mean = mean(volume_cleaned, na.rm = TRUE),
    volume_min = min(volume_cleaned, na.rm = TRUE),
    volume_max = max(volume_cleaned, na.rm = TRUE),
    turnover_median = median(turnover_cleaned, na.rm = TRUE),
    turnover_mean = mean(turnover_cleaned, na.rm = TRUE),
    turnover_min = min(turnover_cleaned, na.rm = TRUE),
    turnover_max = max(turnover_cleaned, na.rm = TRUE),
    total_days = n()
  )

# Prepare comparison data
comparison <- etf_2800_recent %>%
  select(trade_date, volume_cleaned, turnover_cleaned) %>%
  mutate(
    volume_vs_median = ifelse(is.na(volume_cleaned), NA, volume_cleaned / stats$volume_median),
    volume_vs_mean = ifelse(is.na(volume_cleaned), NA, volume_cleaned / stats$volume_mean),
    turnover_vs_median = ifelse(is.na(turnover_cleaned), NA, turnover_cleaned / stats$turnover_median),
    turnover_vs_mean = ifelse(is.na(turnover_cleaned), NA, turnover_cleaned / stats$turnover_mean),
    volume_pct_rank = NA_real_,
    turnover_pct_rank = NA_real_
  )

# Calculate percentile ranks
for (i in 1:nrow(comparison)) {
  if (!is.na(comparison$volume_cleaned[i])) {
    comparison$volume_pct_rank[i] <- mean(etf_2800_all$volume_cleaned <= comparison$volume_cleaned[i], na.rm = TRUE) * 100
  }
  if (!is.na(comparison$turnover_cleaned[i])) {
    comparison$turnover_pct_rank[i] <- mean(etf_2800_all$turnover_cleaned <= comparison$turnover_cleaned[i], na.rm = TRUE) * 100
  }
}

Data Overview

cat("**Stock Code:**", STOCK_CODE, "\n\n")
## **Stock Code:** 2800
cat("**Total Records:**", nrow(etf_2800_all), "\n\n")
## **Total Records:** 62
cat("**Date Range:**", format(min(etf_2800_all$trade_date)), "to", format(max(etf_2800_all$trade_date)), "\n\n")
## **Date Range:** 2025-09-17 to 2025-12-16
cat("**Last Updated:**", format(Sys.Date()))
## **Last Updated:** 2025-12-17

Closing Price Trend

# Prepare data for chart - order by date ascending for line chart
chart_data <- etf_2800_all %>%
  select(trade_date, closing_price) %>%
  arrange(trade_date) %>%
  mutate(
    is_latest = trade_date == max(trade_date, na.rm = TRUE)
  )

# Get latest date for labeling
latest_date <- max(chart_data$trade_date, na.rm = TRUE)
latest_price <- chart_data$closing_price[chart_data$trade_date == latest_date][1]

ggplot(chart_data, aes(x = trade_date, y = closing_price)) +
  geom_line(color = "#007bff", size = 0.8) +
  geom_point(data = chart_data %>% filter(is_latest), 
             color = "red", size = 3, shape = 19) +
  geom_point(data = chart_data %>% filter(!is_latest), 
             color = "#007bff", size = 0.5, alpha = 0.6) +
  labs(
    title = paste("Closing Price Trend - ETF", STOCK_CODE),
    subtitle = paste("Latest:", format(latest_date), "- Price:", ifelse(is.na(latest_price), "NA", formatC(latest_price, format = "f", digits = 4))),
    x = "Date",
    y = "Closing Price (HKD)",
    caption = "Red dot indicates latest data point"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 14, face = "bold"),
    plot.subtitle = element_text(size = 11, color = "gray50"),
    axis.title = element_text(size = 11),
    axis.text = element_text(size = 9),
    plot.caption = element_text(size = 9, color = "gray60", hjust = 0),
    panel.grid.minor = element_blank()
  ) +
  scale_x_date(date_labels = "%Y-%m-%d", date_breaks = "1 month") +
  scale_y_continuous(labels = scales::number_format(accuracy = 0.01))


Latest Date Verification

latest_date <- max(etf_2800_all$trade_date)
dec_16 <- etf_2800_recent %>%
  filter(trade_date == latest_date)

if (nrow(dec_16) > 0) {
  # Create a summary table
  latest_summary <- data.frame(
    Metric = c("Date", "Volume", "Turnover", "AUM", "Closing Price", "NAV", "Suspension Flag"),
    Value = c(
      format(latest_date),
      ifelse(is.na(dec_16$volume_cleaned[1]), "NA (missing/suspended)", 
             formatC(dec_16$volume_cleaned[1], format = "d", big.mark = ",")),
      ifelse(is.na(dec_16$turnover_cleaned[1]), "NA (missing/suspended)",
             formatC(dec_16$turnover_cleaned[1], format = "f", digits = 2, big.mark = ",")),
      ifelse(is.na(dec_16$aum_cleaned[1]), "NA",
             formatC(dec_16$aum_cleaned[1], format = "f", digits = 0, big.mark = ",")),
      ifelse(is.na(dec_16$closing_price[1]), "NA",
             formatC(dec_16$closing_price[1], format = "f", digits = 4)),
      ifelse(is.na(dec_16$nav[1]), "NA",
             formatC(dec_16$nav[1], format = "f", digits = 4)),
      ifelse(is.na(dec_16$suspension_flag[1]), "NA", dec_16$suspension_flag[1])
    ),
    stringsAsFactors = FALSE
  )
  
  # Add comparison metrics if available
  if (!is.na(dec_16$volume_cleaned[1]) && !is.na(dec_16$turnover_cleaned[1])) {
    vol_idx <- which(comparison$trade_date == latest_date)
    comparison_metrics <- data.frame(
      Metric = c("Volume vs Median", "Volume vs Mean", 
                 "Turnover vs Median", "Turnover vs Mean"),
      Value = c(
        sprintf("%.2fx (%s percentile)", 
                dec_16$volume_cleaned[1] / stats$volume_median,
                sprintf("%.1f%%", comparison$volume_pct_rank[vol_idx])),
        sprintf("%.2fx", dec_16$volume_cleaned[1] / stats$volume_mean),
        sprintf("%.2fx (%s percentile)",
                dec_16$turnover_cleaned[1] / stats$turnover_median,
                sprintf("%.1f%%", comparison$turnover_pct_rank[vol_idx])),
        sprintf("%.2fx", dec_16$turnover_cleaned[1] / stats$turnover_mean)
      ),
      stringsAsFactors = FALSE
    )
    latest_summary <- rbind(latest_summary, comparison_metrics)
  }
  
  latest_summary %>%
    gt() %>%
    tab_header(
      title = "Latest Date Data Summary",
      subtitle = paste("ETF", STOCK_CODE, "-", format(latest_date))
    ) %>%
    cols_label(
      Metric = "Metric",
      Value = "Value"
    ) %>%
    tab_style(
      style = cell_text(weight = "bold"),
      locations = cells_column_labels()
    ) %>%
    tab_style(
      style = cell_text(align = "left"),
      locations = cells_body(columns = Metric)
    ) %>%
    tab_style(
      style = cell_text(align = "right"),
      locations = cells_body(columns = Value)
    ) %>%
    opt_table_font(font = "Arial") %>%
    tab_options(
      table.width = pct(60),
      column_labels.background.color = "#f8f9fa",
      table_body.hlines.color = "#e9ecef",
      heading.border.bottom.color = "#dee2e6"
    )
} else {
  cat("⚠ Warning: No data found for latest date\n")
}
Latest Date Data Summary
ETF 2800 - 2025-12-16
Metric Value
Date 2025-12-16
Volume 397,147,709
Turnover 10,089,945,020.00
AUM NA
Closing Price 25.4400
NAV NA
Suspension Flag No
Volume vs Median 0.80x (27.4% percentile)
Volume vs Mean 0.73x
Turnover vs Median 0.78x (19.4% percentile)
Turnover vs Mean 0.70x

Comparison: Recent vs Historical

Percentile Explanation: Percentile rank shows what percentage of historical values are less than or equal to the current value. For example, a 75th percentile means the current value is higher than 75% of all historical values (higher is better for volume/turnover). A 25th percentile means it’s higher than only 25% of historical values (lower activity).

comparison_display <- comparison %>%
  select(trade_date, volume_cleaned, volume_vs_median, volume_vs_mean, volume_pct_rank,
         turnover_cleaned, turnover_vs_median, turnover_vs_mean, turnover_pct_rank)

comparison_display %>%
  gt() %>%
  tab_header(
    title = "Recent Performance vs Historical Statistics",
    subtitle = paste("ETF", STOCK_CODE, "- Comparison of last 5 days")
  ) %>%
  fmt_date(columns = trade_date, date_style = "yMd") %>%
  fmt_number(columns = volume_cleaned, decimals = 0, use_seps = TRUE) %>%
  fmt_number(columns = volume_vs_median, decimals = 2, pattern = "{x}x") %>%
  fmt_number(columns = volume_vs_mean, decimals = 2, pattern = "{x}x") %>%
  fmt_number(columns = volume_pct_rank, decimals = 1, pattern = "{x}%") %>%
  fmt_number(columns = turnover_cleaned, decimals = 2, use_seps = TRUE) %>%
  fmt_number(columns = turnover_vs_median, decimals = 2, pattern = "{x}x") %>%
  fmt_number(columns = turnover_vs_mean, decimals = 2, pattern = "{x}x") %>%
  fmt_number(columns = turnover_pct_rank, decimals = 1, pattern = "{x}%") %>%
  sub_missing(columns = everything(), missing_text = "NA") %>%
  cols_label(
    trade_date = "Date",
    volume_cleaned = "Volume",
    volume_vs_median = "vs Median",
    volume_vs_mean = "vs Mean",
    volume_pct_rank = "Percentile",
    turnover_cleaned = "Turnover",
    turnover_vs_median = "vs Median",
    turnover_vs_mean = "vs Mean",
    turnover_pct_rank = "Percentile"
  ) %>%
  tab_spanner(
    label = "Volume",
    columns = c(volume_cleaned, volume_vs_median, volume_vs_mean, volume_pct_rank)
  ) %>%
  tab_spanner(
    label = "Turnover",
    columns = c(turnover_cleaned, turnover_vs_median, turnover_vs_mean, turnover_pct_rank)
  ) %>%
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_column_labels()
  ) %>%
  tab_style(
    style = cell_text(align = "right"),
    locations = cells_body()
  ) %>%
  tab_style(
    style = cell_text(weight = "bold", align = "center"),
    locations = cells_column_spanners()
  ) %>%
  opt_table_font(font = "Arial") %>%
  tab_options(
    table.width = pct(100),
    column_labels.background.color = "#f8f9fa",
    table_body.hlines.color = "#e9ecef",
    heading.border.bottom.color = "#dee2e6"
  )
Recent Performance vs Historical Statistics
ETF 2800 - Comparison of last 5 days
Date
Volume
Turnover
Volume vs Median vs Mean Percentile Turnover vs Median vs Mean Percentile
12/16/2025 397,147,709 0.80x 0.73x 27.4% 10,089,945,020.00 0.78x 0.70x 19.4%
12/15/2025 494,817,249 1.00x 0.91x 50.0% 12,830,176,890.00 0.99x 0.89x 48.4%
12/12/2025 374,899,285 0.75x 0.69x 16.1% 9,777,151,290.00 0.75x 0.68x 16.1%
12/11/2025 422,527,872 0.85x 0.78x 30.6% 10,904,808,490.00 0.84x 0.76x 30.6%
12/10/2025 393,066,671 0.79x 0.72x 24.2% 10,053,365,920.00 0.77x 0.70x 17.7%

Recent 5 Days

display_data <- etf_2800_recent %>%
  select(any_of(key_vars))

display_data %>%
  gt() %>%
  tab_header(
    title = "Recent 5 Days - Key Metrics",
    subtitle = paste("ETF", STOCK_CODE)
  ) %>%
  fmt_date(columns = trade_date, date_style = "yMd") %>%
  fmt_number(columns = volume_cleaned, decimals = 0, use_seps = TRUE) %>%
  fmt_number(columns = turnover_cleaned, decimals = 2, use_seps = TRUE) %>%
  fmt_number(columns = aum_cleaned, decimals = 0, use_seps = TRUE) %>%
  fmt_number(columns = closing_price, decimals = 4) %>%
  fmt_number(columns = nav, decimals = 4) %>%
  fmt_number(columns = day_high, decimals = 4) %>%
  fmt_number(columns = day_low, decimals = 4) %>%
  fmt_number(columns = outstanding_units_cleaned, decimals = 0, use_seps = TRUE) %>%
  fmt_number(columns = premium_discount_percent, decimals = 2) %>%
  cols_label(
    trade_date = "Date",
    volume_cleaned = "Volume",
    turnover_cleaned = "Turnover",
    aum_cleaned = "AUM",
    closing_price = "Close Price",
    nav = "NAV",
    day_high = "Day High",
    day_low = "Day Low",
    outstanding_units_cleaned = "Outstanding Units",
    premium_discount_percent = "Premium/Discount %"
  ) %>%
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_column_labels()
  ) %>%
  tab_style(
    style = cell_text(align = "right"),
    locations = cells_body()
  ) %>%
  opt_table_font(font = "Arial") %>%
  tab_options(
    table.width = pct(100),
    column_labels.background.color = "#f8f9fa",
    table_body.hlines.color = "#e9ecef",
    heading.border.bottom.color = "#dee2e6"
  )
Recent 5 Days - Key Metrics
ETF 2800
Date Volume Turnover AUM Close Price NAV Day High Day Low Outstanding Units Premium/Discount %
12/16/2025 397,147,709 10,089,945,020.00 NA 25.4400 NA 25.7600 25.3000 5,376,992,500 NA
12/15/2025 494,817,249 12,830,176,890.00 138,960,000,000 25.8400 25.8400 26.0200 25.8000 5,376,992,500 −0.02
12/12/2025 374,899,285 9,777,151,290.00 140,840,000,000 26.1600 26.1900 26.2400 25.9200 5,376,992,500 −0.13
12/11/2025 422,527,872 10,904,808,490.00 138,430,000,000 25.7600 25.7500 26.0000 25.6800 5,376,992,500 0.06
12/10/2025 393,066,671 10,053,365,920.00 136,760,000,000 25.7000 25.7600 25.7000 25.4600 5,309,992,500 −0.22

Recent 30 Days

table_30days <- etf_2800_last30 %>%
  select(any_of(key_vars)) %>%
  arrange(desc(trade_date))

table_30days %>%
  gt() %>%
  tab_header(
    title = "Recent 30 Days - Complete Data",
    subtitle = paste("ETF", STOCK_CODE, "- Most recent dates first")
  ) %>%
  fmt_date(columns = trade_date, date_style = "yMd") %>%
  fmt_number(columns = volume_cleaned, decimals = 0, use_seps = TRUE) %>%
  fmt_number(columns = turnover_cleaned, decimals = 2, use_seps = TRUE) %>%
  fmt_number(columns = aum_cleaned, decimals = 0, use_seps = TRUE) %>%
  fmt_number(columns = closing_price, decimals = 4) %>%
  fmt_number(columns = nav, decimals = 4) %>%
  fmt_number(columns = day_high, decimals = 4) %>%
  fmt_number(columns = day_low, decimals = 4) %>%
  fmt_number(columns = outstanding_units_cleaned, decimals = 0, use_seps = TRUE) %>%
  fmt_number(columns = premium_discount_percent, decimals = 2) %>%
  sub_missing(columns = everything(), missing_text = "NA") %>%
  cols_label(
    trade_date = "Date",
    volume_cleaned = "Volume",
    turnover_cleaned = "Turnover",
    aum_cleaned = "AUM",
    closing_price = "Close Price",
    nav = "NAV",
    day_high = "Day High",
    day_low = "Day Low",
    outstanding_units_cleaned = "Outstanding Units",
    premium_discount_percent = "Premium/Discount %"
  ) %>%
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_column_labels()
  ) %>%
  tab_style(
    style = cell_text(align = "right"),
    locations = cells_body()
  ) %>%
  opt_table_font(font = "Arial") %>%
  tab_options(
    table.width = pct(100),
    column_labels.background.color = "#f8f9fa",
    table_body.hlines.color = "#e9ecef",
    heading.border.bottom.color = "#dee2e6",
    table.font.size = px(12)
  )
Recent 30 Days - Complete Data
ETF 2800 - Most recent dates first
Date Volume Turnover AUM Close Price NAV Day High Day Low Outstanding Units Premium/Discount %
12/16/2025 397,147,709 10,089,945,020.00 NA 25.4400 NA 25.7600 25.3000 5,376,992,500 NA
12/15/2025 494,817,249 12,830,176,890.00 138,960,000,000 25.8400 25.8400 26.0200 25.8000 5,376,992,500 −0.02
12/12/2025 374,899,285 9,777,151,290.00 140,840,000,000 26.1600 26.1900 26.2400 25.9200 5,376,992,500 −0.13
12/11/2025 422,527,872 10,904,808,490.00 138,430,000,000 25.7600 25.7500 26.0000 25.6800 5,376,992,500 0.06
12/10/2025 393,066,671 10,053,365,920.00 136,760,000,000 25.7000 25.7600 25.7000 25.4600 5,309,992,500 −0.22
12/9/2025 395,383,580 10,183,826,380.00 136,180,000,000 25.6200 25.6500 26.0200 25.5800 5,309,992,500 −0.11
12/8/2025 326,054,948 8,507,777,976.00 137,770,000,000 25.9800 25.9800 26.3600 25.9600 5,302,992,500 NA
12/5/2025 298,803,923 7,806,092,749.00 139,480,000,000 26.2800 26.3000 26.3600 25.9800 5,302,992,500 −0.09
12/4/2025 498,794,543 12,991,441,220.00 139,230,000,000 26.1400 26.1500 26.2000 25.8600 5,323,992,500 −0.05
12/3/2025 398,237,220 10,365,822,890.00 140,160,000,000 25.9400 25.9600 26.1800 25.9000 5,399,992,500 −0.07
12/2/2025 339,903,581 8,934,172,670.00 140,200,000,000 26.2400 26.2600 26.4400 26.1400 5,338,992,500 −0.08
12/1/2025 321,924,139 8,422,362,995.00 139,770,000,000 26.2000 26.1800 26.3400 26.0400 5,338,992,500 0.08
11/28/2025 230,461,371 6,000,255,473.00 139,120,000,000 26.0400 26.0000 26.1600 25.9600 5,349,992,500 0.14
11/27/2025 546,820,348 14,281,995,180.00 141,930,000,000 26.0600 26.0900 26.2600 25.9600 5,439,992,500 −0.12
11/26/2025 537,131,355 14,044,518,780.00 146,480,000,000 26.0800 26.0700 26.2800 26.0400 5,617,992,500 0.03
11/25/2025 533,695,075 13,899,951,510.00 142,480,000,000 26.0400 26.0400 26.2000 25.8800 5,471,992,500 0.00
11/24/2025 490,723,815 12,631,415,320.00 146,960,000,000 25.8200 25.8600 25.9200 25.5200 5,682,992,500 −0.16
11/21/2025 727,619,188 18,543,114,350.00 144,430,000,000 25.3600 25.3600 25.6600 25.3200 5,694,992,500 −0.01
11/20/2025 1,187,476,759 30,855,474,080.00 142,500,000,000 26.0000 25.9800 26.2000 25.8200 5,484,992,500 0.07
11/19/2025 661,002,576 17,190,756,400.00 143,100,000,000 25.9800 25.9800 26.1800 25.8800 5,508,992,500 0.02
11/18/2025 504,318,531 13,183,014,150.00 144,640,000,000 26.0600 26.0800 26.3200 25.9600 5,546,992,500 −0.06
11/17/2025 689,586,244 18,283,162,870.00 145,550,000,000 26.5200 26.5300 26.6800 26.4000 5,485,992,500 −0.05
11/14/2025 595,660,489 15,996,781,990.00 151,850,000,000 26.7200 26.7200 27.0400 26.6800 5,682,992,500 0.00
11/13/2025 735,260,946 19,901,956,980.00 154,710,000,000 27.1800 27.2200 27.3200 26.8600 5,682,992,500 −0.16
11/12/2025 462,244,627 12,495,378,720.00 153,010,000,000 27.0400 27.0700 27.1600 26.9000 5,651,992,500 −0.12
11/11/2025 575,306,746 15,399,258,560.00 150,800,000,000 26.8400 26.8400 26.9400 26.6600 5,617,992,500 −0.01
11/10/2025 331,961,140 8,835,370,880.00 150,510,000,000 26.8000 26.8000 26.8400 26.4400 5,616,992,500 0.01
11/7/2025 386,895,252 10,208,929,210.00 147,670,000,000 26.3400 26.3800 26.5000 26.3000 5,597,992,500 −0.15
11/6/2025 554,722,356 14,657,484,840.00 149,100,000,000 26.6000 26.6300 26.6400 26.1800 5,599,992,500 −0.10
11/5/2025 537,528,465 13,901,467,440.00 146,200,000,000 26.0400 26.0600 26.1000 25.6000 5,610,992,500 −0.07

Historical Statistics

Volume Statistics

volume_stats_df <- data.frame(
  Metric = c("Median", "Mean", "Minimum", "Maximum", "Total Days"),
  Value = c(
    stats$volume_median,
    stats$volume_mean,
    stats$volume_min,
    stats$volume_max,
    stats$total_days
  )
)

volume_stats_df %>%
  gt() %>%
  fmt_number(columns = Value, rows = 1:4, decimals = 0, use_seps = TRUE) %>%
  fmt_number(columns = Value, rows = 5, decimals = 0, use_seps = FALSE) %>%
  cols_label(
    Metric = "Metric",
    Value = "Value"
  ) %>%
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_column_labels()
  ) %>%
  tab_style(
    style = cell_text(align = "right"),
    locations = cells_body(columns = Value)
  ) %>%
  opt_table_font(font = "Arial") %>%
  tab_options(
    table.width = pct(50),
    column_labels.background.color = "#f8f9fa",
    table_body.hlines.color = "#e9ecef"
  )
Metric Value
Median 496,805,896
Mean 542,322,622
Minimum 164,529,440
Maximum 1,317,057,344
Total Days 62

Turnover Statistics

turnover_stats_df <- data.frame(
  Metric = c("Median", "Mean", "Minimum", "Maximum"),
  Value = c(
    stats$turnover_median,
    stats$turnover_mean,
    stats$turnover_min,
    stats$turnover_max
  )
)

turnover_stats_df %>%
  gt() %>%
  fmt_number(columns = Value, decimals = 2, use_seps = TRUE) %>%
  cols_label(
    Metric = "Metric",
    Value = "Value"
  ) %>%
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_column_labels()
  ) %>%
  tab_style(
    style = cell_text(align = "right"),
    locations = cells_body(columns = Value)
  ) %>%
  opt_table_font(font = "Arial") %>%
  tab_options(
    table.width = pct(50),
    column_labels.background.color = "#f8f9fa",
    table_body.hlines.color = "#e9ecef"
  )
Metric Value
Median 12,999,645,215.00
Mean 14,382,061,931.32
Minimum 4,562,791,823.00
Maximum 34,709,271,360.00

Report generated: 2025-12-17 08:41:49.177438