Multiple trades per bar?

 

Hi Everyone! I am working on learning MQ4, and I had a couple of questions I hope someone can help me out with.

I have been studying the great MQL4 Course by the Guru, and I have been experimenting with backtesting. When I look at the chart of a backtest I see several bars that have multiple open and close triangles on them. For example (on the same bar) it will have open buy#3, close buy#3, and then open buy#4. I am assuming that what is happening is that the price has an up tick and then a down tick which makes the cross positive, then uncrossed, then crossed again which results in the multiple buys and closes. Is this what is actually happening?

If that is what is occurring, how can I make the EA wait until the finish of a bar to initiate a trade, or how can I make it only initiate one trade per bar?

Thanks very much for your help!

Trep

 

What you need to do is to have a "static" variable which is something like

"bool ITradedOnThisBar;"

Which is cleared to 'false' every time you have a new bar, and set

to "true" whenever each new trade is signaled.

You check for a new bar by saving the Bar time, e.g. Time[0];

into a variable (check for the right data type) also static or

declared globally.

Then when you enter start(), which happens on each new tick,

you then check the present Time[0] and compare---if unequal

there is a new bar. You can set a boolean variable for that too.

I

 
Files:
chart_1.jpg  46 kb
 

mbkennel, is there anyway you could type an example of what you are talking about that could be copied into an EA that would stop trades from opening multiple trades in the same bar?

 
Treponema:
... When I look at the chart of a backtest I see several bars that have multiple open and close triangles on them. For example (on the same bar) it will have open buy#3, close buy#3, and then open buy#4. I am assuming that what is happening is that the price has an up tick and then a down tick which makes the cross positive, then uncrossed, then crossed again which results in the multiple buys and closes. Is this what is actually happening?

If that is what is occurring, how can I make the EA wait until the finish of a bar to initiate a trade, or how can I make it only initiate one trade per bar?

... Trep

I got the same problem in backtesting, but fixed it a different way. Here is the coding (with comments):

// Go trading only for first tick of new bar

if(Volume[0]>1) return(0); // or else exit

You may want other modifications as Trailing Stop is best adjusted by tick. I am still testing to see the best way to close an existing order on adverse crossover, by tick or by first tick of new bar.

I notice that when the short EMA and the long EMA hover near each other for a period of time, it would be nice to have a crossover system wait before making a change until there is a significant breakout either up or down.

 

Finally!

I think I finally figured out how to prevent my EAs from opening trades in the same bar. Here's a copy of the code that I used.

In my definitions:

static bool ITradedOnThisBar;

Just before opening any trades:

if(ITradedOnThisBar != Bars)

{

Right after opening a sell or buy order:

ITradedOnThisBar = Bars;

Also, if you want it to prevent it from showing trades that close during the same bar that they opened, which is the real problem with my old EA back testing, try this right before closing a buy or sell to prevent it from closing during the same bar that it opened the order.

Right before closing a sell or buy order:

if(ITradedOnThisBar != Bars)

{

I hope this helps! I know I was really wanting to use something like this for a long time and just figured it out a few minutes ago. It seems to be working at preventing those occasional bars that reopen and close a trade over and over again on the same stinkin' bar! I am glad to forever be rid of this annoyance!

Happy trading!

 
Willis11of12:

static bool ITradedOnThisBar;

Just before opening any trades:

if(ITradedOnThisBar != Bars)

{

Right after opening a sell or buy order:

ITradedOnThisBar = Bars;

Willis11of12's solution is very good, i also use it to prevent multiple orders in the same bar, but it dosen't prevent wrong Order Closes, my solution for that problem is to get a confirmation from MA

when we get a buy signal, the fastEMA crossed the SLowEMA from down, so the close time would be whenever FastEMA cross SlowEMA from up

for simpler coding i write a few code for checking EMA direction, when the EA wants to close the order, EMA direction must be Oposite Open condition

 

Great! Anyone knows the coding for opening a trade from the first inital cross of the fast EMA with the Slow EMA until the next opposite crossing; anyone figured out the coding for that one...

Impressive, thanx for your input; brilliant!

 

look for CodersGuru tutorials

Reason: