Skip to content

Commit

Permalink
Deriv.com - 筛选日内高频量化交易统计模型 校阅(第IV部)
Browse files Browse the repository at this point in the history
Deriv.com - 筛选日内高频量化交易统计模型 校阅(第IV部)

🚩🦔量化对冲,中科红旗;一带一路,一统天下:频率=1

**大秦赋 (Chinese Emperor)**<br>
春秋战国《*礼记•经解*》<br>
孔子曰:『君子慎始,差若毫厘,缪以千里。』

> <span style='color:#FFEBCD; background-color:#D2B48C;'>**《礼记·经解》孔子曰:**</span><span style='color:#A9A9A9'; background-color:#696969;'>*「君子慎始。差若毫厘,谬以千里。」*</span>[^1]

*引用:[「快懂百科」《礼记•经解》](https://www.baike.com/wikiid/2225522569881832051?view_id=2tt3iw3blkq000)和[第一范文网:差之毫厘,谬以千里的故事](https://www.diyifanwen.com/chengyu/liuziyishangchengyugushi/2010051523105152347092749890.htm)和[「百度百科」春秋时期孔子作品《礼记•经解》](https://baike.baidu.com/item/%E7%A4%BC%E8%AE%B0%C2%B7%E7%BB%8F%E8%A7%A3/2523092)和[「當代中國」差之毫釐 謬以千里](https://www.ourchinastory.com/zh/2962/%E5%B7%AE%E4%B9%8B%E6%AF%AB%E9%87%90%20%E8%AC%AC%E4%BB%A5%E5%8D%83%E9%87%8C)*

[^1]: [HTML Color Codes](https://html-color.codes)
  • Loading branch information
englianhu committed Dec 31, 2022
1 parent 2f2e9e0 commit 1b9379a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 17 deletions.
15 changes: 12 additions & 3 deletions binary-Q1Inter-HFT-RV4.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ library('conflicted', warn.conflicts = FALSE)
conflict_prefer('nth', 'tidyft', quiet = TRUE)
conflict_prefer('fill', 'tidyft', quiet = TRUE)
conflict_prefer('nest', 'tidyft', quiet = TRUE)
conflict_prefer('unnest', 'tidyft', quiet = TRUE)
conflict_prefer('cummean', 'tidyft', quiet = TRUE)
conflict_prefer('group_by', 'tidyft', quiet = TRUE)
Expand All @@ -372,8 +373,13 @@ conflict_prefer('summarise', 'tidyft', quiet = TRUE)
conflict_prefer('separate', 'tidyft', quiet = TRUE)
conflict_prefer('lead', 'tidyft', quiet = TRUE)
conflict_prefer('lag', 'tidyft', quiet = TRUE)
conflict_prefer('unite', 'tidyft', quiet = TRUE)
conflict_prefer('left_join', 'tidyft', quiet = TRUE)
conflict_prefer('right_join', 'tidyft', quiet = TRUE)
conflict_prefer('inner_join', 'tidyft', quiet = TRUE)
conflict_prefer('full_join', 'tidyft', quiet = TRUE)
conflict_prefer('anti_join', 'tidyft', quiet = TRUE)
conflict_prefer('semi_join', 'tidyft', quiet = TRUE)
conflict_prefer('select_dt', 'tidyft', quiet = TRUE)
conflict_prefer('transpose', 'tidyft', quiet = TRUE)
conflict_prefer('setDT', 'tidyft', quiet = TRUE)
Expand Down Expand Up @@ -643,7 +649,10 @@ source('函数/日内高频季节性自回归.R')
.季节性规律参数 <- permutations(6, 3, 0:5, repeats.allowed = TRUE) %>%
as.data.table
.季节性规律参数 <- setnames(.季节性规律参数, old = c('V1', 'V2', 'V3'),
new = c('季节性自回归阶数', '季节性差分阶数', '季节性滑均阶数'))[季节性差分阶数 <= 2]
new = c('季节性自回归阶数', '季节性差分阶数',
'季节性滑均阶数'))[季节性差分阶数 <= 2]
# 循环周期 <- 数据量/频率
# seasonal <- list(order = .季节性规律参数, period = 循环周期)
```

**0.02小时一周期(1分钟循环1200次)**
Expand Down Expand Up @@ -675,7 +684,7 @@ llply(.模型选项, function(模型) {
允许截距与否 = '勾', 允许包含均值与否 = '勾',
博克斯考克斯变换 = NULL,
偏差调整与否 = '叉', 多管齐下与否 = '叉', 核心量 = 2,
包含均值与否 = '勾')
包含均值与否 = '勾', 包含截距与否 = '勾')
})
})
})
Expand All @@ -693,7 +702,7 @@ llply(.模型选项, function(模型) {
.时序规律 = unlist(.时序规律[迭数1,]),
.季节性规律参数 = unlist(.季节性规律参数[迭数2,]), 趋势 = NULL,
允许包含均值与否 = '勾', 允许截距与否 = '叉',
包含均值与否 = '勾', 包含截距 = '勾',
包含均值与否 = '勾', 包含截距与否 = '勾',
#包含常数与否 = 包含常数与否,
#统计模型 = NULL, 博克斯考克斯变换 = 博克斯考克斯变换, x = y,
偏差调整与否 = '叉', 计策谋略 = c('CSS-ML', 'ML', 'CSS'))
Expand Down
13 changes: 8 additions & 5 deletions 函数/日内高频季节性自回归.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
.差分阶数 = .差分阶数, .季节性差分阶数 = .季节性差分阶数,
季节性与否 = 季节性与否, .时序规律 = .时序规律,
.季节性规律参数 = .季节性规律参数, 静态与否 = 静态与否,
记载自回归与否 = 记载自回归与否, 信息量准则 = 信息量准则,
记载自回归与否 = 记载自回归与否, 信息量准则 = c('aicc', 'aic', 'bic'),
逐步精化与否 = 逐步精化与否, 逐步精化量 = 逐步精化量,
#近似值与否 = (length(x) > 150 | frequency(x) > 12),
省略 = 省略, #x = y, method = NULL,
Expand All @@ -45,7 +45,7 @@
博克斯考克斯变换 = 博克斯考克斯变换, 偏差调整与否 = 偏差调整与否,
多管齐下与否 = 多管齐下与否, 核心量 = 核心量, 包含均值与否 = 包含均值与否,
#趋势 = NULL, 包含常数与否,
包含截距 = 包含截距, 统计模型 = 统计模型,
包含截距与否 = 包含截距与否, 统计模型 = 统计模型,
#博克斯考克斯变换 = 统计模型$lambda, x = y, 偏差调整与否 = FALSE,
计策谋略 = 计策谋略, 列印 = '') {

Expand Down Expand Up @@ -80,6 +80,7 @@
if (!偏差调整与否 %in% c('', '')) stop("偏差调整与否 = '勾' 或 '叉'")
if (!多管齐下与否 %in% c('', '')) stop("多管齐下与否 = '勾' 或 '叉'")
if (!包含均值与否 %in% c('', '')) stop("包含均值与否 = '勾' 或 '叉'")
if (!包含截距与否 %in% c('', '')) stop("包含截距与否 = '勾' 或 '叉'")

季节性与否 <- ifelse2(季节性与否 == '', TRUE, FALSE)
静态与否 <- ifelse2(静态与否 == '', TRUE, FALSE)
Expand All @@ -90,6 +91,7 @@
偏差调整与否 <- ifelse2(偏差调整与否 == '', TRUE, FALSE)
多管齐下与否 <- ifelse2(多管齐下与否 == '', TRUE, FALSE)
包含均值与否 <- ifelse2(包含均值与否 == '', TRUE, FALSE)
包含截距与否 <- ifelse2(包含截距与否 == '', TRUE, FALSE)

## 假设数据量 = 1200分钟
## 频率 = 1200
Expand Down Expand Up @@ -125,7 +127,7 @@
rownames(季回归) <- 培训样本$年月日时分

if (!.模型选项 %in% .模型选项表)
stop("错误信息:.模型选项表 = '自动化' 或 '自回归滑均'")
stop("错误信息:.模型选项 = '自动化' 或 '自回归滑均'")

if (.模型选项 == '自动化') {
半成品 <- tryCatch({
Expand Down Expand Up @@ -160,9 +162,10 @@
# 半成品 <- 培训样本[, .(年月日时分, 闭市价)] %>% as.xts
半成品 <- tryCatch({
Arima(
季回归, order = .时序规律, seasonal = .季节性规律参数,
季回归, order = .时序规律,
seasonal = list(order = .季节性规律参数, period = 循环周期),
xreg = 趋势, include.mean = 包含均值与否,
include.drift = 包含截距, #include.constant = 包含常数,
include.drift = 包含截距与否, #include.constant = 包含常数,
#model = 统计模型, lambda = 博克斯考克斯变换, x = y,
biasadj = 偏差调整与否, method = 计策谋略)
}, 错误信息 = function(错误信息参数) NULL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ library('conflicted', warn.conflicts = FALSE)

conflict_prefer('nth', 'tidyft', quiet = TRUE)
conflict_prefer('fill', 'tidyft', quiet = TRUE)
conflict_prefer('nest', 'tidyft', quiet = TRUE)
conflict_prefer('unnest', 'tidyft', quiet = TRUE)
conflict_prefer('cummean', 'tidyft', quiet = TRUE)
conflict_prefer('group_by', 'tidyft', quiet = TRUE)
Expand All @@ -149,8 +150,13 @@ conflict_prefer('summarise', 'tidyft', quiet = TRUE)
conflict_prefer('separate', 'tidyft', quiet = TRUE)
conflict_prefer('lead', 'tidyft', quiet = TRUE)
conflict_prefer('lag', 'tidyft', quiet = TRUE)
conflict_prefer('unite', 'tidyft', quiet = TRUE)
conflict_prefer('left_join', 'tidyft', quiet = TRUE)
conflict_prefer('right_join', 'tidyft', quiet = TRUE)
conflict_prefer('inner_join', 'tidyft', quiet = TRUE)
conflict_prefer('full_join', 'tidyft', quiet = TRUE)
conflict_prefer('anti_join', 'tidyft', quiet = TRUE)
conflict_prefer('semi_join', 'tidyft', quiet = TRUE)
conflict_prefer('select_dt', 'tidyft', quiet = TRUE)
conflict_prefer('transpose', 'tidyft', quiet = TRUE)
conflict_prefer('setDT', 'tidyft', quiet = TRUE)
Expand Down Expand Up @@ -279,6 +285,7 @@ source('函数/日内高频季节性自回归.R')
## https://stackoverflow.com/questions/37400062/seasonality-in-auto-arima-from-forecast-package
#近似值与否 <- c(TRUE, FALSE)
#逐步精化与否 <- c(TRUE, FALSE)
信息量准则 = c('aicc', 'aic', 'bic')

## .时序规律 <- c(0, 0, 0)
.时序规律 <- permutations(6, 3, 0:5, repeats.allowed = TRUE) %>%
Expand Down Expand Up @@ -355,7 +362,9 @@ source('函数/日内高频季节性自回归.R')

## ======== 居住在英国布里斯托尔港口,修读气象学系的英国基督洋妞儿芈拉不可以死 ========
文件名 = '日内高频季节性自回归'
.模型选项 = .模型选项[1]
# .模型选项 = c('自动化', '自回归滑均')
# .模型选项 = .模型选项[1]
.模型选项 = .模型选项[2]
列印 = ''
样本 = 样本2018半年
季节性与否 = ''
Expand All @@ -367,6 +376,7 @@ source('函数/日内高频季节性自回归.R')
偏差调整与否 = ''
多管齐下与否 = ''
包含均值与否 = ''
包含截距与否 = ''
迭数1 <- 迭代基准[1]
省略 = NULL
计策谋略 = NULL
Expand Down Expand Up @@ -404,6 +414,7 @@ if (!允许包含均值与否 %in% c('勾', '叉')) stop("允许包含均值与
if (!偏差调整与否 %in% c('', '')) stop("偏差调整与否 = '勾' 或 '叉'")
if (!多管齐下与否 %in% c('', '')) stop("多管齐下与否 = '勾' 或 '叉'")
if (!包含均值与否 %in% c('', '')) stop("包含均值与否 = '勾' 或 '叉'")
if (!包含截距与否 %in% c('', '')) stop("包含截距与否 = '勾' 或 '叉'")

季节性与否 <- ifelse2(季节性与否 == '', TRUE, FALSE)
静态与否 <- ifelse2(静态与否 == '', TRUE, FALSE)
Expand All @@ -414,6 +425,7 @@ if (!包含均值与否 %in% c('勾', '叉')) stop("包含均值与否 = '勾'
偏差调整与否 <- ifelse2(偏差调整与否 == '', TRUE, FALSE)
多管齐下与否 <- ifelse2(多管齐下与否 == '', TRUE, FALSE)
包含均值与否 <- ifelse2(包含均值与否 == '', TRUE, FALSE)
包含截距与否 <- ifelse2(包含截距与否 == '', TRUE, FALSE)

## 假设数据量 = 1200分钟
## 频率 = 1200
Expand All @@ -422,6 +434,7 @@ if (!包含均值与否 %in% c('勾', '叉')) stop("包含均值与否 = '勾'
循环周期 <- 数据量/频率
迭代基准 <- 样本[日期 %chin% 时间索引]$序列


迭数列表 <- (迭数1 - 数据量):(迭数1 - 1)
培训样本 <- 样本[序列 %chin% 迭数列表]

Expand All @@ -438,21 +451,21 @@ if (列印 == '勾') {
print(预测样本)
}

# 半成品 <- 培训样本[, .(年月日时分, 闭市价)] %>%
# 季回归 <- 培训样本[, .(年月日时分, 闭市价)] %>%
# as.matrix %>%
# tk_ts(frequency = 频率)
半成品 <- 培训样本$闭市价 |>
季回归 <- 培训样本$闭市价 |>
{\(.) matrix(., dimnames = list(培训样本$年月日时分, '闭市价'))}() |>
{\(.) tk_ts(., frequency = 频率)}()
rownames(半成品) <- 培训样本$年月日时分
rownames(季回归) <- 培训样本$年月日时分

if (!.模型选项 %in% .模型选项表)
stop("错误信息:.模型选项表 = '自动化' 或 '自回归滑均'")
stop("错误信息:.模型选项 = '自动化' 或 '自回归滑均'")

if (.模型选项 == '自动化') {
半成品 <- tryCatch({
auto.arima(
半成品, d = .差分阶数, D = .季节性差分阶数, seasonal = 季节性与否,
季回归, d = .差分阶数, D = .季节性差分阶数, seasonal = 季节性与否,
stationary = 静态与否, trace = 记载自回归与否,
ic = 信息量准则, stepwise = 逐步精化与否, nmodels = 逐步精化量,
#approximation = 近似值与否,
Expand All @@ -464,13 +477,47 @@ if (.模型选项 == '自动化') {
parallel = 多管齐下与否, num.cores = 核心量)
}, 错误信息 = function(错误信息参数) NULL)

半成品 <- 预测样本 |>
{\(.) mutate_dt(., 市场价 = 闭市价, 预测价 = coef(半成品))}() |>
{\(.) select_dt(., 年月日时分, 市场价, 预测价)}()

模型名称 <- paste0(
.模型选项, '_差分阶数', .差分阶数, '_季节性差分阶数',
.季节性差分阶数, '_季节性与否=', 季节性与否, '_数据量', 数据量,
.模型选项, '_差分阶数_', .差分阶数, '_季节性差分阶数_', .季节性差分阶数,
'_季节性与否_', 季节性与否, '_静态与否_', 静态与否,
'_记载自回归与否_', 记载自回归与否, '_信息量准则_', 信息量准则,
'_逐步精化与否_', 逐步精化与否, '_计策谋略_', 计策谋略,
'_数据量', 数据量,
'_频率', 频率, '_预测时间单位', 预测时间单位,
'_', 培训样本$年月日时分[迭数1], 'CST.rds')
'_', 预测样本$年月日时分, 'CST.rds')
}

if (.模型选项 == '自回归滑均') {
# 半成品 <- 培训样本[, .(年月日时分, 闭市价)] %>% as.xts
半成品 <- tryCatch({
Arima(
季回归, order = .时序规律, seasonal = .季节性规律参数,
xreg = 趋势, include.mean = 包含均值与否,
include.drift = 包含截距与否, #include.constant = 包含常数,
#model = 统计模型, lambda = 博克斯考克斯变换, x = y,
biasadj = 偏差调整与否, method = 计策谋略)
}, 错误信息 = function(错误信息参数) NULL)

模型名称 <- paste0(
'自回归滑动平均',
paste(.时序规律, collapse = ''), '_季节性',
paste(.季节性规律参数, collapse = ''), '_', 数据量, '_',
预测时间单位, '_', 预测样本$年月日时分, 'CST.rds')
}

if (列印 == '') {
cat('\n------ 秦孝公🌟陈祯禄,商鞅变法,铲除巫裔,推翻马来回教宦官巫师政权,千古一帝。------\n')
cat('预测样本[', '数据量:', 数据量, '频率:', 频率, '-',
'预测数据序列号:', 迭数1, ']\n')
print(半成品)
}

文件名 <- paste0('季回归_', .模型选项, '_数据量', 数据量,
'_频率', 频率, '_', 半成品$年月日时分, 'CST.rds')


source('函数/日内高频季节性自回归.R')
Expand Down Expand Up @@ -502,6 +549,25 @@ source('函数/日内高频季节性自回归.R')
{\(.) mutate_dt(., 市场价 = 闭市价, 预测价 = coef(半成品))}() |>
{\(.) select_dt(., 年月日时分, 市场价, 预测价)}()

半成品 <- tryCatch({
Arima(
季回归, order = unlist(.时序规律[1,]),
seasonal = list(order = unlist(.季节性规律参数[1,]), period = 循环周期),
xreg = 趋势, include.mean = 包含均值与否,
include.drift = 包含截距与否, #include.constant = 包含常数,
#model = 统计模型, lambda = 博克斯考克斯变换, x = y,
biasadj = 偏差调整与否, method = 计策谋略)
}, 错误信息 = function(错误信息参数) NULL)













0 comments on commit 1b9379a

Please sign in to comment.