Fuzzy Logic Candlestick Trading System R Code I

In this post I will discuss a fuzzy logic candlestick trading system. You might be wondering what is this fuzzy logic stuff. Fuzzy logic has been ignored by the mainstream finance for a long time despite the fact that it has been used in many industrial application. Did you read the post on how to make 100-800 pips per trade? In trading, fuzzy logic is seldom used. In MQL5, there is a fuzzy logic library that we can use to code our indicators. But that library does basic stuff. What we need is the capability to build neural networks on our fuzzy logic variables. We also need to do data mining. This can be done using R language.R is a powerful scripting language that has more than 3000 packages or what you call libraries for doing data science and machine learning. Fuzzy logic is a powerful concept. If you are interested,  I have developed a course on Fuzzy Logic For Traders. In this course I take you by hand and explain everything you need to know about fuzzy logic to start applying it to your trading. I develop a few trading systems based on fuzzy logic in the course.

Algorithmic Trading Is The Future

Algorithmic trading is about to takeover the world of trading. Today more than 70% of the trades that are being placed at Wall Street are being placed through algorithmic trading systems. The days of manual trading are coming to an en. If you want, this is the time to learn algorithmic trading. You can take a look at our course Java For Algorithmic Trading. Java is a powerful system programming language. In this course we teach you how to do machine learning and data science using Java. Then we show you how to build your trading systems on Dukascopy JForex and Interactive Brokers Trader Work Station. So if you really want to master algorithmic trading, you should take this course as this course is full of practical examples and totally focuses on trading. This is unlike other courses that take their examples of other areas of interest. Java has got its powerful fuzzy logic library JFuzzyLogic. In this course we also teach you how to use this JFuzzyLogic library in developing your own fuzzy logic trading systems.

If you are into algorithmic trading, then you need to also master R language. In this post we are going to use R scripting language. R is the first language that we use to prototype our algorithmic trading system. Once we build the system using R and have thoroughly tested it and believe that we have a very good system, we can further develop the system using Python, Java or C++. Why we need to do that? We need to do that for the reason that R is a slow language. C++ is the fastest language. After that comes Java and Python. We would like to build the algorithmic system with a language that gets executed fastest. So once we have done the testing we then use Java or C++ to develop the final product. Did you read the post on British Pound Flash Crash? This flash crash had been caused by a rogue algorithm. This is what a veteran trader of many decades told me. According to him markets have changed drastically in the last few years. Today algorithms are trading against algorithms and whoever wins the nuclear arms race bring the big bucks home.

What is Fuzzy Logic?

Now coming back to the topic of fuzzy logic. When we talk of traditional logic theory, there are only two values. Yes or No. True of False. However, in everyday life, things are fuzzy. Thing are never black and white. There are many shades of grey in between the black and white values. We thing it might rain. It is very likely that it will rain. It is somewhat likely that it will rain. It is possible that it will rain. It may rain. These are linguistic variables that we use to describe our degree of belief that it might rain today. When we use traditional logic, we can only say yes it will rain or no it will not rain. But when using fuzzy logic we can use our degree of belief to express ourselves more confidently by that I believe that it is somewhat likely that it will rain. This degree of belief that it will rain somewhat likely comes in between the black and while value of rain or no rain. We can express rain as 1 and no rain as 0. Somewhat likely to rain will come in between this 0 and 1 value.Did you read the post on how to use support vector machines in daily trading?

Now most examples of fuzzy logic use MATLAB. MATLAB license is expensive so I cannot use it. We will be using R language to build our fuzzy logic trading system. Fuzzy logic controllers are being used widely in industry. You can well imagine from this how good fuzzy logic is in controlling industrial processes. The first application of fuzzy logic took place in Japan in controlling the Bullet trains. Watch the video below that explains how to develop a fuzzy logic controller. The video below explains how you are going to design an egg boiling fuzzy logic robot.

Developing a trading system is also like developing a fuzzy logic controller that boils an egg in the above video. A trading system also have two outputs buy and sell just like a fuzzy logic controller that has two outputs on and off. Candlestick patterns are vague and imprecise. It takes a lot of experience to decipher candlestick patterns. Doji, Harami, Engulfing patterns are good names that can mislead you into opening  a losing trade. You need a lot of experience in dealing with candlestick patterns. Did you watch this video on one candlestick pattern that changed everything for me? Below is a video on how to use fuzzy logic in air conditioning using MATLAB. Most of the literature that you will find will be in MATLAB. Wny? MATLAB is a powerful machine learning and data science language. The problem is MATLAB is not open source. You will have to buy a commercial license that can cose $2K to $3K per year. If you want modules added than you will have to pay for the modules as well that can cost $500 to $2000 over and above the basic license cost. Despite the heavy cost, there is no denying the fact that MATLAB is user friendly and very powerful.

Now after watching the above video you should get a fair idea of how these fuzzy logic controllers are designed. Now back to our fuzzy logic daily trading system. Before we continue watch this video on how to trade reversals naked. In trading we are trying to identify the reversal points. In essence this is what we are doing all the time in trading. Buy when the price hits a support and reverses and sell when the price hits resistance and reverses again and vice versa.

Fuzzy Logic Candlestick Pattern Prediction Algorithm

What we want is a method that can predict the closing price for the next candle. It can be the weekly candle. It can be the daily candle. It can be the hourly candle. We will use fuzzy logic in predicting the next candle closing price. Did you read the post on how to make 400 pips with a 20 pips stop loss? So trading is all about finding the turning points in the market. Let’s start now. First we need to load the weekly GBPUSD data in R. Data preprocessiong is very important.

> # Import the csv file
> quotes <- read.csv("E:/MarketData/GBPUSD10080.csv", header=FALSE)
> 
> colnames(quotes) <- c("Date", "Time", "Open", "High",
+                     "Low", "Close", "Volume")
> 
> #number of candles in the dataset
> x <- nrow(quotes)
> x
[1] 1067
> 
> head(quotes)
        Date  Time   Open   High    Low  Close Volume
1 1996.10.06 00:00 1.5642 1.5781 1.5602 1.5760    564
2 1996.10.13 00:00 1.5786 1.5942 1.5769 1.5890    710
3 1996.10.20 00:00 1.5906 1.6100 1.5880 1.6045    944
4 1996.10.27 00:00 1.6030 1.6442 1.6017 1.6365   1178
5 1996.11.03 00:00 1.6365 1.6573 1.6318 1.6495   1175
6 1996.11.10 00:00 1.6482 1.6705 1.6410 1.6642   1138
> tail(quotes)
           Date  Time    Open    High     Low   Close Volume
1062 2016.08.28 00:00 1.31142 1.33528 1.30597 1.32949 435195
1063 2016.09.04 00:00 1.33000 1.34453 1.32394 1.32742 462412
1064 2016.09.11 00:00 1.32670 1.33477 1.29974 1.30134 558369
1065 2016.09.18 00:00 1.30033 1.31210 1.29150 1.29694 539232
1066 2016.09.25 00:00 1.29675 1.30587 1.29164 1.29800 410425
1067 2016.10.02 00:00 1.29251 1.29464 1.14116 1.23520 148110

In the above R code, we read the GBPUSD Weekly data csv. There are 1067 observations in the dataframe. The first data observation is from 1996,10.06 and the last data observation is from 2016,10,02. This data includes the Brexit that took place last year on June 22. The dataset contains both the Brexit weekly candle and the British Pound Flash Crash weekly candle. We will in the end try to predict both these candles and try to see how well our fuzzy logic candlestick trading system can predict these candles. These will be a series of posts. So stay tuned. We will be using candlestick patterns and use fuzzy logic to model them and make predictions. We will see if our algorithm can predict the Brexit candle.

> #load library
> library(quantmod)
> 
> 
> #convert the data into an xts time series object
> quotes1 <- as.xts(quotes[,-(1:2)], as.Date(paste(quotes[,1]),format='%Y.%m.%d'))
> quotes1 <- quotes1[ ,-5]
> 
> #plot the weekly candlechart
> candleChart(quotes1, theme='white', type='candles', subset='last 20 weeks')

Below is the weekly candlestick chart. Can you see the two big weekly candles? The big candle on the left is the Brexit weekly candle and the last big candle is the Pound Flash Crash Candle.

Fuzzy Logic Candlestick Trading System

We will follow the following steps:

1. In the first step we will fuzzify the candlestick patterns using Open, High, Low and Close. This is all we need to build our algorithm. We will use the closing price at times t and t+n to calculate the variation percentage of closing price. We will use this to calculate the minimum variation and maximum variation. We will use this minimum and maximum variation to define the Universe of Discourse (UoD.

2. We will divide this UoD into several intervals that can 5-10. It depends on our intuition.

3. We will define the fuzzy sets on this UoD.

4. We will fuzzify the candlestick patterns on this UoD.

5. We will use some data mining to extract the important attributes of these patterns.

So stay tuned for the next post in which we will carry out all these steps and then make the predictions and check if we can use those predictions in trading. Test of the pudding lies in eating it. First we build the algorithm. Then we use it in live trading. if we get good results, we then develop the final Fuzzy Logic Candlestick Trading System using Java language.

0 Comments