If you have been trading on MT4 for sometime, you know this that you can only watch these timeframes: 1M, 5M, 15M, 30M, 1H, 4H, Daily, Weekly and Monthly. Sometimes you would like to take a look at 3M chart or for that matter 2 hour chart or 8 hour chart and maybe 15 hour chart but on MT4 you cannot do it. People have developed indicators in MQL4 that draw the custom candlestick charts in a window below the main window which doesn’t give a good feel when you look at those custom charts.

Candlestick patterns are very important. A candle on 4 hour chart is a powerful signal that encompasses price action for the last 4 hours. How about 8 hour timeframe candle? Obviously an 8 hour candle is 2 times more powerful than a 4 hour candlestick as it encompasses 8 hours of price action. In the same manner a 12 hour candle is more powerful than a 8 hour candle. Instead of waiting for the daily candle to close you can look at 12 hour candle and know what price action is planning to do in the next 24 hours. **Did you download your Cash Flow Channels System FREE PDF plus indicators**? The important question that comes to mind is how to draw candlestick charts on these custom timeframes?

### How to draw 2 hour candlestick chart?

In this post we will be using R data analysis and machine learning software to write a script that will draw candlestick chart for any custom timeframe that we want. **Last time we did a random walk with GBPUSD** and found that this model cannot make good predictions. We promised in the last post to improve this model. We can improve this model by making a Kalman Filter that uses Bayesian analysis to closely follow price action. We will giving you the Kalman filter script in a new post in the near future.

Another problem that we face in making predictions is the fact that as we increase the number of steps ahead, predictive accuracy decreases. We can improve predictive accuracy by using the data to make predictions. You should have R and RStudio installed. You should be familiar with R language. Below is the screenshot of 2 hour candle. Did you see the long tail candle? This is the candle that was formed when GBPUSD crashed 1000 pips from 1.24000 to 1.14000 in just 1 minute. You can look at the chart and see that the candles are telling about the downtrend. In a future post we will show you how everything was written on the charts for a big downward move on GBPUSD after it broke the support found after the Brexit referendum. This chart has been made using R Quantmod library.

> #import the data > > data <- read.csv("E:/MarketData/GBPUSD60.csv", header = FALSE) > > > > colnames(data) <- c("Date", "Time", "Open", "High", + "Low", "Close", "Volume") > > str(data) 'data.frame': 9727 obs. of 7 variables: $ Date : Factor w/ 408 levels "2015.03.18","2015.03.19",..: 1 1 1 1 1 1 1 1 1 1 ... $ Time : Factor w/ 32 levels "00:00","01:00",..: 8 10 11 12 13 14 15 16 18 20 ... $ Open : num 1.48 1.48 1.48 1.48 1.48 ... $ High : num 1.48 1.48 1.48 1.48 1.48 ... $ Low : num 1.48 1.48 1.48 1.48 1.47 ... $ Close : num 1.48 1.48 1.48 1.48 1.48 ... $ Volume: int 1769 967 2207 2460 6785 9570 17085 10025 8877 7332 ... > > x <- nrow(data) > > > library(quantmod) > > #convert this data to n timeframe > > n=2 > > #define lookback > > lb=300 > > data1 <-data.frame(matrix(0, ncol=6, nrow=300)) > > colnames(data1) <- c("Date", "Time", "Open", "High", + "Low", "Close") > > # run the sequence to convert to a new timeframe > > for ( k in (1:lb)) + { + data1[k,1] <- as.character(data[x-lb*n+n*(k-1),1]) + data1[k,2] <- as.character(data[x-lb*n+n*(k-1),2]) + data1[k,3] <- data[x-lb*n+n*(k-1)+1,3] + data1[k,6] <- data[x-lb*n+n*k,6] + data1[k,4] <- max(data[(x-lb*n+n*(k-1)+1):(x-lb*n+k*n), 4:5]) + data1[k,5] <- min(data[(x-lb*n+n*(k-1)+1):(x-lb*n+k*n), 4:5]) + } > > tail(data1) Date Time Open High Low Close 295 2016.10.07 02:00 1.23892 1.24843 1.23490 1.23974 296 2016.10.07 04:00 1.23980 1.24438 1.23848 1.24293 297 2016.10.07 06:00 1.24294 1.24696 1.24231 1.24626 298 2016.10.07 08:00 1.24622 1.24776 1.24185 1.24587 299 2016.10.07 10:00 1.24590 1.24607 1.23141 1.23153 300 2016.10.07 12:00 1.23148 1.23969 1.22282 1.23529 > > #convert data1 into an xts object > > data2 <- as.xts(data1[,-(1:2)], as.POSIXct(paste(data1[,1],data1[,2]), + format='%Y.%m.%d %H:%M')) > > > candleChart(data2, + theme='white', type='candles', subset='last 3 days')

In the above script. n is the timeframe. We have read GBPUSD H1 csv. In the above script, n is 2. This will make a 2 hour candle. We could have selected n as 10 which would have made 10 hour candle. Above script calculated 300 candles based on the lookback variable lb. If we had selected lb as 500, it would have calculated 500 candles. We can read M1 csv candle and select n as 2 to draw a 2 minute candle. We can read M5 candle and select n as 2 which will make a 10 minute candle. **Watch these video tutorials on forex breakout strategies**.

### How to draw 8 Hour Candlestick Chart?

In the same manner we could have read H4 csv and selected n as 2 which would have made 8 hour candle. Below is a 8 hour candlestick chart. Can you see the inverted hammer at the start of the chart which is signalling a strong downtrend.

You might be wondering why we need to draw different timeframe candles. The purpose is to predict the next candle using a neural network or a support vector machine. **Read this post in which we explain how we made 270% in 1 week**. In the script below we made an 8 hour candle.

> #import the data > > data <- read.csv("E:/MarketData/GBPUSD240.csv", header = FALSE) > > > > colnames(data) <- c("Date", "Time", "Open", "High", + "Low", "Close", "Volume") > > str(data) 'data.frame': 10513 obs. of 7 variables: $ Date : Factor w/ 1766 levels "2009.12.21","2009.12.22",..: 1 1 1 1 1 1 2 2 2 2 ... $ Time : Factor w/ 13 levels "00:00","00:05",..: 1 3 5 7 9 11 1 3 5 7 ... $ Open : num 1.61 1.62 1.61 1.61 1.61 ... $ High : num 1.62 1.62 1.62 1.61 1.61 ... $ Low : num 1.61 1.61 1.61 1.61 1.61 ... $ Close : num 1.62 1.61 1.61 1.61 1.61 ... $ Volume: int 5892 6251 11887 12436 10584 5136 6234 3828 12690 13307 ... > > x <- nrow(data) > > > 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. > > #convert this data to n timeframe > > n=2 > > #define lookback > > lb=300 > > data1 <-data.frame(matrix(0, ncol=6, nrow=300)) > > colnames(data1) <- c("Date", "Time", "Open", "High", + "Low", "Close") > > # run the sequence to convert to a new timeframe > > for ( k in (1:lb)) + { + data1[k,1] <- as.character(data[x-lb*n+n*(k-1),1]) + data1[k,2] <- as.character(data[x-lb*n+n*(k-1),2]) + data1[k,3] <- data[x-lb*n+n*(k-1)+1,3] + data1[k,6] <- data[x-lb*n+n*k,6] + data1[k,4] <- max(data[(x-lb*n+n*(k-1)+1):(x-lb*n+k*n), 4:5]) + data1[k,5] <- min(data[(x-lb*n+n*(k-1)+1):(x-lb*n+k*n), 4:5]) + } > > tail(data1) Date Time Open High Low Close 295 2016.10.03 04:00 1.29403 1.29452 1.28460 1.28612 296 2016.10.03 12:00 1.28611 1.28718 1.28177 1.28410 297 2016.10.03 20:00 1.28363 1.28609 1.28166 1.28423 298 2016.10.04 04:00 1.28416 1.28494 1.27364 1.27632 299 2016.10.04 12:00 1.27634 1.27750 1.27196 1.27285 300 2016.10.04 20:00 1.27259 1.27459 1.27206 1.27326 > > #convert data1 into an xts object > > data2 <- as.xts(data1[,-(1:2)], as.POSIXct(paste(data1[,1],data1[,2]), + format='%Y.%m.%d %H:%M')) > > > candleChart(data2, + theme='white', type='candles', subset='last 3 days')

In the above scrip we read a GBPUSD 240 minute csv (240 minutes are equal to 4 hours). Now we need to make a predictive model using either fuzzy logic, neural network or support vector machines. Suppose we succeed in making a good predictive model that can predict the next candle. Using the above script, we can predict the 15 hour candle which will tell us the predicted price after 15 hours.

## 0 Comments