data-raw/create-rdata2.R

options(scipen = 10)

library(rquants)
library(dplyr)
library(foreach)

rquants_env = new.env()
rquants_env$time <- 0
rquants_env$date <- as.POSIXct(Sys.time())
rquants_env$number_of_symbols <- 0
rquants_env$number_of_prices <- 0
rquants_env$number_of_orders <- 0
rquants_env$last_price_id <- 0
rquants_env$last_order_id <- 0
rquants_env$account <- NULL
rquants_env$orders <- list()
rquants_env$orders_summary <- data.frame(id = 0,
                                         group_id = 0,
                                         time = 0,
                                         date = as.POSIXct("2000/01/01 00:00"),
                                         symbol = "",
                                         order_status = "",
                                         order_type = "",
                                         side = "",
                                         amount = 0,
                                         price_new = 0,
                                         price_close = 0,
                                         price_profit_take = 0,
                                         price_loss_cut = 0,
                                         pl = 0,
                                         pl_point = 0,
                                         pl_income = 0,
                                         pl_income_point = 0,
                                         pl_realized = 0,
                                         pl_realized_point = 0,
                                         pl_unrealized = 0,
                                         pl_unrealized_point = 0,
                                         cost = 0,
                                         cost_point = 0,
                                         margin = 0,
                                         surpuls = 0,
                                         need = 0,
                                         comment = ""
                                         )
rquants_env$prices <- list()
rquants_env$prices_summary <- data.frame(id = 0,
                                         symbol = "",
                                         time = 0,
                                         date = as.POSIXct("2000/01/01 00:00"),
                                         bid = 0,
                                         offer = 0,
                                         stringsAsFactors = F
                                         )
#constant number
rquants_env$symbol_list$usdjpy <- "usdjpy"
rquants_env$symbol_list$eurjpy <- "eurjpy"
rquants_env$symbol_list$eurusd <- "eurusd"
rquants_env$symbol_list$audjpy <- "audjpy"
rquants_env$symbol_list$audusd <- "audusd"
rquants_env$symbol_list$nzdjpy <- "nzdjpy"
rquants_env$symbol_list$nzdusd <- "nzdusd"
rquants_env$symbol_list$cadjpy <- "cadjpy"
rquants_env$symbol_list$gbpjpy <- "gbpjpy"
rquants_env$symbol_list$tryjpy <- "tryjpy"
rquants_env$symbol_list$zarjpy <- "zarjpy"
#symbol infomation
rquants_env$symbol_data$usdjpy$symbol_type <- "fx"
rquants_env$symbol_data$usdjpy$digit <- 2
rquants_env$symbol_data$usdjpy$to_point <- 100
rquants_env$symbol_data$usdjpy$to_price <- 0.01
rquants_env$symbol_data$usdjpy$spread <- 4
rquants_env$symbol_data$usdjpy$cost <- -1
rquants_env$symbol_data$usdjpy$margin_ratio <- 0.01
rquants_env$symbol_data$usdjpy$price_to_jpy <- 1
rquants_env$symbol_data$usdjpy$minimum_amount <- 1000
#eurjpy
rquants_env$symbol_data$eurjpy$symbol_type <- "fx"
rquants_env$symbol_data$eurjpy$digit <- 2
rquants_env$symbol_data$eurjpy$to_point <- 100
rquants_env$symbol_data$eurjpy$to_price <- 0.01
rquants_env$symbol_data$eurjpy$spread <- 5
rquants_env$symbol_data$eurjpy$cost <- -1
rquants_env$symbol_data$eurjpy$margin_ratio <- 0.01
rquants_env$symbol_data$eurjpy$price_to_jpy <- 1
rquants_env$symbol_data$eurjpy$minimum_amount <- 1000
#eurusd
rquants_env$symbol_data$eurusd$symbol_type <- "fx"
rquants_env$symbol_data$eurusd$digit <- 4
rquants_env$symbol_data$eurusd$to_point <- 10000
rquants_env$symbol_data$eurusd$to_price <- 0.0001
rquants_env$symbol_data$eurusd$spread <- 5
rquants_env$symbol_data$eurusd$cost <- -1
rquants_env$symbol_data$eurusd$margin_ratio <- 0.01
rquants_env$symbol_data$eurusd$price_to_jpy <- 100
rquants_env$symbol_data$eurusd$minimum_amount <- 1000
#audjpy
rquants_env$symbol_data$audjpy$symbol_type <- "fx"
rquants_env$symbol_data$audjpy$digit <- 2
rquants_env$symbol_data$audjpy$to_point <- 100
rquants_env$symbol_data$audjpy$to_price <- 0.01
rquants_env$symbol_data$audjpy$spread <- 6
rquants_env$symbol_data$audjpy$cost <- -1
rquants_env$symbol_data$audjpy$margin_ratio <- 0.01
rquants_env$symbol_data$audjpy$price_to_jpy <- 1
rquants_env$symbol_data$audjpy$minimum_amount <- 1000
#audusd
rquants_env$symbol_data$audusd$symbol_type <- "fx"
rquants_env$symbol_data$audusd$digit <- 4
rquants_env$symbol_data$audusd$to_point <- 10000
rquants_env$symbol_data$audusd$to_price <- 0.0001
rquants_env$symbol_data$audusd$spread <- 5
rquants_env$symbol_data$audusd$cost <- -1
rquants_env$symbol_data$audusd$margin_ratio <- 0.01
rquants_env$symbol_data$audusd$price_to_jpy <- 100
rquants_env$symbol_data$audusd$minimum_amount <- 1000
#nzdjpy
rquants_env$symbol_data$nzdjpy$symbol_type <- "fx"
rquants_env$symbol_data$nzdjpy$digit <- 2
rquants_env$symbol_data$nzdjpy$to_point <- 100
rquants_env$symbol_data$nzdjpy$to_price <- 0.01
rquants_env$symbol_data$nzdjpy$spread <- 8
rquants_env$symbol_data$nzdjpy$cost <- -1
rquants_env$symbol_data$nzdjpy$margin_ratio <- 0.01
rquants_env$symbol_data$nzdjpy$price_to_jpy <- 1
rquants_env$symbol_data$nzdjpy$minimum_amount <- 1000
#nzdusd
rquants_env$symbol_data$nzdusd$symbol_type <- "fx"
rquants_env$symbol_data$nzdusd$digit <- 4
rquants_env$symbol_data$nzdusd$to_point <- 10000
rquants_env$symbol_data$nzdusd$to_price <- 0.0001
rquants_env$symbol_data$nzdusd$spread <- 5
rquants_env$symbol_data$nzdusd$cost <- -1
rquants_env$symbol_data$nzdusd$margin_ratio <- 0.01
rquants_env$symbol_data$nzdusd$price_to_jpy <- 100
rquants_env$symbol_data$nzdusd$minimum_amount <- 1000
#cadjpy
rquants_env$symbol_data$cadjpy$symbol_type <- "fx"
rquants_env$symbol_data$cadjpy$digit <- 2
rquants_env$symbol_data$cadjpy$to_point <- 100
rquants_env$symbol_data$cadjpy$to_price <- 0.01
rquants_env$symbol_data$cadjpy$spread <- 6
rquants_env$symbol_data$cadjpy$cost <- -1
rquants_env$symbol_data$cadjpy$margin_ratio <- 0.01
rquants_env$symbol_data$cadjpy$price_to_jpy <- 1
rquants_env$symbol_data$cadjpy$minimum_amount <- 1000
#gbpjpy
rquants_env$symbol_data$gbpjpy$symbol_type <- "fx"
rquants_env$symbol_data$gbpjpy$digit <- 2
rquants_env$symbol_data$gbpjpy$to_point <- 100
rquants_env$symbol_data$gbpjpy$to_price <- 0.01
rquants_env$symbol_data$gbpjpy$spread <- 8
rquants_env$symbol_data$gbpjpy$cost <- -1
rquants_env$symbol_data$gbpjpy$margin_ratio <- 0.01
rquants_env$symbol_data$gbpjpy$price_to_jpy <- 1
rquants_env$symbol_data$gbpjpy$minimum_amount <- 1000
#tryjpy
rquants_env$symbol_data$tryjpy$symbol_type <- "fx"
rquants_env$symbol_data$tryjpy$digit <- 2
rquants_env$symbol_data$tryjpy$to_point <- 100
rquants_env$symbol_data$tryjpy$to_price <- 0.01
rquants_env$symbol_data$tryjpy$spread <- 8
rquants_env$symbol_data$tryjpy$cost <- -1
rquants_env$symbol_data$tryjpy$margin_ratio <- 0.01
rquants_env$symbol_data$tryjpy$price_to_jpy <- 1
rquants_env$symbol_data$tryjpy$minimum_amount <- 1000
#zarjpy
rquants_env$symbol_data$zarjpy$symbol_type <- "fx"
rquants_env$symbol_data$zarjpy$digit <- 2
rquants_env$symbol_data$zarjpy$to_point <- 100
rquants_env$symbol_data$zarjpy$to_price <- 0.01
rquants_env$symbol_data$zarjpy$spread <- 3
rquants_env$symbol_data$zarjpy$cost <- -1
rquants_env$symbol_data$zarjpy$margin_ratio <- 0.01
rquants_env$symbol_data$zarjpy$price_to_jpy <- 1
rquants_env$symbol_data$zarjpy$minimum_amount <- 1000
#period of price
rquants_env$price_period$tick <- "tick"
rquants_env$price_period$minute <- "minute"
rquants_env$price_period$hour <- "hour"
rquants_env$price_period$day <- "day"
rquants_env$price_period$week <- "week"
rquants_env$price_period$month <- "month"
rquants_env$price_period$default <- "-"
#country
rquants_env$currency$jpy <- "jpy"
rquants_env$currency$usd <- "usd"
rquants_env$currency$eur <- "eur"
rquants_env$currency$aud <- "aud"
rquants_env$currency$nzd <- "nzd"
rquants_env$currency$cad <- "cad"
rquants_env$currency$gbp <- "gbp"
rquants_env$currency$zar <- "zar"
rquants_env$currency$try <- "try"
#order satus
rquants_env$order_status$default <- "-"
rquants_env$order_status$working <- "working"
rquants_env$order_status$new <- "new"
rquants_env$order_status$keep <- "keep"
rquants_env$order_status$modify <- "modify"
rquants_env$order_status$cancel <- "cancel"
rquants_env$order_status$close <- "close"
#order kind
rquants_env$order_type$default <- "-"
rquants_env$order_type$market <- "market"
rquants_env$order_type$limit <- "limit"
rquants_env$order_type$stop <- "stop"
rquants_env$order_type$trif_limit <- "trif_limit"
rquants_env$order_type$trif_stop <- "trif_stop"
rquants_env$order_type$trif_lmtimit_trail <- "trif_limit_trail"
#price mode
rquants_env$price_mode$history<- "history"
rquants_env$price_mode$normal_distribution <- "normal_distribution"
rquants_env$price_mode$t_distribution <- "t_distribution"
rquants_env$price_mode$power_law_distribution <- "power_law_distribution"
#buy or sell
rquants_env$side$default <- "-"
rquants_env$side$buy <- "buy"
rquants_env$side$sell <- "sell"
#save enviroment dsata
save(rquants_env, file = "data/rquants_env.RData")


#daily historical data
source_data_dir = "C:\\dev/data"
usdjpy_day <- read.csv(paste(source_data_dir, "usdjpy_day.csv", sep = "/"))
save(usdjpy_day, file = paste("data", "usdjpy_day.RData", sep = "/"))
eurjpy_day <- read.csv(paste(source_data_dir, "eurjpy_day.csv", sep = "/"))
save(eurjpy_day, file = paste("data", "eurjpy_day.RData", sep = "/"))
eurusd_day <- read.csv(paste(source_data_dir, "eurusd_day.csv", sep = "/"))
save(eurusd_day, file = paste("data", "eurusd_day.RData", sep = "/"))
audjpy_day <- read.csv(paste(source_data_dir, "audjpy_day.csv", sep = "/"))
save(audjpy_day, file = paste("data", "audjpy_day.RData", sep = "/"))
audusd_day <- read.csv(paste(source_data_dir, "audusd_day.csv", sep = "/"))
save(audusd_day, file = paste("data", "audusd_day.RData", sep = "/"))
nzdjpy_day <- read.csv(paste(source_data_dir, "nzdjpy_day.csv", sep = "/"))
save(nzdjpy_day, file = paste("data", "nzdjpy_day.RData", sep = "/"))
nzdusd_day <- read.csv(paste(source_data_dir, "nzdusd_day.csv", sep = "/"))
save(nzdusd_day, file = paste("data", "nzdusd_day.RData", sep = "/"))
gbpjpy_day <- read.csv(paste(source_data_dir, "gbpjpy_day.csv", sep = "/"))
save(gbpjpy_day, file = paste("data", "gbpjpy_day.RData", sep = "/"))
cadjpy_day <- read.csv(paste(source_data_dir, "cadjpy_day.csv", sep = "/"))
save(cadjpy_day, file = paste("data", "cadjpy_day.RData", sep = "/"))
tryjpy_day <- read.csv(paste(source_data_dir, "tryjpy_day.csv", sep = "/"))
save(tryjpy_day, file = paste("data", "tryjpy_day.RData", sep = "/"))
zarjpy_day <- read.csv(paste(source_data_dir, "zarjpy_day.csv", sep = "/"))
save(zarjpy_day, file = paste("data", "zarjpy_day.RData", sep = "/"))
#swap csv data
swap <- read.csv(paste(source_data_dir, "SWAP_DATA.csv", sep = "/"))
colnames(swap) <- c("symbol", "date", "days", "swap_buy", "swap_sell")
swap$date <- paste(substring(text = swap$date, first = 1, last = 4),
                   substring(text = swap$date, first = 5, last = 6),
                   substring(text = swap$date, first = 7, last = 8),
                   sep = "/")
swap$date <- as.POSIXct(swap$date)
swap$symbol <- tolower(swap$symbol)
swap$swap_buy <- swap$swap_buy / 10000
swap$swap_sell <- swap$swap_sell / 10000
save(swap, file = paste("data", "swap.RData", sep = "/"))
#summer time calender
years <- 30
calender <- data.frame(date = seq(as.POSIXct("2000/01/01"), len=(365*years), by="1 day"), week_number = 0, day_number = 0, day = 0)
calender$week_number <- as.numeric(format(calender$date, "%U"))
calender$day_number <- as.numeric(format(calender$date, "%w"))
calender$day <- as.numeric(format(calender$date, "%d"))
#get start date of summer time
row_odd <- seq(0, 60, 2)[-1]
date_summer_time_start <-
  (calender %>%
     dplyr::filter(day_number == 0 &
                     week_number %in% c(9, 10, 11) &
                     day <= 14
                   )
   )
date_summer_time_start <- date_summer_time_start[row_odd, "date"]
#get end date of summer time
date_summer_time_end <-
  (calender %>%
     dplyr::filter(day_number == 0 &
                     week_number %in% c(44, 45) &
                     day <= 7
                   )
   )$date
#calender of summer time
summer_time_calender <-
  data.frame(year = format(date_summer_time_start,"%Y"),
             date_start = date_summer_time_start,
             date_end = date_summer_time_end
             )
save(summer_time_calender, file = paste("data",
                                        "summer_time_calender.RData",
                                        sep = "/")
     )
#reset
rm(list = ls())
gc()
gc()


#'stores M2J farm rate into package data folder
#'@param source_dir directory of import data which is json format tick data from M2J
#'@param symbol symbol
#'@param date_start date of start
#'@param date_end date of end
#'@examples generate_price_minute(source_dir = "C:\\dev/data/fx/USDJPY/TICK", symbol = "usdjpy", date_start = "2014/12/01", date_end = "2014/12/31")
#'@encoding SHIFT-JIS
generate_price_minute <- function (source_dir = "C:\\dev/data/fx/USDJPY/TICK",
                                   symbol = "usdjpy",
                                   date_start = "2015/01/01",
                                   date_end = "2015/12/01"
                                   ){
  #this is unit test parameters: source_dir = "C:\\dev/data/fx/USDJPY/TICK"; symbol = "usdjpy"; date_start = "2015/01/01"; date_end = "2016/07/01"
  #install RJSONIO package when it is not installed
  err <- try(library(RJSONIO), silent = FALSE)
  if (class(err) == "try-error"){
    install.packages("RJSONIO")
    library(RJSONIO)
  }
  #create calender data
  date_start <- as.Date(date_start)
  date_end <- as.Date(date_end)
  date_read_start <- as.Date(format(as.Date(date_start), "%Y/%m/%01"))
  date_read_end <- as.Date(format(as.Date(date_end), "%Y/%m/%01"))
  calendar <- seq(date_read_start, date_read_end, by = "month")
  for (i in 1:length(calendar)){
    #file information
    file_name <- paste("firmRate_",
                       toupper(symbol), "_",
                       format(calendar[i], "%Y"), "_",
                       format(calendar[i], "%m"), ".json",
                       sep = ""
                       )
    file_path <- paste(source_dir, "/", file_name, sep = "")
    #get summer time
    filter_year <- filter_year <- format(calendar[i], "%Y")
    summer_time_start <- (
      summer_time_calender %>% dplyr::filter(year == filter_year)
    )$date_start
    summer_time_end <- (
      summer_time_calender %>% dplyr::filter(year == filter_year)
    )$date_end
    if (file_name %in% dir(source_dir)){
      tick <- t(unlist(fromJSON(file_path)))
      tick_n <- length(tick)
      #narrow price data down to necessary
      tick <- data.frame(key = 0,
                       date = as.character(tick[, seq(1, tick_n, 7)]),
                       price = as.numeric(tick[, seq(1, tick_n, 7) + 1]),
                       stringsAsFactors = F)
      #create key for dply
      tick$key <- as.numeric(substr(tick$date, 1, 12))
      #create POSIXct date
      tick$date <-
        as.POSIXct(
          paste(substr(tick$date, 1, 4), "/",
                substr(tick$date, 5, 6), "/",
                substr(tick$date, 7, 8), " ",
                substr(tick$date, 9, 10), ":",
                substr(tick$date, 11, 12), ":",
                substr(tick$date, 13, 14),
                sep = ""
          )
        )
      #create price data
      tick$price <- tick$price * rquants_env$symbol_data[[symbol]]$to_price
      #functions for summarise
      first_number <- function(x = 1:10){ x[1] }
      last_number <- function(x = 1:10){ x[length(x)] }
      #format tick data to ohlc data
      ohlc <- tick %>%
        dplyr::group_by(key) %>%
        dplyr::summarise(date = min(date),
                         open =  first_number(price),
                         high = max(price),
                         low = min(price),
                         close = last_number(price)
                         )
      ohlc$date <- as.POSIXct(format(ohlc$date, "%Y-%m-%d %H:%M:00"))
      #identify getting swap date time
      ohlc$hour <- as.numeric(format(ohlc$date, "%H"))
      ohlc$day <- as.numeric(format(ohlc$date, "%d"))
      #get daily summer time and winter time
      tokyo_open_time_summer <- 
        ohlc %>%
        dplyr::filter(date >= summer_time_start &
                        date <= summer_time_end &
                        hour >= 6 &
                        hour <= 7) %>%
        dplyr::group_by(day) %>%
        dplyr::summarise(date = min(date),
                         key = min(key)
                         )
      tokyo_open_time_winter <- 
        ohlc %>%
        dplyr::filter((date < summer_time_start |
                        date > summer_time_end) &
                        hour >= 7 &
                        hour <= 8) %>%
        dplyr::group_by(day) %>%
        dplyr::summarise(date = min(date),
                         key = min(key)
        )
      tokyo_open_time <-
        rbind(tokyo_open_time_summer, tokyo_open_time_winter) %>%
        dplyr::arrange(key)      
      tokyo_open_time$key_swap <- 1
      tokyo_open_time$day <- NULL
      tokyo_open_time$date <- NULL
      ohlc$day <- NULL
      ohlc$hour <- NULL
      #merge swap data to ohlc
      ohlc <- dplyr::left_join(x = ohlc, y = tokyo_open_time, by = "key")
      ohlc$key_day <- as.POSIXct(format(ohlc$date, "%Y-%m-%d"))
      filter_symbol <- symbol
      filter_date_read_start <- as.POSIXct(calendar[i])
      filter_date_read_end <- as.POSIXct(
        as.Date(format(calendar[i] + 31, "%Y%m01"), "%Y%m%d") - 1
      )
      ohlc_swap <- swap %>% dplyr::filter(symbol == filter_symbol &
                                            date >= filter_date_read_start &
                                            date <= filter_date_read_end)
      ohlc_swap <- data.frame(
        key_day = ohlc_swap$date,
        key_swap = 1,
        swap_buy = ohlc_swap$swap_buy,
        swap_sell = ohlc_swap$swap_sell
        )
      ohlc <- dplyr::left_join(x = ohlc, y = ohlc_swap, by = c("key_day", "key_swap"))
      ohlc <- ohlc[,-which(colnames(ohlc) == c("key_swap", "key_day"))]
      ohlc$swap_buy <- ifelse(is.na(ohlc$swap_buy) == T, 0, ohlc$swap_buy)
      ohlc$swap_sell <- ifelse(is.na(ohlc$swap_sell) == T, 0, ohlc$swap_sell)
      #format tick data to ohlc time series data
      ohlc_time_series_open <- data.frame(ohlc = "open", date = ohlc$date, price = ohlc$open, swap_buy = ohlc$swap_buy, swap_sell = ohlc$swap_sell)
      ohlc_time_series_high <- foreach(key_i = ohlc$key, price_i = ohlc$high, .combine='rbind') %do% {
        data.frame(ohlc = "high", tick %>% dplyr::filter(key == key_i, price == price_i) %>% dplyr::summarise(date = min(date)), price = price_i, swap_buy = 0, swap_sell = 0)
      }
      ohlc_time_series_low <- foreach(key_i = ohlc$key, price_i = ohlc$low, .inorder=FALSE, .combine='rbind') %dopar% {
        data.frame(ohlc = "low", tick %>% dplyr::filter(key == key_i, price == price_i) %>% dplyr::summarise(date = min(date)), price = price_i, swap_buy = 0, swap_sell = 0)
      }
      ohlc_time_series_close <- data.frame(ohlc = "close", date = as.POSIXct(format(ohlc$date,  "%Y-%m-%d %H:%M:59")), price = ohlc$close, swap_buy = 0, swap_sell = 0)
      price <- rbind(ohlc_time_series_open, ohlc_time_series_high, ohlc_time_series_low, ohlc_time_series_close) %>% dplyr::arrange(date)
      #save as CSV format
      write_path_price <- paste("data/", symbol, "_minute_",  format(calendar[i], "%Y%m"), ".RData", sep = "")
      save(price, file = write_path_price)
      #progress
      cat("process: ",
          sprintf("%1.2f", round( (i / length(calendar)) * 100, 2)),
          "%  detail:", write_path_price, " \n", sep = "")
      if (i == length(calendar)){
        cat("finish!! \n")
      }
    } else {
      #the relevant file does not exist
      stop(
        paste("generate_price_minute: symbol[", symbol,
              "]: the file of date[",
              format(as.Date(calendar[i]), "%Y%m"),
              "] does not exist", sep = "")
      )
    }
  }
}


#'stores M2J farm rate into package data folder
#'@param source_dir directory of import data which is json format tick data from M2J
#'@param symbol symbol
#'@param date_start date of start
#'@param date_end date of end
#'@examples generate_price_minute(source_dir = "C:\\dev/data/fx/USDJPY/TICK", symbol = "usdjpy", date_start = "2014/12/01", date_end = "2014/12/31")
#'@encoding SHIFT-JIS
generate_price_hour <- function (symbol = "usdjpy",
                                 date_start = "2015/01/01",
                                 date_end = "2015/12/01"
){
  #this is unit test parameters: symbol = "usdjpy"; date_start = "2015/01/01"; date_end = "2016/07/01"
  #create calender data
  date_start <- as.Date(date_start)
  date_end <- as.Date(date_end)
  date_read_start <- as.Date(format(as.Date(date_start), "%Y/%m/%01"))
  date_read_end <- as.Date(format(as.Date(date_end), "%Y/%m/%01"))
  calendar <- seq(date_read_start, date_read_end, by = "month")
  for (i in 1:length(calendar)){
    #file information
    file_name <- paste(symbol, "_minute_",
                       format(calendar[i], "%Y%m"), ".RData",
                       sep = ""
                       )
    file_path <- paste("data/", file_name, sep = "")
    if (file_name %in% dir("data")){
      #load data
      load(file_path)
      price_source <- price
      #create key for dply
      price_source$key <- format(price_source$date, "%Y%m%d%H")
      #functions for summarise
      first_number <- function(x = 1:10){ x[1] }
      last_number <- function(x = 1:10){ x[length(x)] }
      #format tick data to ohlc data
      ohlc <- price_source %>%
        dplyr::group_by(key) %>%
        dplyr::summarise(date = min(date),
                         open =  first_number(price),
                         high = max(price),
                         low = min(price),
                         close = last_number(price),
                         swap_buy = sum(swap_buy),
                         swap_sell = sum(swap_sell)
                         )
      ohlc$date <- as.POSIXct(format(ohlc$date, "%Y-%m-%d %H:00:00"))
      #format tick data to ohlc time series data
      ohlc_time_series_open <- data.frame(ohlc = "open", date = ohlc$date, price = ohlc$open, swap_buy = ohlc$swap_buy, swap_sell = ohlc$swap_sell)
      ohlc_time_series_high <- foreach(key_i = ohlc$key, price_i = ohlc$high, .combine='rbind') %do% {
        data.frame(ohlc = "high", price_source %>% dplyr::filter(key == key_i, price == price_i) %>% dplyr::summarise(date = min(date)), price = price_i, swap_buy = 0, swap_sell = 0)
      }
      ohlc_time_series_low <- foreach(key_i = ohlc$key, price_i = ohlc$low, .inorder=FALSE, .combine='rbind') %dopar% {
        data.frame(ohlc = "low", price_source %>% dplyr::filter(key == key_i, price == price_i) %>% dplyr::summarise(date = min(date)), price = price_i, swap_buy = 0, swap_sell = 0)
      }
      ohlc_time_series_close <- data.frame(ohlc = "close", date = as.POSIXct(format(ohlc$date,  "%Y-%m-%d %H:59:59")), price = ohlc$close, swap_buy = 0, swap_sell = 0)
      price <- rbind(ohlc_time_series_open, ohlc_time_series_high, ohlc_time_series_low, ohlc_time_series_close) %>% dplyr::arrange(date)
      #save as CSV format
      write_path_price <- paste("data/", symbol, "_hour_",  format(calendar[i], "%Y%m"), ".RData", sep = "")
      save(price, file = write_path_price)
      #progress
      cat("process: ",
          sprintf("%1.2f", round( (i / length(calendar)) * 100, 2)),
          "%  detail:", write_path_price, " \n", sep = "")
      if (i == length(calendar)){
        cat("finish!! \n")
      }
    } else {
      #the relevant file does not exist
      stop(
        paste("generate_price_hour: symbol[", symbol,
              "]: the file of date[",
              format(as.Date(calendar[i]), "%Y%m"),
              "] does not exist",
              sep = ""
              )
        )
    }
  }
}
naokiookura/rquants documentation built on May 23, 2017, 10:31 a.m.