knitr::opts_chunk$set(echo = TRUE) library(magrittr)
CFM (Casablanca Financial Market) is a R package that has been designed to simplify financial programming workflow with Casablanca stock exchange data.
Currently, it implements 5 mains functions:
get_stocks
: Returns a list of stock symbols available in Casablanca stock exchange.get_indexes
: Returns a list of all market indexes including MASI, MADEX...stock_data
: Returns historical prices for a given stock symbol during the specified date range.adjusted_data
: Same as stock_data
function but it includes also the adjusted prices.index_data
: Returns historical values for a given index during the specified date range.library(cfm) all_symbols <- cfm::get_stocks() length(all_symbols)
dplyr::data_frame(Firm = all_symbols) %>% head(n = 13) %>% knitr::kable()
all_indexes <- cfm::get_indexes() all_indexes %>% nrow()
all_indexes %>% head(n = 15) %>% knitr::kable()
iam <- cfm::stock_data('ITISSALAT AL-MAGHRIB', '01/01/2015', '11/09/2017') iam %>% dplyr::as_data_frame() %>% dplyr::select(Session, `Reference price`, `Last price`, `Number of shares traded`) %>% tail(n = 20) %>% knitr::kable()
masi <- cfm::index_data('MASI', '01/01/2015', '11/09/2017') masi %>% dplyr::as_data_frame() %>% tail(n = 20) %>% knitr::kable()
masi %>% dplyr::inner_join(iam, by = 'Session') %>% dplyr::select(Session, `Last price`, Value) %>% dplyr::mutate(`Last price` = scales::rescale(`Last price`, to = c(0, 1))) %>% dplyr::mutate(Value = scales::rescale(Value, to = c(0, 1))) %>% ggplot2::ggplot(ggplot2::aes(x = Session)) + ggplot2::geom_line(ggplot2::aes(y = `Last price`, colour = 'IAM')) + ggplot2::geom_line(ggplot2::aes(y = Value, colour = 'MASI')) + ggplot2::ylab('Scaled Prices') + ggplot2::theme_minimal() + ggplot2::theme(legend.title = ggplot2::element_blank(), panel.grid.major = ggplot2::element_blank(), panel.grid.minor = ggplot2::element_blank()) + ggplot2::ggtitle('ITISSALAT AL-MAGHRIB Price vs MASI Value', subtitle = 'From 01/01/2015 to 11/09/2017')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.