R/stocks.R

#################################################################################
##
##   R package rcsi by Alexios Ghalanos Copyright (C) 2008, 2009
##   This file is part of the R package rcsi.
##
##   The R package rcsi is free software: you can redistribute it and/or modify
##   it under the terms of the GNU General Public License as published by
##   the Free Software Foundation, either version 3 of the License, or
##   (at your option) any later version.
##
##   The R package rcsi is distributed in the hope that it will be useful,
##   but WITHOUT ANY WARRANTY; without even the implied warranty of
##   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##   GNU General Public License for more details.
##
#################################################################################

stkcontrol = function(IncludeSaturdays = FALSE, IncludeHolidays = FALSE, DetrendMethod = 0,
		ApplyStockSplitAdjustments = TRUE, ApplyStockDividendAdjustments = TRUE, PropStockAdjustments = FALSE,
		PropStockVolumeAdjustments = FALSE, CloseOutOfRangeAdjustmentMethod = 0,
		RoundToTick = FALSE, UseTradeableTick = TRUE, CoverMutualFundHigh = TRUE, OnHolidaysUsePreviousData = 0, 
		OnHolidaysUseCloseOnly = 0, AggregateVolAs = 3, AggregateOiAs = 3, AggregateCashAs = 3, 
		AggregateDateAsLastRecord = 1, AggregateDayOfWeekAs = 6)
{
	# convert to integer
	IncludeSaturdays = as.integer(IncludeSaturdays)
	IncludeHolidays = as.integer(IncludeHolidays)
	DetrendMethod = as.integer(DetrendMethod)
	ApplyStockSplitAdjustments = as.integer(ApplyStockSplitAdjustments)
	ApplyStockDividendAdjustments = as.integer(ApplyStockDividendAdjustments)
	PropStockAdjustments = as.integer(PropStockAdjustments)
	PropStockVolumeAdjustments = as.integer(PropStockVolumeAdjustments)
	CloseOutOfRangeAdjustmentMethod = as.integer(CloseOutOfRangeAdjustmentMethod)
	CoverMutualFundHigh = as.integer(CoverMutualFundHigh)
	RoundToTick = as.integer(RoundToTick)
	UseTradeableTick = as.integer(UseTradeableTick)
	OnHolidaysUsePreviousData = as.integer(OnHolidaysUsePreviousData)
	OnHolidaysUseCloseOnly = as.integer(OnHolidaysUseCloseOnly)
	AggregateVolAs = as.integer(AggregateVolAs)
	AggregateOiAs = as.integer(AggregateOiAs)
	AggregateCashAs = as.integer(AggregateCashAs)
	AggregateDateAsLastRecord = as.integer(AggregateDateAsLastRecord)
	AggregateDayOfWeekAs = as.integer(AggregateDayOfWeekAs)
	# DetrendMethod
	# -1 (Detrend By Second Contract), 0 (None), 1 (Detrend by Second Day)
	v.DetrendMethod = c(-1, 0, 1)
	if(!any(DetrendMethod[1] == v.DetrendMethod))
		stop("invalid DetrendMethod choice", call. = FALSE)
	
	# CloseOutOfRangeAdjustmentMethod
	# 0 (LeaveAlone), 1 (CompromiseHighLow), 2 (CompromiseClose)
	v.CloseOutOfRangeAdjustmentMethod = c(0, 1, 2)
	if(!any(CloseOutOfRangeAdjustmentMethod[1] == v.CloseOutOfRangeAdjustmentMethod))
		stop("invalid CloseOutOfRangeAdjustmentMethod choice", call. = FALSE)
	
	# Aggregate (Vol, Oi, Cash) As
	# 0 (Largest), 1 (Smallest), 2 (Average), 3 (Last), 4 (Total)
	v.AggregateVolAs = c(0, 1, 2, 3, 4)
	if(!any(AggregateVolAs[1] == v.AggregateVolAs))
		stop("invalid AggregateVolAs choice", call. = FALSE)
	
	v.AggregateOiAs = c(0, 1, 2, 3, 4)
	if(!any(AggregateOiAs[1] == v.AggregateOiAs))
		stop("invalid AggregateVolAs choice", call. = FALSE)
	
	v.AggregateCashAs = c(0, 1, 2, 3, 4)
	if(!any(AggregateCashAs[1] == v.AggregateCashAs))
		stop("invalid AggregateVolAs choice", call. = FALSE)
	
	# Aggregate Date As
	# 0 (First Day of Week), 1 (Last Day of Week)
	v.AggregateDateAsLastRecord = c(0, 1)
	if(!any(AggregateDateAsLastRecord[1] == v.AggregateDateAsLastRecord))
		stop("invalid AggregateDateAs choice", call. = FALSE)
	
	# AggregateDayOfWeekAs
	# 1 (Sunday)...7 (Saturday)
	v.AggregateDayOfWeekAs = c(1, 2, 3, 4, 5, 6, 7)
	if(!any(AggregateDayOfWeekAs[1] == v.AggregateDayOfWeekAs))
		stop("invalid AggregateDayOfWeekAs choice", call. = FALSE)
	
	clist = list(IncludeSaturdays = IncludeSaturdays,
	IncludeHolidays = IncludeHolidays,
	DetrendMethod = DetrendMethod,
	ApplyStockSplitAdjustments = ApplyStockSplitAdjustments,
	ApplyStockDividendAdjustments = ApplyStockDividendAdjustments,
	PropStockAdjustments = PropStockAdjustments,
	PropStockVolumeAdjustments = PropStockVolumeAdjustments,
	CloseOutOfRangeAdjustmentMethod = CloseOutOfRangeAdjustmentMethod,
	CoverMutualFundHigh = CoverMutualFundHigh,
	RoundToTick = RoundToTick,
	UseTradeableTick = UseTradeableTick,
	OnHolidaysUsePreviousData = OnHolidaysUsePreviousData,
	OnHolidaysUseCloseOnly = OnHolidaysUseCloseOnly,
	AggregateVolAs = AggregateVolAs,
	AggregateOiAs = AggregateOiAs,
	AggregateCashAs = AggregateCashAs,
	AggregateDateAsLastRecord = AggregateDateAsLastRecord,
	AggregateDayOfWeekAs = AggregateDayOfWeekAs)
	return(clist)
}
.callstkcontrol = function(control)
{
	.ua[["IncludeSaturdays"]] = control$IncludeSaturdays
	.ua[["IncludeHolidays"]] = control$IncludeHolidays
	.ua[["DetrendMethod"]] = control$DetrendMethod
	.ua[["ApplyStockSplitAdjustments"]] = control$ApplyStockSplitAdjustments
	.ua[["ApplyStockDividendAdjustments"]] = control$ApplyStockDividendAdjustments
	.ua[["PropStockAdjustments"]] = control$PropStockAdjustments
	.ua[["PropStockVolumeAdjustments"]] = control$PropStockVolumeAdjustments
	.ua[["CloseOutOfRangeAdjustmentMethod"]] = control$CloseOutOfRangeAdjustment
	.ua[["RoundToTick"]] = control$RoundToTick
	.ua[["UseTradeableTick"]] = control$UseTradeableTick
	.ua[["CoverMutualFundHigh"]] = control$CoverMutualFundHigh
	.ua[["OnHolidaysUsePreviousData"]] = control$OnHolidaysUsePreviousData
	.ua[["OnHolidaysUseCloseOnly"]] = control$OnHolidaysUseCloseOnly
	.ua[["AggregateVolAs"]] = control$AggregateVolAs
	#.ua[["AggregateOiAs"]] = control$AggregateOiAs
	#.ua[["AggregateCashAs"]] = control$AggregateCashAs
	#.ua[["AggregateDateAs"]] = control$AggregateDateAs
	.ua[["AggregateDayOfWeekAs"]] = control$AggregateDayOfWeekAs
	return(1)
}

# contract series
.stockseries = function(symbol, start.date = -1, end.date = -1, freq = c("d", "w", "m", "q", "y"), stk.control = NULL)
{
	if(!exists(".ua")) startUA()
	if(is.null(stk.control)) stk.control = stkcontrol()
	setprop = .callstkcontrol(stk.control)
	.ua[["IsStock"]] = as.integer(1)
	csinum = .getCSInum(symbol)
	if(csinum==-1) stop("invalid symbol", call. = FALSE)
	getn = .ua$RetrieveStock( csinum, start.date, end.date )
	if(getn == -1)
		stop("no data...\n", call. = FALSE)
	v.freq = c("d", "w", "m", "q", "y")
	freq = tolower(freq[1])
	if(!any(freq == v.freq))
		stop("not a valid frequency choice", call. = FALSE)
	freq = switch(EXPR=freq, d = 0, w = 1, m = 2, q = 3, y = 4)
	freq = as.integer(freq)
	
	ans = .ua$GetUAStrClipFormat()
	ans = eval(parse(text=paste(".ua$CombineDataToClipboard2(",freq,",","'",",","'",")",sep="")),envir=.GlobalEnv)
	s  = read.csv(file = "clipboard", header = FALSE, sep = ",", dec = ".")
	
	colnames(s) = try(c("Date","DayOfWeek","DeliveryCode","Open","High","Low", "Close", "Cash", "CVolume", "COpenInt", 
					"TVolume", "TOpenInt", "ClosingBid", "ClosingAsk", "Unadj.Close", "UnadjVol", "UnadjOI", "Expansion"), 
			silent = TRUE)	
	if(inherits(s, "try-error"))
		stop("no data...\n", call. = FALSE)
	s = as.data.frame(s)	
	sol = new("uaStockSeries",
			csinum = csinum,
			symbol = symbol,
			freq = freq,
			data = s)
	return(sol)
}

Try the RCSI package in your browser

Any scripts or data that you put into this service are public.

RCSI documentation built on May 2, 2019, 4:50 p.m.