Forex
Google

Go Back   Forex Trading > Metatrader Training > Metatrader 4 mql 4 - Development course > Questions
Forex Forum Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Register in Forex TSD!
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time).
Click here to register and get more information

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-25-2006, 12:59 AM
Treponema Treponema is offline
Junior Member
 
Join Date: Jan 2006
Posts: 2
Treponema is on a distinguished road
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-25-2006, 01:34 AM
mbkennel mbkennel is offline
Junior Member
 
Join Date: Sep 2005
Posts: 5
mbkennel is on a distinguished road
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-25-2006, 03:15 AM
Treponema Treponema is offline
Junior Member
 
Join Date: Jan 2006
Posts: 2
Treponema is on a distinguished road
Hi! Thanks for your reply.

I have been working on adding what you suggested, but this whole thing is becoming frustrating! Look at the attached picture: it is the EA that Coders’ Guru used for his lesson, and it does not work at all. It opens and closes multiple trades next to each other, and it opens shorts when it should be opening longs. Am I doing something wrong, or does this code not work on backtest?

Thanks,

Trep
Attached Images
File Type: jpg chart.JPG (45.6 KB, 146 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-01-2006, 05:12 AM
Willis11of12's Avatar
Willis11of12 Willis11of12 is offline
Senior Member
 
Join Date: Dec 2005
Posts: 105
Willis11of12 is on a distinguished road
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-07-2006, 10:55 PM
mcintyd1 mcintyd1 is offline
Junior Member
 
Join Date: Dec 2005
Location: Portland, Oregon, USA
Posts: 7
mcintyd1 is on a distinguished road
Lightbulb

Quote:
Originally Posted by 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):
Code:
// 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.
__________________
Regards,

Dean
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-25-2006, 12:55 PM
Willis11of12's Avatar
Willis11of12 Willis11of12 is offline
Senior Member
 
Join Date: Dec 2005
Posts: 105
Willis11of12 is on a distinguished road
Lightbulb 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!

Last edited by Willis11of12 : 03-25-2006 at 02:03 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-30-2006, 12:58 PM
intelligent_14's Avatar
intelligent_14 intelligent_14 is offline
Junior Member
 
Join Date: Mar 2006
Location: Tehran/Iran
Posts: 24
intelligent_14 is on a distinguished road
Quote:
Originally Posted by 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
__________________
there wil be nothig for a person exept his efforts (Emam Ali Alayhesalam)
M.A.Gh
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-07-2006, 06:57 AM
fix_man fix_man is offline
Member
 
Join Date: Mar 2006
Posts: 28
fix_man is on a distinguished road
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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-07-2006, 07:29 AM
intelligent_14's Avatar
intelligent_14 intelligent_14 is offline
Junior Member
 
Join Date: Mar 2006
Location: Tehran/Iran
Posts: 24
intelligent_14 is on a distinguished road
look for CodersGuru tutorials
__________________
there wil be nothig for a person exept his efforts (Emam Ali Alayhesalam)
M.A.Gh
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple charts CiTiFx Metatrader 4 2 10-30-2007 08:21 PM
Multiple sessions of MT4 pip-gandalf Metatrader 4 4 07-03-2006 12:35 PM
Multiple EAs on the same account einvestor Metatrader 4 2 06-14-2006 04:13 PM
having trouble with multiple EAs ycomp Expert Advisors - Metatrader 4 6 03-03-2006 04:41 AM
multiple timeframes sailor Suggestions for Trading Systems 20 02-21-2006 04:04 AM


All times are GMT. The time now is 11:05 AM.