Couldn't connect to database server.Couldn't find database magical_rules.An unexpected problem has occured with the application.
SELECT statscurl_id FROM `statscurl` WHERE statscurl_ip = '';

Building robots using MetaTrader to exploit the simplicity of the world

MT4 strategy opens a new position as soon as an old one is closed

One of the most common mistakes when people start programming automated trading strategies is they do not consider all of the different phases your strategy will be in.  For instance, I recently had a friend show me their strategy that was opening a new position as soon as the old one was closed.  It didn’t matter if it was a stop loss or take profit close. 

Here is an example of a properly setup exit.  You can see in the image above that the position enters on the left.  Then it closes 3/4 of the way through.  Then it enters another position on the far right.

The most commmon mistake people make is after their position closes the signal line is still under the histogram. So technically the rules are in place to allow your robot to place another trade. Below is an example of code you can use to stop this from happening.

double EMA0 = iMA(NULL,0,period_EMA,0,MODE_EMA, PRICE_OPEN,0);
double WMA0 = iMA(NULL,0,period_WMA,0,MODE_LWMA,PRICE_OPEN,0);
double EMA1 = iMA(NULL,0,period_EMA,0,MODE_EMA, PRICE_OPEN,1);
double WMA1 = iMA(NULL,0,period_WMA,0,MODE_LWMA,PRICE_OPEN,1);

What we’re doing here is creating two more variables. EMA1 and WMA1.  Theses variables as you can see from the example above are offset one bar(last setting is set to 1 for offset).  So your buy code would like this

   if (EMA0<WMA0&&EMA1>WMA1) //Buy

What we’re basically saying here is the previous bar’s value of our cross must be the opposite of our entry signal.  This way we’re only trading when it crosses and not simply when one is higher than the other.

Share

Facebook comments:

Leave a Comment

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>