Articles by R on msperlin

Investments Costs

April 24, 2020 | R on msperlin

Investing is hard. People often
get_perf <- function(starting_cash = 1, 
                     perc_perf = 0.2, 
                     perc_manag = 0.02, 
                     manager_skill_down = 0.8,
                     manager_skill_up = 1.2) {
  
  require(BatchGetSymbols)
  
  df_sp500 <- BatchGetSymbols(tickers = '^GSPC', 
                              first.date = '1950-01-01', last.date = Sys.Date())[[2]]
  
  df_invest <- df_sp500 %>%
    mutate(ref_year = lubridate::year(ref.date)) %>%
    group_by(ref_year) %>%
    summarise(last_price = last(price.adjusted)) %>%
    mutate(ret = last_price/lag(last_price) - 1)
  
  df_out <- tibble()
  port_value <- starting_cash
  invest_value <- starting_cash
  for (i_year in df_invest$ref_year[2:nrow(df_invest)]) {
    
    idx <- which(i_year == df_invest$ref_year)
    ret_year <- df_invest$ret[idx]
    invest_value <- invest_value*(1 + ret_year)

    fund_return <- (ret_year>0)*manager_skill_up*ret_year + 
      (ret_year<=0)*manager_skill_down*ret_year
    
    bench_cost <- (fund_return > 0)*(fund_return - ret_year)*perc_perf*port_value
    management_cost <- port_value*perc_manag
    
    port_value <- port_value*(1+fund_return) - bench_cost - management_cost
    
    df_out <- bind_rows(df_out, tibble(i_year, 
                                       ret_investment = ret_year,
                                       fund_return,
                                       bench_cost,
                                       management_cost,
                                       fund_cost = bench_cost + management_cost,
                                       port_value, 
                                       invest_value))
    
  }
  
  df_tab <- tibble(perc_perf, 
                   perc_manag,
                   manager_skill_down,
                   manager_skill_up,
                   total_ret_asset = last(df_out$invest_value)/starting_cash-1,
                   total_fund_return = last(cumprod(1+df_out$fund_return)) - 1,
                   total_ret_portfolio = last(df_out$port_value)/starting_cash-1,
                   total_cash_fund_cost = sum(df_out$fund_cost),
                   total_cash_investor = last(port_value),
                   CAGR_asset = (1+total_ret_asset)^(1/nrow(df_out))-1,
                   CAGR_fund = (1+total_fund_return)^(1/nrow(df_out))-1,
                   CAGR_portfolio = (1+total_ret_portfolio)^(1/nrow(df_out))-1
                   )
                   
  return(df_tab)
  
}

perc_perf <- 0.2
perc_manag <- 0.02
bench <- 0.05
starting_cash <- 1000
manager_skill_down <- 0.5
manager_skill_up <- 1.5

df_tab <- get_perf(starting_cash = starting_cash , 
                   perc_perf = perc_perf, 
                   perc_manag = perc_manag, 
                   manager_skill_down = manager_skill_down,
                   manager_skill_up = manager_skill_up)
## Loading required package: BatchGetSymbols
## Loading required package: rvest
## Loading required package: xml2
## Loading required package: dplyr
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
##
## 
## Running BatchGetSymbols for:
##    tickers =^GSPC
##    Downloading data for benchmark ticker
## ^GSPC | yahoo (1|1) | Found cache file
## ^GSPC | yahoo (1|1) | Found cache file - Got 100% of valid prices | Looking good!
[Read more...]

New R package: GetCVMData

April 19, 2020 | R on msperlin

Package GetCVMData is an alternative to GetDFPData. Both have the same objective: fetch corporate data of Brazilian companies trading at B3, but diverge in their source. While GetDFPData imports data directly from the DFP and FRE systems, GetCVMData uses the CVM ftp site for grabbing compiled .csv files. When doing ... [Read more...]

Financial Datasets Available in the Website

April 19, 2020 | R on msperlin

I’ve been researching financial data for over 10 years and compiled a great deal of compiled tables. Most of these comes from my R packages and have been used for creating class material, doing research and even writing a book. These files were mostly found in many copies across different ... [Read more...]

A GARCH Tutorial in R

March 28, 2020 | R on msperlin

Myself, Mauro Mastella, Daniel Vancin and Henrique Ramos, just finished a tutorial paper about GARCH models in R and I believe it is a good content for those learning financial econometrics. You can find the full paper in this link. In a nutshell, t... [Read more...]

Book slides – Analyzing Financial and Economic Data with R

February 24, 2020 | R on msperlin

The slides for my newly released book Analyzing Financial and Economic Data with R are finally ready! I apologize for keep you guys waiting. The slides are available as independent .Rmd files for all book chapters including:
##  [1] "afedR-Slides_Chapter-01_Introduction.Rmd"        
##  [2] "afedR-Slides_Chapter-02_BasicOperations.Rmd"     
##  [3] "afedR-Slides_Chapter-03_ResearchScripts.Rmd"     
##  [4] "afedR-Slides_Chapter-04_ImportingLocal.Rmd"      
##  [5] "afedR-Slides_Chapter-05_ImportingInternet.Rmd"   
##  [6] "afedR-Slides_Chapter-06_DataStructureObjects.Rmd"
##  [7] "afedR-Slides_Chapter-07_BasicClasses.Rmd"        
##  [8] "afedR-Slides_Chapter-08_Programming.Rmd"         
##  [9] "afedR-Slides_Chapter-09_CleaningData.Rmd"        
## [10] "afedR-Slides_Chapter-10_Figures.Rmd"             
## [11] "afedR-Slides_Chapter-11_Models.Rmd"              
## [12] "afedR-Slides_Chapter-12_ReportingResults.Rmd"    
## [13] "afedR-Slides_Chapter-13_OptimizingCode.Rmd"
All content is released with a generous MIT license, so fell free ... [Read more...]

Looking back at 2019 and plans for 2020

December 11, 2019 | R on msperlin

I’m just about to leave for my vacation and, as usual, I’ll write about the highlights of 2019 and my plans for the year to come. First, let’s talk about my work in 2019. Highlights of 2019 The year of 2019 was not particularly fruitful in journal publications. I only had ... [Read more...]

Static and Dynamic Book Exercises with R

December 1, 2019 | R on msperlin

In the new edition of my R book, to be released in early 2020 (see current TOC, new packages and notification form), I’m giving special attention to its use in the classroom. For that, I’ve created class slides and R exercises in the static and dynamic form. All the ... [Read more...]

New package: simfinR

October 31, 2019 | R on msperlin

Introduction In my latest post I wrote about package GetEdgarData, which downloaded structured data from the SEC. I’ve been working on this project and soon realized that the available data at the SEC/DERA section is not complete. For example, all Q4 statements are missing. This seems to be ... [Read more...]

New package: GetEdgarData

October 15, 2019 | R on msperlin

Introduction Every company traded in the US stock market must report its quarterly and yearly documents to the SEC and the public in general. This includes its accounting statements (10-K, 10-K) and any other corporate event that is relevant to investors. Edgar is the interface where we can search for ... [Read more...]

Help support GetDFPData

October 11, 2019 | R on msperlin

The shiny version of GetDFPData is currently hosted in a private server at DigitalOcean. A problem with the basic (5 USD) server I was using is with the low amount of available memory (RAM and HD). With that, I had to limit all xlsx queries for the ... [Read more...]

New package: GetQuandlData

September 30, 2019 | R on msperlin

Introduction Quandl is one of the best platforms for finding and downloading financial and economic time series. The collection of free databases is solid and I’ve used it intensively in my research and class material. But, a couple of things from the native package Quandl always bothered me: Multiple ... [Read more...]

B3 is NOT shutting down its ftp site, for now..

August 7, 2019 | R on msperlin

Surprise, surprise. B3’s ftp site is still up and running. Following previous post regarding the shutdown of B3’s ftp site and its impact over GetHFData, I’m happy to report that the site is up and running. We can check it with code:
library(GetHFData)
library(tidyverse)

df.ftp <- ghfd_get_ftp_contents(type.market = 'equity')
## 
## Reading ftp contents for equity(trades) (attempt = 1|10)
# check time difference
max(df.ftp$dates) - min(df.ftp$dates)
## Time difference of 158 days
Let’... [Read more...]

B3 is shutting down its ftp site

June 30, 2019 | R on msperlin

Well, bad news travels fast. Over the last couple of weeks I’ve been receiving a couple of emails regarding B3’s decision of shutting down its ftp site. More specifically, users are eager to know how it will impact my data grabbing packages in ... [Read more...]

The Effect of Consistency on Corporate Net Income

May 19, 2019 | R on msperlin

One of the investment concepts that every long term investor should know is the effect of consistency over corporate performance. The main idea is that older and profitable companies are likely to continue to be profitable and even improve its performance in the upcoming years. Likewise, companies with constant losses ... [Read more...]

R usage in Brazil

May 16, 2019 | R on msperlin

I’m using R for at least five years and always been curious about its usage in Brazil. I see some minor personal evidence that the number of users is increasing over time. My book in portuguese is steadily increasing its sells, and I’ve been receiving far more emails ... [Read more...]

Risk and return for B3

April 30, 2019 | R on msperlin

One of the subjects that I teach in my undergraduate finance class is the relationship between risk and expected returns. In short, the riskier the investment, more returns should be expected by the investor. It is not a difficult argument to make. All that you need to understand is to ... [Read more...]

New package: GetBCBData

April 14, 2019 | R on msperlin

The Central Bank of Brazil (BCB) offers access to its SGS system (sistema gerenciador de series temporais) with a official API available here. With time, I find myself using more and more of the available datasets in my regular research and studies. Over last weekend I decided to write my ... [Read more...]
1 2 3

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)