• 本策略在第一期的基础上做了扩展,分析各个商
品期货品种之间的日内行情相关性。
• 商品期货的合约均选用的是连续合约的数据。
• 所分析用的行情数据为1分钟K线数据。
• 行情价格用的是K线数据的Close价格。
本示例的网址为
http://180.153.251.80:16889/sample-apps/cor02/
需要加载的包
library(shiny)
library(quantmod)
library(DTSR)
Shiny:Web Application Framework
Quantmod:Quantitative Financial Modelling Framework
DTSR:Get DTS interface through R
- # This is the server logic for a Shiny web application.
- # You can find out more about building applications with Shiny here:
- #
- # http://www.rstudio.com/shiny/
- #
- library(shiny)
- library(quantmod)
- library(DTSR)
- Getk1mc <- function(symbol,beginDate="20140101", endDate=Sys.Date()) {
- d <- NULL
- if (mode(symbol) == "character" & length(symbol) == 1) {
- beginDate <- gsub("-","",beginDate)
- endDate <- gsub("-","",endDate)
- field.set <- "DateTime,ClosePrice"
- rs <- dts.GetKLine("k1m", symbol, beginDate, endDate, field.set)
- if (rs$ErrorCode == 0){
- d <- rs$Data
- d <- xts(d$ClosePrice,as.POSIXlt(strptime(d$DateTime,"%Y%m%d%H%M%S")),attr=symbol);
- }
- }
- print(c("Getk1mc",Sys.time()))
- return(d)
- }
- issue.name <- c("沪银","沪铝","沪金","沪铜","燃油",
- "沪铅","螺纹","橡胶","线材","沪锌",
- "沥青","热轧卷板","棕榈油","玉米","焦炭",
- "聚氯乙烯","聚乙烯","豆二","豆粕","豆一",
- "豆油","焦煤","铁矿石","鲜鸡蛋","纤维板",
- "胶合板","聚丙烯","早籼稻","郑棉","普麦",
- "强麦","白糖","甲醇","菜油","PTA",
- "玻璃","油菜籽","菜籽粕","动力煤","粳稻"
- )
- issue.code <- c("SCag0001.cmdty","SCal0001.cmdty","SCau0001.cmdty","SCcu0001.cmdty","SCfu0001.cmdty",
- "SCpb0001.cmdty","SCrb0001.cmdty","SCru0001.cmdty","SCwr0001.cmdty","SCzn0001.cmdty",
- "SCbu0001.cmdty","SChc0001.cmdty","DCp0001.cmdty","DCc0001.cmdty","DCj0001.cmdty",
- "DCv0001.cmdty","DCl0001.cmdty","DCb0001.cmdty","DCm0001.cmdty","DCa0001.cmdty",
- "DCy0001.cmdty","DCjm0001.cmdty","DCi0001.cmdty","DCjd0001.cmdty","DCfb0001.cmdty",
- "DCbb0001.cmdty","DCpp0001.cmdty","ZCRI0001.cmdty","ZCCF0001.cmdty","ZCPM0001.cmdty",
- "ZCWH0001.cmdty","ZCSR0001.cmdty","ZCME0001.cmdty","ZCOI0001.cmdty","ZCTA0001.cmdty",
- "ZCFG0001.cmdty","ZCRS0001.cmdty","ZCRM0001.cmdty","ZCTC0001.cmdty","ZCJR0001.cmdty"
- )
- shinyServer(function(input, output,session) {
- observe({
- names(issue.code) <- issue.name;
- updateSelectInput(session, "issue",choices=issue.code)
- })
-
- result<-reactive({
- print("start query")
- print(input$issue)
- print(input$range)
- if (length(input$issue) < 2){
- return();
- }
-
- stime <- input$range[1];
- etime <- input$range[2];
- mcs <- lapply(input$issue,Getk1mc,stime,etime);
- merged <- xts();
- for(i in 1:length(mcs)) {
- if (length(mcs[[i]]) > 0){
- merged <- merge(merged,mcs[[i]],all = T);
- names(merged) <- c(names(merged)[-length(names(merged))],
- xtsAttributes(mcs[[i]])$attr);
- }
- }
- names(merged) <- issue.name[which(issue.code %in% names(merged))];
- print(c("merged",Sys.time()))
- merged <- na.fill(merged, "extend")
- mx <- as.matrix(merged);
- cor(mx)
- })
-
- output$result<-renderTable({
- d<-result()
- print(c("cor",Sys.time()))
- if (mode(d) == "numeric" & length(d) > 0) {
- d
- }
- else{
- matrix(NA)
- }
- })
-
- })
- # This is the user-interface definition of a Shiny web application.
- # You can find out more about building applications with Shiny here:
- #
- # http://www.rstudio.com/shiny/
- #
- library(shiny)
- shinyUI(pageWithSidebar(
-
- # Application title
- headerPanel("商品期货连续合约日内相关性分析"),
-
- # Sidebar with a slider input for number of observations
- sidebarPanel(
- selectInput("issue","商品期货",c(),multiple = TRUE),
- dateRangeInput("range","时间段",start = Sys.Date()-7),
- submitButton("计算")
- ),
-
- # Show a plot of the generated distribution
- mainPanel(
- textOutput("hint"),
- tableOutput("result")
- )
- ))