Skip to content

Commit

Permalink
upload testing codes
Browse files Browse the repository at this point in the history
  • Loading branch information
c3h3 committed Aug 30, 2017
0 parents commit faf758c
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
13 changes: 13 additions & 0 deletions environment-testing-r.Rproj
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions environment_installer_1.R
Original file line number Diff line number Diff line change
@@ -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)


6 changes: 6 additions & 0 deletions environment_installer_2.R
Original file line number Diff line number Diff line change
@@ -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)
54 changes: 54 additions & 0 deletions getStockMajorData.R
Original file line number Diff line number Diff line change
@@ -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)
}
71 changes: 71 additions & 0 deletions test-exe-getStockMajorData.R
Original file line number Diff line number Diff line change
@@ -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)





2 changes: 2 additions & 0 deletions test-getStockMajorData.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source("getStockMajorData.R")
View(getStockMajorData(2330))
5 changes: 5 additions & 0 deletions test-pttr.R
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions test-quantmod.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
library(quantmod)
getSymbols("^TWII")
chartSeries(TWII)
31 changes: 31 additions & 0 deletions test-rmd.Rmd
Original file line number Diff line number Diff line change
@@ -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 <http://rmarkdown.rstudio.com>.

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.
2 changes: 2 additions & 0 deletions test-rpart.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
library(rpart)
example("rpart")
50 changes: 50 additions & 0 deletions test-shiny/app.R
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit faf758c

Please sign in to comment.