R/futures.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.
##
#################################################################################

contractlist = function(symbol, is.stock = FALSE, is.option = FALSE)
{
	if(!exists(".ua")) startUA()
	csinum = .getCSInum(symbol)
	.ua[["IsStock"]] = as.integer(is.stock)
	if(csinum==-1) stop("invalid symbol", call. = FALSE)
	.ua[["MarketNumber"]] = csinum
	n = .ua$BuildContractList(csinum, as.integer(is.stock), as.integer(is.option))
	# 0 is the cash
	futcode = vector(mode = "integer", length = n)
	futstart = vector(mode = "integer", length = n)
	futend = vector(mode = "integer", length = n)
	futyear = vector(mode = "integer", length = n)
	futmonth = vector(mode = "integer", length = n)
	if(n==-1) stop("could not build contract list...", call. = FALSE)
	for(i in 1:n){
		futcode[i] = .ua$GetContractDeliveryCode(i-1)
		futstart[i] = .ua$GetContractFirstDate(i-1)
		futend[i] = .ua$GetContractLastDate(i-1)
		if(futcode[i]==54) next()
		futyear[i] = as.integer(substr(as.character(futcode[i]),1,4))
		futmonth[i] = as.integer(substr(as.character(futcode[i]),5,6))
	}
	ctable = data.frame(futcode = futcode, futstart = futstart, futend = futend, futyear = futyear,
			futmonth = futmonth)
	# get valid months
	uniqmonths = sort(unique(ctable[,5]))
	# remove the case series from the unique months
	if(uniqmonths[1]==0) uniqmonths = uniqmonths[-1]
	# in case of some error:
	if(any(uniqmonths>12)) uniqmonths = uniqmonths[uniqmonths<12]
	mmat = match(uniqmonths,1:12)
	valid.months = vector(mode="character", length=12)
	valid.months = rep("I",12)
	valid.months[mmat] = "V"
	valid.months = paste(valid.months,sep="",collapse="")
	# clear the contract list
	clrfut = .ua$InvalidateContractList()
	return(list(contractTable = ctable, validMonths = valid.months))
}



# individual contract
contractspec = function( contract.code = 54)
{
	clist = list( contract.code = contract.code, type = "Individual Contract")
	ans = new("uaContractSpec",
			spec = clist)
	return(ans)
}

# perpetual spec
perpetualspec = function(MonthsAheadToViewMarket = 1, OIWeighted = 1, DayOfMonthToRoll = 1,
		MonthsPriorToRoll = 0, valid.months = "VVVVVVVVVVVV")
{
	clist = list( MonthsAheadToViewMarket = MonthsAheadToViewMarket, OIWeighted = OIWeighted,
			DayOfMonthToRoll = DayOfMonthToRoll, MonthsPriorToRoll = MonthsPriorToRoll, 
			valid.months = valid.months, type = "CSI Perpetual Contract")
	
	ans = new("uaPerpetualSpec",
			spec = clist)
	return(ans)
}

# gann contract
gannspec = function(DeliveryMonthToView = 1, DayOfMonthToRoll = 1, MonthsPriorToRoll = 0)
{
	clist = list( DeliveryMonthToView = DeliveryMonthToView, DayOfMonthToRoll = DayOfMonthToRoll, 
			MonthsPriorToRoll = MonthsPriorToRoll, type = "Gann Contract")
	ans = new("uaGannSpec",
			spec = clist)
	return(ans)
}

# backadjusted contract
backadjspec = function(rollLogicType = -1, rollWhen = 1, DayOfMonthToRoll = 1, MonthsPriorToRoll = 0,
		accumulationMethod = 0, rollDeltaType = 0, valid.months = "VVVVVVVVVVVV", GenerateForward = 0,
		rollNDays = 5, ProportionalAdjustment = 1)
{
	
	# do checks on valid entries:
	
	# RollOnOpenInterest, RollOnVolume, RollOnOpenInterestAndVolume, RollOnOpenInterestOrVolume, 
	# RollOnDayRelativeToStartOfMonth, RollOnDayRelativeToEndOfMonth
	v.rollLogicType = c(-1, -2, -3, -4, -5, -6)
	if(!any(rollLogicType[1] == v.rollLogicType))
		stop("invalid rollLogicType choice", call. = FALSE)
	# OpenToOpen, CloseToClose, CloseToOpen, CloseToOpenOldGap, CloseToOpenNewGap
	v.rollDeltaType = c(-1, 0, 1, 2, 3)
	if(!any(rollDeltaType[1] == v.rollDeltaType))
		stop("invalid rollDeltaType choice", call. = FALSE)
	# Reported, Known, Anticipated, Lagged2
	v.rollWhen = c(0, 1, 2,  3)
	if(!any(rollWhen[1] == v.rollWhen))
		stop("invalid rollWhen choice", call. = FALSE)
	# BackAdjust, ForwardAdjust
	v.accumulationMethod = c(0, 1)
	if(!any(accumulationMethod[1] == v.accumulationMethod))
		stop("invalid accumulationMethod choice", call. = FALSE)
	
	clist = list( rollLogicType = rollLogicType, rollWhen = rollWhen, DayOfMonthToRoll = DayOfMonthToRoll, 
			MonthsPriorToRoll = MonthsPriorToRoll, accumulationMethod = accumulationMethod, rollDeltaType = rollDeltaType, 
			valid.months = valid.months, GenerateForward = GenerateForward, rollNDays = rollNDays, 
			ProportionalAdjustment = ProportionalAdjustment, type = "BackAdjusted Contract")
	ans = new("uaBackAdjSpec",
			spec = clist)
	return(ans)
}

# nth nearest contract
nnearestspec = function(TargetForwardContract = 1, rollLogicType = -1, rollWhen = 1,
		DayOfMonthToRoll = 5, MonthsPriorToRoll = 0, valid.months = "VVVVVVVVVVVV", GenerateForward = 0,
		rollNDays = 5)
{
	# do checks on valid entries:
	
	# RollOnOpenInterest, RollOnVolume, RollOnOpenInterestAndVolume, RollOnOpenInterestOrVolume, 
	# RollOnDayRelativeToStartOfMonth, RollOnDayRelativeToEndOfMonth
	v.rollLogicType = c(-1, -2, -3, -4, -5, -6)
	if(!any(rollLogicType[1] == v.rollLogicType))
		stop("invalid rollLogicType choice", call. = FALSE)

	# Reported, Known, Lagged2
	v.rollWhen = c(0, 1, 3)
	if(!any(rollWhen[1] == v.rollWhen))
		stop("invalid rollWhen choice", call. = FALSE)

	# 1:9
	v.TargetForwardContract = 1:9
	if(!any(TargetForwardContract[1] == v.TargetForwardContract))
		stop("invalid TargetForwardContract choice", call. = FALSE)
	
	clist = list(TargetForwardContract = TargetForwardContract, rollLogicType = rollLogicType, rollWhen = rollWhen,
			DayOfMonthToRoll = DayOfMonthToRoll, MonthsPriorToRoll = MonthsPriorToRoll, valid.months = valid.months, 
			GenerateForward = GenerateForward, rollNDays = rollNDays, type = "Nth Nearest Contract")
	ans = new("uaNthNearestSpec",
			spec = clist)
	return(ans)
}

futcontrol = function(IncludeSaturdays = FALSE, IncludeHolidays = FALSE, DetrendMethod = 0,
		FillInCashPrice = FALSE, ApplyCommodityAdjustments = TRUE, UseAlternateBackAdjuster = TRUE,
		RaiseNegBackAdjustSeries = FALSE, NumDaysConfirmation = 2, CloseOutOfRangeAdjustmentMethod = 0,
		RoundToTick = FALSE, UseTradeableTick = TRUE, OnHolidaysUsePreviousData = FALSE, OnHolidaysUseCloseOnly = FALSE,
		AggregateVolAs = 3, AggregateOiAs = 3, AggregateCashAs = 3, AggregateDateAsLastRecord = 1, AggregateDayOfWeekAs = 6)
{
	# convert to integers
	IncludeSaturdays = as.integer(IncludeSaturdays)
	IncludeHolidays = as.integer(IncludeHolidays)
	DetrendMethod = as.integer(DetrendMethod)
	FillInCashPrice = as.integer(FillInCashPrice)
	ApplyCommodityAdjustments = as.integer(ApplyCommodityAdjustments)
	UseAlternateBackAdjuster = as.integer(UseAlternateBackAdjuster)
	RaiseNegBackAdjustSeries = as.integer(RaiseNegBackAdjustSeries)
	NumDaysConfirmation = as.integer(NumDaysConfirmation)
	CloseOutOfRangeAdjustmentMethod = as.integer(CloseOutOfRangeAdjustmentMethod)
	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, FillInCashPrice = FillInCashPrice, 
			ApplyCommodityAdjustments = ApplyCommodityAdjustments, 
			UseAlternateBackAdjuster = UseAlternateBackAdjuster,
			RaiseNegBackAdjustSeries = RaiseNegBackAdjustSeries, 
			NumDaysConfirmation = NumDaysConfirmation, 
			CloseOutOfRangeAdjustmentMethod = CloseOutOfRangeAdjustmentMethod,
			RoundToTick = RoundToTick, UseTradeableTick = UseTradeableTick, 
			OnHolidaysUsePreviousData = OnHolidaysUsePreviousData, 
			OnHolidaysUseCloseOnly = OnHolidaysUseCloseOnly,
			AggregateVolAs = AggregateVolAs, AggregateOiAs = AggregateOiAs, 
			AggregateCashAs = AggregateCashAs, AggregateDateAsLastRecord = AggregateDateAsLastRecord, 
			AggregateDayOfWeekAs = AggregateDayOfWeekAs)
	return(clist)
}

.callfutcontrol = function(control)
{
	.ua[["IncludeSaturdays"]] = as.integer(control$IncludeSaturdays)
	.ua[["IncludeHolidays"]] = as.integer(control$IncludeHolidays)
	.ua[["DetrendMethod"]] = as.integer(control$DetrendMethod)
	.ua[["FillInCashPrice"]] = as.integer(control$FillInCashPrice)
	.ua[["ApplyCommodityAdjustments"]] = as.integer(control$ApplyCommodityAdjustments)
	.ua[["UseAlternateBackAdjuster"]] = as.integer(control$UseAlternateBackAdjuster)
	.ua[["RaiseNegBackAdjustSeries"]] = as.integer(control$RaiseNegBackAdjustSeries)
	.ua[["NumDaysConfirmation"]] = as.integer(control$NumDaysConfirmation)
	.ua[["CloseOutOfRangeAdjustmentMethod"]] = as.integer(control$CloseOutOfRangeAdjustmentMethod)
	.ua[["RoundToTick"]] = as.integer(control$RoundToTick)
	.ua[["UseTradeableTick"]] = as.integer(control$UseTradeableTick)
	.ua[["OnHolidaysUsePreviousData"]] = as.integer(control$OnHolidaysUsePreviousData)
	.ua[["OnHolidaysUseCloseOnly"]] = as.integer(control$OnHolidaysUseCloseOnly)
	.ua[["AggregateVolAs"]] = as.integer(control$AggregateVolAs)
	# the values below seem to return an error --> contact csi
	#.ua[["AggregateOiAs"]] = as.integer(control$AggregateOiAs)
	#.ua[["AggregateCashAs"]] = as.integer(control$AggregateCashAs)
	.ua[["AggregateDateAsLastRecord"]] = as.integer(control$AggregateDateAsLastRecord)
	.ua[["AggregateDayOfWeekAs"]] = as.integer(control$AggregateDayOfWeekAs)
	return(1)
}

# contract series
.futureseries1 = function(symbol, spec, start.date = -1, end.date = -1, freq = c("d", "w", "m", "q", "y"), fut.control = NULL)
{
	if(!exists(".ua")) startUA()
	if(is.null(fut.control)) fut.control = futcontrol()
	setprop = .callfutcontrol(fut.control)
	.ua[["IsStock"]] = as.integer(0)
	csinum = .getCSInum(symbol)
	if(csinum==-1) stop("invalid symbol", call. = FALSE)
	getn = .ua$RetrieveContract( csinum, spec@spec$contract.code, 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(.ua$CombineDataToClipboard2(freq,","), 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 = s[,c(1,3,4,5,6,7,8,9,10,11,12)]
	s = as.data.frame(s)
	
	sol = new("uaContractSeries",
			csinum = csinum,
			symbol = symbol,
			type = spec@spec$type,
			freq = freq,
			data = s)
	
	return(sol)
}

# perpetual series
.futureseries2 = function(symbol, spec, start.date = -1, end.date = -1, freq = c("d", "w", "m", "q", "y"), fut.control = NULL)
{
	if(!exists(".ua")) startUA()
	if(is.null(fut.control)) fut.control = futcontrol()
	setprop = .callfutcontrol(fut.control)
	.ua[["IsStock"]] = as.integer(0)
	csinum = .getCSInum(symbol)
	if(csinum==-1) stop("invalid symbol", call. = FALSE)
	getn = .ua$RetrievePerpetualContract( csinum, spec@spec$MonthsAheadToViewMarket, 
			spec@spec$OIWeighted, spec@spec$DayOfMonthToRoll, spec@spec$MonthsPriorToRoll, 
			spec@spec$valid.months, 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(.ua$CombineDataToClipboard2(freq,","), 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 = s[,c(1,3,4,5,6,7,8,9,10,11,12)]
	s = as.data.frame(s)
	
	sol = new("uaPerpetualSeries",
			csinum = csinum,
			symbol = symbol,
			type = spec@spec$type,
			freq = freq,
			data = s)
	
	return(sol)
}

# gann series
.futureseries3 = function(symbol, spec, start.date = -1, end.date = -1, freq = c("d", "w", "m", "q", "y"), fut.control = NULL)
{
	if(!exists(".ua")) startUA()
	if(is.null(fut.control)) fut.control = futcontrol()
	setprop = .callfutcontrol(fut.control)
	.ua[["IsStock"]] = as.integer(0)
	csinum = .getCSInum(symbol)
	if(csinum==-1) stop("invalid symbol", call. = FALSE)
	getn = .ua$RetrieveGannContract( csinum, spec@spec$DeliveryMonthToView, 
			spec@spec$DayOfMonthToRoll, spec@spec$MonthsPriorToRoll, 
			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(.ua$CombineDataToClipboard2(freq,","), 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 = s[,c(1,3,4,5,6,7,8,9,10,11,12)]
	s = as.data.frame(s)
	
	sol = new("uaGannSeries",
			csinum = csinum,
			symbol = symbol,
			type = spec@spec$type,
			freq = freq,
			data = s)
	
	return(sol)
}

.futureseries4 = function(symbol, spec, start.date = -1, end.date = -1, freq = c("d", "w", "m", "q", "y"), fut.control = NULL)
{
	if(!exists(".ua")) startUA()
	if(is.null(fut.control)) fut.control = futcontrol()
	setprop = .callfutcontrol(fut.control)
	.ua[["IsStock"]] = as.integer(0)
	csinum = .getCSInum(symbol)
	if(csinum==-1) stop("invalid symbol", call. = FALSE)
	if(spec@spec$ProportionalAdjustment){
		getn = .ua$RetrieveBackAdjustedContract2(csinum, spec@spec$rollLogicType, spec@spec$rollWhen,
				spec@spec$DayOfMonthToRoll, spec@spec$MonthsPriorToRoll, spec@spec$accumulationMethod,
				spec@spec$rollDeltaType, spec@spec$valid.months, spec@spec$GenerateForward,
				spec@spec$rollNDays, start.date, end.date, 1)
	} else{
		getn = .ua$RetrieveBackAdjustedContract(csinum, spec@spec$rollLogicType, spec@spec$rollWhen,
				spec@spec$DayOfMonthToRoll, spec@spec$MonthsPriorToRoll, spec@spec$accumulationMethod,
				spec@spec$rollDeltaType, spec@spec$valid.months, spec@spec$GenerateForward,
				spec@spec$rollNDays, 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(.ua$CombineDataToClipboard2(freq,","), 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 = s[,c(1,3,4,5,6,7,8,9,10,11,12)]
	s = as.data.frame(s)
	
	sol = new("uaBackAdjSeries",
			csinum = csinum,
			symbol = symbol,
			type = spec@spec$type,
			freq = freq,
			data = s)
	
	return(sol)
}

# nth nearest series
.futureseries5 = function(symbol, spec, start.date = -1, end.date = -1,  freq = c("d", "w", "m", "q", "y"), fut.control = NULL)
{
	if(!exists(".ua")) startUA()
	if(is.null(fut.control)) fut.control = futcontrol()
	setprop = .callfutcontrol(fut.control)
	.ua[["IsStock"]] = as.integer(0)
	csinum = .getCSInum(symbol)
	if(csinum==-1) stop("invalid symbol", call. = FALSE)
	getn = .ua$RetrieveNthNearestFuture( csinum, spec@spec$TargetForwardContract,
			spec@spec$rollLogicType, spec@spec$rollWhen,
			spec@spec$DayOfMonthToRoll, spec@spec$MonthsPriorToRoll, spec@spec$valid.months,
			spec@spec$GenerateForward, spec@spec$rollNDays,
			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(.ua$CombineDataToClipboard2(freq,","), 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 = s[,c(1,3,4,5,6,7,8,9,10,11,12)]
	s = as.data.frame(s)
	
	sol = new("uaNthNearestSeries",
			csinum = csinum,
			symbol = symbol,
			type = spec@spec$type,
			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.