systematicinvestor / sit Goto Github PK
View Code? Open in Web Editor NEWSystematic Investor Toolkit
Systematic Investor Toolkit
I am having issues I cannot seem to find the solution to in regards to the DCF analysis here.
I am running the DCF analysis for a number of firms and for many, the code works as expected.
The problem is that when I run the analysis for some firms I have 'gaps' in the DCF data.
For example when I run the following code for ticker 'LOW' I do not get any gaps.
###############################################################################
# Load Systematic Investor Toolbox (SIT)
# http://systematicinvestor.wordpress.com/systematic-investor-toolbox/
###############################################################################
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
source(con)
close(con)
#*****************************************************************
# Load historical fundamental and pricing data
#******************************************************************
load.packages('quantmod')
tickers = spl('LOW')
tickers.temp = spl('NASDAQ:LOW')
# get fundamental data
data.fund <- new.env()
for(i in 1:len(tickers)) {
data.fund[[tickers[i]]] = fund.data(tickers.temp[i], 80, 'quarterly') #Put 'annual' for annual data
}
# get pricing data
data <- new.env()
getSymbols(tickers, src = 'yahoo', from = '1970-01-01', env = data, auto.assign = T)
for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T)
# prepare data
fund = data.fund[[tickers[1]]]
fund.date = date.fund.data(fund)
price = Cl(data[[tickers[1]]]['1995::'])
#*****************************************************************
# Extract Inputs for DCF Valuation
#******************************************************************
# Free Cash Flows
FCF = get.fund.data('free cash flow', fund, fund.date)
# Invested Capital
IC = get.fund.data('invested capital', fund, fund.date)
# Sales
SALE = get.fund.data('total revenue', fund, fund.date)
# Common Equity
CEQ = get.fund.data('total equity', fund, fund.date)
# Common Shares Outstanding
CSHO = get.fund.data('total common shares out', fund, fund.date)
# Growth Rate
CROIC = FCF/IC
# Average inputs
g = runMean(CROIC, 5)
cash = runMean(FCF, 5)
#*****************************************************************
# Helper function to compute Intrinsic Value
#******************************************************************
compute.DCF.IV <- function(cash, eqity, shares, g, R) {
if( cash <= 0 ) return(NA)
if( len(R) == 1 ) R = rep(R, len(g))
value = eqity + sum(cash * cumprod(1 + g) / cumprod(1 + R))
return( value / shares )
}
#*****************************************************************
# Compute Intrinsic Value, assumptions:
# Company will grow for the first 3 years at current Growth Rate
# slowed down by 20% for the next 4 years, and slowed down by a further 20% for the next 3 years
# and finally 3% growth for the next 10 years
#
# The Discount Rate is 9%
#
# http://www.oldschoolvalue.com/blog/stock-analysis/apple-aapl-valuation/
#******************************************************************
dcf.price = NA * g
i.start = which(!is.na(g))[1]
for(i in i.start : nrow(g)) {
# Create Growth Rate scenario:
g.scenario = c(rep(g[i],3), rep(g[i],4)*0.8, rep(g[i],3)*0.8*0.8, rep(3/100,10))
# Compute Intrinsic Value
dcf.price[i] = compute.DCF.IV(cash[i], CEQ[i], CSHO[i], g.scenario, 9/100)
}
#*****************************************************************
# Create Plots
#******************************************************************
#Remove last row of dcf.price
#dcf.price <- dcf.price[-nrow(dcf.price),]
plota(price, type='l', log = 'y', col='blue', main=tickers[1],
ylim=range(price,dcf.price,na.rm=T))
plota.lines(dcf.price, type='s', col='red', lwd=2)
plota.legend('Close,Intrinsic Value', 'blue,red', list(price, dcf.price))
# plota(g, type='b', col='blue', pch=0, main='Growth Rate')
#
#
# plota(cash, type='b', col='blue', pch=0, main='Free Cash Flows')
Which gives the following output:
However when I change the firm to ticker 'MU' I obtain gaps in the DCF analysis and obtain the following graphic.
The data for both firms downloads fine from the ADVFN website so I assuming something is not going right in the calculations for 'MU'.
Any help on how I can proceed with this would be great!
An error occurred when the package was installed according to the steps in the README.
`
curl_download('https://github.com/systematicinvestor/SIT/raw/master/SIT.tar.gz', 'sit',mode = 'wb',quiet=T)
install.packages('sit', repos = NULL, type='source')
ERROR: dependency ‘SIT.date’ is not available for package ‘SIT’
removing ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library/SIT’
Warning in install.packages :
installation of package ‘sit’ had non-zero exit status`
SIT certainly made a lot of things a lot easier. I have a lot of code I've written from the ground up, but it would be nice to have a go-to solution like SIT was.
This package looks great, would it be possible to create a package instead of sourcing the files?
When prices are downloaded, they are indexed by Date. In bt.merge, conversion is done to POSIXct with the local timezone. Downloaded data converted to POSIXct, with no TZ conversion, has a time of midnight GMT. For any timezone earlier than that, the conversion results in each date being moved back one day. In my case, I am in the America/Chicago TZ, which is UTC-6 hours. The class statement at the bottom of bt.merge() converted 12-31-18 to 12-30-18 18:00:00:00.
I've used this code for years, but only noticed the problem this weekend, when it appeared that I had a market price for Sunday.
I realize this is an old problem with date conversions in R. Just curious if it was intentional to leave it unaddressed, or if the idea was to always run with the sysenv(TZ) set to GMT. My original fix was to do the latter, but I thought I saw that it screwed up my results - haven't gone back to verify that. My next fix was to add 6 hours to Unique.dates prior to the class statement (not pretty, I know, but effective for now).
Thanks.
Below is the error :
http://stackoverflow.com/questions/36988554/vector-error-multiple-stocks-by-using-quantmod-on-shiny-web-application
And then I tried to get the relevant functions and using Rmarkdown to test the origin of error...
library(shiny)
createNonReactiveTextInput <- function(id, label, value,
button.label = '') {
value = lapply(value, as.character)
if(button.label != '')
list(
tagList(
tags$label(label), tags$input(id = id, type = "text", value = value,
style = "display:none;"),
tags$input(id = paste(id, "Temp", sep=''), type = "text",
value = value, style = "display:inline;",
onkeypress = paste("{if (event.keyCode==13) $('#", id,
"TempChange').click()}", sep = ''))),
div(
tags$button(id = paste(id, "TempChange", sep=''), type = "button",
class = "btn btn-primary", onclick = paste("$('#", id,
"').val($('#", id, "Temp').val()).change();",
sep = ''), button.label)))
else
list(
tagList(
tags$label(label), tags$input(id = id, type = "text", value = value,
style = "display:none;"),
tags$input(id = paste(id, "Temp", sep = ''), type = "text",
value = value, style = "display:inline;",
onkeypress = paste("{if (event.keyCode==13) $('#", id,
"').val($('#", id,
"Temp').val()).change()}",
sep = ''))))
}
createNonReactiveTextInputCustom <- function(id, label, tag.label = 'input',
button.label = '',
enableEnter = TRUE, opts) {
onkeypress = ''
if(button.label != '') {
if(enableEnter)
onkeypress = paste("{if (event.keyCode==13) $('#", id,
"TempChange').click()}", sep = '')
list(tagList(tags$label(label),
tag(tag.label, c(id = id, style = "display:none;", opts)),
tag(tag.label, c(id = paste(id, "Temp", sep = ''),
style = "display:inline;",
onkeypress = onkeypress, opts))),
div(
tags$button(id = paste(id, "TempChange", sep = ''),
type = "button", class = "btn btn-primary", onclick =
paste("$('#", id, "').val($('#", id,
"Temp').val()).change();", sep = ''),
button.label)))
} else {
if(enableEnter)
onkeypress = paste("{if (event.keyCode==13) $('#", id,
"').val($('#", id,
"Temp').val()).change()}", sep = '')
list(
tagList(
tags$label(label), tag(tag.label, c(id = id, style =
"display:none;", opts)),
tag(tag.label, c(id = paste(id,"Temp", sep = ''),
style = "display:inline;",
onkeypress = onkeypress, opts))))
}
}
input = ls()
createNonReactiveTextInput(
id = "symbols",
label = "Yahoo Ticker(s) separated by comma:",
value = "AAPL,GOOG", button.label = "Update")
str(input$symbols)
and the system prompt me below error message, I believed that is because of the createNonReactiveTextInput()
and input$symbols
doesn't work properly with the shiny input$xxx
. Would you look into this? Thank you.
- Quitting from lines 88-89 (test2.Rmd)
- Error in input$symbols : $ operator is invalid for atomic vectors
- Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> str
- Execution halted
Shiny version :
> devtools::session_info()$platform
setting value
version R version 3.2.3 (2015-12-10)
system x86_64, linux-gnu
ui RStudio (0.99.896)
language (EN)
collate en_US.UTF-8
tz GMT
date 2016-05-04
No changes to my code or system, but as of last week:
fund.data('NVDA',80,'quarterly')
Returns:
Downloading http://uk.advfn.com/p.php?pid=financials&symbol=NVDA&btn=quarterly_reports No Data Found for NVDA NULL
Any idea on what is happening?
Hi -
I'm trying to load historical data from a .csv file rather than Yahoo using some of your code. I get the following error:
Load Systematic Investor Toolbox (SIT)
http://systematicinvestor.wordpress.com/systematic-investor-toolbox/
setInternet2(TRUE)
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
source(con)
close(con)*****************************************************************
Load historical data
******************************************************************
load.packages('quantmod')
tickers = spl('IWM,TLT,GLD')
data <- new.env()
getSymbols(tickers, src = 'yahoo', from = '1980-01-01', env = data, auto.assign = T)
getSymbols(tickers, src = 'csv', dir= "C:/Users/Admiral/Downloads/", from = '1980-01-01', env = data, auto.assign = T)
<environment: 0x0000000012377c00>for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T)
bt.prep(data, align='keep.all', dates='2004:12::')*****************************************************************
Code Strategies
******************************************************************
prices = data$prices
n = ncol(prices)models = list()
find period ends
period.ends = endpoints(prices, 'weeks')
period.ends = period.ends[period.ends > 0]Adaptive Asset Allocation parameters
n.top = 5 # number of momentum positions
n.mom = 70 # length of momentum look back
n.vol = 10 # length of volatility look back*****************************************************************
Equal Weight
******************************************************************
data$weight[] = NA
data$weight[period.ends,] = ntop(prices[period.ends,], n)
models$equal.weight = bt.run.share(data, clean.signal=F)
Latest weights :
GLD IWM TLT
2012-08-29 751.0891 751.0891 567.1828
Error in [.xts
(x, i, which.i = TRUE) : subscript out of bounds
*****************************************************************
Volatliliy Position Sizing
******************************************************************
ret.log = bt.apply.matrix(prices, ROC, type='continuous')
hist.vol = bt.apply.matrix(ret.log, runSD, n = n.vol)
1 Error in runCov(x, x, n, use = "all.obs", sample = sample, cumulative) :
Invalid 'n'
2 Error in runCov(x, x, n, use = "all.obs", sample = sample, cumulative) :
Invalid 'n'
3 Error in runCov(x, x, n, use = "all.obs", sample = sample, cumulative) :
Invalid 'n'
Do you have any ideas how I can fix the subscript out of bounds error?
Thanks!
The wikipedia link in SP500 needs to be updated I think.
The correct url is https://en.wikipedia.org/wiki/List_of_S%26P_500_companies
Hello SIT !
I know this dates back several years, but I would have a question on the FTCA algorithm implementation:
Line 2276 in 6327d4e
Compared to the pseudo-code of Mr Varadi (https://cssanalytics.wordpress.com/2013/11/26/fast-threshold-clustering-algorithm-ftca/), I can read that if the correlation between HC and LC > threshold, while Mr Varadi pseudo-code is Add to Cluster all other assets that have yet been assigned to a Cluster and have an Average Correlation to HC and LC > Threshold, the FTCA code implemented here seems to assign all remaining assets to the HC/LC cluster.
Would you remember why is that ?
Cheers,
Roman
I am new to SIT and want to use it for backtesting my strategy. I searched google for a simple tutorial of using SIT but find nothing. The only tutorial I found was from Inovance and yours but its really complicated and I have no idea how to perform a simple moving average crossover strategy with one stock microsoft(MSFT) close price using SIT and quantmod(I know this library). If possible can you write a tutorial for MA crossover strategy?
this is code I wrote
library(quantmod)
ticker = 'MSFT'
symbol = getSymbols(ticker,from="1990-01-01",auto.assign=F)
prices=Cl(symbol)
sma.fast=SMA(prices, 5)
sma.slow=SMA(prices, 30)
buy = ifelse((sma.fast> sma.slow), 1, NA)
sell=ifelse((sma.fast< sma.slow), -1, NA)
How can I use above code with SIT's tools for backtesting?
Also I have installed SIT in R
but when I tried this
library(SIT)
plota.test()
its giving this
null device
1
Why its giving this? is it a error?
Hi!
FILE: SIT/R/aa.R
LINE: 268
Should this code
if( as.numeric( sessionInfo()$R.version$minor ) < 9 ) {
be replaced with
if( as.numeric( sessionInfo()$R.version$major ) < 3 && as.numeric( sessionInfo()$R.version$minor ) < 9 ) {
?
I came across this situation in my project. R Version 3.0.2, Rdonlp2 version 3.0.0
Best regards,
Alex
Hi,
I try to get prices from the combined tickers like the following.
However, I could not load 'data.proxy.raw.Rdata' and find the code in this area.
How can I solve this problem?
Thank you in advance
library(SIT)
tickers = '
load('data.proxy.raw.Rdata')
Error in readChar(con, 5L, useBytes = TRUE) : cannot open the connection
In addition: Warning message:
In readChar(con, 5L, useBytes = TRUE) :
cannot open compressed file 'data.proxy.raw.Rdata', probable reason 'No such file or directory'
Line 213 has a typo where argument "commision" should be "commission".
213 models$test.com.new = bt.run.share(data, commision=commsion, trade.summary=T, clean.signal=T)
I am trying to backtest this simple strategy of buying today and selling tomorrow or from n days but I dont know how to code the strategy in this package
strategy=iif(open>lag(open),1,iif(nextday,0,NA))
The above strategy is simple if yesterdays open price is less than todays open price then buy and sell the share next day.
The buy part I am able to code but how should I tell SIT to sell it the next day or after n days?
When I try installing SIT from github:
curl_download('https://github.com/systematicinvestor/SIT/raw/master/SIT.tar.gz', 'sit', mode='wb', quiet=T)
install.packages('sit', repos=NULL, type='source')
I get an error:
ERROR: dependency 'SIT.date' is not available for package 'SIT'
First off, I am a big fan of your work and all the code runs smoothly most of the time. However, I encountered an issue with the portfolio construction procedure in two instances:
the code appears to stop working as soon as one of the stock price series from yahoo finance references an irregular stock ticker (in my case it was MOG-A) which concerns most companies with two types of shares
If I run this code: data$weight[week.ends,] = ntop(prices[week.ends,], n)
I get this error message:
Error in colnames<-
(*tmp*
, value = c("..." :
length of 'dimnames' [2] not equal to array extent
when the number of stocks to be included in the portfolio optimization exceeds 20, the code tends to break as well.
do you have any quickfix for these issues? my guess is that the main problem is that your code uses price data in environments rather than objects.
Cheers
MR
When trying to build SIT package locally I got following errors:
I am using SIT for downloading intraday data from Indian market
data = getSymbol.intraday.google('NIFTY', 'NSE', 60, '1d')
Indian markets opens at 9:00 am but by using the above function time starts from 9:45 am which is wrong
Open High Low Close Volume
2016-09-09 09:45:00 8936.80 8936.80 8934.30 8934.30 0
2016-09-09 09:46:00 8914.30 8933.80 8913.60 8933.30 0
2016-09-09 09:47:00 8914.45 8916.80 8913.90 8914.35 0
2016-09-09 09:48:00 8919.30 8919.30 8914.70 8914.75 0
2016-09-09 09:49:00 8921.30 8922.50 8917.05 8919.75 0
2016-09-09 09:50:00 8917.05 8922.55 8916.60 8921.20 0
How to set time from 9:00 am instead of 9:45?
Hi there,
Do you have the code in python?
I'm really interested in!
Let me know please!
Have a nice day
Martin
Installation process seems to have an error due to Zoo package. Not sure it has been deprecated.
error when installing the SIT package. Reviewed other post and have done the installation in correct sequence.
Received this error
object 'autoplot' not found whilst loading namespace 'zoo'
Error : package 'zoo' could not be loaded
Installation aborted
I just freshly installed R and RStudio, as well as SIT. I haven't used SIT in a while, so I'm not sure if the problem is SIT being dead or if it's a problem particular to me, but every code I attempt to run in SIT produces:
Error in if (type == "Date" || type == "yearmon" || type == "yearqtr") temp = as.Date(temp) :
missing value where TRUE/FALSE needed
All my other past code (non-SIT related) works fine on my new system, so I have to wonder whether SIT has stopped working with the newest update of R.
Any input would be appreciated. Thanks!
Error in roxygenize("pkg", copy.package = F, unlink.target = F, overwrite = T) :
unused arguments (copy.package = F, unlink.target = F, overwrite = T)
because roxygenize in roxygen2 doesn' t have that arguments. Why are you using roxygen and roxygen2 at the same time?
This link is referred in the plot.table.r file and it is broken - http://us.ishares.com/content/stream.jsp?url=/content/en_us/repository/resource/single_country_periodic_table.pdf&mimeType=application/pdf
Hi,
I try to using SIT with other packages as well, unfortunately it breaks my exists code due to compatibility of the fast alternative of index.xts.
Error in `[.xts`(data, timestamp) : subscript out of bounds
In addition: Warning messages:
Removing the following function everything works smoothly.
###############################################################################
# Fast alternative to index(x) for XTS object
###############################################################################
index.xts <- function
(
x # XTS object
)
{
temp = attr(x, 'index')
class(temp)='POSIXct'
if( attr(x, '.indexCLASS')[1] == 'Date') {
as.Date(temp)
} else {
as.POSIXct(temp, tz = Sys.getenv('TZ'))
}
}
I think there is an error with the zacks.info(ticker) function. When I run I get the below error.
events = zacks.info(ticker)
Loading required package: jsonlite
Error: parse error: premature EOF
{"data"
(right here) ------^
Hi, I have this error. Somebody could help to deal with it please?
In Charting the Santa Claus Rally
plota.test()
Error in if (type == "Date" || type == "yearmon" || type == "yearqtr") temp = as.Date(temp) :
missing value where TRUE/FALSE needed
Called from: index.xts(y1)
When I try to run some parts of the code I found the same error.
Buy the way this is still working for somebody? I have lots of code not working.
There is some instructions step by step? Thank you
Source it without problem in C locates,
$export LC_ALL=C
$R
> source('sources/R/sit.r')
>
Does not work on zh_CN.utf-8
$export LC_ALL=zh_CN.utf-8
$R
...
> source('sources/R/sit.r')
警告信息:
1: In grepl("\n", lines, fixed = TRUE) : 输入的字符串420不适用于此语言环境
2: In grepl("\n", lines, fixed = TRUE) :
输入的字符串6853不适用于此语言环境
3: In grepl("\n", lines, fixed = TRUE) :
输入的字符串8529不适用于此语言环境
4: In grepl("\n", lines, fixed = TRUE) :
输入的字符串8601不适用于此语言环境
5: In grepl("\n", lines, fixed = TRUE) :
输入的字符串9423不适用于此语言环境
6: In grepl("\n", lines, fixed = TRUE) :
输入的字符串9653不适用于此语言环境
7: In grepl("\n", lines, fixed = TRUE) :
输入的字符串10149不适用于此语言环境
>
>source('sources/R/sit.r', encoding='latin1')
>
source with given latin1 encoding without problem.
Following up on this issue...
#2
Here's an example to reproduce the problem:
library(PerformanceAnalytics)
example(charts.PerformanceSummary)
setInternet2(TRUE)
con = gzcon(url('https://github.com/systematicinvestor/SIT/raw/master/sit.gz', 'rb'))
source(con)
close(con)
example(charts.PerformanceSummary)
rm(index.xts)
example(charts.PerformanceSummary)
So they are not in the help page in the package. Many of them should be changed to #' for roxygen to generate the documentation correctly.
Hi
I am having a problem after this code:
png(filename = 'plot.time.seasonality.png', width = 600, height = 500, units = 'px', pointsize = 12, bg = 'white')
layout(1)
time.seasonality(data, 1 + month.ends[index], 20, ticker)
dev.off()
RSTudioGD
2
Any suggestions on how to fix it? Thank you
Enrique
Hi,
There seems to be issues with aa.test():
Error in create.historical.ia(hist.returns, 12, symbols, symbol.names) :
unused arguments (symbols, symbol.names)
3 create.historical.ia(hist.returns, 12, symbols, symbol.names) at con#2690
2 aa.test.create.ia() at con#1696
1 aa.test()
After overwriting so that the symbols and symbol.names are not passed to the create.histroical.aa:
Error in t(ia$hist.returns) - repmat(colMeans(ia$hist.returns), 1, nt) :
non-conformable arrays
6 match.fun(add.constraint.fn)(ia, 0, ">=", constraints) at con#708
5 optimize.portfolio(ia, constraints, add.constraint.fn, min.risk.fn) at con#693
4 min.portfolio(ia, constraints, add.constraint.mad, portfolio.mad) at con#849
3 match.fun(min.risk.fn)(ia, constraints) at con#1502
2 portopt(ia, constraints, 50, "MAD", min.mad.portfolio) at con#1719
1 aa.test()
Thanks.
I run code as following:
setInternet2(TRUE)
con = gzcon(url('https://github.com/systematicinvestor/SIT/raw/master/sit.gz', 'rb'))
source(con)
close(con)
asset.allocation.strategy.test()
Then I got error:
Error in min.risk.fns[[f]](ia, constraints) :
unused argument (constraints)
Not sure if I have to submit here or at tidyquant package, but when tidyquant package is loaded the "last" function in compute.cagr throw an error.
If using last(prices, 1) I get following error.
"Error in x[[order(order_by)[n]]] : subscript out of bounds".
Hi,
Is it possible to execute twice in a day, e.g. buy at open and sell at close each day, using EOD data in SIT? I saw this example: https://systematicinvestor.wordpress.com/2012/04/03/transaction-cost-and-execution-price-functionality-in-the-backtesting-library-in-the-systematic-investor-toolbox/, which determines the signal from the Close (time=t) and trades at next open (t+1), but it is not clear how I might sell that same day at Close (t+1) to avoid holding overnight, because execution.price (for EOD) is only 1 column with 1 price per day.
Any help would be greatly appreciated,
Gavin.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.