Tuesday, August 28, 2012

Channel trading

By looking at the losing trades of my previous expert advisor I thought about the next trading strategy. It should trade in channels. A channel is a range of candles bounded by two lines with similar slopes.
Those lines are the support and resistance lines of this group of bars.
The idea is detecting those lines, decide if this is a good channel or not, and trade when the price touches the lower line for long and vice verse for short.
The stop loss should be very small since it's a resistance line and the target should be near the support line.
The first challenge in writing such and expert advisor is find the best support/resistance zone. Then decide if it's a good zone for trading.
I already implemented the channel detection. I also implemented the trading section and the results are not bad. It has a small profit factor and now I am facing the second mission: decide if the calculated zone has a trading potential or not.
By visual testing the EA I see a lot of good trades. It makes a lot of trades (I tested it on EURUSD 15 minutes time frame) and it worth further development from my point of view.
My EA draws the lines for testing purposes.


An example for a trade made by channel EA

Friday, August 24, 2012

Strength EA - the fall

I added one limitation to the live EA: if a certain profit in percents is achieved close all the trades and quit for today. In addition I restricted the amount for opened trades to 3 and it went well for the first two days.
The demo account already gained 28% profit. It looked wonderful and then came the big fall.
48 hours of stamping market deleted almost all the EA's profit. This is the major problem of the EA: stamping market. Every trade it made became a loss.
The demo account did great for two weeks. Most of the loosing trades were closed due to reversing signal and not because reaching the stop loss.
I added a flag to control the reverse signal closures but it's not enough. My EA did great also in short stamping market periods. I don't see any solution for this problem. At the meantime I'll let it run in demo and I guess that it would keep the trend from the beginning but there are bad times like those two days.
One good solution I can think of is daily target stop in percents like the profit I set in the live EA.

And it gave me the idea for the next EA...

Tuesday, August 21, 2012

Moving to the real world

After one day of free automatic trading in demo with OANDA I decided to run it on my live account as well.
I have a small account for such purposes exactly.
It started to work in parallel to the demo account on the same VPS. This way allows me to monitor and verify that everything goes well.
I decided to move to live so quickly because it's not a big account and since the EA made many trades I felt confident enough to try it exactly like the demo.
It ran two weeks in that configuration without any logical change. Since the beginning I noticed some differences that required some code improvements on the live account.
I didn't see those problems on the demo account:

  1. FIFO closure rules - the orders should be closed according to their size. If there are a few orders with the same size then the oldest should be closed first.
  2. Since the EA may open more than one trade sometimes there is not enough money to open another trade (because of the margin). In those cases I try to open the trade in half the original amount = half the risk.
But even after fixing those issues the results were not the same. The demo account gets bigger every day and the demo is around zero. It wins some and then it looses some. I compared the trades and realized that the data is not exact (which influences my targets and entry price) and also execution times are not the same.
The Metatrader's execution time is relatively long while in demo it's almost instant.

The real account account 's progress
The real account account 's progress


How can I proceed form here? I have a good working EA on the demo account which fulfills all my requirements and it doesn't work exactly on the real account. It seems that those changes are significant to my EA. 
I can think of three good options:
  1. Using the demo account as signalling account to the live account. I can write an EA that would run on both sides (demo platform and live platform) and imitate exactly the demo account's behavior.
  2. This option is very similar to the previous option except the trading platform. Instead of using the Metatrder in live I can use OANDA's API. I can build a small application / DLL that will be activated from the demo EA.
  3. Modify the expert advisor for the real world.
Since the differences between the demo and live may be bigger than I see right now I am not sure with option #1. In live I am feeling confident the EA won't loose money. I observed it in live for two weeks and it does have a potential. Why? It makes a lot of trades and it has a consistent behavior. Option #1 is also slower and it may be crucial in rapid price changes.
At the moment I am trying to avoid risks and option #2 is much riskier than the others because of integrating a new API in the system.
I choose the last option but I will may have to go back to this decision later on the quest.




Sunday, August 19, 2012

Strength expert advisor

My new idea is a multi-currency expert advisor.
It uses a group of selected currencies in order to estimate the strength of two single currencies.
If I will be able to know the currency's strength I can use it for trading.
Since the EURUSD has a low spread it would be my first choice.
I would like to estimate the strength of the EUR and the strength of the USD.
Each strength would be in the scale of 0 to 1. 1 is the strongest and 0 is the weakest.
As a start if the EUR's strength is over 0.8 and USD's strength is less than 0.2 then it should go long and vice verse for short. Trades are closed upon reverse signals.
It's a simple strategy with a big problem: it cannot be back tested using the Metatrader since it doesn't support mufti-currency EA's. For that purpose I needed a VPS.
For my needs a 1GHz CPU with 1GB RAM will do.
It's important that it will be alive 24/7 without any connection problems.
I used a money management system in the EA. The take profit is a factor of the loss, and the loss is defined as a risk in % of the account's equity.
I ran it for 24 hours on a demo account and the results were amazing:

  • A lot of trades were made
  • The profit factor was between 1.5-2
After a week and a half the demo account earned 15%. 
115 trades with a profit factor of 1.66.

Strength EA's result in the first 10 days








That's a good start for the EA I'm looking for!

Wednesday, August 15, 2012

Neural networks - part II

On the last post I described the theoretical background of planning a trading system base on a neural network predictor. In short the network is used to predict the daily bar's high and low prices.
Using this information I decided to place pending orders in a form of a grid based on the network's predicted prices, and the error.
The probability of the estimated low price to be lower than the real low price is noted as P{estimated L<L).
The prediction is not perfect. There is an error which is the estimated L - L.
If we'll use the estimated low price and we sub struct X pips from it then we enlarge our chance to avoid a loss, but we reduce the chance to enter a trade because that price may be out of the daily range.
I created a map of probabilities based on the training data I used in the training stage for each type of operation (long or short).
For long I found what is the probability that the estimated low price - X pips is lower than the actual low price: Prob{est L - X pips < L).
I also calculated a similar map for the exit price: Prob{est H - X pips < H}. Here I would like to be on the safe side. The bigger X is the lower the exit price which is more realistic.
Here is an example for such a map:

Neural network probability map
The upper graph is for short and the lower is for long.
The red color bars are entry prices and the blue for exit.
For example lets make a short pending order:
The probability that the high price will be below 1.246 is 80%.
The probability that the low price will be above 1.235 is over 80%.
So if the daily bar headed towards 1.246 first, we will open a short trade with a take profit around 1.235.
The stop loss is set also according to this grid map. 30 or 40 pips above the entry price we'll place the stop loss. The probability that the price will reach there is very low (a few percents).
In addition we can split the money using a money management model among a few levels like a grid.

Using the neural network requires an integration of the network itself into Metatrader (MQL). I used Matlab and for the development process. Training the network took around 25 minutes per symbol. That trading strategy is trading against the trend. Sometimes it works and sometimes not.
I traded manually using the network's products as I showed above. I didn't recognize a good enough potential at that moment which justified making that effort.
Cons:

  • It required a complicated additional work
  • The volume was low - 0-4 trades per day


It was an interesting try but that's not what I am looking for.
A potential use of this system would be as targets combined with another trading strategy.

Sunday, August 12, 2012

Neural networks - part I

Artificial intelligence always intrigued me. I can think of two main uses of artificial intelligence algorithms in forex: pattern recognition and predictors.
Pattern recognition is used to teach your system to detect special patterns and then embed it in an expert advisor as a trend detector or a signaling module.
The predictor is used to predict a certain price (such as the close price of a candle) and use it in the expert advisor. It doesn't have to be based on artificial intelligence.
A predictor/estimator is measured according to it's error. The error is defined as the difference between the real value and the predicted value.

Two properties of the error define the predictor's quality:

  • The error's mean value
  • The error's variance.
The process of using machine learning is divided into two stages:
1. Training - training the system with a sub-set of the training data
2. Testing - testing the trained system with the other sub-set of that data.

My idea was using the predicted daily bar's high and low prices for trading.
For that purpose I need a predictor which I chose to develop using a neural network. Such a system requires data. The input data for the predictor is the last N bars (Open, Close, High, Low) and it's output is the predicted high price and low price. Later I found that the open and close prices doesn't really matter but I used the current day's open price for that.
So the input is the last N bars high and low prices, and the current day's open price. That's the data needed for predicting one bar's prices. I had to collect the same kind of  data for each bar that was part of the training. I wrote a script in Metatrader that collects all the data for the training phase.
At that point I need a good neural network tool. There is a library called FANN but it's performance were poor. I had no choice to work with MATLAB. The performance of MATAB was much better from the error point of view, but it's very slow.

Now the big question is how can I use such a predictor? Suppose you know with a high probability and small error what would be the current day's low/high prices, what would you do?
One option is to use those prices as targets. The other is as entry prices.
I chose the second option. 
In addition I checked statistically the error and created a distribution of the error. Using that distribution I created a graph of entry prices which may be used a grid.

How?

Suppose that in 50% of the cases the predicted low price was lower than the actual low price,
And in 60% of the cases the predicted low price - 10 pips was lower than the actual low price.
In that case if the trend is long the money may be divided between those levels according to their probabilities. Sometimes the -10 pips level won't be hit at all. It's a game of numbers and probabilities.
How to spread the risk among the levels vs. the probability that it would be hit.

Starting the quest...

I am a software engineer who majored in different programming languages, platforms and systems. Around eight years ago my father asked me to program a short piece of code for him in a forex platform called Metatrader 2. That's how I entered the forex world. After two months Metatrader 4 was published. After a few months of programming tasks for my dad I joined a few friends and together we established a small group who offers forex programming services.
During the upcoming years I gained a lot of technical experience with the forex world, platforms and  brokers. In addition to the programming knowledge I already had the technical forex knowledge is very important.
I also developed a few forex automatic systems for me and my family which traded with live money. Some in Metatrader and some in dedicated broker's API such as MBTrading.
One of the systems was very profitable. We doubled our account in one month. After backtesting the expert advisor with the Metatrader platform we executed the expert advisor with a money management system and it worked great. After one month it started to loose money and it contradicted my back test's results.
At that moment it was stopped and I started to search for the problem. My conclusion was that the EA had too many parameters, and an optimization over a certain period won't yield the good results in future. It wasn't robust because of the big amount of parameters.

That was my last active attempt until a few weeks ago.

After developing hundreds of systems for other clients I feel that I should develop my own system.
Why? this market is unlimited. "Algo trading" is a common term these days and the market is composed from a lot of other trading robots. (I know - I wrote some of them :) )

The requirements of my target system are:
1. It should be profitable - profit factor of at least two. It means that for every dollar it losses it should earn at least two.
2. Trading frequency - at least a few trades per day. Why? I want to make money fast.
3. Money management - risk a certain percent of your balance in each trade.
4. No hedging

This is an ambitious goal but for me it's the perfect automatic trading system. A high profit factor robot that makes one or two trades per week won't do the job. This system should have a high volume.

The EA would run on the Metatrader 4 platform. Pros - It's comfortable and easy. Cons - execution time is slow and a lot of brokers are dishonest.
The solution: I am going to use a reliable American broker (OANDA) and maybe in the future rewrite it  as a dedicated a.

I started the process a few weeks ago. In this blog I will share my thoughts, experiments and results.