require(dplyr)
require(tidyr)
require(magrittr)
require(ggplot2)
require(scales)

knitr::opts_chunk$set(echo = FALSE,
                      warning = FALSE,
                      message = FALSE)

## Set options
options(scipen = 999, # prevent scientific notation on large numbers
        stringsAsFactors = F) # prevent R from turning everything to factors

## read parameters
scorecard_dir <- params$scorecard_dir
# scorecard_ws <- params$scorecard_ws
# confirm the scorecard directory
if (is.na(scorecard_dir) || stringr::str_length(scorecard_dir) < 1) {
  stop("Scorecard output directory missing: use scorecard_dir")
}

# verify the transaction file exists
transaction_file <- file.path(scorecard_dir,"transactions.csv")

# read the workspace and recover variable values
message(paste("Using transaction file", transaction_file))

# check that transaction file can be read
if ( as.numeric(file.access(transaction_file, mode = 4)) < 0 ) {
  stop(paste("Cannot read transaction file:", transaction_file))
}

# read the file
transactions.df <- read.csv(transaction_file,header=TRUE,stringsAsFactors = FALSE)
last.date <- transactions.df[nrow(transactions.df),'Date']

Summary

Share Balance

The consolidation of tickers and models yields the current share position as of r last.date:

consol <- transactions.df %>% 
  group_by(Model,Ticker) %>% 
  summarize(Balance=sum(Shares)) %>% 
  filter(Balance != 0) %>%
  filter(Ticker != "#CASH")

consol$Balance <- comma(consol$Balance)

knitr::kable(consol,row.names = FALSE,align=c('l','l','r'))

Model Cash Attribution

The consolidation of cash by model yields the cash positions as of r last.date. This position includes cash collected by models normally attributed to dividends or spin-outs, but does not include cash specifically allocated to model positions. All rows containing words Dividend or Distribution in the Note column are omitted.

consol <- transactions.df %>% 
  filter(Ticker == "#CASH") %>%
  filter( ! grepl("Dividend", Note) ) %>%
  filter( ! grepl("Distribution", Note) ) %>%
  group_by(Model,Ticker) %>% 
  summarize(Balance=sum(Shares)) %>% 
  select(Model,Balance)

consol$Balance <- dollar(consol$Balance)

knitr::kable(consol,row.names = FALSE, align=c('l','r'))

Model Cash Position Allocation

Specific allocation of cash positions to models as of r last.date, not including model income or expenses:

consol <- transactions.df %>% 
  filter(Ticker == "#CASH") %>%
  filter(Note == "Allocation") %>%
  group_by(Model,Ticker) %>% 
  summarize(Balance=sum(Shares)) %>% 
  select(Model,Balance)

consol$Balance <- dollar(consol$Balance)

knitr::kable(consol,row.names = FALSE, align=c('l','r'))

if ( nrow(consol) == 0 )
  cat(paste("None",'\n'))

Transaction History

The following is the complete history of transactions.

ft <- transactions.df
ft$Shares <- comma(ft$Shares)
ft$Price <- dollar(ft$Price)
ft$Value <- dollar(ft$Value)
ft$Comm <- dollar(ft$Comm)

knitr::kable(ft, row.names=FALSE, align=c('l','l','l','l','r','r','r','r','l'))

Transactions by Model

ft <- transactions.df
ft$Shares <- comma(ft$Shares)
ft$Price <- dollar(ft$Price)
ft$Value <- dollar(ft$Value)
ft$Comm <- dollar(ft$Comm)

models <- unique(ft$Model)
for ( m in models ) {
  cat(paste("Model",m,'\n'))
  ft.m <- ft %>% filter(Model == m)
  print(knitr::kable(ft.m, row.names=FALSE, align=c('l','l','l','l','r','r','r','r','l')))
  cat('\n\n')
}


greatgray/scorecard documentation built on Feb. 14, 2018, 4:28 a.m.