diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b6a065 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata diff --git a/environment-testing-r.Rproj b/environment-testing-r.Rproj new file mode 100644 index 0000000..8e3c2eb --- /dev/null +++ b/environment-testing-r.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX diff --git a/environment_installer_1.R b/environment_installer_1.R new file mode 100644 index 0000000..c31ca3b --- /dev/null +++ b/environment_installer_1.R @@ -0,0 +1,45 @@ +## === install required packages === +pkg_list <- c("devtools","xts","magrittr", "dplyr","tidyr","ggplot2","httr", "rvest", "XML", "stringr", "data.table", + "jsonlite", "RSQLite","RMySQL", "readr", "Rcpp", "foreach", + "PerformanceAnalytics","quantmod","blotter","quantstrat","QuantitativeBacktestingTools", + "IKTrading", "lattice", "doSNOW", "reshape", "beepr","partykit","ada","spls","e1071","plyr","randomForest","fpc") + +pkg_new <- pkg_list[!(pkg_list %in% installed.packages()[,"Package"])] + +for (pkg in pkg_new){ + if (pkg == "quantmod"){ + devtools::install_github("joshuaulrich/quantmod") + } + else if (pkg == "quantstrat"){ + # https://r-forge.r-project.org/R/?group_id=316 + # install.packages("quantstrat", repos="http://R-Forge.R-project.org") + devtools::install_github("braverock/quantstrat") + } + else if (pkg == "blotter"){ + # install.packages("blotter", repos="http://R-Forge.R-project.org") + devtools::install_github("braverock/blotter") + } + else if (pkg == "xts"){ + devtools::install_github("joshuaulrich/xts") + } + else if (pkg == "IKTrading"){ + devtools::install_github("IlyaKipnis/IKTrading") + } + else if (pkg == "data.table"){ + install.packages("data.table", type = "source", + repos = "https://Rdatatable.github.io/data.table") + } + else if (pkg == "xmlview"){ + devtools::install_github("hrbrmstr/xmlview") + } + else if (pkg == "QuantitativeBacktestingTools"){ + devtools::install_github("c3h3/QuantitativeBacktestingTools") + } + else{ + install.packages(pkg) + } +} + +rm(pkg_new, pkg_list) + + diff --git a/environment_installer_2.R b/environment_installer_2.R new file mode 100644 index 0000000..68d7a40 --- /dev/null +++ b/environment_installer_2.R @@ -0,0 +1,6 @@ +pkg_list <- c("magrittr", "dplyr", "tidyr", "data.table", "ggplot2", + "readr", "lubridate", "extrafont", "ggrepel", + "rmarkdown", "shiny", "knitr", "prettydoc", "plotly", "DT","mclust","fpc") +pkg_new <- pkg_list[!(pkg_list %in% installed.packages()[,"Package"])] +if(length(pkg_new)) install.packages(pkg_new) +rm(pkg_new, pkg_list) diff --git a/getStockMajorData.R b/getStockMajorData.R new file mode 100644 index 0000000..5476783 --- /dev/null +++ b/getStockMajorData.R @@ -0,0 +1,54 @@ +library(httr) +library(XML) +# library(stringr) + +getStockMajorData = function(stockId) { + # Connector + # url = "http://tw.stock.yahoo.com/d/s/major_2451.html" + # stockId = "2451" + url = sprintf("http://tw.stock.yahoo.com/d/s/major_%s.html",as.character(stockId)) + res <- GET(url) + content(res, "text", encoding = "big5") + html <- htmlParse(content(res, "text", encoding = "big5"), encoding = "utf8") + + # Parser + tables <- readHTMLTable(html) + + # figure out filtering condition ... + filter_condition <- (sapply(tables,NCOL)==8)&(sapply(tables,NROW) <= 15) + data_table <- tables[filter_condition][[1]] + + # extract date info + DataString_source = content(res, "text", encoding = "big5") + # DataString_regexp <- "([[:digit:]]{3}) /([[:digit:]]{2}) /([[:digit:]]{2})" + # DataString_Location = str_locate_all(DataString_source,DataString_regexp)[[1]] + # DataString = str_sub(DataString_source, DataString_Location[1],DataString_Location[2]) + DateString = regmatches(DataString_source,regexpr("([0-9]+) /([0-9]+) /([0-9]+)",DataString_source)) + DateVector = as.numeric(unlist(strsplit(DateString,split = " /"))) + DateVector[1] = DateVector[1] + 1911 + DataDate = as.Date(paste(DateVector,collapse = "-")) + + + # change the data type of each column + Data_Table = data_table + Data_Table[,1] = as.factor(Data_Table[,1]) + Data_Table[,2] = as.integer(as.character(Data_Table[,2])) + Data_Table[,3] = as.integer(as.character(Data_Table[,3])) + Data_Table[,4] = as.integer(as.character(Data_Table[,4])) + Data_Table[,5] = as.factor(Data_Table[,5]) + Data_Table[,6] = as.integer(as.character(Data_Table[,6])) + Data_Table[,7] = as.integer(as.character(Data_Table[,7])) + Data_Table[,8] = as.integer(as.character(Data_Table[,8])) + + # Convert data to table in db + + names(Data_Table)[c(1,5)] <- "Broker" + Data_Table <- rbind(Data_Table[,1:3],Data_Table[,5:7]) + + names(Data_Table) + names(Data_Table)[2:3]<-c("Buy","Sell") + + Data_Table = data.frame(StockId=stockId,Date=DataDate,Data_Table) + + return(Data_Table) +} \ No newline at end of file diff --git a/test-exe-getStockMajorData.R b/test-exe-getStockMajorData.R new file mode 100644 index 0000000..3b6a762 --- /dev/null +++ b/test-exe-getStockMajorData.R @@ -0,0 +1,71 @@ +USER_LIB_PATH = Sys.getenv("USER_LIB_PATH", "C:/Users/chiachi/Documents/R/win-library/3.4") +.libPaths(USER_LIB_PATH) + +library(httr) +library(XML) +# library(stringr) + + +getStockMajorData = function(stockId) { + # Connector + # url = "http://tw.stock.yahoo.com/d/s/major_2451.html" + # stockId = "2451" + url = sprintf("http://tw.stock.yahoo.com/d/s/major_%s.html",as.character(stockId)) + res <- GET(url) + content(res, "text", encoding = "big5") + html <- htmlParse(content(res, "text", encoding = "big5"), encoding = "utf8") + + # Parser + tables <- readHTMLTable(html) + + # figure out filtering condition ... + filter_condition <- (sapply(tables,NCOL)==8)&(sapply(tables,NROW) <= 15) + data_table <- tables[filter_condition][[1]] + + # extract date info + DataString_source = content(res, "text", encoding = "big5") + # DataString_regexp <- "([[:digit:]]{3}) /([[:digit:]]{2}) /([[:digit:]]{2})" + # DataString_Location = str_locate_all(DataString_source,DataString_regexp)[[1]] + # DataString = str_sub(DataString_source, DataString_Location[1],DataString_Location[2]) + DateString = regmatches(DataString_source,regexpr("([0-9]+) /([0-9]+) /([0-9]+)",DataString_source)) + DateVector = as.numeric(unlist(strsplit(DateString,split = " /"))) + DateVector[1] = DateVector[1] + 1911 + DataDate = as.Date(paste(DateVector,collapse = "-")) + + + # change the data type of each column + Data_Table = data_table + Data_Table[,1] = as.factor(Data_Table[,1]) + Data_Table[,2] = as.integer(as.character(Data_Table[,2])) + Data_Table[,3] = as.integer(as.character(Data_Table[,3])) + Data_Table[,4] = as.integer(as.character(Data_Table[,4])) + Data_Table[,5] = as.factor(Data_Table[,5]) + Data_Table[,6] = as.integer(as.character(Data_Table[,6])) + Data_Table[,7] = as.integer(as.character(Data_Table[,7])) + Data_Table[,8] = as.integer(as.character(Data_Table[,8])) + + # Convert data to table in db + + names(Data_Table)[c(1,5)] <- "Broker" + Data_Table <- rbind(Data_Table[,1:3],Data_Table[,5:7]) + + names(Data_Table) + names(Data_Table)[2:3]<-c("Buy","Sell") + + Data_Table = data.frame(StockId=stockId,Date=DataDate,Data_Table) + + return(Data_Table) +} + + +library(readr) +STOCK_ID = Sys.getenv("STOCK_ID", "2330") +OUTPUT_PATH = Sys.getenv("OUTPUT_PATH", sprintf("%s.csv",STOCK_ID)) + +df = getStockMajorData(STOCK_ID) +write_csv(df,OUTPUT_PATH) + + + + + diff --git a/test-getStockMajorData.R b/test-getStockMajorData.R new file mode 100644 index 0000000..e733673 --- /dev/null +++ b/test-getStockMajorData.R @@ -0,0 +1,2 @@ +source("getStockMajorData.R") +View(getStockMajorData(2330)) diff --git a/test-pttr.R b/test-pttr.R new file mode 100644 index 0000000..8c91a9a --- /dev/null +++ b/test-pttr.R @@ -0,0 +1,5 @@ +devtools::install_github("leoluyi/PTTr") + +library(PTTr) +get_post_content("https://www.ptt.cc/bbs/Gossiping/M.1467117389.A.62D.html") +dt <- get_all_posts("Gossiping", max_post = 10) diff --git a/test-quantmod.R b/test-quantmod.R new file mode 100644 index 0000000..626ba2c --- /dev/null +++ b/test-quantmod.R @@ -0,0 +1,3 @@ +library(quantmod) +getSymbols("^TWII") +chartSeries(TWII) diff --git a/test-rmd.Rmd b/test-rmd.Rmd new file mode 100644 index 0000000..d79a322 --- /dev/null +++ b/test-rmd.Rmd @@ -0,0 +1,31 @@ +--- +title: "test-rmd-pdf" +output: + word_document: default + pdf_document: default + html_document: default +--- + +```{r setup, include=FALSE} +knitr::opts_chunk$set(echo = TRUE) +``` + +## R Markdown + +This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see . + +When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this: + +```{r cars} +summary(cars) +``` + +## Including Plots + +You can also embed plots, for example: + +```{r pressure, echo=FALSE} +plot(pressure) +``` + +Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot. diff --git a/test-rpart.R b/test-rpart.R new file mode 100644 index 0000000..914d32f --- /dev/null +++ b/test-rpart.R @@ -0,0 +1,2 @@ +library(rpart) +example("rpart") diff --git a/test-shiny/app.R b/test-shiny/app.R new file mode 100644 index 0000000..628d1a4 --- /dev/null +++ b/test-shiny/app.R @@ -0,0 +1,50 @@ +# +# This is a Shiny web application. You can run the application by clicking +# the 'Run App' button above. +# +# Find out more about building applications with Shiny here: +# +# http://shiny.rstudio.com/ +# + +library(shiny) + +# Define UI for application that draws a histogram +ui <- fluidPage( + + # Application title + titlePanel("Old Faithful Geyser Data"), + + # Sidebar with a slider input for number of bins + sidebarLayout( + sidebarPanel( + sliderInput("bins", + "Number of bins:", + min = 1, + max = 50, + value = 30) + ), + + # Show a plot of the generated distribution + mainPanel( + plotOutput("distPlot") + ) + ) +) + +# Define server logic required to draw a histogram +server <- function(input, output) { + + output$distPlot <- renderPlot({ + # generate bins based on input$bins from ui.R + x <- faithful[, 2] + bins <- seq(min(x), max(x), length.out = input$bins + 1) + + # draw the histogram with the specified number of bins + hist(x, breaks = bins, col = 'darkgray', border = 'white') + }) +} + +# Run the application +shinyApp(ui = ui, server = server) +