%% \VignetteIndexEntry{Overview of the PMwR package} \documentclass[a4paper,11pt]{article} \usepackage[left = 3cm, top = 2cm, bottom = 2cm, right = 4cm]{geometry} \usepackage[noae,nogin]{Sweave} \usepackage{libertine} \usepackage[scaled=0.9]{inconsolata} % \usepackage[T1]{fontenc} \renewcommand*\familydefault{\sfdefault} \usepackage{amsmath,amstext} \usepackage{hyperref} \usepackage{natbib} \usepackage{xcolor} \usepackage{framed} \usepackage{fancyvrb} \usepackage[flushmargin]{footmisc} \definecolor{grau2}{rgb}{.2,.2,.2} \definecolor{grau7}{rgb}{.7,.7,.7} \DefineVerbatimEnvironment{Sinput}{Verbatim}{} \DefineVerbatimEnvironment{Soutput}{Verbatim}{frame=single,xleftmargin=0em,% formatcom=\color{grau2},rulecolor=\color{grau7}} \DefineVerbatimEnvironment{Scode}{Verbatim}{xleftmargin=2em} \fvset{listparameters={\setlength{\topsep}{0pt}}} \renewenvironment{Schunk}{\vspace{\topsep}}{\vspace{\topsep}} \SweaveOpts{keep.source = TRUE, eps = TRUE} <>= options(continue = " ", digits = 3, width = 60, useFancyQuotes = FALSE) pv <- packageVersion("PMwR") pv <- gsub("(.*)[.](.*)", "\\1-\\2", pv) @ \begin{document} \title{An Overview of the PMwR Package} \author{Enrico Schumann\\\url{es@enricoschumann.net}} {\raggedright{\LARGE An Overview of the PMwR Package}}\hspace*{\fill} {\footnotesize Package version \Sexpr{pv}}\medskip \noindent Enrico Schumann\\ \noindent \url{es@enricoschumann.net}\\ \bigskip <>= library("PMwR") library("zoo") @ \noindent \textsc{pm}w\textsc{r} provides a small set of reliable,\!% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \footnote{ <>= if (requireNamespace("tinytest")) { tinytest.results <- try(suppressMessages( suppressWarnings(tinytest::test_package("PMwR")))) if (inherits(tinytest.results, "try-error")) nt <- "several hundred" else nt <- length(tinytest.results) } else nt <- "several hundred" @ \textsc{pm}w\textsc{r} is to provide well-documented and reliable code. For all computations, unit tests are included. As of package version % \Sexpr{pv}, \Sexpr{nt} tests are included. These tests are stored in subdirectory \texttt{tinytest}.} % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% efficient and convenient tools that help with processing and analysing trade and portfolio data. The package grew out of various pieces of software that I have written since 2008.% \footnote{In the unlikely case that you come across a really-old version of the package: it was called \textsc{pm} before 2012.} % The package is on \textsc{cran} and can be installed from there. The very latest (i.e. development) version of the package is available from \url{https://enricoschumann.net/R/packages/PMwR/}\,. To install that version from within R, type <>= install.packages('PMwR', repos = c('https://enricoschumann.net/R', getOption('repos'))) @ within a session. The package depends on several other packages, which can be obtained from the same repository and from \textsc{cran}. The source code is also hosted at publicly-available repositories; see the \texttt{DESCRIPTION} file. \section*{What the package provides} There are three main topics for which the package provides functionality: \begin{description} \item[Keeping track of transactions] The package provides functions that work with journals (sometimes called blotters). See \texttt{?journal} and \texttt{?position}. \item[Computing \textsc{p}\&\textsc{l} and returns] See \texttt{?pl}, \texttt{?returns}, \texttt{?rc} and \texttt{?unit\_prices}. \item[Testing strategies] See \texttt{?btest}. \end{description} % Several examples will be provided in the next section. % Other than that, there are a number of helper % functions. \noindent All details are in the manual: \begin{center} \url{https://enricoschumann.net/R/packages/PMwR/manual/PMwR.html} \end{center} \noindent I am grateful for comments, suggestions and corrections. Please send bug reports directly to the package maintainer, for instance by using \texttt{bug.report}.\index{bug reports} <>= utils::bug.report("[PMwR] Unexpected behaviour in function ", maintainer("PMwR"), package = "PMwR") @ \noindent Applications, as long as they are finance-related, should be discussed on the \texttt{R-SIG-Finance} mailing list:\smallskip \url{https://stat.ethz.ch/mailman/listinfo/r-sig-finance} %% \noindent An \textsc{rss} feed of the package %% \textsc{news} file is available at\smallskip %% \url{https://enricoschumann.net/R/packages/PMwR/PMwR_news.xml}\,. %% % \medskip % \section*{Examples} % <>= % library("PMwR") % @ % \subsection*{Keeping track of transactions} % List of transactions can be handled as so-called journals. % Let us invent a small trading history. % <>= % J <- read.table(text = " % instrument , timestamp , amount , price % MSFT , 2020-05-04 , 10 , 178.0 % MSFT , 2020-05-04 , -10 , 178.5 % MSFT , 2020-05-05 , 20 , 180.7 % AMZN , 2020-05-06 , 5 , 2315.0", % header = TRUE, sep = ",", strip.white = TRUE) % J$timestamp <- as.Date(J$timestamp) % J <- as.journal(J) % J % @ % A \texttt{summary} method provides some stats. % <>= % summary(J, by.instrument = TRUE, by.timestamp = TRUE) % @ % The function \texttt{positions} aggregates the trades to a position. % <>= % position(J) % @ % We may as well specifiy one or more timestamps % <>= % position(J, when = as.Date("2020-05-04") + 0:2) % @ % \subsection*{Computing P\&L and Returns, and valuing open positions} % We continue the previous example. % <>= % pl(J) % @ % pl(along.timestamp, vprice) % \subsection*{Testing investment strategies} % <<>>= % library("PMwR") % timestamp <- as.Date(row.names(DAX)) % P <- DAX[[1]] % signal <- function(k) { % av <- sum(Close(n = k))/k % if (Close() > av) % 1 % else % -1 % } % btest(P, signal, k = 5, % b = 5) % @ % single instrument: moving average % returns in unit of instrument % vol targeting % A trivial strategy \end{document}