Where are you getting your tick data from? Do you buy from commercial sources or just from a brokerage?
A quick question for you Clark, do you see the commercial shift to mt5 from mt4 coming anytime soon, the testing facilities within mt5 are much more thorough than that of mt4. Also the object based coding opportunities in mql5 are more advanced than those of mql4, in my opinion anyway.
Some brokers offering it as a demo but many of them are only offering it as a demo, no live option.
Gary
There are lots of free sources as well as paid sources for tick data. I will be talking about that very soon. Personally, I collect my own.
Hi Gary,
I’ll be quite honest with you. I don’t know as I hardly use MT4 to begin with. But with the multithread backtesting and optimisation it seems like many algo traders will push for the move forward. And as such brokers will eventually switch. When? I really don’t know. Since I last checked MT5 was in its development stages.
Clark
The biggest (and maybe only) advantage of MQL5 is the ability to backtest multi-pair strategies. Personally I haven’t worked with MT5 since everybody says it’s still very buggy. However, there have been proposed methods to do multi-pair testing in mql4 as well: Break Through The Strategy Tester Limit On Testing Hedge EA - MQL4 Articles Not to mention that eventually it may be easier to do all your testing in a totally unrelated language like C++ or PHP, which will offer superior speed. You don’t have to implement any trading functions, all you need is to import data from csv files and then check whether your strategy is profitable.
[B]
“For those of you who don’t know me, my name is Clark. That is all”.[/B]
LOL< thats funny, to me anyways,
OK, back to the thread, looking forward to this myself,
Have you played with MT4’s DDE and backtesting/optimisation via Excel?
Nope, I don’t think I need to. I have built DLLs or separate C++ programs when I needed more advanced testing.
Nice! I’m glad to see so many programmers here, definitely much more experienced than me in this area. Would love to learn from you guys.
New post coming up!
Clark
How did I miss a thread like this! There are 10 kinds of people. Those who understand binary and those who don’t.
6920646f6e7420756e6465727374616e642062696e6172792074686174732077687920492077726f7465207468697320696e2068657861646563696d616c
61 6c 77 61 79 73 20 67 6f 74 74 61 20 62 65 20 74 68 65 20 66 75 6e 6e 79 20 67 75 79 20 68 75 68 3f
[B]Backtesting: [/B]
Once a trading idea has been generated, an algorithm can now be written and one can begin the testing process. I will leave this part for you guys as people will choose different software to do this. Backtesting is one of the primary steps in testing the validity of a trading system. It can help you see how a trading system performed historically, using given price data you use. As a result, you can evaluate its profitability. I’ll share some stuff I know about this topic in this post.
[B]Tick Data:[/B]
When backtesting an algorithm, one needs to use high quality historical price data so that its results are as accurate as possible. The most precise historical price data you can use for backtesting is tick data which can come be in seconds or milliseconds. As you test strategies that require a smaller time frame its importance grows. Typically in Forex, you will not have volume data, instead you will have the date and time stamps as well as the bid and ask prices at the time. Tick data comes in many formats, usually as text files (.txt) or comma separated values (.csv). One of the key benefits to using tick data is the ability to backtest using variable spreads; something that cannot be done using default MT4 data.
[B]Pros and Cons of Backtesting using MT4[/B]
MetaTrader 4 allows backtesting and optimisation of Expert Advisors which are algorithms written in MQL4. Many traders opt to use it because of its simplicity, low costs and easy access as many Forex brokers use it as its main trading platform. This is great for the beginning algo trader but MT4 does have it’s disadvantages. By default, MT4 uses only 1m bars in its backtesting, “ticks” are randomly generated inside a minute bar based on it’s tick volume. In other words, ticks movements generated in MT4 backtesting are fake. This is why modelling quality rarely exceeds 90%.
Another huge disadvantage is that MT4 uses a fixed spread system in its backtesting. So whatever your current spread is on the specific symbol you are testing, that’s what it will use throughout the entire backtest. This has huge issues as spreads are typically variable and on smaller time frames, it can mean the difference between a profitable system and a losing one. For example, scalping strategies, arbitrage, etc.
Platform limitations include the fact that you cannot test a basket of symbols. At the same time, the results given after a backtest or optimisation is lacking the criteria needed for a proper evaluation of a given trading system. MT4 also does not support Walk Forward Testing.
Fortunately, there has been many groups who have developed ways of overcoming these setbacks. I’ll talk about some tools I’d recommend to get started.
[B]Resources:[/B]
For getting tick data there are various free sources online. The most popular one is Tick Data | Birt’s EA review as it gives you a step by step process on how one can download free tick data, as well as converting the files so that one is able to use it on MT4. This will give us a modelling quality of ~99%. One of my favorite websites for free tick data is Free Forex Historical Data. They offer free tick and minute data dating back to as early as 2001. The files are separated by months so it may take a while to download all of it.
There are paid tick data services as well. Forex Tester 2, VIP offers this for $50USD/month compared to some companies that sell tick data for thousands. I am not affiliated with them, just throwing out the idea. In any case, these are just some options if you don’t want to collect your own. Personally, I run an EA which collects the tick data from the symbols I choose from various brokers.
[I]As a side note, one needs to be aware of the timezones the tick data is collected in as well as the format. Keep in mind that MT4 currently does not support milliseconds![/I]
In order to accommodate MT4 users, Walk Forward Analyzer Software for MetaTrader :: Easy Expert Forex has made a Walk Forward Analyzer that is compatible with MT4. I have never personally used it, so I cannot say if it’s good or bad. But for $30, I figure it wouldn’t hurt trying, especially if one needs to keeps costs down and stick with MT4. I am NOT affiliated with them, but it’s just a tool I found which may be useful to some
[B]Backtesting Duration:[/B]
The duration of a backtest needs to be carefully determined. It is always advised to backtest a system for an amount of time that is appropriate for the time frame on which it is traded. The main objective here is to collect a sufficient sample size to consider the backtest accurate and representative of future performance. Other things to consider is past events that may have affected the market quite drastically. These things may need to be avoided (For example, a testing period starting before 2003 on the EUR/USD will lead to inaccurate results, as some data vendors simply extrapolate the data).
Typically, a minimum is 100 trades. Normally I go for 300 to 1000+ for a more accurate picture. Of course 1000 trades on the daily or longer time frame may not be possible.
Standard Error = Square root (n+1); where n = number of trades. The more trades we have, the less risk we have that these trades are profitable by luck.
[B]Simplicity & Degrees of Freedom:[/B]
Simplicity in a system is key. With more filters and more technical indicators, the system runs the risk of curve fitting historical prices. What you’ll end up with is a multi-million dollar producing trading system historically, but put it in a live account and watch it fail miserably.
A concept we use as algo traders is known as “Degrees of Freedom”. This concept is used in other subjects such as statistics or sciences. But I’ll give a simple definition followed by an example of this for our purposes.
It is “the number of variables and rules” in relation to the data series. The more rules, the less degrees of freedom you have in your trading system. Ideally, we are looking for a DoF of over 90%.
As an example let’s say we have a moving average crossover system on the daily time frame. We have a 20EMA and 45EMA plus an RSI14 on the close. The EMAs are the same, so we have 45 variables (45 consumes the 20) NOT 65. Plus 1 rule. We also have 14 variables on the RSI, plus 1 rule. Giving us a total of (45 + 1) + (14 + 1) = 61 degrees of freedom. Now we plan on backtesting on 3 years worth of daily bars so roughly 720 bars. Each bar consists of an open, high, low and close. So 720 * 4 = 2880. We now divide our degrees of freedom by the number of data points. 61 / 2880 = 2.1%. Therefore, 97.1% degrees of freedom remain.
Hopefully you get most of that and learned something new. I will write a new post as soon as I can get to it which will go over optimisation and walk forward analysis.
Clark
I will check into MT5 when i go to Moscow.
Great, I’ll talk to some friends at the brokerage firms.
" Master Tang;368934]61 6c 77 61 79 73 20 67 6f 74 74 61 20 62 65 20 74 68 65 20 66 75 6e 6e 79 20 67 75 79 20 68 75 68 3f "
Well of course!
Nice thread and timing couldn’t be better. Gives me 2 months and 19 days to get ready for the mql5 championship this year.
Glad I could help! Good luck!
hey Clark, can we discus scripts here also?
I need a break even script,
Can’t you just adapt the partial close EA?
You probably need an ea. A script only runs once and wont continuously scan your order.
As Tansen said. Adapt another EA.
If you are going to be an EA developer the last thing you want to do is re-invent or rewrite code. You need to cheat a bit and reuse code.
Use a general EA code as a template. Then you only need to change a few lines of code or write a couple of functions, etc.
A couple of useable templates
Sleepafterxlosses
stevehopwoodforex.com • Index page Just download one of his ea’s. Use it as a template.
You can add functions to your template and use them as needed.
Ok…
If you wanted to use an ea as a break even tool. Here’s what you could do.
If you used a magic number to open the original trade.
Simply set your template ea up with no indicators. So it makes no trades.
Your template already has a break even function. Set the magic no the same in the ea. The ea should close your trade.
If you have no magic number you might have to modify the function that selects the trade. All you really have to do is change the magicnumber part of the code.
void movebreakeven(double breakevengain,double breakeven){
RefreshRates();
if(OrdersTotal()>0){
for(i=OrdersTotal();i>=0;i--){
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==magic){
if(OrderType()==OP_BUY){
if(NormalizeDouble((Bid-OrderOpenPrice()),dg)>=NormalizeDouble(breakevengain*pt,dg)){
if((NormalizeDouble((OrderStopLoss()-OrderOpenPrice()),dg)<NormalizeDouble(breakeven*pt,dg)) || OrderStopLoss()==0){
OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+breakeven*pt,dg),OrderTakeProfit(),0,Blue);
return(0);
}
Look for this line
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==magic){
and change it to this
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol(){
Now your ea will ignore magic numbers… This might be good in your situation, but obviously not good in others.
Or you could ignore the magic number just by not using it set Magic to “”