Technical Analysis with R
Preface to the second edtion
Coffee for me?
Preface to the first edtion
Acknowledgement
Sturcture of the book
About Author
I R Basics
1
Introduction to R
1.1
What is R and RStudio?
1.2
Download
1.2.1
Download R
1.2.2
Download R Studio
1.3
Using RStduio
1.4
Interactive console
1.5
Code Editor
1.6
Assignment
1.7
Hello World
1.8
Script and Data Files
1.9
Readability
1.10
End of Chapter Exercise
2
Working with Data
2.1
Data Type
2.1.1
Logical
2.1.2
Integer
2.1.3
Numeric
2.1.4
Complex
2.1.5
Character
2.2
Data type conversion
2.2.1
Numeric to Character
2.2.2
Character to Number
2.2.3
Character to Date
2.2.4
Character to Date-time
2.3
Data Structure
2.3.1
Vector
2.3.2
Factor
2.3.3
Matrix
2.3.4
Array
2.3.5
Dataframe
2.3.6
List
2.4
Functions
2.4.1
Defining a function
2.4.2
Vectorized operation
2.4.3
One-line function
2.4.4
Function with default input
2.4.5
Assignment of function outputs
2.4.6
Help on function
2.4.7
Source code of a function
2.5
Financial Data
2.5.1
Time Series data
2.6
Extensible time series
2.7
Data Management
2.7.1
Loading built-in dataset
2.7.2
Peeping Data
2.7.3
Sorting Data
2.7.4
Joining dataframes
2.7.5
Stacking Dataframe
2.7.6
Selecting Columns (Variables)
2.7.7
Selecting Rows (observation) based on row number
2.7.8
Selecting Rows (observation) based on condition
2.7.9
Selecting rows (observations) and columns (variables)
2.7.10
Create New Column
2.7.11
Remove Duplicated Observation
2.7.12
Collapse Data by Group
2.8
Packages
2.8.1
dplyr package
2.8.2
Tidyr package
2.8.3
reshape package
2.8.4
Application to World Bank Data
2.9
External Datasets
2.9.1
Working Directory
2.9.2
Import from CSV
2.9.3
Import from Stata
2.9.4
Import from Excel
2.9.5
Export to CSV
2.9.6
Export to xlsx
2.9.7
Export to Word: Knitr
2.9.8
Export Table: stargazer
2.10
End of Chapter Exercise
Data Conversion
Vector
Factor
Vector
Matrix
Function
Packages
3
Programming
3.1
Logical statements
3.1.1
Relational Operators
3.1.2
Logical Operators
3.1.3
Operators on vector
3.2
Conditional
3.2.1
Simple if statement
3.2.2
if-else statement
3.2.3
Application: Nested-if statement
3.2.4
Application: Checking Data Type
3.2.5
Application: Missing data
3.3
Loop
3.3.1
While loop
3.3.2
Loop over vectors
3.3.3
For Loop over integers
3.3.4
Simple loop using which
3.3.5
Simple loop using ifelse
3.4
Apply Family
3.4.1
apply
3.4.2
sapply and lapply
3.5
Reduce
3.5.1
On vector
3.5.2
On list
3.6
Tricks
3.6.1
Timing code
3.6.2
Suppress annoying message
3.6.3
All files names in a folder
3.6.4
Merging all csv files in a folder
3.7
End of Chapter Exercise
Conditional
While loop
For Loop
Application
II Technical Analysis
4
Data and Charting
4.1
Quantmod package
4.2
Downloading data
4.3
Charting
4.3.1
Line Chart
4.3.2
Bar Chart
4.3.3
Candle Stick Chart
4.4
End of Chapter Exercise
5
Technical Indicators
5.1
TTR package
5.2
Simple Moving Average (SMA)
5.2.1
TTR
5.2.2
Trading signal
5.2.3
Charting
5.3
Exponential moving average (EMA)
5.3.1
TTR
5.3.2
Trading signal
5.3.3
Charting
5.4
Bollinger band
5.4.1
TTR
5.4.2
Trading Signal
5.4.3
Charting
5.5
Momentum
5.6
TTR
5.6.1
Trading signal
5.6.2
Charting
5.7
Rate of Change (ROC)
5.8
TTR
5.8.1
Trading signal
5.8.2
Charting
5.9
MACD
5.9.1
TTR
5.9.2
Trading signal
5.9.3
Charting
5.10
Relative Strength Index (RSI)
5.11
TTR
5.11.1
Trading signal
5.11.2
Charting
5.12
Custom indicators
5.13
End of Chapter Exercise
6
Candle Stick Pattern
6.1
Basis of candle stick
6.1.1
Remark: More compact code
6.2
Sizes
6.2.1
Movement
6.3
Doji
6.3.1
Doji
6.3.2
Dragonfly Doji
6.3.3
Gravestone Doji
6.4
Hammers
6.4.1
Hammer and Hang Man
6.4.2
Inverted Harmer and Shooting Stars
6.5
Engulfing Patterns
6.5.1
Bullish Engulfing
6.6
Bearish Enguling
6.7
Harami
6.7.1
Bullish Harami
6.7.2
Bearish Harami
6.8
Median Reversal
6.8.1
Piercing Line
6.8.2
Dark cloud cover
6.9
Two in a row
6.9.1
Kicking up
6.9.2
Kicking Down
6.10
Three in a row
6.10.1
Three white solders
6.10.2
Three Black Crows
6.11
Star
6.11.1
Morning star
6.11.2
Evening Star
6.12
Three Methods
6.12.1
Rising Three
6.12.2
Falling three
6.13
CandleStickPattern Package
6.14
End of Chapter exercise
Doji
Hammer
Englufing
Harami
Median reversal
Two in a row
Three in a row
Star
Three methods
III Testing Trading Rule
7
Day trading
7.1
Performance Evaluation
7.2
Simple filter buy rule
7.3
Simple fiter buy-sell
7.4
Buy rule based on RSI
7.5
Remark: More efficient code
7.6
Comparing rules
7.7
Combining two indicators: EMA and RSI
7.8
Trading Size
7.9
End of Chapter Exercise
8
Intra-day trading rule
8.1
Interday Trading
8.2
Blotter Package
8.2.1
Buy and hold
8.2.2
Buy Filter Rule
8.3
End of chapter exerecise
Interday trading
Blotter Package
Further questions
9
Complex trading rule
9.1
Install standard packages
9.2
Install custom packages
9.3
Simple Filter Rule
9.3.1
Step 1: Initialization
9.3.2
Step 2: Define indicator
9.3.3
Step 3: Trading Signals
9.3.4
Step 4. Trading Rules
9.3.5
Step 5. Evaluation Results
9.4
Buy and Hold
9.4.1
Step 1: Initialization
9.4.2
Steps 2-4: Applying trading rule
9.4.3
Step 5: Evaluation
9.5
SMA Rule
9.5.1
Step 1: Initialization
9.5.2
Step 2: Indicator
9.5.3
Step 3: Signals
9.5.4
Step 4: Rules
9.5.5
Step 5: Evaluation
9.6
SMA rule with volatility filter
9.6.1
Step 1: Initialization
9.6.2
Step 2: Indicator
9.6.3
Step 3: Signals
9.6.4
Step 4: Rules
9.6.5
Step 5: Evaluation
9.7
End of chapter Exercise
IV Appendix
10
Statistical Analysis
10.1
Basic Statistics
10.2
Random number
10.3
Regression
10.3.1
Simple linear regression
10.3.2
Muliple linear regression
10.3.3
Interaction terms
10.3.4
Robust standard error
10.4
Time Series Analysis
10.4.1
Time Series data
10.4.2
Extensible Time Series Data
10.4.3
ARMA
10.4.4
GARCH
10.4.5
VAR
10.5
Panel Data
10.5.1
Clustering
10.5.2
Fixed Effect Model
10.6
Simulation
10.6.1
Random Walk
10.6.2
Roll Model
11
Plotting
11.1
ggplot2 package
11.1.1
Qplot
11.1.2
ggplot
11.1.3
Aesthetic
11.1.4
Decoration
11.1.5
Multiple Plots
11.1.6
Basic themes
11.1.7
ggthemes package
11.1.8
Application: Worldbank
11.2
dygraphs package
11.2.1
Get Data Using Quantmod
11.2.2
Standard dynamic graph
11.2.3
Event line
11.2.4
Shading
11.2.5
Candle Chart
12
Scrapping
12.1
PDF files
12.1.1
Offline pdf file
12.1.2
Online pdf file
12.1.3
Scrapping through massive download
12.2
Scrapping from Web
12.2.1
Wikipedia Table
12.2.2
Other Websites
13
Dynamic Report
13.1
Sweave: R code in LaTex
13.2
Knitr: Rmarkdown
Published with bookdown
Techincal Analysis with R (second edition)
Chapter 12
Scrapping
We will cover two common ways to extract files that is not from database directly:
pdf files, and
html files.