05-30-2018, 03:26 AM
It is very important for you to measure the accuracy of a trading strategy on the historical data. This can clarify a lot about the trading strategy. I have an MACD Trading Strategy in mind. MACD (Moving Average Convergence/Divergence) is a powerful oscillator that is used to measure the momentum in the market. When MACD is rising we have an up momentum in the market. Similarly when MACD is decreasing we have a down momentum in the market.
MACD Trading Strategy
First we determine the market direction on daily timeframe using MACD. If MACD is increasing on daily timeframe we will assume market direction as up. Similarly if MACD is decreasing on daily timeframe we will assume market direction as down. We will use M5 and M15 to enter into a trade in the direction of daily timeframe. You can read about this MACD Trading Strategy and download its MQL4 code in this thread.
Important question that comes to mind is what is the accuracy of this MACD Trading Strategy. First we determine the accuracy of the MACD Trading Strategy on daily timeframe. In MQL4 backtesting is a cumbersome and tedious job. With R you can easily do the backtesting on historical data in just a few minutes. Below is the R code that will backtest the MACD Trading Strategy on daily timeframe.
> #backtesting the MACD Trading Strategy
> #Regression Model
> # Import the csv file
> data1 <- read.csv("D:/Shared/MarketData/GBPUSD1440.csv",
+ header=FALSE)
> colnames(data1) <- c("Date", "Time", "Open", "High",
+ "Low", "Close", "Volume")
> library(quantmod)
Loading required package: xts
Loading required package: zoo
Attaching package: ‘zoo’
The following objects are masked from ‘package:base’:
as.Date, as.Date.numeric
Loading required package: TTR
Version 0.4-0 included new data defaults. See ?getSymbols.
Learn from a quantmod author: https://www.datacamp.com/courses/importi...-data-in-r
> data1 <- as.xts(data1[,-(1:2)],
+ as.POSIXct(paste(data1[,1],data1[,2]),
+ format='%Y.%m.%d %H:%M'))
>
> tail(data1)
Open High Low Close Volume
2018-01-30 1.40719 1.41656 1.39787 1.41542 55936
2018-01-31 1.41540 1.42315 1.41206 1.41980 60953
2018-02-01 1.41983 1.42772 1.41585 1.42649 52142
2018-02-02 1.42650 1.42769 1.41017 1.41153 54029
2018-02-04 1.41074 1.41125 1.40816 1.41094 2790
2018-02-05 1.41090 1.41493 1.39986 1.40001 33006
> #number of rows
> x1 <- nrow(data1)
>
> data1$MACD <- MACD(data1$Close, 12, 26, 9, maType="EMA" )
> data1 <- data1[,-7]
> data1 <- data1[, -(1:3)]
> data1 <- data1[,-2]
> data1$Close <- lag(data1[,1], k=-1)
> data1 <- cbind(data1, data1[,2], data1[,2])
> data1 <- data1[, c(1,4,3,2)]
> data1$macd.1 <- lag(data1$macd.1, k=1)
> data1$macd <- lag(data1$macd.1, k=1)
> data1$Direction <- diff(data1$Close)
> data1 <- data1[-(1:30),]
> data1 <- as.data.frame(data1)
> data1$Direction <- ifelse(data1$Direction > 0, 1, -1)
> #downtrend
> data1$Prediction <- ifelse(((data1$macd.2 < data1$macd.1 &
+ data1$macd < data1$macd.1) |
+ (data1$macd.2 > data1$macd.1 &
+ data1$macd.1 > data1$macd)),-1,
+ ifelse(((data1$macd.2 > data1$macd.1 &
+ data1$macd > data1$macd.1) |
+ (data1$macd.2 < data1$macd.1 &
+ data1$macd.1 < data1$macd)),1 , 0))
>
> tail(data1)
Close macd.2 macd.1 macd Direction Prediction
2018-01-30 1.41980 1.2267670 1.231510 1.279458 1 1
2018-01-31 1.42649 1.2336942 1.226767 1.231510 1 1
2018-02-01 1.41153 1.2626800 1.233694 1.226767 -1 -1
2018-02-02 1.41094 1.1858707 1.262680 1.233694 -1 -1
2018-02-04 1.40001 1.1087917 1.185871 1.262680 -1 1
2018-02-05 NA 0.9737999 1.108792 1.185871 NA 1
> head(data1)
Close macd.2 macd.1 macd Direction Prediction
2011-06-23 1.5957 -0.4486519 -0.3784313 -0.3005196 -1 1
2011-06-24 1.5951 -0.5298428 -0.4486519 -0.3784313 -1 1
2011-06-26 1.5985 -0.5906509 -0.5298428 -0.4486519 1 1
2011-06-27 1.5985 -0.6149452 -0.5906509 -0.5298428 -1 1
2011-06-28 1.6064 -0.6270967 -0.6149452 -0.5906509 1 1
2011-06-29 1.6018 -0.5903830 -0.6270967 -0.6149452 -1 1
> data1 <- data1[,c(1,2,3,4,6,5)]
> library(caret)
Loading required package: lattice
Loading required package: ggplot2
Find out what's changed in ggplot2 at http://github.com/tidyverse/ggplot2/releases.
> #calculate acccuracy on the training data
> confusionMatrix(data1$Direction, data1$Prediction)
Confusion Matrix and Statistics
Reference
Prediction -1 1
-1 537 503
1 511 509
Accuracy : 0.5078
95% CI : (0.4859, 0.5296)
No Information Rate : 0.5087
P-Value [Acc > NIR] : 0.5439
Kappa : 0.0154
Mcnemar's Test P-Value : 0.8260
Sensitivity : 0.5124
Specificity : 0.5030
Pos Pred Value : 0.5163
Neg Pred Value : 0.4990
Prevalence : 0.5087
Detection Rate : 0.2607
Detection Prevalence : 0.5049
Balanced Accuracy : 0.5077
'Positive' Class : -1
You can check above the accuracy of MACD Trading Strategy on daily timeframe is just 50% which is just like flipping the coin.
MACD Trading Strategy
First we determine the market direction on daily timeframe using MACD. If MACD is increasing on daily timeframe we will assume market direction as up. Similarly if MACD is decreasing on daily timeframe we will assume market direction as down. We will use M5 and M15 to enter into a trade in the direction of daily timeframe. You can read about this MACD Trading Strategy and download its MQL4 code in this thread.
Important question that comes to mind is what is the accuracy of this MACD Trading Strategy. First we determine the accuracy of the MACD Trading Strategy on daily timeframe. In MQL4 backtesting is a cumbersome and tedious job. With R you can easily do the backtesting on historical data in just a few minutes. Below is the R code that will backtest the MACD Trading Strategy on daily timeframe.
> #backtesting the MACD Trading Strategy
> #Regression Model
> # Import the csv file
> data1 <- read.csv("D:/Shared/MarketData/GBPUSD1440.csv",
+ header=FALSE)
> colnames(data1) <- c("Date", "Time", "Open", "High",
+ "Low", "Close", "Volume")
> library(quantmod)
Loading required package: xts
Loading required package: zoo
Attaching package: ‘zoo’
The following objects are masked from ‘package:base’:
as.Date, as.Date.numeric
Loading required package: TTR
Version 0.4-0 included new data defaults. See ?getSymbols.
Learn from a quantmod author: https://www.datacamp.com/courses/importi...-data-in-r
> data1 <- as.xts(data1[,-(1:2)],
+ as.POSIXct(paste(data1[,1],data1[,2]),
+ format='%Y.%m.%d %H:%M'))
>
> tail(data1)
Open High Low Close Volume
2018-01-30 1.40719 1.41656 1.39787 1.41542 55936
2018-01-31 1.41540 1.42315 1.41206 1.41980 60953
2018-02-01 1.41983 1.42772 1.41585 1.42649 52142
2018-02-02 1.42650 1.42769 1.41017 1.41153 54029
2018-02-04 1.41074 1.41125 1.40816 1.41094 2790
2018-02-05 1.41090 1.41493 1.39986 1.40001 33006
> #number of rows
> x1 <- nrow(data1)
>
> data1$MACD <- MACD(data1$Close, 12, 26, 9, maType="EMA" )
> data1 <- data1[,-7]
> data1 <- data1[, -(1:3)]
> data1 <- data1[,-2]
> data1$Close <- lag(data1[,1], k=-1)
> data1 <- cbind(data1, data1[,2], data1[,2])
> data1 <- data1[, c(1,4,3,2)]
> data1$macd.1 <- lag(data1$macd.1, k=1)
> data1$macd <- lag(data1$macd.1, k=1)
> data1$Direction <- diff(data1$Close)
> data1 <- data1[-(1:30),]
> data1 <- as.data.frame(data1)
> data1$Direction <- ifelse(data1$Direction > 0, 1, -1)
> #downtrend
> data1$Prediction <- ifelse(((data1$macd.2 < data1$macd.1 &
+ data1$macd < data1$macd.1) |
+ (data1$macd.2 > data1$macd.1 &
+ data1$macd.1 > data1$macd)),-1,
+ ifelse(((data1$macd.2 > data1$macd.1 &
+ data1$macd > data1$macd.1) |
+ (data1$macd.2 < data1$macd.1 &
+ data1$macd.1 < data1$macd)),1 , 0))
>
> tail(data1)
Close macd.2 macd.1 macd Direction Prediction
2018-01-30 1.41980 1.2267670 1.231510 1.279458 1 1
2018-01-31 1.42649 1.2336942 1.226767 1.231510 1 1
2018-02-01 1.41153 1.2626800 1.233694 1.226767 -1 -1
2018-02-02 1.41094 1.1858707 1.262680 1.233694 -1 -1
2018-02-04 1.40001 1.1087917 1.185871 1.262680 -1 1
2018-02-05 NA 0.9737999 1.108792 1.185871 NA 1
> head(data1)
Close macd.2 macd.1 macd Direction Prediction
2011-06-23 1.5957 -0.4486519 -0.3784313 -0.3005196 -1 1
2011-06-24 1.5951 -0.5298428 -0.4486519 -0.3784313 -1 1
2011-06-26 1.5985 -0.5906509 -0.5298428 -0.4486519 1 1
2011-06-27 1.5985 -0.6149452 -0.5906509 -0.5298428 -1 1
2011-06-28 1.6064 -0.6270967 -0.6149452 -0.5906509 1 1
2011-06-29 1.6018 -0.5903830 -0.6270967 -0.6149452 -1 1
> data1 <- data1[,c(1,2,3,4,6,5)]
> library(caret)
Loading required package: lattice
Loading required package: ggplot2
Find out what's changed in ggplot2 at http://github.com/tidyverse/ggplot2/releases.
> #calculate acccuracy on the training data
> confusionMatrix(data1$Direction, data1$Prediction)
Confusion Matrix and Statistics
Reference
Prediction -1 1
-1 537 503
1 511 509
Accuracy : 0.5078
95% CI : (0.4859, 0.5296)
No Information Rate : 0.5087
P-Value [Acc > NIR] : 0.5439
Kappa : 0.0154
Mcnemar's Test P-Value : 0.8260
Sensitivity : 0.5124
Specificity : 0.5030
Pos Pred Value : 0.5163
Neg Pred Value : 0.4990
Prevalence : 0.5087
Detection Rate : 0.2607
Detection Prevalence : 0.5049
Balanced Accuracy : 0.5077
'Positive' Class : -1
You can check above the accuracy of MACD Trading Strategy on daily timeframe is just 50% which is just like flipping the coin.
Subscribe My YouTube Channel:
https://www.youtube.com/channel/UCUE7VPo...F_BCoxFXIw
Join Our Million Dollar Trading Challenge:
https://www.doubledoji.com/million-dolla...challenge/